Light Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix enable_fts() when FTS table already exists without replace=True #709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking "Sign up for GitHub", you agree to our terms of service and privacy statement. We'll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RamiNoodle733 wants to merge 1 commit into simonw:main
base: main
Choose a base branch
Loading
from RamiNoodle733:fix-fts-replace-issue-694
Open

Fix enable_fts() when FTS table already exists without replace=True #709

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
View file
Open in desktop
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
View file
Open in desktop
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