Skip to content

Commit 9e125ee

Browse files
committed
fix(sqlite3): preserve AUTOINCREMENT counters in iterdump() (gh-162646)
1 parent 87fc280 commit 9e125ee

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎Lib/sqlite3/dump.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _iterdump(connection, *, filter=None):
5757
sqlite_sequence = []
5858
for table_name, type, sql in schema_res.fetchall():
5959
if table_name == 'sqlite_sequence':
60-
rows = cu.execute('SELECT * FROM "sqlite_sequence";')
60+
rows = cu.execute('SELECT * FROM main."sqlite_sequence";')
6161
sqlite_sequence = ['DELETE FROM "sqlite_sequence"']
6262
sqlite_sequence += [
6363
f'INSERT INTO "sqlite_sequence" VALUES({_quote_value(table_name)},{seq_value})'

‎Lib/test/test_sqlite3/test_dump.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ def test_dump_autoincrement_create_new_db(self):
189189
rows = res.fetchall()
190190
self.assertEqual(rows[0][0], seq)
191191

192+
def test_dump_autoincrement_with_temp_table(self):
193+
# gh-162646: a dropped temporary AUTOINCREMENT table leaves an empty
194+
# temp.sqlite_sequence behind, which must not shadow the main one.
195+
self.cu.executescript("\n".join([
196+
"CREATE TABLE t1 (id INTEGER PRIMARY KEY AUTOINCREMENT);",
197+
"INSERT INTO t1 DEFAULT VALUES;",
198+
"INSERT INTO t1 DEFAULT VALUES;",
199+
"DELETE FROM t1 WHERE id = 2;",
200+
"CREATE TEMP TABLE staging (id INTEGER PRIMARY KEY AUTOINCREMENT);",
201+
"DROP TABLE temp.staging;",
202+
]))
203+
204+
dump = "\n".join(self.cx.iterdump())
205+
self.assertEqual(
206+
self.cu.execute("INSERT INTO t1 DEFAULT VALUES").lastrowid, 3)
207+
with memory_database() as cx2:
208+
cx2.executescript(dump)
209+
self.assertEqual(
210+
cx2.execute("INSERT INTO t1 DEFAULT VALUES").lastrowid, 3)
211+
192212
def test_unorderable_row(self):
193213
# iterdump() should be able to cope with unorderable row types (issue #15545)
194214
class UnorderableRow:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :meth:`sqlite3.Connection.iterdump` to preserve ``AUTOINCREMENT``
2+
counters when a temporary table with an ``AUTOINCREMENT`` column was created
3+
and dropped: the ``sqlite_sequence`` query now explicitly reads the ``main``
4+
database instead of a leftover empty ``temp.sqlite_sequence`` table.

0 commit comments

Comments
 (0)