mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-26 10:54:32 +02:00
sqlite-utils create-table --transform, refs #467
This commit is contained in:
parent
b09b7a5129
commit
5ef47d6c30
3 changed files with 27 additions and 4 deletions
|
|
@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue