Skip to content

gh-162646: preserve AUTOINCREMENT counters in iterdump() - #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.

@picnixz picnixz added the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Nov 28, 2026
@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

We believe this contribution does not meet our guidelines for using AI tools, so we're closing it. If you think we've misunderstood your contribution, please let us know. You're welcome to explain your approach or ask questions about the guidelines.

Please read our guidelines for using AI tools.

@bedevere-app bedevere-app Bot closed this Nov 28, 2026
@picnixz

picnixz commented Nov 28, 2026

Copy link
Copy Markdown
Member

wrong label, sorry

@picnixz picnixz reopened this Nov 28, 2026
@picnixz picnixz removed the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Nov 28, 2026
@picnixz picnixz changed the title fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() (gh-162646) fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() ) Nov 28, 2026
@picnixz picnixz changed the title fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() ) gh-162646: preserve AUTOINCREMENT counters in iterdump() Nov 28, 2026
@picnixz picnixz added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor and removed needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 28, 2026
@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

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

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

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

2 participants