mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-23 10:24:12 +02:00
table.drop(ignore=True) option, refs #237
This commit is contained in:
parent
a76e3b33ac
commit
c236894caa
4 changed files with 37 additions and 5 deletions
|
|
@ -1026,6 +1026,12 @@ You can drop a table or view using the ``.drop()`` method:
|
||||||
|
|
||||||
db["my_table"].drop()
|
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:
|
.. _python_api_transform:
|
||||||
|
|
||||||
Transforming a table
|
Transforming a table
|
||||||
|
|
|
||||||
|
|
@ -1212,8 +1212,12 @@ class Table(Queryable):
|
||||||
self.add_foreign_key(col_name, fk, fk_col)
|
self.add_foreign_key(col_name, fk, fk_col)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def drop(self):
|
def drop(self, ignore=False):
|
||||||
self.db.execute("DROP TABLE [{}]".format(self.name))
|
try:
|
||||||
|
self.db.execute("DROP TABLE [{}]".format(self.name))
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
if not ignore:
|
||||||
|
raise
|
||||||
|
|
||||||
def guess_foreign_table(self, column):
|
def guess_foreign_table(self, column):
|
||||||
column = column.lower()
|
column = column.lower()
|
||||||
|
|
@ -2195,8 +2199,12 @@ class View(Queryable):
|
||||||
self.name, ", ".join(c.name for c in self.columns)
|
self.name, ", ".join(c.name for c in self.columns)
|
||||||
)
|
)
|
||||||
|
|
||||||
def drop(self):
|
def drop(self, ignore=False):
|
||||||
self.db.execute("DROP VIEW [{}]".format(self.name))
|
try:
|
||||||
|
self.db.execute("DROP VIEW [{}]".format(self.name))
|
||||||
|
except sqlite3.OperationalError:
|
||||||
|
if not ignore:
|
||||||
|
raise
|
||||||
|
|
||||||
def enable_fts(self, *args, **kwargs):
|
def enable_fts(self, *args, **kwargs):
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from sqlite_utils.db import (
|
||||||
NoObviousTable,
|
NoObviousTable,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
Table,
|
Table,
|
||||||
|
View,
|
||||||
)
|
)
|
||||||
from sqlite_utils.utils import sqlite3
|
from sqlite_utils.utils import sqlite3
|
||||||
import collections
|
import collections
|
||||||
|
|
@ -944,6 +945,21 @@ def test_drop_view(fresh_db):
|
||||||
assert [] == fresh_db.view_names()
|
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):
|
def test_insert_all_empty_list(fresh_db):
|
||||||
fresh_db["t"].insert({"foo": 1})
|
fresh_db["t"].insert({"foo": 1})
|
||||||
assert 1 == fresh_db["t"].count
|
assert 1 == fresh_db["t"].count
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,9 @@ def test_extract_single_column(fresh_db, table, fk_column):
|
||||||
" [name] TEXT,\n"
|
" [name] TEXT,\n"
|
||||||
" [{}] INTEGER,\n".format(expected_fk)
|
" [{}] INTEGER,\n".format(expected_fk)
|
||||||
+ " [end] INTEGER,\n"
|
+ " [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 == (
|
assert fresh_db[expected_table].schema == (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue