Use new TransformError instead of AssertionError

Refs https://github.com/simonw/sqlite-utils/pull/634#discussion_r1835474885
This commit is contained in:
Simon Willison 2024-11-23 11:39:36 -08:00
commit 4d2274c53f
2 changed files with 20 additions and 14 deletions

View file

@ -156,6 +156,10 @@ XIndexColumn = namedtuple(
Trigger = namedtuple("Trigger", ("name", "table", "sql"))
class TransformError(Exception):
pass
ForeignKeyIndicator = Union[
str,
ForeignKey,
@ -1974,20 +1978,22 @@ class Table(Queryable):
"""SELECT sql FROM sqlite_master WHERE type = 'index' AND name = :index_name;""",
{"index_name": index.name},
).fetchall()[0][0]
assert index_sql is not None, (
f"Index '{index.name}' on table '{self.name}' does not have a "
"CREATE INDEX statement. You must manually drop this index prior to running this "
"transformation and manually recreate the new index after running this transformation."
)
if index_sql is None:
raise TransformError(
f"Index '{index.name}' on table '{self.name}' does not have a "
"CREATE INDEX statement. You must manually drop this index prior to running this "
"transformation and manually recreate the new index after running this transformation."
)
if keep_table:
sqls.append(f"DROP INDEX IF EXISTS [{index.name}];")
for col in index.columns:
assert col not in rename.keys() and col not in drop, (
f"Index '{index.name}' column '{col}' is not in updated table '{self.name}'. "
f"You must manually drop this index prior to running this transformation "
f"and manually recreate the new index after running this transformation. "
f"The original index sql statement is: `{index_sql}`. No changes have been applied to this table."
)
if col in rename.keys() or col in drop:
raise TransformError(
f"Index '{index.name}' column '{col}' is not in updated table '{self.name}'. "
f"You must manually drop this index prior to running this transformation "
f"and manually recreate the new index after running this transformation. "
f"The original index sql statement is: `{index_sql}`. No changes have been applied to this table."
)
sqls.append(index_sql)
return sqls

View file

@ -1,4 +1,4 @@
from sqlite_utils.db import ForeignKey
from sqlite_utils.db import ForeignKey, TransformError
from sqlite_utils.utils import OperationalError
import pytest
@ -625,7 +625,7 @@ def test_transform_with_indexes_errors(fresh_db, transform_params):
dogs.create_index(["name", "age"])
with pytest.raises(AssertionError) as excinfo:
with pytest.raises(TransformError) as excinfo:
dogs.transform(**transform_params)
assert (
@ -650,7 +650,7 @@ def test_transform_with_unique_constraint_implicit_index(fresh_db):
dogs.insert({"id": 1, "name": "Cleo", "age": 5})
# Attempt to transform the table without modifying 'name'
with pytest.raises(AssertionError) as excinfo:
with pytest.raises(TransformError) as excinfo:
dogs.transform(types={"age": str})
assert (