sqlite-utils transform --column-order option, closes #176

This commit is contained in:
Simon Willison 2020-09-24 09:11:53 -07:00
commit d13c123100
3 changed files with 10 additions and 0 deletions

View file

@ -571,6 +571,9 @@ Every option for this table (with the exception of ``--pk-none``) can be specifi
``--rename column-name new-name``
Rename this column to a new name.
``--column-order column``
Use this multiple times to specify a new order for your columns. ``-o`` shortcut is also available.
``--not-null column-name``
Set this column as ``NOT NULL``.

View file

@ -904,6 +904,7 @@ def rows(ctx, path, dbtable, nl, arrays, csv, no_headers, table, fmt, json_cols)
@click.option(
"--rename", type=(str, str), multiple=True, help="Rename this column to X"
)
@click.option("-o", "--column-order", type=str, multiple=True, help="Reorder columns")
@click.option("--not-null", type=str, multiple=True, help="Set this column to NOT NULL")
@click.option(
"--not-null-false", type=str, multiple=True, help="Remove NOT NULL from this column"
@ -934,6 +935,7 @@ def transform(
type,
drop,
rename,
column_order,
not_null,
not_null_false,
pk,
@ -969,6 +971,7 @@ def transform(
kwargs["types"] = types
kwargs["drop"] = set(drop)
kwargs["rename"] = dict(rename)
kwargs["column_order"] = column_order or None
kwargs["not_null"] = not_null_dict
if pk:
if len(pk) == 1:

View file

@ -1496,6 +1496,10 @@ def test_add_foreign_keys(db_path):
["--default-none", "age"],
'CREATE TABLE "dogs" (\n [id] INTEGER PRIMARY KEY,\n [age] INTEGER NOT NULL,\n [name] TEXT\n)',
),
(
["-o", "name", "--column-order", "age", "-o", "id"],
"CREATE TABLE \"dogs\" (\n [name] TEXT,\n [age] INTEGER NOT NULL DEFAULT '1',\n [id] INTEGER PRIMARY KEY\n)",
),
],
)
def test_transform(db_path, args, expected_schema):