Skip to content

Commit d759b08

Browse files
committed
Ignore inline REFERENCES clauses during CREATE TABLE to match MySQL
1 parent 70bdaf3 commit d759b08

3 files changed

Lines changed: 57 additions & 108 deletions

File tree

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -591,27 +591,23 @@ public function record_create_table( WP_Parser_Node $node ): void {
591591
}
592592

593593
// Extract inline column constraints and indexes.
594-
$index_data = $this->extract_column_statistics_data(
594+
$index_data = $this->extract_column_statistics_data(
595595
$table_name,
596596
$column_name,
597597
$column_node,
598598
'YES' === $column_data['is_nullable']
599599
);
600-
$constraint_data = $this->extract_table_constraint_data(
600+
$constraint_data = $this->extract_table_constraint_data(
601601
$column_node,
602602
$table_name,
603603
$index_data['index_name'] ?? null
604604
);
605-
$referential_constraint_data = $this->extract_referential_constraint_data(
606-
$column_node,
607-
$table_name
608-
);
609-
$key_column_usage_data = $this->extract_key_column_usage_data(
605+
$key_column_usage_data = $this->extract_key_column_usage_data(
610606
$column_node,
611607
$table_name,
612608
$index_data['index_name'] ?? null
613609
);
614-
$check_constraint_data = $this->extract_check_constraint_data(
610+
$check_constraint_data = $this->extract_check_constraint_data(
615611
$column_node,
616612
$table_name
617613
);
@@ -629,12 +625,6 @@ public function record_create_table( WP_Parser_Node $node ): void {
629625
$constraint_data
630626
);
631627
}
632-
if ( null !== $referential_constraint_data ) {
633-
$this->insert_values(
634-
$this->get_table_name( $table_is_temporary, 'referential_constraints' ),
635-
$referential_constraint_data
636-
);
637-
}
638628
foreach ( $key_column_usage_data as $key_column_usage_item ) {
639629
$this->insert_values(
640630
$this->get_table_name( $table_is_temporary, 'key_column_usage' ),
@@ -1892,6 +1882,10 @@ public function extract_table_constraint_data(
18921882
* @return array|null The referential constraint data as stored in information schema.
18931883
*/
18941884
private function extract_referential_constraint_data( WP_Parser_Node $node, string $table_name ): ?array {
1885+
if ( 'tableConstraintDef' !== $node->rule_name ) {
1886+
return null;
1887+
}
1888+
18951889
$references = $node->get_first_descendant_node( 'references' );
18961890
if ( null === $references ) {
18971891
return null;
@@ -1984,7 +1978,7 @@ private function extract_key_column_usage_data(
19841978
): array {
19851979
$is_primary = $node->get_first_descendant_token( WP_MySQL_Lexer::PRIMARY_SYMBOL );
19861980
$is_unique = $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL );
1987-
$references = $node->get_first_descendant_node( 'references' );
1981+
$references = 'tableConstraintDef' === $node->rule_name ? $node->get_first_descendant_node( 'references' ) : null;
19881982
if ( null === $references && ! $is_primary && ! $is_unique ) {
19891983
return array();
19901984
}
@@ -2928,7 +2922,7 @@ public function get_table_constraint_name( WP_Parser_Node $node, string $table_n
29282922
return $this->get_value( $name_node->get_first_child_node( 'identifier' ) );
29292923
}
29302924

2931-
$foreign_key = $node->get_first_descendant_node( 'references' );
2925+
$foreign_key = 'tableConstraintDef' === $node->rule_name ? $node->get_first_descendant_node( 'references' ) : null;
29322926
$check_constraint = $node->get_first_descendant_node( 'checkConstraint' );
29332927

29342928
// FOREIGN KEY and CHECK constraints without a name get a generated name.
@@ -2986,7 +2980,7 @@ private function get_table_constraint_type( WP_Parser_Node $node ): ?string {
29862980
if ( $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ) ) {
29872981
return 'UNIQUE';
29882982
}
2989-
if ( $node->get_first_descendant_node( 'references' ) ) {
2983+
if ( 'tableConstraintDef' === $node->rule_name && $node->get_first_descendant_node( 'references' ) ) {
29902984
return 'FOREIGN KEY';
29912985
}
29922986
if ( $node->get_first_descendant_node( 'checkConstraint' ) ) {

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

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,99 +2015,15 @@ public function testInformationSchemaInlineForeignKeys(): void {
20152015

20162016
// INFORMATION_SCHEMA.TABLE_CONSTRAINTS
20172017
$result = $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't2'" );
2018-
$this->assertEquals(
2019-
array(
2020-
(object) array(
2021-
'CONSTRAINT_CATALOG' => 'def',
2022-
'CONSTRAINT_SCHEMA' => 'wp',
2023-
'CONSTRAINT_NAME' => 't2_ibfk_1',
2024-
'TABLE_SCHEMA' => 'wp',
2025-
'TABLE_NAME' => 't2',
2026-
'CONSTRAINT_TYPE' => 'FOREIGN KEY',
2027-
'ENFORCED' => 'YES',
2028-
),
2029-
(object) array(
2030-
'CONSTRAINT_CATALOG' => 'def',
2031-
'CONSTRAINT_SCHEMA' => 'wp',
2032-
'CONSTRAINT_NAME' => 't2_ibfk_2',
2033-
'TABLE_SCHEMA' => 'wp',
2034-
'TABLE_NAME' => 't2',
2035-
'CONSTRAINT_TYPE' => 'FOREIGN KEY',
2036-
'ENFORCED' => 'YES',
2037-
),
2038-
),
2039-
$result
2040-
);
2018+
$this->assertEquals( array(), $result );
20412019

20422020
// INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
20432021
$result = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't2'" );
2044-
$this->assertEquals(
2045-
array(
2046-
(object) array(
2047-
'CONSTRAINT_CATALOG' => 'def',
2048-
'CONSTRAINT_SCHEMA' => 'wp',
2049-
'CONSTRAINT_NAME' => 't2_ibfk_1',
2050-
'UNIQUE_CONSTRAINT_CATALOG' => 'def',
2051-
'UNIQUE_CONSTRAINT_SCHEMA' => 'wp',
2052-
'UNIQUE_CONSTRAINT_NAME' => null,
2053-
'MATCH_OPTION' => 'NONE',
2054-
'UPDATE_RULE' => 'NO ACTION',
2055-
'DELETE_RULE' => 'NO ACTION',
2056-
'TABLE_NAME' => 't2',
2057-
'REFERENCED_TABLE_NAME' => 't1',
2058-
),
2059-
(object) array(
2060-
'CONSTRAINT_CATALOG' => 'def',
2061-
'CONSTRAINT_SCHEMA' => 'wp',
2062-
'CONSTRAINT_NAME' => 't2_ibfk_2',
2063-
'UNIQUE_CONSTRAINT_CATALOG' => 'def',
2064-
'UNIQUE_CONSTRAINT_SCHEMA' => 'wp',
2065-
'UNIQUE_CONSTRAINT_NAME' => null,
2066-
'MATCH_OPTION' => 'NONE',
2067-
'UPDATE_RULE' => 'NO ACTION',
2068-
'DELETE_RULE' => 'CASCADE',
2069-
'TABLE_NAME' => 't2',
2070-
'REFERENCED_TABLE_NAME' => 't1',
2071-
),
2072-
),
2073-
$result
2074-
);
2022+
$this->assertEquals( array(), $result );
20752023

20762024
// INFORMATION_SCHEMA.KEY_COLUMN_USAGE
20772025
$result = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't2'" );
2078-
$this->assertEquals(
2079-
array(
2080-
(object) array(
2081-
'CONSTRAINT_CATALOG' => 'def',
2082-
'CONSTRAINT_SCHEMA' => 'wp',
2083-
'CONSTRAINT_NAME' => 't2_ibfk_1',
2084-
'TABLE_CATALOG' => 'def',
2085-
'TABLE_SCHEMA' => 'wp',
2086-
'TABLE_NAME' => 't2',
2087-
'COLUMN_NAME' => 't1_id',
2088-
'ORDINAL_POSITION' => '1',
2089-
'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
2090-
'REFERENCED_TABLE_SCHEMA' => 'wp',
2091-
'REFERENCED_TABLE_NAME' => 't1',
2092-
'REFERENCED_COLUMN_NAME' => 'id',
2093-
),
2094-
(object) array(
2095-
'CONSTRAINT_CATALOG' => 'def',
2096-
'CONSTRAINT_SCHEMA' => 'wp',
2097-
'CONSTRAINT_NAME' => 't2_ibfk_2',
2098-
'TABLE_CATALOG' => 'def',
2099-
'TABLE_SCHEMA' => 'wp',
2100-
'TABLE_NAME' => 't2',
2101-
'COLUMN_NAME' => 't1_name',
2102-
'ORDINAL_POSITION' => '1',
2103-
'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
2104-
'REFERENCED_TABLE_SCHEMA' => 'wp',
2105-
'REFERENCED_TABLE_NAME' => 't1',
2106-
'REFERENCED_COLUMN_NAME' => 'name',
2107-
),
2108-
),
2109-
$result
2110-
);
2026+
$this->assertEquals( array(), $result );
21112027

21122028
// SHOW CREATE TABLE
21132029
$result = $this->assertQuery( 'SHOW CREATE TABLE t2' );
@@ -2121,9 +2037,7 @@ public function testInformationSchemaInlineForeignKeys(): void {
21212037
'CREATE TABLE `t2` (',
21222038
' `id` int DEFAULT NULL,',
21232039
' `t1_id` int DEFAULT NULL,',
2124-
' `t1_name` varchar(255) DEFAULT NULL,',
2125-
' CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`),',
2126-
' CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`t1_name`) REFERENCES `t1` (`name`) ON DELETE CASCADE',
2040+
' `t1_name` varchar(255) DEFAULT NULL',
21272041
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
21282042
)
21292043
),
@@ -2444,7 +2358,8 @@ public function testInformationSchemaAlterTableDropForeignKeys(): void {
24442358
$this->assertQuery(
24452359
'CREATE TABLE t2 (
24462360
id INT,
2447-
t1_id INT REFERENCES t1 (id),
2361+
t1_id INT,
2362+
FOREIGN KEY (t1_id) REFERENCES t1 (id),
24482363
FOREIGN KEY (t1_id) REFERENCES t1 (id),
24492364
CONSTRAINT fk1 FOREIGN KEY (t1_id) REFERENCES t1 (id) ON DELETE CASCADE
24502365
)'

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11266,6 +11266,46 @@ public function testForeignKeyOnDeleteSetDefault(): void {
1126611266
$this->assertEquals( array( (object) array( 'id' => '0' ) ), $result );
1126711267
}
1126811268

11269+
public function testInlineReferencesIgnored(): void {
11270+
$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
11271+
$this->assertQuery( 'CREATE TABLE t (id INT, a INT REFERENCES p (id))' );
11272+
11273+
// The insert succeeds because MySQL ignores inline REFERENCES clauses.
11274+
$this->assertQuery( 'INSERT INTO t (a) VALUES (999)' );
11275+
$result = $this->assertQuery( 'SELECT a FROM t' );
11276+
$this->assertEquals( array( (object) array( 'a' => '999' ) ), $result );
11277+
11278+
// Foreign-key metadata is absent for inline references.
11279+
$table_constraints = $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't'" );
11280+
$this->assertEquals( array(), $table_constraints );
11281+
$referential_constraints = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't'" );
11282+
$this->assertEquals( array(), $referential_constraints );
11283+
$key_column_usage = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't'" );
11284+
$this->assertEquals( array(), $key_column_usage );
11285+
11286+
// Single-column ALTER TABLE ADD also ignores inline REFERENCES.
11287+
$this->assertQuery( 'CREATE TABLE t_alter (id INT)' );
11288+
$this->assertQuery( 'ALTER TABLE t_alter ADD a INT REFERENCES p (id)' );
11289+
$this->assertQuery( 'INSERT INTO t_alter (a) VALUES (999)' );
11290+
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_alter'" ) );
11291+
11292+
// Parenthesized ALTER TABLE ADD (...) also ignores inline REFERENCES.
11293+
$this->assertQuery( 'CREATE TABLE t_alter_paren (id INT)' );
11294+
$this->assertQuery( 'ALTER TABLE t_alter_paren ADD (a INT REFERENCES p (id))' );
11295+
$this->assertQuery( 'INSERT INTO t_alter_paren (a) VALUES (999)' );
11296+
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_alter_paren'" ) );
11297+
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't_alter_paren'" ) );
11298+
11299+
// Explicit table-level FOREIGN KEY constraint still rejects the insert.
11300+
$this->assertQuery( 'CREATE TABLE t_explicit (id INT, a INT, FOREIGN KEY (a) REFERENCES p (id))' );
11301+
$explicit_fk_meta = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_explicit'" );
11302+
$this->assertCount( 1, $explicit_fk_meta );
11303+
11304+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
11305+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
11306+
$this->assertQuery( 'INSERT INTO t_explicit (a) VALUES (999)' );
11307+
}
11308+
1126911309
public function testUpdateWithJoinedTables(): void {
1127011310
$sqlite_version = $this->engine->get_sqlite_version();
1127111311
if ( version_compare( $sqlite_version, '3.33.0', '<' ) ) {

0 commit comments

Comments
 (0)