From 075eb9ea92fd04c16c3fe5d2d677ea8d2b86b87c Mon Sep 17 00:00:00 2001 From: khushalx Date: Wed, 2 Dec 2026 11:25:32 +0530 Subject: [PATCH 1/2] Fix FTS detection for table names containing double quotes Assisted-by: OpenAI Codex --- sqlite_utils/db.py | 2 +- tests/test_introspect.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index e85ac52a9..6ec3235ed 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -3996,7 +3996,7 @@ def detect_fts(self) -> str | None: ) args = { "like": f"%VIRTUAL TABLE%USING FTS%content=[{escaped_name}]%", - "like2": f'%VIRTUAL TABLE%USING FTS%content="{escaped_name}"%', + "like2": f"%VIRTUAL TABLE%USING FTS%content={quote_identifier(escaped_name)}%", "table": self.name, } rows = self.db.execute(sql, args).fetchall() diff --git a/tests/test_introspect.py b/tests/test_introspect.py index 5f36151cc..d4953425e 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -79,6 +79,31 @@ def test_detect_fts_legacy_bracket_quoted_wildcard(fresh_db): assert fresh_db["demo_1"].detect_fts() == "demo_1_fts" +@pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"]) +@pytest.mark.parametrize( + "name", ['has"quote', '"leading', 'trailing"', 'quote"_percent%\\tail'] +) +def test_detect_fts_double_quotes(fresh_db, fts_version, name): + fresh_db[f"{name}extra"].insert({"title": "Other"}).enable_fts( + ["title"], fts_version=fts_version + ) + table = fresh_db[name].insert({"title": "Hello"}) + table.enable_fts(["title"], fts_version=fts_version, create_triggers=True) + + assert table.detect_fts() == f"{name}_fts" + assert fresh_db[f"{name}_fts"].detect_fts() == f"{name}_fts" + assert [row["title"] for row in table.search("Hello")] == ["Hello"] + + table.disable_fts() + assert table.detect_fts() is None + assert not fresh_db[f"{name}_fts"].exists() + assert table.triggers == [] + assert list(table.rows) == [{"title": "Hello"}] + assert [row["title"] for row in fresh_db[f"{name}extra"].search("Other")] == [ + "Other" + ] + + def test_tables(existing_db): assert len(existing_db.tables) == 1 assert existing_db.tables[0].name == "foo" From 9cf6394b8758fe77bc856f83439d58b1de5d1c59 Mon Sep 17 00:00:00 2001 From: khushalx Date: Thu, 3 Dec 2026 10:42:16 +0530 Subject: [PATCH 2/2] Exclude quoted-prefix neighbors from FTS detection Refs #984 Assisted-by: OpenAI Codex --- sqlite_utils/db.py | 10 ++++++++-- tests/test_introspect.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_tracer.py | 6 +++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 6ec3235ed..cde1e0c28 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -3984,7 +3984,10 @@ def detect_fts(self) -> str | None: WHERE rootpage = 0 AND ( sql LIKE :like ESCAPE '\\' - OR sql LIKE :like2 ESCAPE '\\' + OR ( + sql LIKE :like2 ESCAPE '\\' + AND sql NOT LIKE :not_like2 ESCAPE '\\' + ) OR ( tbl_name = :table AND sql LIKE '%VIRTUAL TABLE%USING FTS%' @@ -3994,9 +3997,12 @@ def detect_fts(self) -> str | None: escaped_name = ( self.name.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") ) + # A quote followed by another quote is escaped, not the end of the name. + quoted_name = quote_identifier(escaped_name) args = { "like": f"%VIRTUAL TABLE%USING FTS%content=[{escaped_name}]%", - "like2": f"%VIRTUAL TABLE%USING FTS%content={quote_identifier(escaped_name)}%", + "like2": f"%VIRTUAL TABLE%USING FTS%content={quoted_name}%", + "not_like2": f'%VIRTUAL TABLE%USING FTS%content={quoted_name}"%', "table": self.name, } rows = self.db.execute(sql, args).fetchall() diff --git a/tests/test_introspect.py b/tests/test_introspect.py index d4953425e..e7ccf0222 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -104,6 +104,42 @@ def test_detect_fts_double_quotes(fresh_db, fts_version, name): ] +@pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"]) +@pytest.mark.parametrize("has_fts", [False, True]) +@pytest.mark.parametrize( + "name,neighbor", + [ + ("a", 'a"b'), + ("docs", 'docs"'), + ("x%", 'x%"y'), + ("u_", 'u_"v'), + ("back\\slash", 'back\\slash"next'), + ('has"quote', 'has"quote"next'), + ], +) +def test_detect_fts_quoted_prefix(fresh_db, fts_version, has_fts, name, neighbor): + other = fresh_db[neighbor].insert({"title": "Other"}) + other.enable_fts(["title"], fts_version=fts_version, create_triggers=True) + other_triggers = other.triggers_dict + table = fresh_db[name].insert({"title": "Hello"}) + if has_fts: + table.enable_fts(["title"], fts_version=fts_version, create_triggers=True) + + assert table.detect_fts() == (f"{name}_fts" if has_fts else None) + if has_fts: + assert [row["title"] for row in table.search("Hello")] == ["Hello"] + + table.disable_fts() + assert table.detect_fts() is None + assert not fresh_db[f"{name}_fts"].exists() + assert table.triggers == [] + assert list(table.rows) == [{"title": "Hello"}] + assert other.detect_fts() == f"{neighbor}_fts" + assert other.triggers_dict == other_triggers + other.insert({"title": "Another"}) + assert [row["title"] for row in other.search("Another")] == ["Another"] + + def test_tables(existing_db): assert len(existing_db.tables) == 1 assert existing_db.tables[0].name == "foo" diff --git a/tests/test_tracer.py b/tests/test_tracer.py index 64224edb5..d2c1e33c3 100644 --- a/tests/test_tracer.py +++ b/tests/test_tracer.py @@ -58,7 +58,10 @@ def tracer(sql, params): " WHERE rootpage = 0\n" " AND (\n" " sql LIKE :like ESCAPE '\\'\n" - " OR sql LIKE :like2 ESCAPE '\\'\n" + " OR (\n" + " sql LIKE :like2 ESCAPE '\\'\n" + " AND sql NOT LIKE :not_like2 ESCAPE '\\'\n" + " )\n" " OR (\n" " tbl_name = :table\n" " AND sql LIKE '%VIRTUAL TABLE%USING FTS%'\n" @@ -68,6 +71,7 @@ def tracer(sql, params): { "like": "%VIRTUAL TABLE%USING FTS%content=[dogs]%", "like2": '%VIRTUAL TABLE%USING FTS%content="dogs"%', + "not_like2": '%VIRTUAL TABLE%USING FTS%content="dogs""%', "table": "dogs", }, ),