Skip to content

Commit b159e34

Browse files
committed
fix: preserve inline references for MySQL 9 and record check constraint type
1 parent d67f3d1 commit b159e34

3 files changed

Lines changed: 116 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,8 @@ function ( $key ) {
964964
// Initialize information schema builder.
965965
$this->information_schema_builder = new WP_SQLite_Information_Schema_Builder(
966966
self::RESERVED_PREFIX,
967-
$this->connection
967+
$this->connection,
968+
$this->mysql_version
968969
);
969970

970971
// Ensure that the database is configured.

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,41 @@ class WP_SQLite_Information_Schema_Builder {
358358
*/
359359
private $connection;
360360

361+
/**
362+
* Emulated MySQL version as an integer.
363+
*
364+
* @var int
365+
*/
366+
private $mysql_version;
367+
361368
/**
362369
* Constructor.
363370
*
364371
* @param string $reserved_prefix An identifier prefix for internal database objects.
365372
* @param WP_SQLite_Connection $connection An instance of the SQLite connection.
373+
* @param int $mysql_version Optional. The emulated MySQL version as an integer. Default 80038.
366374
*/
367-
public function __construct( string $reserved_prefix, WP_SQLite_Connection $connection ) {
375+
public function __construct(
376+
string $reserved_prefix,
377+
WP_SQLite_Connection $connection,
378+
int $mysql_version = WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION
379+
) {
368380
$this->connection = $connection;
369381
$this->table_prefix = $reserved_prefix . 'mysql_information_schema_';
370382
$this->temporary_table_prefix = $reserved_prefix . 'mysql_information_schema_tmp_';
383+
$this->mysql_version = $mysql_version;
384+
}
385+
386+
/**
387+
* Check whether inline REFERENCES clauses are enforced for foreign keys.
388+
*
389+
* In MySQL, inline REFERENCES clauses are parsed but ignored prior to MySQL 9.0.
390+
* Starting with MySQL 9.0, inline REFERENCES clauses define foreign key constraints.
391+
*
392+
* @return bool True if inline REFERENCES clauses are enforced, false if ignored.
393+
*/
394+
private function supports_inline_references(): bool {
395+
return $this->mysql_version >= 90000;
371396
}
372397

373398
/**
@@ -591,23 +616,26 @@ public function record_create_table( WP_Parser_Node $node ): void {
591616
}
592617

593618
// Extract inline column constraints and indexes.
594-
$index_data = $this->extract_column_statistics_data(
619+
$index_data = $this->extract_column_statistics_data(
595620
$table_name,
596621
$column_name,
597622
$column_node,
598623
'YES' === $column_data['is_nullable']
599624
);
600-
$constraint_data = $this->extract_table_constraint_data(
625+
$constraint_data = $this->extract_table_constraint_data(
601626
$column_node,
602627
$table_name,
603628
$index_data['index_name'] ?? null
604629
);
605-
$key_column_usage_data = $this->extract_key_column_usage_data(
630+
$referential_constraint_data = $this->supports_inline_references()
631+
? $this->extract_referential_constraint_data( $column_node, $table_name )
632+
: null;
633+
$key_column_usage_data = $this->extract_key_column_usage_data(
606634
$column_node,
607635
$table_name,
608636
$index_data['index_name'] ?? null
609637
);
610-
$check_constraint_data = $this->extract_check_constraint_data(
638+
$check_constraint_data = $this->extract_check_constraint_data(
611639
$column_node,
612640
$table_name
613641
);
@@ -625,6 +653,12 @@ public function record_create_table( WP_Parser_Node $node ): void {
625653
$constraint_data
626654
);
627655
}
656+
if ( null !== $referential_constraint_data ) {
657+
$this->insert_values(
658+
$this->get_table_name( $table_is_temporary, 'referential_constraints' ),
659+
$referential_constraint_data
660+
);
661+
}
628662
foreach ( $key_column_usage_data as $key_column_usage_item ) {
629663
$this->insert_values(
630664
$this->get_table_name( $table_is_temporary, 'key_column_usage' ),
@@ -1882,7 +1916,7 @@ public function extract_table_constraint_data(
18821916
* @return array|null The referential constraint data as stored in information schema.
18831917
*/
18841918
private function extract_referential_constraint_data( WP_Parser_Node $node, string $table_name ): ?array {
1885-
if ( 'tableConstraintDef' !== $node->rule_name ) {
1919+
if ( 'tableConstraintDef' !== $node->rule_name && ! $this->supports_inline_references() ) {
18861920
return null;
18871921
}
18881922

@@ -1978,7 +2012,7 @@ private function extract_key_column_usage_data(
19782012
): array {
19792013
$is_primary = $node->get_first_descendant_token( WP_MySQL_Lexer::PRIMARY_SYMBOL );
19802014
$is_unique = $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL );
1981-
$references = 'tableConstraintDef' === $node->rule_name ? $node->get_first_descendant_node( 'references' ) : null;
2015+
$references = ( 'tableConstraintDef' === $node->rule_name || $this->supports_inline_references() ) ? $node->get_first_descendant_node( 'references' ) : null;
19822016
if ( null === $references && ! $is_primary && ! $is_unique ) {
19832017
return array();
19842018
}
@@ -2922,7 +2956,7 @@ public function get_table_constraint_name( WP_Parser_Node $node, string $table_n
29222956
return $this->get_value( $name_node->get_first_child_node( 'identifier' ) );
29232957
}
29242958

2925-
$foreign_key = 'tableConstraintDef' === $node->rule_name ? $node->get_first_descendant_node( 'references' ) : null;
2959+
$foreign_key = ( 'tableConstraintDef' === $node->rule_name || $this->supports_inline_references() ) ? $node->get_first_descendant_node( 'references' ) : null;
29262960
$check_constraint = $node->get_first_descendant_node( 'checkConstraint' );
29272961

29282962
// FOREIGN KEY and CHECK constraints without a name get a generated name.
@@ -2980,7 +3014,7 @@ private function get_table_constraint_type( WP_Parser_Node $node ): ?string {
29803014
if ( $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ) ) {
29813015
return 'UNIQUE';
29823016
}
2983-
if ( 'tableConstraintDef' === $node->rule_name && $node->get_first_descendant_node( 'references' ) ) {
3017+
if ( ( 'tableConstraintDef' === $node->rule_name || $this->supports_inline_references() ) && $node->get_first_descendant_node( 'references' ) ) {
29843018
return 'FOREIGN KEY';
29853019
}
29863020
if ( $node->get_first_descendant_node( 'checkConstraint' ) ) {

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11309,6 +11309,77 @@ public function testInlineReferencesIgnored(): void {
1130911309
$this->assertQuery( 'INSERT INTO t_explicit (a) VALUES (999)' );
1131011310
}
1131111311

11312+
public function testInlineReferencesWithCheckConstraintRecordsCheckType(): void {
11313+
$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
11314+
$this->assertQuery( 'CREATE TABLE t (id INT, a INT CHECK (a > 0) REFERENCES p (id))' );
11315+
11316+
// CHECK constraint t_chk_1 is recorded with CONSTRAINT_TYPE = 'CHECK' (not 'FOREIGN KEY').
11317+
$table_constraints = $this->assertQuery( "SELECT constraint_name, constraint_type FROM information_schema.table_constraints WHERE table_name = 't'" );
11318+
$this->assertEquals(
11319+
array(
11320+
(object) array(
11321+
'CONSTRAINT_NAME' => 't_chk_1',
11322+
'CONSTRAINT_TYPE' => 'CHECK',
11323+
),
11324+
),
11325+
$table_constraints
11326+
);
11327+
11328+
// No foreign key metadata is created in MySQL 8.4.
11329+
$referential_constraints = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't'" );
11330+
$this->assertEquals( array(), $referential_constraints );
11331+
11332+
$key_column_usage = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't'" );
11333+
$this->assertEquals( array(), $key_column_usage );
11334+
11335+
// The CHECK constraint is enforced while inline REFERENCES is ignored.
11336+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
11337+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed' );
11338+
$this->assertQuery( 'INSERT INTO t (a) VALUES (-5)' );
11339+
}
11340+
11341+
public function testInlineReferencesEnforcedInMySQL9(): void {
11342+
$this->engine = new WP_MySQL_On_SQLite(
11343+
'mysql-on-sqlite:dbname=wp',
11344+
null,
11345+
null,
11346+
array(
11347+
'sqlite_pdo' => $this->sqlite,
11348+
'mysql_version' => 90000,
11349+
)
11350+
);
11351+
11352+
$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
11353+
$this->assertQuery( 'CREATE TABLE t (id INT, a INT REFERENCES p (id))' );
11354+
11355+
// In MySQL 9.0+, inline REFERENCES generates foreign key metadata.
11356+
$table_constraints = $this->assertQuery( "SELECT constraint_name, constraint_type FROM information_schema.table_constraints WHERE table_name = 't'" );
11357+
$this->assertCount( 1, $table_constraints );
11358+
$this->assertEquals( 't_ibfk_1', $table_constraints[0]->CONSTRAINT_NAME );
11359+
$this->assertEquals( 'FOREIGN KEY', $table_constraints[0]->CONSTRAINT_TYPE );
11360+
11361+
$referential_constraints = $this->assertQuery( "SELECT constraint_name, referenced_table_name FROM information_schema.referential_constraints WHERE table_name = 't'" );
11362+
$this->assertCount( 1, $referential_constraints );
11363+
$this->assertEquals( 't_ibfk_1', $referential_constraints[0]->CONSTRAINT_NAME );
11364+
$this->assertEquals( 'p', $referential_constraints[0]->REFERENCED_TABLE_NAME );
11365+
11366+
$key_column_usage = $this->assertQuery( "SELECT constraint_name, column_name, referenced_table_name, referenced_column_name FROM information_schema.key_column_usage WHERE table_name = 't'" );
11367+
$this->assertCount( 1, $key_column_usage );
11368+
$this->assertEquals( 't_ibfk_1', $key_column_usage[0]->CONSTRAINT_NAME );
11369+
$this->assertEquals( 'a', $key_column_usage[0]->COLUMN_NAME );
11370+
$this->assertEquals( 'p', $key_column_usage[0]->REFERENCED_TABLE_NAME );
11371+
$this->assertEquals( 'id', $key_column_usage[0]->REFERENCED_COLUMN_NAME );
11372+
11373+
// SHOW CREATE TABLE includes the foreign key constraint.
11374+
$create_table = $this->assertQuery( 'SHOW CREATE TABLE t' );
11375+
$this->assertStringContainsString( 'CONSTRAINT `t_ibfk_1` FOREIGN KEY (`a`) REFERENCES `p` (`id`)', $create_table[0]->{'Create Table'} );
11376+
11377+
// Foreign key constraint is enforced in MySQL 9.0+.
11378+
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
11379+
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
11380+
$this->assertQuery( 'INSERT INTO t (a) VALUES (999)' );
11381+
}
11382+
1131211383
public function testUpdateWithJoinedTables(): void {
1131311384
$sqlite_version = $this->engine->get_sqlite_version();
1131411385
if ( version_compare( $sqlite_version, '3.33.0', '<' ) ) {

0 commit comments

Comments
 (0)