diff --git a/docs/cli.rst b/docs/cli.rst index 40ac963..119e49d 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -401,6 +401,14 @@ You can specify foreign key relationships between the tables you are creating us If a table with the same name already exists, you will get an error. You can choose to silently ignore this error with ``--ignore``, or you can replace the existing table with a new, empty table using ``--replace``. +.. _cli_drop_table: + +Dropping tables +=============== + +You can drop a table using the ``drop-table`` command:: + + $ sqlite-utils drop-table mytable .. _cli_create_view: @@ -416,6 +424,15 @@ You can create a view using the ``create-view`` command:: Use ``--replace`` to replace an existing view of the same name, and ``--ignore`` to do nothing if a view already exists. +.. _cli_drop_view: + +Dropping views +============== + +You can drop a view using the ``drop-view`` command:: + + $ sqlite-utils drop-view myview + .. _cli_add_column: Adding columns diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index f1b5ac6..fd74499 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -594,6 +594,22 @@ def create_table(path, table, columns, pk, not_null, default, fk, ignore, replac ) +@cli.command(name="drop-table") +@click.argument( + "path", + type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), + required=True, +) +@click.argument("table") +def drop_table(path, table): + "Drop the specified table" + db = sqlite_utils.Database(path) + if table in db.table_names(): + db[table].drop() + else: + raise click.ClickException('Table "{}" does not exist'.format(table)) + + @cli.command(name="create-view") @click.argument( "path", @@ -626,6 +642,23 @@ def create_view(path, view, select, ignore, replace): db.create_view(view, select) +@cli.command(name="drop-view") +@click.argument( + "path", + type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), + required=True, +) +@click.argument("view") +def drop_view(path, view): + "Drop the specified view" + db = sqlite_utils.Database(path) + if view in db.view_names(): + db[view].drop() + else: + raise click.ClickException('View "{}" does not exist'.format(view)) + + + @cli.command() @click.argument( "path", diff --git a/tests/test_cli.py b/tests/test_cli.py index 32ea1db..e7ded29 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1065,3 +1065,73 @@ def test_create_view_replace(): ) assert 0 == result.exit_code assert "CREATE VIEW version AS select sqlite_version()" == db["version"].schema + + +def test_drop_table(): + runner = CliRunner() + with runner.isolated_filesystem(): + db = Database("test.db") + db["t"].create({"pk": int}, pk="pk") + assert "t" in db.table_names() + result = runner.invoke( + cli.cli, + [ + "drop-table", + "test.db", + "t", + ], + ) + assert 0 == result.exit_code + assert "t" not in db.table_names() + + +def test_drop_table_error(): + runner = CliRunner() + with runner.isolated_filesystem(): + db = Database("test.db") + db["t"].create({"pk": int}, pk="pk") + result = runner.invoke( + cli.cli, + [ + "drop-table", + "test.db", + "t2", + ], + ) + assert 1 == result.exit_code + assert 'Error: Table "t2" does not exist' == result.output.strip() + + +def test_drop_view(): + runner = CliRunner() + with runner.isolated_filesystem(): + db = Database("test.db") + db.create_view("hello", "select 1") + assert "hello" in db.view_names() + result = runner.invoke( + cli.cli, + [ + "drop-view", + "test.db", + "hello", + ], + ) + assert 0 == result.exit_code + assert "hello" not in db.view_names() + + +def test_drop_view_error(): + runner = CliRunner() + with runner.isolated_filesystem(): + db = Database("test.db") + db["t"].create({"pk": int}, pk="pk") + result = runner.invoke( + cli.cli, + [ + "drop-view", + "test.db", + "t2", + ], + ) + assert 1 == result.exit_code + assert 'Error: View "t2" does not exist' == result.output.strip()