Skip to content

Commit

Permalink
db.dropcolumn: Circumvent length limit for sqlite3 SQL STDIN string (#…
Browse files Browse the repository at this point in the history
…3273)

* db.dropcolumn: support more recent sqlite3 drop column command
* use sqlite_version()
* Update scripts/db.dropcolumn/db.dropcolumn.py
* enhance version comparison

Co-authored-by: Vaclav Petras <[email protected]>
  • Loading branch information
2 people authored and neteler committed Dec 15, 2023
1 parent 219cb98 commit c003c3f
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions scripts/db.dropcolumn/db.dropcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,42 @@ def main():
return 0

if driver == "sqlite":
# echo "Using special trick for SQLite"
# http://www.sqlite.org/faq.html#q13
colnames = []
coltypes = []
for f in gscript.db_describe(table)["cols"]:
if f[0] != column:
colnames.append(f[0])
coltypes.append("%s %s" % (f[0], f[1]))

colnames = ", ".join(colnames)
coltypes = ", ".join(coltypes)

cmds = [
"BEGIN TRANSACTION",
"CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
"INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
"DROP TABLE ${table}",
"CREATE TABLE ${table}(${coldef})",
"INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
"DROP TABLE ${table}_backup",
"COMMIT",
]
tmpl = string.Template(";\n".join(cmds))
sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames)
sqlite3_version = gscript.read_command(
"db.select",
sql="SELECT sqlite_version();",
flags="c",
database=database,
driver=driver,
).split(".")[0:2]

if [int(i) for i in sqlite3_version] >= [int(i) for i in "3.35".split(".")]:
sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
if column == "cat":
sql = "DROP INDEX %s_%s; %s" % (table, column, sql)
else:
# for older sqlite3 versions, use old way to remove column
colnames = []
coltypes = []
for f in gscript.db_describe(table)["cols"]:
if f[0] != column:
colnames.append(f[0])
coltypes.append("%s %s" % (f[0], f[1]))

colnames = ", ".join(colnames)
coltypes = ", ".join(coltypes)

cmds = [
"BEGIN TRANSACTION",
"CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
"INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
"DROP TABLE ${table}",
"CREATE TABLE ${table}(${coldef})",
"INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
"DROP TABLE ${table}_backup",
"COMMIT",
]
tmpl = string.Template(";\n".join(cmds))
sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames)
else:
sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)

Expand Down

0 comments on commit c003c3f

Please sign in to comment.