Skip to content

Commit 8dc5c27

Browse files
Nandan108claudeJanJakes
authored
Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table (#524)
## Summary When the first temporary table statement of a session is `CREATE TEMPORARY TABLE IF NOT EXISTS` or `DROP TEMPORARY TABLE`, the statement fails: ```sql CREATE TEMPORARY TABLE IF NOT EXISTS t (a INT); -- or DROP TEMPORARY TABLE IF EXISTS t; -- SQLSTATE[42S02]: Base table or view not found: 1146 -- Table '_wp_sqlite_mysql_information_schema_tmp_tables' doesn't exist ``` Both statements use the temporary information schema tables, but those are only created together with the first temporary table of a session. A plain `CREATE TEMPORARY TABLE` works, and once one has run, both statements work too, which is why the existing tests pass. This also breaks the common pattern of `DROP TEMPORARY TABLE IF EXISTS x` followed by `CREATE TEMPORARY TABLE x`. ## Fix Both statements now check for the temporary table with `temporary_table_exists()`, which looks in `sqlite_temp_master`. It already exists for exactly this reason ("it may not exist yet"): - **`CREATE TEMPORARY TABLE IF NOT EXISTS`** uses it for the existence check. Non-temporary tables are unchanged. - **`DROP TEMPORARY TABLE`** skips the information schema update when no such temporary table exists. SQLite then does nothing with `IF EXISTS`, and reports the missing table without it, as it already did once a temporary table had been created. ## Tests The new tests run the statements before any temporary table exists. All of them fail on `trunk` with the error above: - **`testCreateTemporaryTableIfNotExistsAsFirstTemporaryTable`** creates the table, inserts a row, and repeats the statement with the same and a differently cased name, to check it is a no-op that keeps the data. - **`testCreateTemporaryTableIfNotExistsWithStandardTableOfSameName`** checks that a standard table with the same name doesn't make the statement a no-op, and stays unchanged. - **`testDropTemporaryTableIfExistsAsFirstTemporaryTable`** checks that the statement succeeds, and that it doesn't drop a standard table with the same name. - **`testDropTemporaryTableAsFirstTemporaryTable`** checks that the statement reports the missing table. Found while running WooCommerce on WordPress Playground. ## Use of AI This change was prepared with Claude Code (Claude Fable 5.1). I reviewed the code, the tests, and this description, and I take responsibility for the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Temporary-table creation now correctly handles existing tables, including case-insensitive names and tables that share a name with a standard table. * Dropping a temporary table no longer affects a same-named standard table. Attempts to drop a missing temporary table now follow the expected behavior with or without `IF EXISTS`. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent 5deb4d6 commit 8dc5c27

3 files changed

Lines changed: 88 additions & 9 deletions

File tree

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,14 +2925,22 @@ private function execute_create_table_statement( WP_Parser_Node $node ): void {
29252925

29262926
// Handle IF NOT EXISTS.
29272927
if ( $subnode->has_child_node( 'ifNotExists' ) ) {
2928-
$tables_table = $this->information_schema_builder->get_table_name( $table_is_temporary, 'tables' );
2929-
$table_exists = $this->execute_sqlite_query(
2930-
sprintf(
2931-
'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ?',
2932-
$this->quote_sqlite_identifier( $tables_table )
2933-
),
2934-
array( $this->get_saved_db_name(), $table_name )
2935-
)->fetchColumn();
2928+
if ( $table_is_temporary ) {
2929+
/*
2930+
* The temporary information schema tables are created with the
2931+
* first temporary table of a session, so they may not exist yet.
2932+
*/
2933+
$table_exists = $this->information_schema_builder->temporary_table_exists( $table_name );
2934+
} else {
2935+
$tables_table = $this->information_schema_builder->get_table_name( false, 'tables' );
2936+
$table_exists = $this->execute_sqlite_query(
2937+
sprintf(
2938+
'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ?',
2939+
$this->quote_sqlite_identifier( $tables_table )
2940+
),
2941+
array( $this->get_saved_db_name(), $table_name )
2942+
)->fetchColumn();
2943+
}
29362944

29372945
if ( $table_exists ) {
29382946
$this->last_result_statement = $this->create_result_statement_from_data( array(), array() );

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,16 @@ public function record_drop_table( WP_Parser_Node $node ): void {
781781
$table_refs = $child_node->get_first_child_node( 'tableRefList' )->get_child_nodes();
782782
foreach ( $table_refs as $table_ref ) {
783783
$table_name = $this->get_table_name_from_node( $table_ref );
784-
$table_is_temporary = $has_temporary_keyword || $this->temporary_table_exists( $table_name );
784+
$table_is_temporary = $this->temporary_table_exists( $table_name );
785+
786+
/*
787+
* DROP TEMPORARY TABLE drops only temporary tables. When there is no
788+
* such table, there is nothing to record. The temporary information
789+
* schema tables may not even exist yet.
790+
*/
791+
if ( $has_temporary_keyword && ! $table_is_temporary ) {
792+
continue;
793+
}
785794

786795
$this->delete_values(
787796
$this->get_table_name( $table_is_temporary, 'tables' ),

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4433,6 +4433,68 @@ public function testCreateTemporaryTableIfNotExists(): void {
44334433
);
44344434
}
44354435

4436+
public function testCreateTemporaryTableIfNotExistsAsFirstTemporaryTable(): void {
4437+
// No temporary table exists in this session yet.
4438+
$this->assertQuery(
4439+
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
4440+
);
4441+
$this->assertQuery( "INSERT INTO t (ID, name) VALUES (1, 'a')" );
4442+
4443+
// A repeated statement is a no-op and keeps the existing data,
4444+
// also when the table name is cased differently.
4445+
$this->assertQuery(
4446+
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
4447+
);
4448+
$this->assertQuery(
4449+
'CREATE TEMPORARY TABLE IF NOT EXISTS T (ID INTEGER, name TEXT)'
4450+
);
4451+
$result = $this->assertQuery( 'SELECT ID, name FROM t' );
4452+
$this->assertEquals(
4453+
array(
4454+
(object) array(
4455+
'ID' => '1',
4456+
'name' => 'a',
4457+
),
4458+
),
4459+
$result
4460+
);
4461+
}
4462+
4463+
public function testCreateTemporaryTableIfNotExistsWithStandardTableOfSameName(): void {
4464+
$this->assertQuery( 'CREATE TABLE t (a INT)' );
4465+
4466+
// The standard table doesn't prevent creating the temporary table.
4467+
$this->assertQuery( 'CREATE TEMPORARY TABLE IF NOT EXISTS t (b INT)' );
4468+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4469+
$this->assertCount( 1, $result );
4470+
$this->assertEquals( 'b', $result[0]->Field );
4471+
4472+
// The standard table is unchanged.
4473+
$this->assertQuery( 'DROP TEMPORARY TABLE t' );
4474+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4475+
$this->assertCount( 1, $result );
4476+
$this->assertEquals( 'a', $result[0]->Field );
4477+
}
4478+
4479+
public function testDropTemporaryTableIfExistsAsFirstTemporaryTable(): void {
4480+
// No temporary table exists in this session yet.
4481+
$this->assertQuery( 'DROP TEMPORARY TABLE IF EXISTS t' );
4482+
4483+
// A standard table with the same name is not dropped.
4484+
$this->assertQuery( 'CREATE TABLE t (a INT)' );
4485+
$this->assertQuery( 'DROP TEMPORARY TABLE IF EXISTS t' );
4486+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4487+
$this->assertCount( 1, $result );
4488+
$this->assertEquals( 'a', $result[0]->Field );
4489+
}
4490+
4491+
public function testDropTemporaryTableAsFirstTemporaryTable(): void {
4492+
// No temporary table exists in this session yet.
4493+
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
4494+
$this->expectExceptionMessage( "Table 'temp.t' doesn't exist" );
4495+
$this->assertQuery( 'DROP TEMPORARY TABLE t' );
4496+
}
4497+
44364498
public function testTranslatesComplexDelete() {
44374499
$this->sqlite->query(
44384500
"CREATE TABLE wptests_dummy (

0 commit comments

Comments
 (0)