Skip to content

Commit 4b48973

Browse files
committed
Handle generated columns in dump
Fixes rogerbinns#556 I also audited all other uses of table_info and they are fine staying as that because we do want to deal with generated columns as regular ones in those cases
1 parent 0261dd5 commit 4b48973

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

apsw/shell.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,13 @@ def sqldef(s):
14911491
self.write(self.stdout, "DROP TABLE IF EXISTS " + self._fmt_sql_identifier(table) + ";\n")
14921492
self.write(self.stdout, sqldef(sql[0]))
14931493
self._output_table = self._fmt_sql_identifier(table)
1494-
self.process_sql("select * from " + self._fmt_sql_identifier(table), internal=True)
1494+
columns = ",".join(
1495+
self._fmt_sql_identifier(column)
1496+
for (column,) in self.db.execute(
1497+
"select name from pragma_table_xinfo(?) where hidden=0", (table,)
1498+
)
1499+
)
1500+
self.process_sql(f"select {columns} from " + self._fmt_sql_identifier(table), internal=True)
14951501
# Now any indices or triggers
14961502
first = True
14971503
for name, sql in self.db.execute(

apsw/tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5383,6 +5383,38 @@ def __init__(self):
53835383
except ZeroDivisionError:
53845384
pass
53855385

5386+
def testIssue556(self):
5387+
"shell dump with generated columns"
5388+
self.db.execute("""
5389+
create table t(one, two GENERATED ALWAYS AS (one + 2), three, four GENERATED ALWAYS AS (three + 4));
5390+
insert into t values(1, 1), (2, 2), (3, 4);
5391+
""")
5392+
5393+
output = io.StringIO()
5394+
apsw.Shell(db=self.db, stdout=output).process_command(".dump")
5395+
dump = output.getvalue()
5396+
5397+
self.assertIn("GENERATED ALWAYS", dump)
5398+
5399+
self.db.execute("drop table t")
5400+
self.db.execute(dump)
5401+
5402+
# make sure the table defition survived
5403+
self.assertEqual(
5404+
self.db.execute("select * from pragma_table_xinfo('t')").get,
5405+
[
5406+
(0, "one", "", 0, None, 0, 0),
5407+
(1, "two", "", 0, None, 0, 2),
5408+
(2, "three", "", 0, None, 0, 0),
5409+
(3, "four", "", 0, None, 0, 2),
5410+
],
5411+
)
5412+
5413+
# and the contents
5414+
self.assertEqual(
5415+
self.db.execute("select * from t order by one").get, [(1, 3, 1, 5), (2, 4, 2, 6), (3, 5, 4, 8)]
5416+
)
5417+
53865418
def testCursorGet(self):
53875419
"Cursor.get"
53885420
for query, expected in (

doc/changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ APSW changes by version
1313
3.49.2.0
1414
========
1515

16-
No APSW changes.
16+
Shell dump command handles generated columns correctly. (:issue:`556`)
1717

1818
3.49.1.0
1919
========

0 commit comments

Comments
 (0)