From a12e9e74c5f1198037ed4de2d11242246404dd4a Mon Sep 17 00:00:00 2001 From: Claude Lin & Lay Date: Tue, 28 Apr 2026 21:19:03 +0900 Subject: [PATCH] fix(fts): drop and recreate code_fts to clear recurring SQLITE_CORRUPT_VTAB (#135) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit migration 0002 で適用した FTS5 'rebuild' のみではトライグラム側 (code_fts) の SQLITE_CORRUPT_VTAB が再発したため、migration 0003 で `DROP TABLE` + `CREATE VIRTUAL TABLE` + 'rebuild' によりテーブル本体を作り直す形に強化する。 影響範囲は `search_docs_code_fts` のみ。`search_docs_nat_fts` は再発が観測されていないため触らない。0001 のトリガ (`trg_search_docs_ai/ad/au`) はテーブル名参照のため、再作成後そのまま機能する。 適用は merge 後に Master が `npx wrangler d1 migrations apply github-rag-fts --remote` で実行する (`package.json` に専用スクリプトはない)。 Closes #135 --- migrations/0003_fts5_code_recreate.sql | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 migrations/0003_fts5_code_recreate.sql diff --git a/migrations/0003_fts5_code_recreate.sql b/migrations/0003_fts5_code_recreate.sql new file mode 100644 index 0000000..19921e0 --- /dev/null +++ b/migrations/0003_fts5_code_recreate.sql @@ -0,0 +1,37 @@ +-- D1 FTS5 code_fts virtual table fresh recreate — recurring SQLITE_CORRUPT_VTAB +-- +-- Layer = L4 Operations (sparse retrieval surface recovery) +-- +-- Context: +-- 2026-04-24: migration 0002 applied FTS5 'rebuild' to both nat_fts and code_fts +-- to recover from SQLITE_CORRUPT_VTAB. +-- 2026-04-28: corruption recurred on code_fts only (trigram tokenizer side). +-- Enriched logs from PR #137 confirmed errorName=Error / +-- D1_ERROR: database disk image is malformed: SQLITE_CORRUPT_VTAB on every diff +-- upsert across all 5 polled repos. +-- +-- Recovery (more aggressive than 0002): +-- DROP the corrupted virtual table and recreate it from scratch with the +-- same definition as 0001, then repopulate via FTS5 'rebuild' which reads +-- from the content-owner table (search_docs). +-- +-- Triggers from 0001 (trg_search_docs_ai/ad/au) reference search_docs_code_fts +-- by name; they resume working as soon as the new table exists, so they do +-- not need to be redefined. +-- +-- Scope: +-- Affects code_fts only. nat_fts is untouched (no recurring corruption observed there). +-- +-- Idempotency: +-- IF EXISTS / IF NOT EXISTS clauses keep the migration safe to re-run. + +DROP TABLE IF EXISTS search_docs_code_fts; + +CREATE VIRTUAL TABLE IF NOT EXISTS search_docs_code_fts USING fts5 ( + content, + tokenize = 'trigram case_sensitive 0', + content = 'search_docs', + content_rowid = 'rowid' +); + +INSERT INTO search_docs_code_fts(search_docs_code_fts) VALUES('rebuild');