Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,8 @@ function ( $key ) {
// Initialize information schema builder.
$this->information_schema_builder = new WP_SQLite_Information_Schema_Builder(
self::RESERVED_PREFIX,
$this->connection
$this->connection,
$this->mysql_version
);

// Ensure that the database is configured.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,41 @@ class WP_SQLite_Information_Schema_Builder {
*/
private $connection;

/**
* Emulated MySQL version as an integer.
*
* @var int
*/
private $mysql_version;

/**
* Constructor.
*
* @param string $reserved_prefix An identifier prefix for internal database objects.
* @param WP_SQLite_Connection $connection An instance of the SQLite connection.
* @param int $mysql_version Optional. The emulated MySQL version as an integer. Default 80038.
*/
public function __construct( string $reserved_prefix, WP_SQLite_Connection $connection ) {
public function __construct(
string $reserved_prefix,
WP_SQLite_Connection $connection,
int $mysql_version = WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION
) {
$this->connection = $connection;
$this->table_prefix = $reserved_prefix . 'mysql_information_schema_';
$this->temporary_table_prefix = $reserved_prefix . 'mysql_information_schema_tmp_';
$this->mysql_version = $mysql_version;
}

/**
* Check whether inline REFERENCES clauses are enforced for foreign keys.
*
* In MySQL, inline REFERENCES clauses are parsed but ignored prior to MySQL 9.0.
* Starting with MySQL 9.0, inline REFERENCES clauses define foreign key constraints.
*
* @return bool True if inline REFERENCES clauses are enforced, false if ignored.
*/
private function supports_inline_references(): bool {
return $this->mysql_version >= 90000;
}

/**
Expand Down Expand Up @@ -602,10 +627,9 @@ public function record_create_table( WP_Parser_Node $node ): void {
$table_name,
$index_data['index_name'] ?? null
);
$referential_constraint_data = $this->extract_referential_constraint_data(
$column_node,
$table_name
);
$referential_constraint_data = $this->supports_inline_references()
? $this->extract_referential_constraint_data( $column_node, $table_name )
: null;
$key_column_usage_data = $this->extract_key_column_usage_data(
$column_node,
$table_name,
Expand Down Expand Up @@ -1892,6 +1916,10 @@ public function extract_table_constraint_data(
* @return array|null The referential constraint data as stored in information schema.
*/
private function extract_referential_constraint_data( WP_Parser_Node $node, string $table_name ): ?array {
if ( 'tableConstraintDef' !== $node->rule_name && ! $this->supports_inline_references() ) {
return null;
}

$references = $node->get_first_descendant_node( 'references' );
if ( null === $references ) {
return null;
Expand Down Expand Up @@ -1984,7 +2012,7 @@ private function extract_key_column_usage_data(
): array {
$is_primary = $node->get_first_descendant_token( WP_MySQL_Lexer::PRIMARY_SYMBOL );
$is_unique = $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL );
$references = $node->get_first_descendant_node( 'references' );
$references = ( 'tableConstraintDef' === $node->rule_name || $this->supports_inline_references() ) ? $node->get_first_descendant_node( 'references' ) : null;
if ( null === $references && ! $is_primary && ! $is_unique ) {
return array();
}
Expand Down Expand Up @@ -2928,7 +2956,7 @@ public function get_table_constraint_name( WP_Parser_Node $node, string $table_n
return $this->get_value( $name_node->get_first_child_node( 'identifier' ) );
}

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

// FOREIGN KEY and CHECK constraints without a name get a generated name.
Expand Down Expand Up @@ -2986,7 +3014,7 @@ private function get_table_constraint_type( WP_Parser_Node $node ): ?string {
if ( $node->get_first_descendant_token( WP_MySQL_Lexer::UNIQUE_SYMBOL ) ) {
return 'UNIQUE';
}
if ( $node->get_first_descendant_node( 'references' ) ) {
if ( ( 'tableConstraintDef' === $node->rule_name || $this->supports_inline_references() ) && $node->get_first_descendant_node( 'references' ) ) {
return 'FOREIGN KEY';
}
if ( $node->get_first_descendant_node( 'checkConstraint' ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2015,99 +2015,15 @@ public function testInformationSchemaInlineForeignKeys(): void {

// INFORMATION_SCHEMA.TABLE_CONSTRAINTS
$result = $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't2'" );
$this->assertEquals(
array(
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_1',
'TABLE_SCHEMA' => 'wp',
'TABLE_NAME' => 't2',
'CONSTRAINT_TYPE' => 'FOREIGN KEY',
'ENFORCED' => 'YES',
),
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_2',
'TABLE_SCHEMA' => 'wp',
'TABLE_NAME' => 't2',
'CONSTRAINT_TYPE' => 'FOREIGN KEY',
'ENFORCED' => 'YES',
),
),
$result
);
$this->assertEquals( array(), $result );

// INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
$result = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't2'" );
$this->assertEquals(
array(
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_1',
'UNIQUE_CONSTRAINT_CATALOG' => 'def',
'UNIQUE_CONSTRAINT_SCHEMA' => 'wp',
'UNIQUE_CONSTRAINT_NAME' => null,
'MATCH_OPTION' => 'NONE',
'UPDATE_RULE' => 'NO ACTION',
'DELETE_RULE' => 'NO ACTION',
'TABLE_NAME' => 't2',
'REFERENCED_TABLE_NAME' => 't1',
),
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_2',
'UNIQUE_CONSTRAINT_CATALOG' => 'def',
'UNIQUE_CONSTRAINT_SCHEMA' => 'wp',
'UNIQUE_CONSTRAINT_NAME' => null,
'MATCH_OPTION' => 'NONE',
'UPDATE_RULE' => 'NO ACTION',
'DELETE_RULE' => 'CASCADE',
'TABLE_NAME' => 't2',
'REFERENCED_TABLE_NAME' => 't1',
),
),
$result
);
$this->assertEquals( array(), $result );

// INFORMATION_SCHEMA.KEY_COLUMN_USAGE
$result = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't2'" );
$this->assertEquals(
array(
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_1',
'TABLE_CATALOG' => 'def',
'TABLE_SCHEMA' => 'wp',
'TABLE_NAME' => 't2',
'COLUMN_NAME' => 't1_id',
'ORDINAL_POSITION' => '1',
'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
'REFERENCED_TABLE_SCHEMA' => 'wp',
'REFERENCED_TABLE_NAME' => 't1',
'REFERENCED_COLUMN_NAME' => 'id',
),
(object) array(
'CONSTRAINT_CATALOG' => 'def',
'CONSTRAINT_SCHEMA' => 'wp',
'CONSTRAINT_NAME' => 't2_ibfk_2',
'TABLE_CATALOG' => 'def',
'TABLE_SCHEMA' => 'wp',
'TABLE_NAME' => 't2',
'COLUMN_NAME' => 't1_name',
'ORDINAL_POSITION' => '1',
'POSITION_IN_UNIQUE_CONSTRAINT' => '1',
'REFERENCED_TABLE_SCHEMA' => 'wp',
'REFERENCED_TABLE_NAME' => 't1',
'REFERENCED_COLUMN_NAME' => 'name',
),
),
$result
);
$this->assertEquals( array(), $result );

// SHOW CREATE TABLE
$result = $this->assertQuery( 'SHOW CREATE TABLE t2' );
Expand All @@ -2121,9 +2037,7 @@ public function testInformationSchemaInlineForeignKeys(): void {
'CREATE TABLE `t2` (',
' `id` int DEFAULT NULL,',
' `t1_id` int DEFAULT NULL,',
' `t1_name` varchar(255) DEFAULT NULL,',
' CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`t1_id`) REFERENCES `t1` (`id`),',
' CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`t1_name`) REFERENCES `t1` (`name`) ON DELETE CASCADE',
' `t1_name` varchar(255) DEFAULT NULL',
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
)
),
Expand Down Expand Up @@ -2444,7 +2358,8 @@ public function testInformationSchemaAlterTableDropForeignKeys(): void {
$this->assertQuery(
'CREATE TABLE t2 (
id INT,
t1_id INT REFERENCES t1 (id),
t1_id INT,
FOREIGN KEY (t1_id) REFERENCES t1 (id),
FOREIGN KEY (t1_id) REFERENCES t1 (id),
CONSTRAINT fk1 FOREIGN KEY (t1_id) REFERENCES t1 (id) ON DELETE CASCADE
)'
Expand Down
114 changes: 114 additions & 0 deletions packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11266,6 +11266,120 @@ public function testForeignKeyOnDeleteSetDefault(): void {
$this->assertEquals( array( (object) array( 'id' => '0' ) ), $result );
}

public function testInlineReferencesIgnored(): void {
Comment thread
noruzzamans marked this conversation as resolved.
Resolved
$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
$this->assertQuery( 'CREATE TABLE t (id INT, a INT REFERENCES p (id))' );

// The insert succeeds because MySQL ignores inline REFERENCES clauses.
$this->assertQuery( 'INSERT INTO t (a) VALUES (999)' );
$result = $this->assertQuery( 'SELECT a FROM t' );
$this->assertEquals( array( (object) array( 'a' => '999' ) ), $result );

// Foreign-key metadata is absent for inline references.
$table_constraints = $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't'" );
$this->assertEquals( array(), $table_constraints );
$referential_constraints = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't'" );
$this->assertEquals( array(), $referential_constraints );
$key_column_usage = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't'" );
$this->assertEquals( array(), $key_column_usage );

// Single-column ALTER TABLE ADD also ignores inline REFERENCES.
$this->assertQuery( 'CREATE TABLE t_alter (id INT)' );
$this->assertQuery( 'ALTER TABLE t_alter ADD a INT REFERENCES p (id)' );
$this->assertQuery( 'INSERT INTO t_alter (a) VALUES (999)' );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_alter'" ) );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't_alter'" ) );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't_alter'" ) );

// Parenthesized ALTER TABLE ADD (...) also ignores inline REFERENCES.
$this->assertQuery( 'CREATE TABLE t_alter_paren (id INT)' );
$this->assertQuery( 'ALTER TABLE t_alter_paren ADD (a INT REFERENCES p (id))' );
$this->assertQuery( 'INSERT INTO t_alter_paren (a) VALUES (999)' );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_alter_paren'" ) );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.table_constraints WHERE table_name = 't_alter_paren'" ) );
$this->assertEquals( array(), $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't_alter_paren'" ) );

// Explicit table-level FOREIGN KEY constraint still rejects the insert.
$this->assertQuery( 'CREATE TABLE t_explicit (id INT, a INT, FOREIGN KEY (a) REFERENCES p (id))' );
$explicit_fk_meta = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't_explicit'" );
$this->assertCount( 1, $explicit_fk_meta );

$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
$this->assertQuery( 'INSERT INTO t_explicit (a) VALUES (999)' );
}

public function testInlineReferencesWithCheckConstraintRecordsCheckType(): void {
$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
$this->assertQuery( 'CREATE TABLE t (id INT, a INT CHECK (a > 0) REFERENCES p (id))' );

// CHECK constraint t_chk_1 is recorded with CONSTRAINT_TYPE = 'CHECK' (not 'FOREIGN KEY').
$table_constraints = $this->assertQuery( "SELECT constraint_name, constraint_type FROM information_schema.table_constraints WHERE table_name = 't'" );
$this->assertEquals(
array(
(object) array(
'CONSTRAINT_NAME' => 't_chk_1',
'CONSTRAINT_TYPE' => 'CHECK',
),
),
$table_constraints
);

// No foreign key metadata is created in MySQL 8.4.
$referential_constraints = $this->assertQuery( "SELECT * FROM information_schema.referential_constraints WHERE table_name = 't'" );
$this->assertEquals( array(), $referential_constraints );

$key_column_usage = $this->assertQuery( "SELECT * FROM information_schema.key_column_usage WHERE table_name = 't'" );
$this->assertEquals( array(), $key_column_usage );

// The CHECK constraint is enforced while inline REFERENCES is ignored.
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed' );
$this->assertQuery( 'INSERT INTO t (a) VALUES (-5)' );
}

public function testInlineReferencesEnforcedInMySQL9(): void {
$this->engine = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array(
'sqlite_pdo' => $this->sqlite,
'mysql_version' => 90000,
)
);

$this->assertQuery( 'CREATE TABLE p (id INT PRIMARY KEY)' );
$this->assertQuery( 'CREATE TABLE t (id INT, a INT REFERENCES p (id))' );

// In MySQL 9.0+, inline REFERENCES generates foreign key metadata.
$table_constraints = $this->assertQuery( "SELECT constraint_name, constraint_type FROM information_schema.table_constraints WHERE table_name = 't'" );
$this->assertCount( 1, $table_constraints );
$this->assertEquals( 't_ibfk_1', $table_constraints[0]->CONSTRAINT_NAME );
$this->assertEquals( 'FOREIGN KEY', $table_constraints[0]->CONSTRAINT_TYPE );

$referential_constraints = $this->assertQuery( "SELECT constraint_name, referenced_table_name FROM information_schema.referential_constraints WHERE table_name = 't'" );
$this->assertCount( 1, $referential_constraints );
$this->assertEquals( 't_ibfk_1', $referential_constraints[0]->CONSTRAINT_NAME );
$this->assertEquals( 'p', $referential_constraints[0]->REFERENCED_TABLE_NAME );

$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'" );
$this->assertCount( 1, $key_column_usage );
$this->assertEquals( 't_ibfk_1', $key_column_usage[0]->CONSTRAINT_NAME );
$this->assertEquals( 'a', $key_column_usage[0]->COLUMN_NAME );
$this->assertEquals( 'p', $key_column_usage[0]->REFERENCED_TABLE_NAME );
$this->assertEquals( 'id', $key_column_usage[0]->REFERENCED_COLUMN_NAME );

// SHOW CREATE TABLE includes the foreign key constraint.
$create_table = $this->assertQuery( 'SHOW CREATE TABLE t' );
$this->assertStringContainsString( 'CONSTRAINT `t_ibfk_1` FOREIGN KEY (`a`) REFERENCES `p` (`id`)', $create_table[0]->{'Create Table'} );

// Foreign key constraint is enforced in MySQL 9.0+.
$this->expectException( 'WP_MySQL_On_SQLite_Exception' );
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed' );
$this->assertQuery( 'INSERT INTO t (a) VALUES (999)' );
}

public function testUpdateWithJoinedTables(): void {
$sqlite_version = $this->engine->get_sqlite_version();
if ( version_compare( $sqlite_version, '3.33.0', '<' ) ) {
Expand Down
Loading