Skip to content

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 15 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bpo-34828 formatting changes as well as readability improvements in s…
…qlite3 dump tests
  • Loading branch information
itssme committed Oct 15, 2021
commit b2592197a23fe0c47faa4811eafe397401cb52e2
89 changes: 49 additions & 40 deletions Lib/sqlite3/test/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,62 +51,71 @@ def test_table_dump(self):

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);"
"""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]

for sql_statement in expected_sqls:
self.cu.execute(sql_statement)
iterdump = self.cx.iterdump()
actual_sqls = [sql_statement for sql_statement in iterdump]

# 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))]

for i in range(len(expected_sqls)):
self.assertEqual(expected_sqls[i], actual_sqls[i])

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);"
"""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)]
for i in range(1, 10):
old_db.append("""INSERT INTO "tags"
VALUES(NULL,'test{0}',0);""".format(i))
for i in range(1, 5):
old_db.append("""INSERT INTO "tags2"
VALUES(NULL,'test{0}',0);""".format(i))
old_db.append("COMMIT;")
[self.cu.execute(s) for s in old_db]

for sql_statement in old_db:
self.cu.execute(sql_statement)

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)

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()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
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.
:meth:`~sqlite3.Connection.iterdump` now handles databases that use ``AUTOINCREMENT`` in one or more tables.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy