Skip to content

fix(core): correct documents_fts update/delete triggers for external-content FTS5 - #1351

Merged
ajianaz merged 2 commits into
developfrom
fix/documents-fts-update-trigger
Oct 7, 2026
Merged

ajianaz merged 2 commits into
developfrom
fix/documents-fts-update-trigger

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

What

  • documents_fts_update and documents_fts_delete now use the FTS5 'delete' command with the OLD row values (as memories_fts already does). Defined once as constants and used by ensure_documents_fts_create and the v12 migration list.
  • heal_documents_fts_triggers() runs when an existing documents_fts is found on open: if either trigger is the old plain UPDATE/DELETE form it drops and recreates it and runs INSERT INTO documents_fts(documents_fts) VALUES('rebuild') so stale tokens disappear. Idempotent (no-op once correct); no schema version bump.

Why

documents_fts is an external-content FTS5 table (content='documents'). A plain UPDATE documents_fts SET … / DELETE FROM documents_fts … makes FTS5 re-read the already-modified content row, which either aborts with database disk image is malformed (reported as "update document: database corruption") or silently leaves the old title/content tokens in the index. Found while writing the #1341 tests; see #1349.

Testing

  • Written against the baseline first: on develop's schema 3 of the 4 behavioural tests fail, with the fix all pass.
    • title change via update_document + upsert_document keeps FTS in sync (old title no longer matches, new one does);
    • doc_update / doc_upsert title changes work through Uteke with an embedder;
    • a database created with the old triggers: bug reproduced (error or stale hit), heal_documents_fts_triggers() fixes it and removes stale entries, second call is a no-op;
    • reopening a file database with old triggers heals it automatically;
    • delete removes the FTS entry and later writes still work.
  • cargo fmt --check, cargo clippy --workspace --all-targets clean; cargo test --workspace green (core 603); vecq-only core build green (601).
  • Not covered: a very large documents table rebuild time (the rebuild is a one-time cost on first open after upgrade).

Fixes #1349

🤖 Generated with Claude Code

…content FTS5

documents_fts is an external-content FTS5 table, so the update and delete
triggers must issue the 'delete' command with the OLD values. The plain
UPDATE/DELETE forms either failed with 'database disk image is malformed'
(title changes via doc update/upsert) or silently left stale tokens in the
index. Replace the triggers (new databases and the v12 migration) and heal
existing databases on open: swap the outdated triggers and rebuild the index.

Fixes #1349

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@ajianaz ajianaz added bug Something isn't working scope:core Core engine work priority:high Fix in next release labels Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit b92f139 into develop Oct 7, 2026
15 checks passed
@ajianaz
ajianaz deleted the fix/documents-fts-update-trigger branch October 13, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority:high Fix in next release scope:core Core engine work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): changing a document's title fails with 'database corruption' (documents_fts_update trigger)

1 participant