Skip to content

fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() (gh-162646) - #162662

Open
vikash1311994 wants to merge 1 commit into
python:mainfrom
vikash1311994:fix/gh-162646-sqlite3-dump-temp-sequence
Open

vikash1311994 wants to merge 1 commit into
python:mainfrom
vikash1311994:fix/gh-162646-sqlite3-dump-temp-sequence

Conversation

@vikash1311994

Copy link
Copy Markdown

Fixes #162646.

Root cause

_iterdump() reads SELECT * FROM "sqlite_sequence" without a schema qualifier. SQLite resolves unqualified table names by searching temp before main. Creating and dropping a temporary AUTOINCREMENT table leaves an empty temp.sqlite_sequence behind, so the dump sees zero rows, emits only DELETE FROM "sqlite_sequence", and omits the INSERT statements that restore the counters. A database restored from that dump then reuses IDs that were already issued (reproduced: source allocates 3, restored allocates 2).

Fix

Qualify the query as SELECT * FROM main."sqlite_sequence";. main is the schema the rest of iterdump() already reads via sqlite_master, so the dump now reads the counters from the same database it is dumping.

Tests

  • Added DumpTests.test_dump_autoincrement_with_temp_table in Lib/test/test_sqlite3/test_dump.py: after a temp AUTOINCREMENT table is created and dropped, both the source and a database restored from iterdump() allocate id 3.
  • Sabotage check: with the one-line fix reverted, the new test fails (AssertionError: 2 != 3); with the fix restored, it passes.
  • ./python -m test test_sqlite3: 530 run, 4 skipped, 0 failures.
  • Added a Misc/NEWS.d/next/Library/ entry for sqlite3.Connection.iterdump() can lose AUTOINCREMENT counters after a temporary table is dropped #162646.

Risk

Low. One pure-Python query change in Lib/sqlite3/dump.py; no C code, no schema changes, no API changes. Databases without a leftover temp.sqlite_sequence produce identical dumps.

Explicitly excluded

  • No changes to the generated DELETE FROM "sqlite_sequence" / INSERT INTO "sqlite_sequence" ... statements: they are replayed into the destination database, where qualifying them would change existing dump output and is unnecessary here.
  • No backports to 3.12–3.15; this PR targets main only.
  • No unrelated cleanup or formatting changes.

Note for maintainers: the CPython CLA check may be pending on my account; I will complete the CLA personally if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite3.Connection.iterdump() can lose AUTOINCREMENT counters after a temporary table is dropped

1 participant