Skip to content

fix: handle malformed FTS probes during migration#331

Merged
jalehman merged 2 commits into
mainfrom
fix/malformed-fts-migration
Apr 9, 2026
Merged

fix: handle malformed FTS probes during migration#331
jalehman merged 2 commits into
mainfrom
fix/malformed-fts-migration

Conversation

@jalehman

@jalehman jalehman commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Catch failures from the summaries_fts PRAGMA table_info(...) probe during migration and force the existing drop/recreate self-heal path so corrupt legacy FTS tables don't crash plugin startup with malformed database schema (1).

Root cause

Users upgrading from older versions (0.4.x–0.6.x) could have a corrupt or structurally incompatible summaries_fts virtual table. The migration code probed it with PRAGMA table_info(summaries_fts) to detect whether recreation was needed, but this probe itself throws on a malformed FTS table — before the self-heal DROP TABLE IF EXISTS + recreate path ever runs.

Fix

Wrap the PRAGMA probe in a try/catch. If it throws, set summariesFtsColumns = null, which triggers shouldRecreateSummariesFts = true and lets the existing rebuild path run normally.

Testing

  • Added regression test in test/migration.test.ts that simulates the probe failure
  • pnpm test passes (full suite)

Catch failures from the summaries_fts schema probe and force the existing drop/recreate path so corrupt legacy FTS tables can self-heal during migration.

Add a regression test that simulates PRAGMA table_info(summaries_fts) throwing while the real SQLite rebuild SQL still runs and repopulates the table from summaries.

Regeneration-Prompt: |
  Fix the migration crash when a legacy or corrupt summaries_fts virtual table causes PRAGMA table_info(summaries_fts) to throw before the self-heal logic runs. Preserve the existing rebuild path instead of inventing a new repair flow: if the probe throws, treat that as a signal to recreate summaries_fts and repopulate it from summaries. Check whether messages_fts has the same table_info probe pattern earlier in the migration and only change it if that exact risk exists. Add a regression test in the migration test suite that simulates the probe failure against a real database so the drop/create and repopulation SQL are exercised. Verify with the focused migration test and the full pnpm test suite.
@jalehman jalehman merged commit d7a57c5 into main Apr 9, 2026
2 checks passed
@jalehman jalehman deleted the fix/malformed-fts-migration branch April 9, 2026 18:36
@github-actions github-actions Bot mentioned this pull request Apr 9, 2026
jalehman added a commit that referenced this pull request Apr 9, 2026
Restore the broadened migration tests after rebasing onto main and add a
changeset for the startup recovery fixes.

Regeneration-Prompt: |
  Rebase the malformed-FTS recovery branch onto origin/main after PR #331
  landed. Preserve the broader standalone FTS hardening from the branch, but
  fix the review findings before pushing: startup migrations must not reuse a
  cached fts5Available value from the engine constructor, and stale
  summaries_fts_cjk tables on runtimes without trigram support must be dropped
  before any other FTS schema introspection runs. Keep the existing malformed
  summaries_fts probe regression from #331, update it for the quoted PRAGMA
  helper, retain the added shadow-table recovery coverage, add a new regression
  for the stale-trigram ordering issue, and include a patch changeset.
jalehman added a commit that referenced this pull request Apr 9, 2026
* fix migration recovery for malformed FTS tables

* drop stale cjk fts tables without trigram

* drop orphaned fts shadow tables on recovery

* quote identifiers in fts recovery helpers

* fix: finalize FTS recovery hardening after rebase

Restore the broadened migration tests after rebasing onto main and add a
changeset for the startup recovery fixes.

Regeneration-Prompt: |
  Rebase the malformed-FTS recovery branch onto origin/main after PR #331
  landed. Preserve the broader standalone FTS hardening from the branch, but
  fix the review findings before pushing: startup migrations must not reuse a
  cached fts5Available value from the engine constructor, and stale
  summaries_fts_cjk tables on runtimes without trigram support must be dropped
  before any other FTS schema introspection runs. Keep the existing malformed
  summaries_fts probe regression from #331, update it for the quoted PRAGMA
  helper, retain the added shadow-table recovery coverage, add a new regression
  for the stale-trigram ordering issue, and include a patch changeset.

---------

Co-authored-by: Eva <eva@100yen.org>
Co-authored-by: Josh Lehman <josh@martian.engineering>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant