create_index(..., find_unique_name=True) option, refs #335

This commit is contained in:
Simon Willison 2021-11-14 14:49:19 -08:00
commit e8d958109e
3 changed files with 62 additions and 18 deletions

View file

@ -4,6 +4,7 @@ from sqlite_utils.db import (
DescIndex,
AlterError,
NoObviousTable,
OperationalError,
ForeignKey,
Table,
View,
@ -752,6 +753,22 @@ def test_create_index_desc(fresh_db):
)
def test_create_index_find_unique_name():
db = Database(memory=True)
table = db["t"]
table.insert({"id": 1})
table.create_index(["id"])
# Without find_unique_name should error
with pytest.raises(OperationalError, match="index idx_t_id already exists"):
table.create_index(["id"])
# With find_unique_name=True it should work
table.create_index(["id"], find_unique_name=True)
table.create_index(["id"], find_unique_name=True)
# Should have three now
index_names = {idx.name for idx in table.indexes}
assert index_names == {"idx_t_id", "idx_t_id_2", "idx_t_id_3"}
@pytest.mark.parametrize(
"data_structure",
(