fix(memos-local-plugin): two-phase migration to prevent crash-loop on large databases
Problem
Migration 007 (namespace-visibility) in v2.0.2–v2.0.5 runs UPDATE … SET share_scope='private' plus CREATE INDEX on the traces table inside a single db.tx(). On databases larger than ~500MB, this exceeds the host gateway's kill timeout. SQLite rolls back the entire transaction — including the schema_migrations INSERT — so migration 007 is never recorded and the bridge restarts into the same hang forever.
Small databases (~43MB) complete within the timeout, which is why this only manifests on larger installs. Tested against a 687MB database with ~98,000 traces.
Fix
Two-phase migration:
- Phase 1 (inside transaction, milliseconds):
ADD COLUMNonly on all 12 namespace tables, plusDROP INDEX uq_skills_name. Theschema_migrationsrecord for v7 commits here. - Phase 2 (after migration loop, outside any transaction): Batched
UPDATEin 2,000-row chunks (each its own implicit transaction) forshare_scopebackfill, thenCREATE INDEX IF NOT EXISTSfor all 18 owner/share indexes.ensureNamespaceColumnsis called unconditionally on every boot so new tables in the namespace list get their columns.
Restart safety: If the bridge is killed during Phase 2, the v7 schema_migrations record survives (Phase 1 committed). Next boot skips Phase 1 entirely and resumes Phase 2 where it left off. The crash-loop is broken.
Verification
- Confirmed the DB on the affected instance had exactly migrations 1–6 applied — migration 007 was never committed despite hundreds of boot attempts.
- After the fix, Phase 1 committed in 2.4 seconds. Phase 2 backfill and index creation complete across bridge restarts.
- Bridge boots cleanly with all 18 indexes created and
pipeline.ready.
Related
Closes #1787