@@ -8254,6 +8254,99 @@ public function testReservedPrefixInSavepoint(): void {
82548254 $this->assertQuery( 'ROLLBACK' );
82558255 }
82568256
8257+ /**
8258+ * @dataProvider getReservedForeignKeyIdentifiersTestData
8259+ */
8260+ public function testReservedForeignKeyIdentifiers( string $query, string $identifier, bool $ansi_quotes = false ): void {
8261+ if ( $ansi_quotes ) {
8262+ $this->assertQuery( "SET sql_mode = 'ANSI_QUOTES'" );
8263+ }
8264+ $this->assertQuery( 'CREATE TABLE child (n VARCHAR(64))' );
8265+ $this->assertQuery( "INSERT INTO child VALUES ('kept')" );
8266+ $schema = $this->assertQuery( 'SHOW CREATE TABLE child' );
8267+
8268+ $this->assertQueryError( $query, "Invalid identifier '$identifier', prefix '_wp_sqlite_' is reserved" );
8269+
8270+ $this->assertEquals( $schema, $this->assertQuery( 'SHOW CREATE TABLE child' ) );
8271+ $this->assertEquals( array( (object) array( 'n' => 'kept' ) ), $this->assertQuery( 'SELECT * FROM child' ) );
8272+ $this->assertSame( 0, (int) $this->sqlite->query( "SELECT COUNT(*) FROM sqlite_temp_master WHERE name = 'new_child'" )->fetchColumn() );
8273+
8274+ // Rejected references must leave no metadata behind or block later drops.
8275+ $this->assertQuery( 'CREATE TABLE new_child (id INT)' );
8276+ $this->assertQuery( 'DROP TABLE new_child, child' );
8277+ }
8278+
8279+ public function getReservedForeignKeyIdentifiersTestData(): array {
8280+ return array(
8281+ array(
8282+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME))',
8283+ '_wp_sqlite_mysql_information_schema_tables',
8284+ ),
8285+ array(
8286+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_global_variables (name))',
8287+ '_WP_SQLITE_global_variables',
8288+ ),
8289+ array(
8290+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES `wp`.`_Wp_SqLiTe_mysql_information_schema_tables` (`TABLE_NAME`))',
8291+ '_Wp_SqLiTe_mysql_information_schema_tables',
8292+ ),
8293+ array(
8294+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES "_Wp_SqLiTe_global_variables" ("name"))',
8295+ '_Wp_SqLiTe_global_variables',
8296+ true,
8297+ ),
8298+ array(
8299+ 'CREATE TEMPORARY TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_mysql_information_schema_tmp_tables (TABLE_NAME))',
8300+ '_Wp_SqLiTe_mysql_information_schema_tmp_tables',
8301+ ),
8302+ array(
8303+ 'ALTER TABLE child ADD FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME)',
8304+ '_wp_sqlite_mysql_information_schema_tables',
8305+ ),
8306+ array(
8307+ 'ALTER TABLE child ADD COLUMN extra INT, ADD CONSTRAINT fk FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_global_variables (name)',
8308+ '_Wp_SqLiTe_global_variables',
8309+ ),
8310+ array(
8311+ 'CREATE TABLE new_child (n VARCHAR(64) REFERENCES _wp_sqlite_global_variables (name))',
8312+ '_wp_sqlite_global_variables',
8313+ ),
8314+ array(
8315+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_db.parent (n))',
8316+ '_WP_SQLITE_db',
8317+ ),
8318+ array(
8319+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES child (_Wp_SqLiTe_column))',
8320+ '_Wp_SqLiTe_column',
8321+ ),
8322+ );
8323+ }
8324+
8325+ public function testForeignKeyWithSimilarPrefix(): void {
8326+ $this->assertQuery( 'CREATE TABLE `_WP_SQLITE` (n VARCHAR(64) PRIMARY KEY)' );
8327+ $this->assertQuery( "INSERT INTO `_WP_SQLITE` VALUES ('_wp_sqlite_t')" );
8328+ $this->assertQuery(
8329+ "CREATE TABLE child (
8330+ id INT,
8331+ n VARCHAR(64) DEFAULT '_wp_sqlite_t',
8332+ FOREIGN KEY (n) REFERENCES `wp`.`_WP_SQLITE` (n)
8333+ )"
8334+ );
8335+ $this->assertQuery( 'CREATE TABLE other_child (n VARCHAR(64))' );
8336+ $this->assertQuery( 'ALTER TABLE other_child ADD FOREIGN KEY (n) REFERENCES `_WP_SQLITE` (n)' );
8337+ $this->assertQuery( 'INSERT INTO child (id) VALUES (1)' );
8338+ $this->assertQuery( "INSERT INTO other_child VALUES ('_wp_sqlite_t')" );
8339+ $this->assertEquals( array( (object) array( 'n' => '_wp_sqlite_t' ) ), $this->assertQuery( 'SELECT n FROM child' ) );
8340+ $this->assertQueryError(
8341+ "INSERT INTO child (n) VALUES ('missing')",
8342+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8343+ );
8344+ $this->assertQueryError(
8345+ "INSERT INTO other_child VALUES ('missing')",
8346+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8347+ );
8348+ }
8349+
82578350 /**
82588351 * @dataProvider getNamesWithoutReservedPrefixTestData
82598352 */
0 commit comments