Skip to content

Commit 225e300

Browse files
committed
Align metadata with reported MySQL version
Store modern integer types and utf8mb3 names in the emulated information schema so metadata matches MySQL 8.0.38.
1 parent 5464bee commit 225e300

6 files changed

Lines changed: 189 additions & 79 deletions

‎.github/workflows/wp-tests-phpunit-run.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const expectedFailures = [
5252
'Tests_DB_Charset::test_strip_invalid_text with data set #39',
5353
'Tests_DB_Charset::test_strip_invalid_text with data set #40',
5454
'Tests_DB_Charset::test_strip_invalid_text with data set #41',
55-
'Tests_DB_dbDelta::test_spatial_indices',
5655
'Tests_DB::test_mysqli_flush_sync',
5756
'Tests_DB::test_query_value_contains_invalid_chars',
5857
'Tests_DB::test_replace',

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class WP_SQLite_Information_Schema_Builder {
284284
'utf16' => 'utf16_general_ci',
285285
'utf16le' => 'utf16le_general_ci',
286286
'utf32' => 'utf32_general_ci',
287-
'utf8' => 'utf8_general_ci',
287+
'utf8mb3' => 'utf8mb3_general_ci',
288288
'utf8mb4' => 'utf8mb4_0900_ai_ci', // @TODO: This should probably be version-dependent.
289289
// Before MySQL 8, the default was different.
290290
);
@@ -308,7 +308,7 @@ class WP_SQLite_Information_Schema_Builder {
308308
'utf16' => 4,
309309
'utf16le' => 4,
310310
'utf32' => 4,
311-
'utf8' => 3,
311+
'utf8mb3' => 3,
312312
'utf8mb4' => 4,
313313
);
314314

@@ -430,7 +430,7 @@ public function get_computed_information_schema_table_definition( string $table_
430430
FROM (
431431
VALUES
432432
('binary', 'binary', 'Binary pseudo charset', 1),
433-
('utf8', 'utf8_general_ci', 'UTF-8 Unicode', 3),
433+
('utf8mb3', 'utf8mb3_general_ci', 'UTF-8 Unicode', 3),
434434
('utf8mb4', 'utf8mb4_0900_ai_ci', 'UTF-8 Unicode', 4)
435435
)";
436436
case 'collations':
@@ -445,9 +445,9 @@ public function get_computed_information_schema_table_definition( string $table_
445445
FROM (
446446
VALUES
447447
('binary', 'binary', 63, 'Yes', 'Yes', 1, 'NO PAD'),
448-
('utf8_bin', 'utf8', 83, '', 'Yes', 1, 'PAD SPACE'),
449-
('utf8_general_ci', 'utf8', 33, 'Yes', 'Yes', 1, 'PAD SPACE'),
450-
('utf8_unicode_ci', 'utf8', 192, '', 'Yes', 8, 'PAD SPACE'),
448+
('utf8mb3_bin', 'utf8mb3', 83, '', 'Yes', 1, 'PAD SPACE'),
449+
('utf8mb3_general_ci', 'utf8mb3', 33, 'Yes', 'Yes', 1, 'PAD SPACE'),
450+
('utf8mb3_unicode_ci', 'utf8mb3', 192, '', 'Yes', 8, 'PAD SPACE'),
451451
('utf8mb4_bin', 'utf8mb4', 46, '', 'Yes', 1, 'PAD SPACE'),
452452
('utf8mb4_unicode_ci', 'utf8mb4', 224, '', 'Yes', 8, 'PAD SPACE'),
453453
('utf8mb4_0900_ai_ci', 'utf8mb4', 255, 'Yes', 'Yes', 0, 'NO PAD')
@@ -2022,7 +2022,7 @@ private function get_table_collation( WP_Parser_Node $node ): string {
20222022
// @TODO: Use default DB collation or DB_CHARSET & DB_COLLATE.
20232023
return 'utf8mb4_0900_ai_ci';
20242024
}
2025-
return strtolower( $this->get_value( $collate_node ) );
2025+
return $this->normalize_collation_name( strtolower( $this->get_value( $collate_node ) ) );
20262026
}
20272027

20282028
/**
@@ -2360,20 +2360,17 @@ private function get_column_data_types( WP_Parser_Node $node ): array {
23602360
$full_type .= '(' . implode( ',', $values ) . ')';
23612361
}
23622362

2363-
$field_length = $type_node->get_first_descendant_node( 'fieldLength' );
2364-
if ( null !== $field_length ) {
2363+
$field_length = $type_node->get_first_descendant_node( 'fieldLength' );
2364+
$is_integer_type = in_array( $type, array( 'tinyint', 'smallint', 'mediumint', 'int', 'bigint' ), true );
2365+
$is_boolean_type = null !== $field_length && 'tinyint' === $type && '(1)' === $this->get_value( $field_length );
2366+
$has_zerofill = null !== $type_node->get_first_descendant_token( WP_MySQL_Lexer::ZEROFILL_SYMBOL );
2367+
$stores_type_size = ! $is_integer_type || $is_boolean_type || $has_zerofill;
2368+
if ( null !== $field_length && $stores_type_size ) {
23652369
if ( 'decimal' === $type || 'float' === $type || 'double' === $type ) {
23662370
$full_type .= rtrim( $this->get_value( $field_length ), ')' ) . ',0)';
23672371
} else {
23682372
$full_type .= $this->get_value( $field_length );
23692373
}
2370-
/*
2371-
* As of MySQL 8.0.17, the display width attribute is deprecated for
2372-
* integer types (tinyint, smallint, mediumint, int/integer, bigint)
2373-
* and is not stored anymore. However, it may be important for older
2374-
* versions and WP's dbDelta, so it is safer to keep it at the moment.
2375-
* @TODO: Investigate if it is important to keep this.
2376-
*/
23772374
}
23782375

23792376
$precision = $type_node->get_first_descendant_node( 'precision' );
@@ -2463,27 +2460,27 @@ private function get_column_charset_and_collation( WP_Parser_Node $node, string
24632460
$is_binary = true;
24642461
}
24652462
} else {
2466-
// National charsets (in MySQL, it's "utf8").
2463+
// National charsets use the three-byte UTF-8 character set.
24672464
$data_type_node = $node->get_first_descendant_node( 'dataType' );
24682465
if (
24692466
$data_type_node->has_child_node( 'nchar' )
24702467
|| $data_type_node->has_child_token( WP_MySQL_Lexer::NCHAR_SYMBOL )
24712468
|| $data_type_node->has_child_token( WP_MySQL_Lexer::NATIONAL_SYMBOL )
24722469
|| $data_type_node->has_child_token( WP_MySQL_Lexer::NVARCHAR_SYMBOL )
24732470
) {
2474-
$charset = 'utf8';
2471+
$charset = 'utf8mb3';
24752472
}
24762473
}
24772474

24782475
// Normalize charset.
2479-
if ( 'utf8mb3' === $charset ) {
2480-
$charset = 'utf8';
2476+
if ( null !== $charset ) {
2477+
$charset = $this->normalize_charset_name( $charset );
24812478
}
24822479

24832480
// Collation.
24842481
$collation_node = $node->get_first_descendant_node( 'collationName' );
24852482
if ( null !== $collation_node ) {
2486-
$collation = strtolower( $this->get_value( $collation_node ) );
2483+
$collation = $this->normalize_collation_name( strtolower( $this->get_value( $collation_node ) ) );
24872484
}
24882485

24892486
// Defaults.
@@ -2510,6 +2507,29 @@ private function get_column_charset_and_collation( WP_Parser_Node $node, string
25102507
return array( $charset, $collation );
25112508
}
25122509

2510+
/**
2511+
* Normalize a character set to its modern MySQL name.
2512+
*
2513+
* @param string $charset The character set name.
2514+
* @return string The normalized character set name.
2515+
*/
2516+
private function normalize_charset_name( string $charset ): string {
2517+
return 'utf8' === $charset ? 'utf8mb3' : $charset;
2518+
}
2519+
2520+
/**
2521+
* Normalize a collation to its modern MySQL name.
2522+
*
2523+
* @param string $collation The collation name.
2524+
* @return string The normalized collation name.
2525+
*/
2526+
private function normalize_collation_name( string $collation ): string {
2527+
if ( 0 === strpos( $collation, 'utf8_' ) ) {
2528+
return 'utf8mb3_' . substr( $collation, 5 );
2529+
}
2530+
return $collation;
2531+
}
2532+
25132533
/**
25142534
* Extract column length info from the "columnDefinition" or "fieldDefinition" AST node.
25152535
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public function testInfromationSchemaCharacterSets(): void {
112112
'MAXLEN' => '1',
113113
),
114114
(object) array(
115-
'CHARACTER_SET_NAME' => 'utf8',
116-
'DEFAULT_COLLATE_NAME' => 'utf8_general_ci',
115+
'CHARACTER_SET_NAME' => 'utf8mb3',
116+
'DEFAULT_COLLATE_NAME' => 'utf8mb3_general_ci',
117117
'DESCRIPTION' => 'UTF-8 Unicode',
118118
'MAXLEN' => '3',
119119
),
@@ -142,26 +142,26 @@ public function testInfromationSchemaCollations(): void {
142142
'PAD_ATTRIBUTE' => 'NO PAD',
143143
),
144144
(object) array(
145-
'COLLATION_NAME' => 'utf8_bin',
146-
'CHARACTER_SET_NAME' => 'utf8',
145+
'COLLATION_NAME' => 'utf8mb3_bin',
146+
'CHARACTER_SET_NAME' => 'utf8mb3',
147147
'ID' => '83',
148148
'IS_DEFAULT' => '',
149149
'IS_COMPILED' => 'Yes',
150150
'SORTLEN' => '1',
151151
'PAD_ATTRIBUTE' => 'PAD SPACE',
152152
),
153153
(object) array(
154-
'COLLATION_NAME' => 'utf8_general_ci',
155-
'CHARACTER_SET_NAME' => 'utf8',
154+
'COLLATION_NAME' => 'utf8mb3_general_ci',
155+
'CHARACTER_SET_NAME' => 'utf8mb3',
156156
'ID' => '33',
157157
'IS_DEFAULT' => 'Yes',
158158
'IS_COMPILED' => 'Yes',
159159
'SORTLEN' => '1',
160160
'PAD_ATTRIBUTE' => 'PAD SPACE',
161161
),
162162
(object) array(
163-
'COLLATION_NAME' => 'utf8_unicode_ci',
164-
'CHARACTER_SET_NAME' => 'utf8',
163+
'COLLATION_NAME' => 'utf8mb3_unicode_ci',
164+
'CHARACTER_SET_NAME' => 'utf8mb3',
165165
'ID' => '192',
166166
'IS_DEFAULT' => '',
167167
'IS_COMPILED' => 'Yes',
@@ -518,9 +518,9 @@ public function testShowCollation(): void {
518518
$actual = $this->last_result;
519519
$this->assertCount( 7, $actual );
520520
$this->assertEquals( 'binary', $actual[0]->Collation );
521-
$this->assertEquals( 'utf8_bin', $actual[1]->Collation );
522-
$this->assertEquals( 'utf8_general_ci', $actual[2]->Collation );
523-
$this->assertEquals( 'utf8_unicode_ci', $actual[3]->Collation );
521+
$this->assertEquals( 'utf8mb3_bin', $actual[1]->Collation );
522+
$this->assertEquals( 'utf8mb3_general_ci', $actual[2]->Collation );
523+
$this->assertEquals( 'utf8mb3_unicode_ci', $actual[3]->Collation );
524524
$this->assertEquals( 'utf8mb4_bin', $actual[4]->Collation );
525525
$this->assertEquals( 'utf8mb4_unicode_ci', $actual[5]->Collation );
526526
$this->assertEquals( 'utf8mb4_0900_ai_ci', $actual[6]->Collation );
@@ -529,16 +529,16 @@ public function testShowCollation(): void {
529529
$this->assertQuery( "SHOW COLLATION LIKE 'utf8%'" );
530530
$actual = $this->last_result;
531531
$this->assertCount( 6, $actual );
532-
$this->assertEquals( 'utf8_bin', $actual[0]->Collation );
533-
$this->assertEquals( 'utf8_general_ci', $actual[1]->Collation );
534-
$this->assertEquals( 'utf8_unicode_ci', $actual[2]->Collation );
532+
$this->assertEquals( 'utf8mb3_bin', $actual[0]->Collation );
533+
$this->assertEquals( 'utf8mb3_general_ci', $actual[1]->Collation );
534+
$this->assertEquals( 'utf8mb3_unicode_ci', $actual[2]->Collation );
535535
$this->assertEquals( 'utf8mb4_bin', $actual[3]->Collation );
536536

537537
// With WHERE clause.
538-
$this->assertQuery( "SHOW COLLATION WHERE Collation = 'utf8_bin'" );
538+
$this->assertQuery( "SHOW COLLATION WHERE Collation = 'utf8mb3_bin'" );
539539
$actual = $this->last_result;
540540
$this->assertCount( 1, $actual );
541-
$this->assertEquals( 'utf8_bin', $actual[0]->Collation );
541+
$this->assertEquals( 'utf8mb3_bin', $actual[0]->Collation );
542542
}
543543

544544
public function testShowDatabases(): void {

0 commit comments

Comments
 (0)