Skip to content

Commit 7cd15a0

Browse files
wojteknclaude
andcommitted
Inherit column collations from the table default
Character columns without an explicit charset or collation were always recorded with utf8mb4_0900_ai_ci, instead of the table's default collation as in MySQL. Tables created with utf8mb4_unicode_520_ci therefore still got MySQL 8-only collations on their columns. The table collation is now read only from the table options, so that a column COLLATE clause is no longer used as the table collation, and a table DEFAULT CHARSET without a collation now uses that charset's default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 370adde commit 7cd15a0

3 files changed

Lines changed: 210 additions & 30 deletions

File tree

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

Lines changed: 108 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,10 @@ public function record_create_table( WP_Parser_Node $node ): void {
560560
$table_name,
561561
$column_name,
562562
$column_node,
563-
$column_position
563+
$column_position,
564+
function () use ( $table_collation ) {
565+
return $table_collation;
566+
}
564567
);
565568

566569
try {
@@ -866,7 +869,15 @@ private function record_add_column(
866869
array( self::SAVED_DATABASE_NAME, $table_name )
867870
)->fetchColumn();
868871

869-
$column_data = $this->extract_column_data( $table_name, $column_name, $node, (int) $position + 1 );
872+
$column_data = $this->extract_column_data(
873+
$table_name,
874+
$column_name,
875+
$node,
876+
(int) $position + 1,
877+
function () use ( $table_is_temporary, $table_name ) {
878+
return $this->get_recorded_table_collation( $table_is_temporary, $table_name );
879+
}
880+
);
870881
try {
871882
$this->insert_values(
872883
$this->get_table_name( $table_is_temporary, 'columns' ),
@@ -916,7 +927,15 @@ private function record_change_column(
916927
string $new_column_name,
917928
WP_Parser_Node $node
918929
): void {
919-
$column_data = $this->extract_column_data( $table_name, $new_column_name, $node, 0 );
930+
$column_data = $this->extract_column_data(
931+
$table_name,
932+
$new_column_name,
933+
$node,
934+
0,
935+
function () use ( $table_is_temporary, $table_name ) {
936+
return $this->get_recorded_table_collation( $table_is_temporary, $table_name );
937+
}
938+
);
920939
unset( $column_data['ordinal_position'] );
921940
$this->update_values(
922941
$this->get_table_name( $table_is_temporary, 'columns' ),
@@ -1499,13 +1518,21 @@ private function record_drop_check_constraint(
14991518
/**
15001519
* Analyze "columnDefinition" or "fieldDefinition" AST node and extract column data.
15011520
*
1502-
* @param string $table_name The table name.
1503-
* @param string $column_name The column name.
1504-
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
1505-
* @param int $position The ordinal position of the column in the table.
1506-
* @return array Column data for the information schema.
1521+
* @param string $table_name The table name.
1522+
* @param string $column_name The column name.
1523+
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
1524+
* @param int $position The ordinal position of the column in the table.
1525+
* @param callable $get_table_collation Returns the default collation of the table.
1526+
* Called only when the column inherits it.
1527+
* @return array Column data for the information schema.
15071528
*/
1508-
private function extract_column_data( string $table_name, string $column_name, WP_Parser_Node $node, int $position ): array {
1529+
private function extract_column_data(
1530+
string $table_name,
1531+
string $column_name,
1532+
WP_Parser_Node $node,
1533+
int $position,
1534+
callable $get_table_collation
1535+
): array {
15091536
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
15101537

15111538
$default = $this->get_column_default( $node, $data_type, $column_name );
@@ -1514,7 +1541,7 @@ private function extract_column_data( string $table_name, string $column_name, W
15141541
$extra = $this->get_column_extra( $node );
15151542
$comment = $this->get_column_comment( $node );
15161543

1517-
list ( $charset, $collation ) = $this->get_column_charset_and_collation( $node, $data_type );
1544+
list ( $charset, $collation ) = $this->get_column_charset_and_collation( $node, $data_type, $get_table_collation );
15181545
list ( $char_length, $octet_length ) = $this->get_column_lengths( $node, $data_type, $charset );
15191546
list ( $precision, $scale ) = $this->get_column_numeric_attributes( $node, $data_type );
15201547
$datetime_precision = $this->get_column_datetime_precision( $node, $data_type );
@@ -2018,12 +2045,44 @@ private function get_table_engine( WP_Parser_Node $node ): string {
20182045
* @return string The table collation as stored in information schema.
20192046
*/
20202047
private function get_table_collation( WP_Parser_Node $node ): string {
2021-
$collate_node = $node->get_first_descendant_node( 'collationName' );
2022-
if ( null === $collate_node ) {
2023-
// @TODO: Use default DB collation or DB_CHARSET & DB_COLLATE.
2024-
return 'utf8mb4_0900_ai_ci';
2048+
// Only table options apply; column definitions may specify their own collation.
2049+
$options_node = $node->get_first_descendant_node( 'createTableOptions' );
2050+
2051+
$collation_node = $options_node ? $options_node->get_first_descendant_node( 'defaultCollation' ) : null;
2052+
if ( null !== $collation_node ) {
2053+
return strtolower( $this->get_value( $collation_node->get_first_child_node( 'collationName' ) ) );
2054+
}
2055+
2056+
$charset_node = $options_node ? $options_node->get_first_descendant_node( 'defaultCharset' ) : null;
2057+
if ( null !== $charset_node ) {
2058+
$charset = strtolower( $this->get_value( $charset_node->get_first_child_node( 'charsetName' ) ) );
2059+
return $this->get_charset_default_collation( 'utf8mb3' === $charset ? 'utf8' : $charset );
20252060
}
2026-
return strtolower( $this->get_value( $collate_node ) );
2061+
2062+
// @TODO: Use the default collation of the database.
2063+
return 'utf8mb4_0900_ai_ci';
2064+
}
2065+
2066+
/**
2067+
* Get the table collation that is recorded in the information schema.
2068+
*
2069+
* @param bool $table_is_temporary Whether the table is temporary.
2070+
* @param string $table_name The table name.
2071+
* @return string The table collation as stored in information schema.
2072+
*/
2073+
private function get_recorded_table_collation( bool $table_is_temporary, string $table_name ): string {
2074+
$collation = $this->connection->query(
2075+
'
2076+
SELECT table_collation
2077+
FROM ' . $this->connection->quote_identifier( $this->get_table_name( $table_is_temporary, 'tables' ) ) . '
2078+
WHERE table_schema = ?
2079+
AND table_name = ?
2080+
',
2081+
array( self::SAVED_DATABASE_NAME, $table_name )
2082+
)->fetchColumn();
2083+
2084+
// @TODO: Use the default collation of the database.
2085+
return false === $collation || null === $collation ? 'utf8mb4_0900_ai_ci' : $collation;
20272086
}
20282087

20292088
/**
@@ -2443,11 +2502,12 @@ private function get_column_data_types( WP_Parser_Node $node ): array {
24432502
/**
24442503
* Extract column charset and collation from the "columnDefinition" or "fieldDefinition" AST node.
24452504
*
2446-
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2447-
* @param string $data_type The column data type as stored in information schema.
2448-
* @return array{ string|null, string|null } The column charset and collation as stored in information schema.
2505+
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
2506+
* @param string $data_type The column data type as stored in information schema.
2507+
* @param callable $get_table_collation Returns the default collation of the table.
2508+
* @return array{ string|null, string|null } The column charset and collation as stored in information schema.
24492509
*/
2450-
private function get_column_charset_and_collation( WP_Parser_Node $node, string $data_type ): array {
2510+
private function get_column_charset_and_collation( WP_Parser_Node $node, string $data_type, callable $get_table_collation ): array {
24512511
if ( ! (
24522512
'char' === $data_type
24532513
|| 'varchar' === $data_type
@@ -2508,30 +2568,48 @@ private function get_column_charset_and_collation( WP_Parser_Node $node, string
25082568
$collation = strtolower( $this->get_value( $collation_node ) );
25092569
}
25102570

2511-
// Defaults.
2512-
// @TODO: These are hardcoded now. We should get them from table/DB.
2571+
// When neither charset nor collation is set, the table defaults are used.
25132572
if ( null === $charset && null === $collation ) {
2514-
$charset = 'utf8mb4';
2573+
$table_collation = $get_table_collation();
2574+
$charset = $this->get_collation_charset( $table_collation );
2575+
if ( ! $is_binary ) {
2576+
$collation = $table_collation;
2577+
}
25152578
// @TODO: "BINARY" (seems to change varchar to varbinary).
25162579
// @TODO: "DEFAULT"
25172580
}
25182581

25192582
// If only one of charset/collation is set, the other one is derived.
25202583
if ( null === $collation ) {
2521-
if ( $is_binary ) {
2522-
$collation = $charset . '_bin';
2523-
} elseif ( isset( self::CHARSET_DEFAULT_COLLATION_MAP[ $charset ] ) ) {
2524-
$collation = self::CHARSET_DEFAULT_COLLATION_MAP[ $charset ];
2525-
} else {
2526-
$collation = $charset . '_general_ci';
2527-
}
2584+
$collation = $is_binary ? $charset . '_bin' : $this->get_charset_default_collation( $charset );
25282585
} elseif ( null === $charset ) {
2529-
$charset = substr( $collation, 0, strpos( $collation, '_' ) );
2586+
$charset = $this->get_collation_charset( $collation );
25302587
}
25312588

25322589
return array( $charset, $collation );
25332590
}
25342591

2592+
/**
2593+
* Get the default collation for a given charset.
2594+
*
2595+
* @param string $charset The charset name.
2596+
* @return string The default collation of the charset.
2597+
*/
2598+
private function get_charset_default_collation( string $charset ): string {
2599+
return self::CHARSET_DEFAULT_COLLATION_MAP[ $charset ] ?? $charset . '_general_ci';
2600+
}
2601+
2602+
/**
2603+
* Get the charset for a given collation.
2604+
*
2605+
* @param string $collation The collation name.
2606+
* @return string The charset of the collation.
2607+
*/
2608+
private function get_collation_charset( string $collation ): string {
2609+
$separator_position = strpos( $collation, '_' );
2610+
return false === $separator_position ? $collation : substr( $collation, 0, $separator_position );
2611+
}
2612+
25352613
/**
25362614
* Extract column length info from the "columnDefinition" or "fieldDefinition" AST node.
25372615
*

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,107 @@ public function testInfromationSchemaCharacterSets(): void {
128128
);
129129
}
130130

131+
public function testTableCollationFromTableOptions(): void {
132+
$this->assertQuery( 'CREATE TABLE t1 (a TEXT)' );
133+
$this->assertQuery( 'CREATE TABLE t2 (a TEXT COLLATE utf8mb4_bin)' );
134+
$this->assertQuery( 'CREATE TABLE t3 (a TEXT) DEFAULT CHARSET=latin1' );
135+
$this->assertQuery( 'CREATE TABLE t4 (a TEXT) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci' );
136+
137+
$result = $this->assertQuery(
138+
"SELECT table_name, table_collation FROM information_schema.tables WHERE table_schema = 'wp' ORDER BY table_name"
139+
);
140+
$this->assertEquals(
141+
array(
142+
(object) array(
143+
'TABLE_NAME' => 't1',
144+
'TABLE_COLLATION' => 'utf8mb4_0900_ai_ci',
145+
),
146+
(object) array(
147+
'TABLE_NAME' => 't2',
148+
'TABLE_COLLATION' => 'utf8mb4_0900_ai_ci',
149+
),
150+
(object) array(
151+
'TABLE_NAME' => 't3',
152+
'TABLE_COLLATION' => 'latin1_swedish_ci',
153+
),
154+
(object) array(
155+
'TABLE_NAME' => 't4',
156+
'TABLE_COLLATION' => 'utf8mb4_unicode_520_ci',
157+
),
158+
),
159+
$result
160+
);
161+
}
162+
163+
public function testColumnCollationInheritedFromTable(): void {
164+
$this->assertQuery(
165+
'CREATE TABLE t (
166+
a VARCHAR(10),
167+
b VARCHAR(10) BINARY,
168+
c VARCHAR(10) CHARACTER SET utf8mb4,
169+
d VARCHAR(10) COLLATE utf8mb4_bin,
170+
e INT
171+
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci'
172+
);
173+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN f TEXT' );
174+
$this->assertQuery( 'ALTER TABLE t MODIFY COLUMN e VARCHAR(10)' );
175+
$this->assertQuery( 'CREATE TABLE t2 (a TEXT) DEFAULT CHARSET=latin1' );
176+
177+
$result = $this->assertQuery(
178+
"SELECT table_name, column_name, character_set_name, collation_name
179+
FROM information_schema.columns
180+
WHERE table_schema = 'wp'
181+
ORDER BY table_name, column_name"
182+
);
183+
$this->assertEquals(
184+
array(
185+
(object) array(
186+
'TABLE_NAME' => 't',
187+
'COLUMN_NAME' => 'a',
188+
'CHARACTER_SET_NAME' => 'utf8mb4',
189+
'COLLATION_NAME' => 'utf8mb4_unicode_520_ci',
190+
),
191+
(object) array(
192+
'TABLE_NAME' => 't',
193+
'COLUMN_NAME' => 'b',
194+
'CHARACTER_SET_NAME' => 'utf8mb4',
195+
'COLLATION_NAME' => 'utf8mb4_bin',
196+
),
197+
(object) array(
198+
'TABLE_NAME' => 't',
199+
'COLUMN_NAME' => 'c',
200+
'CHARACTER_SET_NAME' => 'utf8mb4',
201+
'COLLATION_NAME' => 'utf8mb4_0900_ai_ci',
202+
),
203+
(object) array(
204+
'TABLE_NAME' => 't',
205+
'COLUMN_NAME' => 'd',
206+
'CHARACTER_SET_NAME' => 'utf8mb4',
207+
'COLLATION_NAME' => 'utf8mb4_bin',
208+
),
209+
(object) array(
210+
'TABLE_NAME' => 't',
211+
'COLUMN_NAME' => 'e',
212+
'CHARACTER_SET_NAME' => 'utf8mb4',
213+
'COLLATION_NAME' => 'utf8mb4_unicode_520_ci',
214+
),
215+
(object) array(
216+
'TABLE_NAME' => 't',
217+
'COLUMN_NAME' => 'f',
218+
'CHARACTER_SET_NAME' => 'utf8mb4',
219+
'COLLATION_NAME' => 'utf8mb4_unicode_520_ci',
220+
),
221+
(object) array(
222+
'TABLE_NAME' => 't2',
223+
'COLUMN_NAME' => 'a',
224+
'CHARACTER_SET_NAME' => 'latin1',
225+
'COLLATION_NAME' => 'latin1_swedish_ci',
226+
),
227+
),
228+
$result
229+
);
230+
}
231+
131232
public function testInfromationSchemaCollations(): void {
132233
$result = $this->assertQuery( 'SELECT * FROM INFORMATION_SCHEMA.COLLATIONS ORDER BY COLLATION_NAME' );
133234
$this->assertEquals(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ public function testAlterTableAddMultipleColumns(): void {
10311031
'INSERT INTO `_wp_sqlite_mysql_information_schema_columns` (`table_schema`, `table_name`, `column_name`, `ordinal_position`, `column_default`, `is_nullable`, `data_type`, `character_maximum_length`, `character_octet_length`, `numeric_precision`, `numeric_scale`, `datetime_precision`, `character_set_name`, `collation_name`, `column_type`, `column_key`, `extra`, `privileges`, `column_comment`, `generation_expression`, `srs_id`)'
10321032
. " VALUES ('sqlite_database', 't', 'a', 2, null, 'YES', 'int', null, null, 10, 0, null, null, null, 'int', '', '', 'select,insert,update,references', '', '', null)",
10331033
"SELECT MAX(ordinal_position) FROM `_wp_sqlite_mysql_information_schema_columns` WHERE table_schema = 'sqlite_database' AND table_name = 't'",
1034+
"SELECT table_collation FROM `_wp_sqlite_mysql_information_schema_tables` WHERE table_schema = 'sqlite_database' AND table_name = 't'",
10341035
'INSERT INTO `_wp_sqlite_mysql_information_schema_columns` (`table_schema`, `table_name`, `column_name`, `ordinal_position`, `column_default`, `is_nullable`, `data_type`, `character_maximum_length`, `character_octet_length`, `numeric_precision`, `numeric_scale`, `datetime_precision`, `character_set_name`, `collation_name`, `column_type`, `column_key`, `extra`, `privileges`, `column_comment`, `generation_expression`, `srs_id`)'
10351036
. " VALUES ('sqlite_database', 't', 'b', 3, null, 'YES', 'text', 65535, 65535, null, null, null, 'utf8mb4', 'utf8mb4_0900_ai_ci', 'text', '', '', 'select,insert,update,references', '', '', null)",
10361037
"SELECT MAX(ordinal_position) FROM `_wp_sqlite_mysql_information_schema_columns` WHERE table_schema = 'sqlite_database' AND table_name = 't'",

0 commit comments

Comments
 (0)