Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _iterdump(connection, *, filter=None):
sqlite_sequence = []
for table_name, type, sql in schema_res.fetchall():
if table_name == 'sqlite_sequence':
rows = cu.execute('SELECT * FROM "sqlite_sequence";')
rows = cu.execute('SELECT * FROM main."sqlite_sequence";')
sqlite_sequence = ['DELETE FROM "sqlite_sequence"']
sqlite_sequence += [
f'INSERT INTO "sqlite_sequence" VALUES({_quote_value(table_name)},{seq_value})'
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ def test_dump_autoincrement_create_new_db(self):
rows = res.fetchall()
self.assertEqual(rows[0][0], seq)

def test_dump_autoincrement_with_temp_table(self):
# gh-162646: a dropped temporary AUTOINCREMENT table leaves an empty
# temp.sqlite_sequence behind, which must not shadow the main one.
self.cu.executescript("\n".join([
"CREATE TABLE t1 (id INTEGER PRIMARY KEY AUTOINCREMENT);",
"INSERT INTO t1 DEFAULT VALUES;",
"INSERT INTO t1 DEFAULT VALUES;",
"DELETE FROM t1 WHERE id = 2;",
"CREATE TEMP TABLE staging (id INTEGER PRIMARY KEY AUTOINCREMENT);",
"DROP TABLE temp.staging;",
]))

dump = "\n".join(self.cx.iterdump())
self.assertEqual(
self.cu.execute("INSERT INTO t1 DEFAULT VALUES").lastrowid, 3)
with memory_database() as cx2:
cx2.executescript(dump)
self.assertEqual(
cx2.execute("INSERT INTO t1 DEFAULT VALUES").lastrowid, 3)

def test_unorderable_row(self):
# iterdump() should be able to cope with unorderable row types (issue #15545)
class UnorderableRow:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :meth:`sqlite3.Connection.iterdump` to preserve ``AUTOINCREMENT``
counters when a temporary table with an ``AUTOINCREMENT`` column was created
and dropped: the ``sqlite_sequence`` query now explicitly reads the ``main``
database instead of a leftover empty ``temp.sqlite_sequence`` table.
Loading