Database: Normalise index names in `dbDelta()`.

When comparing index definitions, normalise the index names to lower case, as they are not case sensitive within MySQL.

Fixes #34874.



git-svn-id: https://develop.svn.wordpress.org/trunk@38591 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-09-12 05:08:17 +00:00
parent f54b5428c4
commit 7511fe88cc
2 changed files with 27 additions and 4 deletions

View File

@ -2283,7 +2283,7 @@ function dbDelta( $queries = '', $execute = true ) {
$index_type = str_replace( 'INDEX', 'KEY', $index_type );
// Escape the index name with backticks. An index for a primary key has no name.
$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . $index_matches['index_name'] . '`';
$index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';
// Parse the columns. Multiple columns are separated by a comma.
$index_columns = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
@ -2407,7 +2407,7 @@ function dbDelta( $queries = '', $execute = true ) {
foreach ($tableindices as $tableindex) {
// Add the index to the index data array.
$keyname = $tableindex->Key_name;
$keyname = strtolower( $tableindex->Key_name );
$index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
$index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
$index_ary[$keyname]['index_type'] = $tableindex->Index_type;
@ -2418,7 +2418,7 @@ function dbDelta( $queries = '', $execute = true ) {
// Build a create string to compare to the query.
$index_string = '';
if ($index_name == 'PRIMARY') {
if ($index_name == 'primary') {
$index_string .= 'PRIMARY ';
} elseif ( $index_data['unique'] ) {
$index_string .= 'UNIQUE ';
@ -2430,7 +2430,7 @@ function dbDelta( $queries = '', $execute = true ) {
$index_string .= 'SPATIAL ';
}
$index_string .= 'KEY ';
if ( 'PRIMARY' !== $index_name ) {
if ( 'primary' !== $index_name ) {
$index_string .= '`' . $index_name . '`';
}
$index_columns = '';

View File

@ -802,6 +802,29 @@ class Tests_dbDelta extends WP_UnitTestCase {
$this->assertEmpty( $updates );
}
/**
* @ticket 34874
*/
function test_key_names_are_not_case_sensitive_and_do_not_recreate_indices() {
global $wpdb;
$updates = dbDelta(
"
CREATE TABLE {$wpdb->prefix}dbdelta_test (
id bigint(20) NOT NULL AUTO_INCREMENT,
column_1 varchar(255) NOT NULL,
column_2 text,
column_3 blob,
PRIMARY KEY (id),
KEY KEY_1 (column_1),
KEY compOUND_key (id,column_1),
FULLTEXT KEY FULLtext_kEY (column_1)
) ENGINE=MyISAM
", false );
$this->assertEmpty( $updates );
}
/**
* @ticket 31679
*/