diff --git a/src/wp-admin/includes/upgrade.php b/src/wp-admin/includes/upgrade.php index 330cfad8e9..b1c4bc28fa 100644 --- a/src/wp-admin/includes/upgrade.php +++ b/src/wp-admin/includes/upgrade.php @@ -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 "
{$table}:
Found index:".$index_string."\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 "{$table}:
Found index:".$index_string."\n";
+ }
}
// todo: Remove this?
//else echo "{$table}:
Did not find index:".$index_string."
".print_r($indices, true)."\n";
diff --git a/tests/phpunit/tests/dbdelta.php b/tests/phpunit/tests/dbdelta.php
new file mode 100755
index 0000000000..6cdc325dab
--- /dev/null
+++ b/tests/phpunit/tests/dbdelta.php
@@ -0,0 +1,43 @@
+ "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 );
+ }
+}