From b9a89a0f2c3559989efe65f25a6e1f8fa76fe8b0 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 15 Jul 2022 15:35:58 -0700 Subject: [PATCH] duplicate --ignore option, refs #450 --- docs/cli-reference.rst | 1 + sqlite_utils/cli.py | 6 ++++-- tests/test_cli.py | 7 +++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index 6d6f837..86225aa 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -1188,6 +1188,7 @@ duplicate Create a duplicate of this table, copying across the schema and all row data. Options: + --ignore If table does not exist, do nothing --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 de223af..fe57fbe 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1501,8 +1501,9 @@ def create_table( ) @click.argument("table") @click.argument("new_table") +@click.option("--ignore", is_flag=True, help="If table does not exist, do nothing") @load_extension_option -def duplicate(path, table, new_table, load_extension): +def duplicate(path, table, new_table, ignore, load_extension): """ Create a duplicate of this table, copying across the schema and all row data. """ @@ -1511,7 +1512,8 @@ def duplicate(path, table, new_table, load_extension): try: db[table].duplicate(new_table) except NoTable: - raise click.ClickException('Table "{}" does not exist'.format(table)) + if not ignore: + raise click.ClickException('Table "{}" does not exist'.format(table)) @cli.command(name="drop-table") diff --git a/tests/test_cli.py b/tests/test_cli.py index 3694fd7..061a578 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2180,6 +2180,13 @@ def test_duplicate_table(tmpdir): ) assert result_error.exit_code == 1 assert result_error.output == 'Error: Table "missing" does not exist\n' + # And check --ignore works + result_error2 = CliRunner().invoke( + cli.cli, + ["duplicate", db_path, "missing", "two", "--ignore"], + catch_exceptions=False, + ) + assert result_error2.exit_code == 0 # Now try for a table that exists result = CliRunner().invoke( cli.cli,