diff --git a/docs/cli.rst b/docs/cli.rst index 85fba97..c05f05d 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -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``. diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 7948d27..535d3d0 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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: diff --git a/tests/test_cli.py b/tests/test_cli.py index 0dcf331..5aad909 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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):