diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 807673a7f0..6b04963aa8 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -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 = ''; diff --git a/tests/phpunit/tests/dbdelta.php b/tests/phpunit/tests/dbdelta.php index c4b23b0274..ce4f78c793 100644 --- a/tests/phpunit/tests/dbdelta.php +++ b/tests/phpunit/tests/dbdelta.php @@ -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 */