From dd09aa21f48886d382e0e5dc39f6fc64a46252d6 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Thu, 2 Apr 2026 23:14:22 -0700 Subject: [PATCH] fix: add FTS5 sync triggers so recipe inserts are indexed immediately Migration 015 did a one-time rebuild of recipes_fts at creation time but omitted triggers, so rows inserted after that point were invisible to MATCH queries. Adds AFTER INSERT/UPDATE/DELETE triggers to 015 (fresh DBs / tests) and migration 016 to backfill them on existing databases. Fixes 3 failing tests: test_search_recipes_by_ingredient_names, test_level1_returns_ranked_suggestions, test_level2_returns_swap_candidates. --- app/db/migrations/015_recipe_fts.sql | 22 +++++++++++++++ app/db/migrations/016_recipe_fts_triggers.sql | 27 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 app/db/migrations/016_recipe_fts_triggers.sql diff --git a/app/db/migrations/015_recipe_fts.sql b/app/db/migrations/015_recipe_fts.sql index 712abea..dbd7c11 100644 --- a/app/db/migrations/015_recipe_fts.sql +++ b/app/db/migrations/015_recipe_fts.sql @@ -14,3 +14,25 @@ CREATE VIRTUAL TABLE IF NOT EXISTS recipes_fts USING fts5( ); INSERT INTO recipes_fts(recipes_fts) VALUES('rebuild'); + +-- Triggers to keep the FTS index in sync with the recipes table. +-- Without these, rows inserted after the initial rebuild are invisible to FTS queries. +CREATE TRIGGER IF NOT EXISTS recipes_fts_ai + AFTER INSERT ON recipes BEGIN + INSERT INTO recipes_fts(rowid, ingredient_names) + VALUES (new.id, new.ingredient_names); +END; + +CREATE TRIGGER IF NOT EXISTS recipes_fts_ad + AFTER DELETE ON recipes BEGIN + INSERT INTO recipes_fts(recipes_fts, rowid, ingredient_names) + VALUES ('delete', old.id, old.ingredient_names); +END; + +CREATE TRIGGER IF NOT EXISTS recipes_fts_au + AFTER UPDATE ON recipes BEGIN + INSERT INTO recipes_fts(recipes_fts, rowid, ingredient_names) + VALUES ('delete', old.id, old.ingredient_names); + INSERT INTO recipes_fts(rowid, ingredient_names) + VALUES (new.id, new.ingredient_names); +END; diff --git a/app/db/migrations/016_recipe_fts_triggers.sql b/app/db/migrations/016_recipe_fts_triggers.sql new file mode 100644 index 0000000..43a76c0 --- /dev/null +++ b/app/db/migrations/016_recipe_fts_triggers.sql @@ -0,0 +1,27 @@ +-- Migration 016: Add FTS5 sync triggers for the recipes_fts content table. +-- +-- Migration 015 created recipes_fts and did a one-time rebuild, but omitted +-- triggers. Without them, INSERT/UPDATE/DELETE on recipes does not update the +-- FTS index, so new rows are invisible to MATCH queries. +-- +-- CREATE TRIGGER IF NOT EXISTS is idempotent — safe to re-run. + +CREATE TRIGGER IF NOT EXISTS recipes_fts_ai + AFTER INSERT ON recipes BEGIN + INSERT INTO recipes_fts(rowid, ingredient_names) + VALUES (new.id, new.ingredient_names); +END; + +CREATE TRIGGER IF NOT EXISTS recipes_fts_ad + AFTER DELETE ON recipes BEGIN + INSERT INTO recipes_fts(recipes_fts, rowid, ingredient_names) + VALUES ('delete', old.id, old.ingredient_names); +END; + +CREATE TRIGGER IF NOT EXISTS recipes_fts_au + AFTER UPDATE ON recipes BEGIN + INSERT INTO recipes_fts(recipes_fts, rowid, ingredient_names) + VALUES ('delete', old.id, old.ingredient_names); + INSERT INTO recipes_fts(rowid, ingredient_names) + VALUES (new.id, new.ingredient_names); +END;