mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 20:34:12 +02:00
drop-table and drop-view commands, closes #111
This commit is contained in:
parent
396bee9236
commit
4e9cb739c7
3 changed files with 120 additions and 0 deletions
17
docs/cli.rst
17
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``.
|
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:
|
.. _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.
|
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:
|
.. _cli_add_column:
|
||||||
|
|
||||||
Adding columns
|
Adding columns
|
||||||
|
|
|
||||||
|
|
@ -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")
|
@cli.command(name="create-view")
|
||||||
@click.argument(
|
@click.argument(
|
||||||
"path",
|
"path",
|
||||||
|
|
@ -626,6 +642,23 @@ def create_view(path, view, select, ignore, replace):
|
||||||
db.create_view(view, select)
|
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()
|
@cli.command()
|
||||||
@click.argument(
|
@click.argument(
|
||||||
"path",
|
"path",
|
||||||
|
|
|
||||||
|
|
@ -1065,3 +1065,73 @@ def test_create_view_replace():
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert 0 == result.exit_code
|
||||||
assert "CREATE VIEW version AS select sqlite_version()" == db["version"].schema
|
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()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue