| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,9 +28,16 @@ def _iterdump(connection): | |||
| 28 | 28 | ORDER BY "name" | |
| 29 | 29 | """ | |
| 30 | 30 | schema_res = cu.execute(q) | |
| 31 | + sqlite_sequence = [] | ||
| 31 | 32 | for table_name, type, sql in schema_res.fetchall(): | |
| 32 | 33 | if table_name == 'sqlite_sequence': | |
| 33 | - yield('DELETE FROM "sqlite_sequence";') | ||
| 34 | + rows = cu.execute('SELECT * FROM "sqlite_sequence";').fetchall() | ||
| 35 | + sqlite_sequence = ['DELETE FROM "sqlite_sequence"'] | ||
| 36 | + sqlite_sequence += [ | ||
| 37 | + f'INSERT INTO "sqlite_sequence" VALUES(\'{row[0]}\',{row[1]})' | ||
| 38 | + for row in rows | ||
| 39 | + ] | ||
| 40 | + continue | ||
| 34 | 41 | elif table_name == 'sqlite_stat1': | |
| 35 | 42 | yield('ANALYZE "sqlite_master";') | |
| 36 | 43 | elif table_name.startswith('sqlite_'): | |
@@ -67,4 +74,9 @@ def _iterdump(connection): | |||
| 67 | 74 | for name, type, sql in schema_res.fetchall(): | |
| 68 | 75 | yield('{0};'.format(sql)) | |
| 69 | 76 | ||
| 77 | + # gh-79009: Yield statements concerning the sqlite_sequence table at the | ||
| 78 | + # end of the transaction. | ||
| 79 | + for row in sqlite_sequence: | ||
| 80 | + yield('{0};'.format(row)) | ||
| 81 | + | ||
| 70 | 82 | yield('COMMIT;') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import unittest | |
| 4 | 4 | import sqlite3 as sqlite | |
| 5 | + from .test_dbapi import memory_database | ||
| 6 | + | ||
| 5 | 7 | ||
| 6 | 8 | class DumpTests(unittest.TestCase): | |
| 7 | 9 | def setUp(self): | |
@@ -49,6 +51,51 @@ def test_table_dump(self): | |||
| 49 | 51 | [self.assertEqual(expected_sqls[i], actual_sqls[i]) | |
| 50 | 52 | for i in range(len(expected_sqls))] | |
| 51 | 53 | ||
| 54 | + def test_dump_autoincrement(self): | ||
| 55 | + expected = [ | ||
| 56 | + 'CREATE TABLE "t1" (id integer primary key autoincrement);', | ||
| 57 | + 'INSERT INTO "t1" VALUES(NULL);', | ||
| 58 | + 'CREATE TABLE "t2" (id integer primary key autoincrement);', | ||
| 59 | + ] | ||
| 60 | + self.cu.executescript("".join(expected)) | ||
| 61 | + | ||
| 62 | + # the NULL value should now be automatically be set to 1 | ||
| 63 | + expected[1] = expected[1].replace("NULL", "1") | ||
| 64 | + expected.insert(0, "BEGIN TRANSACTION;") | ||
| 65 | + expected.extend([ | ||
| 66 | + 'DELETE FROM "sqlite_sequence";', | ||
| 67 | + 'INSERT INTO "sqlite_sequence" VALUES(\'t1\',1);', | ||
| 68 | + 'COMMIT;', | ||
| 69 | + ]) | ||
| 70 | + | ||
| 71 | + actual = [stmt for stmt in self.cx.iterdump()] | ||
| 72 | + self.assertEqual(expected, actual) | ||
| 73 | + | ||
| 74 | + def test_dump_autoincrement_create_new_db(self): | ||
| 75 | + self.cu.execute("BEGIN TRANSACTION") | ||
| 76 | + self.cu.execute("CREATE TABLE t1 (id integer primary key autoincrement)") | ||
| 77 | + self.cu.execute("CREATE TABLE t2 (id integer primary key autoincrement)") | ||
| 78 | + self.cu.executemany("INSERT INTO t1 VALUES(?)", ((None,) for _ in range(9))) | ||
| 79 | + self.cu.executemany("INSERT INTO t2 VALUES(?)", ((None,) for _ in range(4))) | ||
| 80 | + self.cx.commit() | ||
| 81 | + | ||
| 82 | + with memory_database() as cx2: | ||
| 83 | + query = "".join(self.cx.iterdump()) | ||
| 84 | + cx2.executescript(query) | ||
| 85 | + cu2 = cx2.cursor() | ||
| 86 | + | ||
| 87 | + dataset = ( | ||
| 88 | + ("t1", 9), | ||
| 89 | + ("t2", 4), | ||
| 90 | + ) | ||
| 91 | + for table, seq in dataset: | ||
| 92 | + with self.subTest(table=table, seq=seq): | ||
| 93 | + res = cu2.execute(""" | ||
| 94 | + SELECT "seq" FROM "sqlite_sequence" WHERE "name" == ? | ||
| 95 | + """, (table,)) | ||
| 96 | + rows = res.fetchall() | ||
| 97 | + self.assertEqual(rows[0][0], seq) | ||
| 98 | + | ||
| 52 | 99 | def test_unorderable_row(self): | |
| 53 | 100 | # iterdump() should be able to cope with unorderable row types (issue #15545) | |
| 54 | 101 | class UnorderableRow: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -934,6 +934,7 @@ Ron Klatchko | |||
| 934 | 934 | Reid Kleckner | |
| 935 | 935 | Carsten Klein | |
| 936 | 936 | Bastian Kleineidam | |
| 937 | + Joel Klimont | ||
| 937 | 938 | Bob Kline | |
| 938 | 939 | Matthias Klose | |
| 939 | 940 | Jeremy Kloth | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + :meth:`sqlite3.Connection.iterdump` now handles databases that use ``AUTOINCREMENT`` in one or more tables. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments