When `dbDelta()` is checking whether an index is defined in a `CREATE TABLE` statement, don't worry if MySQL has a subpart defined on an index, but the `CREATE TABLE` doesn't.

Fixes #31869.



git-svn-id: https://develop.svn.wordpress.org/trunk@32108 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2015-04-11 10:39:49 +00:00
parent b007147ab4
commit b806e118c6
2 changed files with 59 additions and 5 deletions

View File

@ -1980,12 +1980,23 @@ function dbDelta( $queries = '', $execute = true ) {
$index_columns .= '('.$column_data['subpart'].')';
}
}
// The alternative index string doesn't care about subparts
$alt_index_columns = preg_replace( '/\([^)]*\)/', '', $index_columns );
// Add the column list to the index create string.
$index_string .= ' ('.$index_columns.')';
if (!(($aindex = array_search($index_string, $indices)) === false)) {
unset($indices[$aindex]);
// todo: Remove this?
//echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
$index_strings = array(
"$index_string ($index_columns)",
"$index_string ($alt_index_columns)",
);
foreach( $index_strings as $index_string ) {
if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
unset( $indices[ $aindex ] );
break;
// todo: Remove this?
//echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
}
}
// todo: Remove this?
//else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";

43
tests/phpunit/tests/dbdelta.php Executable file
View File

@ -0,0 +1,43 @@
<?php
/**
* Test dbDelta()
*
* @group upgrade
* @group dbdelta
*/
class Tests_dbDelta extends WP_UnitTestCase {
function test_create_new_table() {
include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
$table_name = 'test_new_table';
$create = "CREATE TABLE $table_name (\n a varchar(255)\n)";
$expected = array( $table_name => "Created table $table_name" );
$actual = dbDelta( $create, false );
$this->assertSame( $expected, $actual );
}
/**
* @ticket 31869
*/
function test_truncated_index() {
global $wpdb;
if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
$this->markTestSkipped( 'This test requires utf8mb4 support in MySQL.' );
}
include_once( ABSPATH . 'wp-admin/includes/upgrade.php');
$table_name = 'test_truncated_index';
$create = "CREATE TABLE $table_name (\n a varchar(255) COLLATE utf8mb4_unicode_ci,\n KEY a (a)\n)";
$wpdb->query( $create );
$actual = dbDelta( $create, false );
$this->assertSame( array(), $actual );
}
}