diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index 2590595..2f4f5a3 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -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. diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index fe57fbe..fe5a402 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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, ) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index e695304..25fded0 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -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