sqlite-utils create-table --transform, refs #467

This commit is contained in:
Simon Willison 2022-08-27 07:54:54 -07:00
commit 5ef47d6c30
3 changed files with 27 additions and 4 deletions

View file

@ -856,6 +856,7 @@ See :ref:`cli_create_table`.
foreign key
--ignore If table already exists, do nothing
--replace If table already exists, replace it
--transform If table already exists, try to transform the schema
--load-extension TEXT SQLite extensions to load
-h, --help Show this message and exit.

View file

@ -1445,9 +1445,24 @@ def create_database(path, enable_wal, init_spatialite, load_extension):
is_flag=True,
help="If table already exists, replace it",
)
@click.option(
"--transform",
is_flag=True,
help="If table already exists, try to transform the schema",
)
@load_extension_option
def create_table(
path, table, columns, pk, not_null, default, fk, ignore, replace, load_extension
path,
table,
columns,
pk,
not_null,
default,
fk,
ignore,
replace,
transform,
load_extension,
):
"""
Add a table with the specified columns. Columns should be specified using
@ -1482,6 +1497,8 @@ def create_table(
return
elif replace:
db[table].drop()
elif transform:
pass
else:
raise click.ClickException(
'Table "{}" already exists. Use --replace to delete and replace it.'.format(
@ -1489,7 +1506,12 @@ def create_table(
)
)
db[table].create(
coltypes, pk=pk, not_null=not_null, defaults=dict(default), foreign_keys=fk
coltypes,
pk=pk,
not_null=not_null,
defaults=dict(default),
foreign_keys=fk,
transform=transform,
)

View file

@ -1689,10 +1689,10 @@ class Table(Queryable):
create_table_not_null.add(key)
elif isinstance(not_null, set):
create_table_not_null.update((rename.get(k) or k) for k in not_null)
elif not_null is None:
elif not not_null:
pass
else:
assert False, "not_null must be a dict or a set or None"
assert False, "not_null must be a dict or a set or None, it was {}".format(repr(not_null))
# defaults=
create_table_defaults = {
(rename.get(c.name) or c.name): c.default_value