Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,10 @@ def drop_view(path, view, ignore, load_extension):
_register_db_for_cleanup(db)
_load_extensions(db, load_extension)
try:
db[view].drop(ignore=ignore)
db.view(view).drop(ignore=ignore)
except sqlite_utils.db.NoView:
if not ignore:
raise click.ClickException('View "{}" does not exist'.format(view))
except OperationalError:
raise click.ClickException('View "{}" does not exist'.format(view))

Expand Down Expand Up @@ -2039,8 +2042,9 @@ def memory(
view_names = ["t{}".format(i + 1)]
if i == 0:
view_names.append("t")
existing_views = set(db.view_names())
for view_name in view_names:
if not db[view_name].exists():
if view_name not in existing_views:
db.create_view(
view_name,
"select * from {}".format(quote_identifier(file_table)),
Expand Down
4 changes: 1 addition & 3 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,13 @@ def tracer(
finally:
self._tracer = prev_tracer

def __getitem__(self, table_name: str) -> Union["Table", "View"]:
def __getitem__(self, table_name: str) -> "Table":
"""
``db[table_name]`` returns a :class:`.Table` object for the table with the specified name.
If the table does not exist yet it will be created the first time data is inserted into it.

:param table_name: The name of the table
"""
if table_name in self.view_names():
return self.view(table_name)
return self.table(table_name)

def __repr__(self) -> str:
Expand Down
11 changes: 7 additions & 4 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ def test_drop(fresh_db):
def test_drop_view(fresh_db):
fresh_db.create_view("foo_view", "select 1")
assert ["foo_view"] == fresh_db.view_names()
assert None is fresh_db["foo_view"].drop()
assert None is fresh_db.view("foo_view").drop()
assert [] == fresh_db.view_names()


Expand All @@ -1093,7 +1093,7 @@ def test_drop_ignore(fresh_db):
# 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"]
view = fresh_db.view("foo_view")
assert isinstance(view, View)
view.drop()
with pytest.raises(sqlite3.OperationalError):
Expand Down Expand Up @@ -1381,9 +1381,12 @@ def test_bad_table_and_view_exceptions(fresh_db):
with pytest.raises(NoTable) as ex:
fresh_db.table("v")
assert ex.value.args[0] == "Table v is actually a view"
with pytest.raises(NoView) as ex2:
with pytest.raises(NoTable) as ex2:
fresh_db["v"]
assert ex2.value.args[0] == "Table v is actually a view"
with pytest.raises(NoView) as ex3:
fresh_db.view("t")
assert ex2.value.args[0] == "View t does not exist"
assert ex3.value.args[0] == "View t does not exist"


# Tests for issue #655: Table configuration should be stored in _defaults
Expand Down