From 4d2274c53f2a46481a25de91f127958af2c2072a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 23 Nov 2024 11:39:36 -0800 Subject: [PATCH] Use new TransformError instead of AssertionError Refs https://github.com/simonw/sqlite-utils/pull/634#discussion_r1835474885 --- sqlite_utils/db.py | 28 +++++++++++++++++----------- tests/test_transform.py | 6 +++--- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 2d76230..d472242 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -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 diff --git a/tests/test_transform.py b/tests/test_transform.py index f9859f8..7ced9d9 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -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 (