From 535a5ea476fb41738c839ac609b43d2f019cea96 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 28 Jul 2019 14:22:12 +0300 Subject: [PATCH] Documentation and tests for table.drop() method --- docs/python-api.rst | 11 +++++++++++ sqlite_utils/db.py | 2 +- tests/test_create.py | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index 362fee0..752e813 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -567,6 +567,17 @@ If you want to ensure that every foreign key column in your database has a corre db.index_foreign_keys() +.. _python_api_drop: + +Dropping a table +================ + +You can drop a table by using the ``.drop()`` method: + +.. code-block:: python + + db["my_table"].drop() + .. _python_api_hash: Setting an ID based on the hash of the row contents diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 586014b..267d0e9 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -621,7 +621,7 @@ class Table: return self def drop(self): - return self.db.conn.execute("DROP TABLE {}".format(self.name)) + self.db.conn.execute("DROP TABLE {}".format(self.name)) def guess_foreign_table(self, column): column = column.lower() diff --git a/tests/test_create.py b/tests/test_create.py index 71b1583..ca73b2d 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -775,3 +775,10 @@ def test_cannot_provide_both_filename_and_memory(): def test_creates_id_column(fresh_db): last_pk = fresh_db.table("cats", pk="id").insert({"name": "barry"}).last_pk assert [{"name": "barry", "id": last_pk}] == list(fresh_db["cats"].rows) + + +def test_drop(fresh_db): + fresh_db["t"].insert({"foo": 1}) + assert ["t"] == fresh_db.table_names() + assert None is fresh_db["t"].drop() + assert [] == fresh_db.table_names()