From 3041c5d5da58ec7159e714fd283093c287881a8c Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 9 Nov 2024 09:30:23 -0800 Subject: [PATCH] Test for does not have a CREATE INDEX case Thanks, o1-preview: https://gist.github.com/simonw/edcd047a703ab6d6759a5a207364d744#response-1 Refs https://github.com/simonw/sqlite-utils/pull/634#issuecomment-2466304653 --- sqlite_utils/db.py | 2 +- tests/test_transform.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index ee401b6..2d76230 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1975,7 +1975,7 @@ class Table(Queryable): {"index_name": index.name}, ).fetchall()[0][0] assert index_sql is not None, ( - f"Index '{index}' on table '{self.name}' does not have a " + 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." ) diff --git a/tests/test_transform.py b/tests/test_transform.py index 42fc88e..f9859f8 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -633,3 +633,31 @@ def test_transform_with_indexes_errors(fresh_db, transform_params): "You must manually drop this index prior to running this transformation" in str(excinfo.value) ) + + +def test_transform_with_unique_constraint_implicit_index(fresh_db): + dogs = fresh_db["dogs"] + # Create a table with a UNIQUE constraint on 'name', which creates an implicit index + fresh_db.execute( + """ + CREATE TABLE dogs ( + id INTEGER PRIMARY KEY, + name TEXT UNIQUE, + age INTEGER + ); + """ + ) + dogs.insert({"id": 1, "name": "Cleo", "age": 5}) + + # Attempt to transform the table without modifying 'name' + with pytest.raises(AssertionError) as excinfo: + dogs.transform(types={"age": str}) + + assert ( + "Index 'sqlite_autoindex_dogs_1' on table 'dogs' does not have a CREATE INDEX statement." + in str(excinfo.value) + ) + assert ( + "You must manually drop this index prior to running this transformation and manually recreate the new index after running this transformation." + in str(excinfo.value) + )