-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-79009: sqlite3.iterdump now correctly handles tables with autoincrement #9621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9bf9eb7
bpo-34828 sqlite3.iterdump now correctly handles tables with autoincr…
itssme 5d7b0ae
bpo-34828: added entry in NEWS.d/next/Library and in ACKS
itssme 1ac20b0
Merge remote-tracking branch 'origin/master' into fix-issue-34828
itssme 365a2f1
bpo-34828 Removed redundant code and made some small formatting chang…
itssme 7c5901b
Merge branch 'main' into fix-issue-34828
itssme b259219
bpo-34828 formatting changes as well as readability improvements in s…
itssme 3812bf6
Merge branch 'main' into fix-issue-34828
itssme daed6e5
bpo-34828: reformatted some tests in test_sqlite3/test.dump.py as wel…
itssme d5daa91
Merge branch 'main' into fix-issue-34828
erlend-aasland 4e573e9
bpo-34828: minified test case for some sqlite3 dump command tests, fi…
itssme 64f4101
Merge branch 'main' into fix-issue-34828
erlend-aasland 1abc7d0
Clean up dump.py
erlend-aasland 6bfac7e
Cleanup: don't use abbreviated link in NEWS entry
erlend-aasland 103c50f
Cleanup: tests
erlend-aasland 53076e9
Fix 1abc7d05b7a605224611a09eaa74e50ef1cc38e1
erlend-aasland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# Author: Paul Kippes <[email protected]> | ||
|
||
import unittest | ||
import sqlite3 as sqlite | ||
import unittest | ||
|
||
itssme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class DumpTests(unittest.TestCase): | ||
def setUp(self): | ||
|
@@ -49,6 +50,67 @@ def test_table_dump(self): | |
[self.assertEqual(expected_sqls[i], actual_sqls[i]) | ||
for i in range(len(expected_sqls))] | ||
|
||
def test_dump_autoincrement(self): | ||
expected_sqls = [ | ||
"""CREATE TABLE "posts" (id int primary key);""" | ||
, | ||
"""INSERT INTO "posts" VALUES(0);""" | ||
, | ||
"CREATE TABLE \"tags\" ( " \ | ||
"id integer primary key autoincrement," \ | ||
"tag varchar(256)," \ | ||
"post int references posts);" | ||
, | ||
"""INSERT INTO "tags" VALUES(NULL,'test',0);""" | ||
, | ||
"CREATE TABLE \"tags2\" ( " \ | ||
"id integer primary key autoincrement," \ | ||
"tag varchar(256)," \ | ||
"post int references posts);" | ||
] | ||
[self.cu.execute(s) for s in expected_sqls] | ||
i = self.cx.iterdump() | ||
actual_sqls = [s for s in i] | ||
# the NULL value should now be automatically be set to 1 | ||
expected_sqls[3] = expected_sqls[3].replace("NULL", "1") | ||
expected_sqls = ['BEGIN TRANSACTION;'] + expected_sqls + \ | ||
['DELETE FROM "sqlite_sequence";'] + \ | ||
["""INSERT INTO "sqlite_sequence" VALUES('tags',1);"""] + \ | ||
['COMMIT;'] | ||
[self.assertEqual(expected_sqls[i], actual_sqls[i]) | ||
for i in range(len(expected_sqls))] | ||
itssme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_dump_autoincrement_create_new_db(self): | ||
old_db = [ | ||
"BEGIN TRANSACTION ;" | ||
, | ||
"""CREATE TABLE "posts" (id int primary key);""" | ||
, | ||
"""INSERT INTO "posts" VALUES(0);""" | ||
, | ||
"CREATE TABLE \"tags\" ( " \ | ||
"id integer primary key autoincrement," \ | ||
"tag varchar(256)," \ | ||
"post int references posts);" | ||
, | ||
"CREATE TABLE \"tags2\" ( " \ | ||
"id integer primary key autoincrement," \ | ||
"tag varchar(256)," \ | ||
"post int references posts);" | ||
] | ||
[old_db.append("""INSERT INTO "tags" VALUES(NULL,'test{0}',0);""".format(i)) for i in range(1, 10)] | ||
[old_db.append("""INSERT INTO "tags2" VALUES(NULL,'test{0}',0);""".format(i)) for i in range(1, 5)] | ||
old_db.append("COMMIT;") | ||
[self.cu.execute(s) for s in old_db] | ||
cx2 = sqlite.connect(":memory:") | ||
query = "".join(line for line in self.cx.iterdump()) | ||
cx2.executescript(query) | ||
cu2 = cx2.cursor() | ||
self.assertEqual(cu2.execute('SELECT "seq" FROM "sqlite_sequence" WHERE "name" == "tags"').fetchall()[0][0], 9) | ||
self.assertEqual(cu2.execute('SELECT "seq" FROM "sqlite_sequence" WHERE "name" == "tags2"').fetchall()[0][0], 4) | ||
cu2.close() | ||
cx2.close() | ||
|
||
def test_unorderable_row(self): | ||
# iterdump() should be able to cope with unorderable row types (issue #15545) | ||
class UnorderableRow: | ||
|
@@ -70,6 +132,7 @@ def __getitem__(self, index): | |
got = list(self.cx.iterdump()) | ||
self.assertEqual(expected, got) | ||
|
||
|
||
def suite(): | ||
tests = [ | ||
DumpTests, | ||
|
@@ -78,9 +141,11 @@ def suite(): | |
[unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] | ||
) | ||
|
||
|
||
def test(): | ||
runner = unittest.TextTestRunner() | ||
runner.run(suite()) | ||
|
||
|
||
if __name__ == "__main__": | ||
test() |
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
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2018-09-28-22-18-03.bpo-34828.5Zyi_S.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Sqlite3.iterdump now correctly deletes the "sqlite_sequence" table and | ||
inserts the rows from the dumped db. Just like the command ".dump" in | ||
sqlite3 does. | ||
itssme marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.