Skip to content

Commit

Permalink
table.drop(ignore=True) option, refs #237
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 25, 2021
1 parent a76e3b3 commit c236894
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,12 @@ You can drop a table or view using the ``.drop()`` method:
db["my_table"].drop()
Pass ``ignore=True`` if you want to ignore the error caused by the table or view not existing.
.. code-block:: python
db["my_table"].drop(ignore=True)
.. _python_api_transform:
Transforming a table
Expand Down
16 changes: 12 additions & 4 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,12 @@ def add_column(
self.add_foreign_key(col_name, fk, fk_col)
return self

def drop(self):
self.db.execute("DROP TABLE [{}]".format(self.name))
def drop(self, ignore=False):
try:
self.db.execute("DROP TABLE [{}]".format(self.name))
except sqlite3.OperationalError:
if not ignore:
raise

def guess_foreign_table(self, column):
column = column.lower()
Expand Down Expand Up @@ -2195,8 +2199,12 @@ def __repr__(self):
self.name, ", ".join(c.name for c in self.columns)
)

def drop(self):
self.db.execute("DROP VIEW [{}]".format(self.name))
def drop(self, ignore=False):
try:
self.db.execute("DROP VIEW [{}]".format(self.name))
except sqlite3.OperationalError:
if not ignore:
raise

def enable_fts(self, *args, **kwargs):
raise NotImplementedError(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
NoObviousTable,
ForeignKey,
Table,
View,
)
from sqlite_utils.utils import sqlite3
import collections
Expand Down Expand Up @@ -944,6 +945,21 @@ def test_drop_view(fresh_db):
assert [] == fresh_db.view_names()


def test_drop_ignore(fresh_db):
with pytest.raises(sqlite3.OperationalError):
fresh_db["does_not_exist"].drop()
fresh_db["does_not_exist"].drop(ignore=True)
# Testing view is harder, we need to create it in order
# to get a View object, then drop it twice
fresh_db.create_view("foo_view", "select 1")
view = fresh_db["foo_view"]
assert isinstance(view, View)
view.drop()
with pytest.raises(sqlite3.OperationalError):
view.drop()
view.drop(ignore=True)


def test_insert_all_empty_list(fresh_db):
fresh_db["t"].insert({"foo": 1})
assert 1 == fresh_db["t"].count
Expand Down
4 changes: 3 additions & 1 deletion tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def test_extract_single_column(fresh_db, table, fk_column):
" [name] TEXT,\n"
" [{}] INTEGER,\n".format(expected_fk)
+ " [end] INTEGER,\n"
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(expected_fk, expected_table)
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(
expected_fk, expected_table
)
+ ")"
)
assert fresh_db[expected_table].schema == (
Expand Down

0 comments on commit c236894

Please sign in to comment.