drop-table and drop-view commands, closes #111

This commit is contained in:
Simon Willison 2020-05-10 17:44:21 -07:00
commit 4e9cb739c7
3 changed files with 120 additions and 0 deletions

View file

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

View file

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

View file

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