Repository navigation
Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table - #524
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
The IF NOT EXISTS check looked the table up in the temporary information schema tables, which are only created together with the first temporary table of a session. When the first temporary table of a session was created with IF NOT EXISTS, the lookup failed with "Table '_wp_sqlite_mysql_information_schema_tmp_tables' doesn't exist". For temporary tables, check for the table in sqlite_temp_master instead, as temporary_table_exists() already does for this reason. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…table A standard table with the same name must not make the statement a no-op. The temporary table is created and shadows the standard table, which stays unchanged.
With the TEMPORARY keyword, the DROP TABLE handler deleted the table's records from the temporary information schema tables. Those are only created with the first temporary table of a session, so when no temporary table had been created yet, DROP TEMPORARY TABLE [IF EXISTS] failed with "Table '_wp_sqlite_mysql_information_schema_tmp_tables' doesn't exist". Skip recording when the TEMPORARY keyword is used and no such temporary table exists. SQLite then drops nothing with IF EXISTS, and reports the missing table without it.
ff50b67 to
8216185
Compare
|
Thanks a lot for the fix, @Nandan108!
I also rebased the branch onto |
CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table
Summary
When the first temporary table statement of a session is
CREATE TEMPORARY TABLE IF NOT EXISTSorDROP TEMPORARY TABLE, the statement fails: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 TABLEworks, 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 xfollowed byCREATE TEMPORARY TABLE x.Fix
Both statements now check for the temporary table with
temporary_table_exists(), which looks insqlite_temp_master. It already exists for exactly this reason ("it may not exist yet"):CREATE TEMPORARY TABLE IF NOT EXISTSuses it for the existence check. Non-temporary tables are unchanged.DROP TEMPORARY TABLEskips the information schema update when no such temporary table exists. SQLite then does nothing withIF 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
trunkwith the error above:testCreateTemporaryTableIfNotExistsAsFirstTemporaryTablecreates 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.testCreateTemporaryTableIfNotExistsWithStandardTableOfSameNamechecks that a standard table with the same name doesn't make the statement a no-op, and stays unchanged.testDropTemporaryTableIfExistsAsFirstTemporaryTablechecks that the statement succeeds, and that it doesn't drop a standard table with the same name.testDropTemporaryTableAsFirstTemporaryTablechecks 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
Summary by CodeRabbit
IF EXISTS.