duplicate --ignore option, refs #450

This commit is contained in:
Simon Willison 2022-07-15 15:35:58 -07:00
commit b9a89a0f2c
3 changed files with 12 additions and 2 deletions

View file

@ -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.

View file

@ -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")

View file

@ -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,