Skip to content

Commit 18f190e

Browse files
committed
sqlite-utils rename-table command, refs #565
1 parent 82e8cd3 commit 18f190e

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

docs/cli-reference.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ This page lists the ``--help`` for every ``sqlite-utils`` CLI sub-command.
4040
"vacuum": "cli_vacuum",
4141
"dump": "cli_dump",
4242
"add-column": "cli_add_column",
43+
"rename-table": "cli_renaming_tables",
44+
"duplicate": "cli_duplicate_table",
4345
"add-foreign-key": "cli_add_foreign_key",
4446
"add-foreign-keys": "cli_add_foreign_keys",
4547
"index-foreign-keys": "cli_index_foreign_keys",
@@ -1328,6 +1330,8 @@ reset-counts
13281330
duplicate
13291331
=========
13301332

1333+
See :ref:`cli_duplicate_table`.
1334+
13311335
::
13321336

13331337
Usage: sqlite-utils duplicate [OPTIONS] PATH TABLE NEW_TABLE
@@ -1340,6 +1344,25 @@ duplicate
13401344
-h, --help Show this message and exit.
13411345

13421346

1347+
.. _cli_ref_rename_table:
1348+
1349+
rename-table
1350+
============
1351+
1352+
See :ref:`cli_renaming_tables`.
1353+
1354+
::
1355+
1356+
Usage: sqlite-utils rename-table [OPTIONS] PATH TABLE NEW_NAME
1357+
1358+
Rename this table.
1359+
1360+
Options:
1361+
--ignore If table does not exist, do nothing
1362+
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
1363+
-h, --help Show this message and exit.
1364+
1365+
13431366
.. _cli_ref_drop_table:
13441367

13451368
drop-table

docs/cli.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,19 @@ If a table with the same name already exists, you will get an error. You can cho
19361936

19371937
You can also pass ``--transform`` to transform the existing table to match the new schema. See :ref:`python_api_explicit_create` in the Python library documentation for details of how this option works.
19381938

1939+
.. _cli_renaming_tables:
1940+
1941+
Renaming a table
1942+
================
1943+
1944+
Yo ucan rename a table using the ``rename-table`` command:
1945+
1946+
.. code-block:: bash
1947+
1948+
sqlite-utils rename-table mydb.db oldname newname
1949+
1950+
Pass ``--ignore`` to ignore any errors caused by the table not existing, or the new name already being in use.
1951+
19391952
.. _cli_duplicate_table:
19401953

19411954
Duplicating tables

sqlite_utils/cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,31 @@ def duplicate(path, table, new_table, ignore, load_extension):
15841584
raise click.ClickException('Table "{}" does not exist'.format(table))
15851585

15861586

1587+
@cli.command(name="rename-table")
1588+
@click.argument(
1589+
"path",
1590+
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
1591+
required=True,
1592+
)
1593+
@click.argument("table")
1594+
@click.argument("new_name")
1595+
@click.option("--ignore", is_flag=True, help="If table does not exist, do nothing")
1596+
@load_extension_option
1597+
def rename_table(path, table, new_name, ignore, load_extension):
1598+
"""
1599+
Rename this table.
1600+
"""
1601+
db = sqlite_utils.Database(path)
1602+
_load_extensions(db, load_extension)
1603+
try:
1604+
db.rename_table(table, new_name)
1605+
except sqlite3.OperationalError as ex:
1606+
if not ignore:
1607+
raise click.ClickException(
1608+
'Table "{}" could not be renamed. {}'.format(table, str(ex))
1609+
)
1610+
1611+
15871612
@cli.command(name="drop-table")
15881613
@click.argument(
15891614
"path",

tests/test_cli.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,38 @@ def test_analyze(tmpdir, options, expected):
22742274
assert list(db["sqlite_stat1"].rows) == expected
22752275

22762276

2277+
def test_rename_table(tmpdir):
2278+
db_path = str(tmpdir / "test.db")
2279+
db = Database(db_path)
2280+
db["one"].insert({"id": 1, "name": "Cleo"}, pk="id")
2281+
# First try a non-existent table
2282+
result_error = CliRunner().invoke(
2283+
cli.cli,
2284+
["rename-table", db_path, "missing", "two"],
2285+
catch_exceptions=False,
2286+
)
2287+
assert result_error.exit_code == 1
2288+
assert result_error.output == (
2289+
'Error: Table "missing" could not be renamed. ' "no such table: missing\n"
2290+
)
2291+
# And check --ignore works
2292+
result_error2 = CliRunner().invoke(
2293+
cli.cli,
2294+
["rename-table", db_path, "missing", "two", "--ignore"],
2295+
catch_exceptions=False,
2296+
)
2297+
assert result_error2.exit_code == 0
2298+
previous_columns = db["one"].columns_dict
2299+
# Now try for a table that exists
2300+
result = CliRunner().invoke(
2301+
cli.cli,
2302+
["rename-table", db_path, "one", "two"],
2303+
catch_exceptions=False,
2304+
)
2305+
assert result.exit_code == 0
2306+
assert db["two"].columns_dict == previous_columns
2307+
2308+
22772309
def test_duplicate_table(tmpdir):
22782310
db_path = str(tmpdir / "test.db")
22792311
db = Database(db_path)

0 commit comments

Comments
 (0)