Skip to content

Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table - #524

Merged
JanJakes merged 3 commits into
WordPress:trunkfrom
Nandan108:fix/create-temporary-table-if-not-exists
Oct 13, 2026
Merged

JanJakes merged 3 commits into
WordPress:trunkfrom
Nandan108:fix/create-temporary-table-if-not-exists

Conversation

@Nandan108

@Nandan108 Nandan108 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When the first temporary table statement of a session is CREATE TEMPORARY TABLE IF NOT EXISTS or DROP TEMPORARY TABLE, the statement fails:

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

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.

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cd1e70f6-9458-4a30-88a7-a049e761b099
📥 Commits

Reviewing files that changed from the base of the PR and between 5deb4d6 and 8216185.

📒 Files selected for processing (3)
  • packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php
  • packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php
  • packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Walkthrough

The change adjusts temporary-table existence checks for CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE. Tests cover repeated creation, same-name persistent tables, and missing temporary tables.

Changes: Temporary Table DDL

Layer / File(s) Summary
Temporary table creation checks
packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php, packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Creation checks for an existing temporary table with temporary_table_exists(). Tests cover first creation and repeated calls with differently cased names.
Temporary table drop checks
packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php, packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Drop handling selects temporary metadata only when the named temporary table exists. Tests cover same-name persistent tables and missing temporary tables, with and without IF EXISTS.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: janjakes

Merge Risk: ⚪ Minimal · up to 82161

The first temporary-table creation is covered by the change and its tests. No actionable merge-blocking risk remains after normal checks.

Pre-merge checks · Passed 5 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Description check Passed The PR description clearly explains the temporary-table behavior, the implementation approach, the added tests, and the reported test results.
Linked Issues check Passed The PR objectives reference the related work in PR #524 and describe the problem that this change addresses.
Out of Scope Changes check Passed The code and tests address temporary-table creation and dropping behavior described in the objectives. No unrelated changes are identified.
Title check Passed The title clearly identifies the fixes for CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE when handling the first temporary table.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Nandan108 and others added 3 commits October 13, 2026 16:31
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.
@JanJakes
JanJakes force-pushed the fix/create-temporary-table-if-not-exists branch from ff50b67 to 8216185 Compare October 13, 2026 14:32
@JanJakes JanJakes changed the title Fix CREATE TEMPORARY TABLE IF NOT EXISTS as the first temporary table Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table Oct 13, 2026
@JanJakes

Copy link
Copy Markdown
Member

Thanks a lot for the fix, @Nandan108!

DROP TEMPORARY TABLE [IF EXISTS] had the same problem as the first temporary table statement of a session, so I added a fix for it here. The common DROP TEMPORARY TABLE IF EXISTS x + CREATE TEMPORARY TABLE x pattern now works too. I added:

  • A test for a standard table with the same name (c2efabd). CREATE TEMPORARY TABLE IF NOT EXISTS still creates the temporary table, and the standard table stays unchanged.
  • A fix for DROP TEMPORARY TABLE as the first temporary table (8216185). It now uses temporary_table_exists() too, and has tests with and without IF EXISTS.

I also rebased the branch onto trunk and updated the title and description.

@JanJakes
JanJakes merged commit 8dc5c27 into WordPress:trunk Oct 13, 2026
24 checks passed
@JanJakes JanJakes changed the title Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table Fix CREATE TEMPORARY TABLE IF NOT EXISTS and DROP TEMPORARY TABLE as the first temporary table Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants