Repository navigation
fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() (gh-162646) - #162662
Open
vikash1311994 wants to merge 1 commit into
Open
vikash1311994 wants to merge 1 commit into
vikash1311994 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #162646.
Root cause
_iterdump()readsSELECT * FROM "sqlite_sequence"without a schema qualifier. SQLite resolves unqualified table names by searchingtempbeforemain. Creating and dropping a temporaryAUTOINCREMENTtable leaves an emptytemp.sqlite_sequencebehind, so the dump sees zero rows, emits onlyDELETE FROM "sqlite_sequence", and omits theINSERTstatements 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";.mainis the schema the rest ofiterdump()already reads viasqlite_master, so the dump now reads the counters from the same database it is dumping.Tests
DumpTests.test_dump_autoincrement_with_temp_tableinLib/test/test_sqlite3/test_dump.py: after a temp AUTOINCREMENT table is created and dropped, both the source and a database restored fromiterdump()allocate id 3.AssertionError: 2 != 3); with the fix restored, it passes../python -m test test_sqlite3: 530 run, 4 skipped, 0 failures.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 leftovertemp.sqlite_sequenceproduce identical dumps.Explicitly excluded
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.mainonly.Note for maintainers: the CPython CLA check may be pending on my account; I will complete the CLA personally if required.