Skip to content

Commit 3041c5d

Browse files
committed
Test for does not have a CREATE INDEX case
Thanks, o1-preview: https://gist.github.com/simonw/edcd047a703ab6d6759a5a207364d744#response-1 Refs #634 (comment)
1 parent c47a2a4 commit 3041c5d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

sqlite_utils/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,7 @@ def transform_sql(
19751975
{"index_name": index.name},
19761976
).fetchall()[0][0]
19771977
assert index_sql is not None, (
1978-
f"Index '{index}' on table '{self.name}' does not have a "
1978+
f"Index '{index.name}' on table '{self.name}' does not have a "
19791979
"CREATE INDEX statement. You must manually drop this index prior to running this "
19801980
"transformation and manually recreate the new index after running this transformation."
19811981
)

tests/test_transform.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,31 @@ def test_transform_with_indexes_errors(fresh_db, transform_params):
633633
"You must manually drop this index prior to running this transformation"
634634
in str(excinfo.value)
635635
)
636+
637+
638+
def test_transform_with_unique_constraint_implicit_index(fresh_db):
639+
dogs = fresh_db["dogs"]
640+
# Create a table with a UNIQUE constraint on 'name', which creates an implicit index
641+
fresh_db.execute(
642+
"""
643+
CREATE TABLE dogs (
644+
id INTEGER PRIMARY KEY,
645+
name TEXT UNIQUE,
646+
age INTEGER
647+
);
648+
"""
649+
)
650+
dogs.insert({"id": 1, "name": "Cleo", "age": 5})
651+
652+
# Attempt to transform the table without modifying 'name'
653+
with pytest.raises(AssertionError) as excinfo:
654+
dogs.transform(types={"age": str})
655+
656+
assert (
657+
"Index 'sqlite_autoindex_dogs_1' on table 'dogs' does not have a CREATE INDEX statement."
658+
in str(excinfo.value)
659+
)
660+
assert (
661+
"You must manually drop this index prior to running this transformation and manually recreate the new index after running this transformation."
662+
in str(excinfo.value)
663+
)

0 commit comments

Comments
 (0)