Make old rows searchable when you add SQLite FTS5
Search for invoice in a notes app just after adding full-text search. The note you saved this morning comes back, and last month’s invoice is missing. When you add an external-content FTS5 index to an existing SQLite database, install the write triggers, run rebuild, and check the index against the notes table before committing the migration.
The notes table holds the text. The FTS5 table uses each note’s id as its index row ID, and the triggers update the index whenever a note changes. A plain SELECT count(*) FROM notes_fts reads through to notes, so even a correct count says nothing about which notes are searchable. MATCH looks up the term in the index. SQLite’s external-content pitfalls
Try a search before the rebuild
Save this example as example.sql and run sqlite3 -bail :memory: < example.sql. The database lives in memory, and -bail stops the script on its first SQL error. The current upstream release is SQLite 3.54.1. FTS5 availability depends on how SQLite was built, so also try the migration on a disposable database through the same driver, build and connection settings as your Go app. SQLite shell options · SQLite downloads · FTS5 build options
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
body TEXT NOT NULL
);
INSERT INTO notes VALUES (1, 'April invoice', 'Paid on Friday');
BEGIN IMMEDIATE;
CREATE VIRTUAL TABLE notes_fts USING fts5(
title, body, content='notes', content_rowid='id'
);
CREATE TRIGGER notes_fts_insert AFTER INSERT ON notes BEGIN
INSERT INTO notes_fts(rowid, title, body)
VALUES (new.id, new.title, new.body);
END;
CREATE TRIGGER notes_fts_delete AFTER DELETE ON notes BEGIN
INSERT INTO notes_fts(notes_fts, rowid, title, body)
VALUES ('delete', old.id, old.title, old.body);
END;
CREATE TRIGGER notes_fts_update AFTER UPDATE ON notes BEGIN
INSERT INTO notes_fts(notes_fts, rowid, title, body)
VALUES ('delete', old.id, old.title, old.body);
INSERT INTO notes_fts(rowid, title, body)
VALUES (new.id, new.title, new.body);
END;
-- Simulate a save after the triggers are in place.
INSERT INTO notes VALUES (2, 'May invoice', 'Due on Monday');
SELECT count(*) FROM notes_fts;
SELECT rowid FROM notes_fts WHERE notes_fts MATCH 'invoice';
INSERT INTO notes_fts(notes_fts) VALUES ('rebuild');
INSERT INTO notes_fts(notes_fts, rank) VALUES ('integrity-check', 1);
COMMIT;
SELECT rowid, title
FROM notes_fts
WHERE notes_fts MATCH 'invoice'
ORDER BY rowid;
Before the rebuild, the count is 2 and the search returns only ID 2. The insert trigger indexed May’s invoice as it was saved. The final query returns both IDs because rebuild reads the existing notes. The delete and update triggers use the old text to remove its terms, and an update then indexes the new text. FTS5’s delete command
Run the example again with the rebuild line removed. The check fails before COMMIT because ID 1 is missing from the index. The 1 in the check tells FTS5 to compare its index with notes. Omitting it checks internal consistency, which can pass when the index holds only May’s invoice. FTS5’s integrity check
Run the check before enabling search
The Boring Stack scaffold reserves migrations/ for SQL. Its /healthz handler returns ok and the version, so a passing deploy check leaves search untested. Put the virtual table, triggers, rebuild and check in your next migration. Keep the sample table, data and queries in a test.
If your runner wraps migrations in a transaction, leave out the example’s BEGIN IMMEDIATE and COMMIT. If you run migrations directly from Go, execute all the statements through the same sql.Tx and roll back on error. Enable the search route after commit succeeds. Go transaction guide · SQLite transaction rules
Check import jobs before rolling out the triggers. When INSERT OR REPLACE replaces a row by primary key and recursive triggers are off, SQLite skips the delete trigger for the displaced row. An invoice changed to a receipt can still match invoice and return the new title. For this schema, use INSERT ... ON CONFLICT(id) DO UPDATE to run the update trigger on a conflicting ID. SQLite conflict handling · SQLite UPSERT
FTS5 runs inside SQLite, which carries no license fee. The index uses disk space, and every save that fires a trigger also updates it. SQLite allows one writer at a time even in WAL mode, so a rebuild and its check can hold up other saves until commit. Time the whole migration on a representative copy before choosing how long to pause writes. SQLite licensing · WAL concurrency
If the extra indexing work pushes normal saves past your latency budget, move search to a separate index and keep notes as the source. First record each insert, edit or delete in an ordered change table in the same SQLite transaction as the note change. For a small cutover, pause writes, let in-flight transactions finish, and seed the new index from notes using the same IDs. Have a worker apply changes after the last sequence number included in the export, and switch the search handler before resuming writes. A saved note can now take time to appear in search.
Add a test that saves a note before running the migration and checks for that exact ID in the search response.