Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2572,7 +2572,11 @@ def enable_fts(
)
)
should_recreate = False
if replace and self.db["{}_fts".format(self.name)].exists():
fts_table_exists = self.db["{}_fts".format(self.name)].exists()
if not replace and fts_table_exists:
# FTS table already exists and replace=False, so return early
return self
if replace and fts_table_exists:
# Does the table need to be recreated?
fts_schema = self.db["{}_fts".format(self.name)].schema
if fts_schema != create_fts_sql:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_fts.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,28 @@ def test_enable_fts_error_message_on_views():
assert e.value.args[0] == "enable_fts() is supported on tables but not on views"


def test_enable_fts_twice_without_replace():
# Regression test for https://github.com/simonw/sqlite-utils/issues/694
# Calling enable_fts() twice without replace=True should not error
db = Database(memory=True)
db["books"].insert(
{
"id": 1,
"title": "Habits of Australian Marsupials",
"author": "Marlee Hawkins",
},
pk="id",
)
# First call creates the FTS table
db["books"].enable_fts(["title", "author"])
assert db["books_fts"].exists()
# Second call without replace=True should return early without error
result = db["books"].enable_fts(["title", "author"])
assert result == db["books"]
# FTS table should still exist
assert db["books_fts"].exists()


@pytest.mark.parametrize(
"kwargs,fts,expected",
[
Expand Down