Skip to content

fix(db): recover missing kv_meta from partial migration failure#114

Merged
BYK merged 1 commit intomainfrom
fix/migrate-partial-failure-recovery
May 2, 2026
Merged

fix(db): recover missing kv_meta from partial migration failure#114
BYK merged 1 commit intomainfrom
fix/migrate-partial-failure-recovery

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 2, 2026

Summary

  • Fix "no such table: kv_meta" on DBs where the schema version is already at latest (11) but kv_meta is missing. Root cause: migration 7 is a multi-statement string (ALTER TABLE knowledge ADD COLUMN embedding BLOB; CREATE TABLE IF NOT EXISTS kv_meta ...). If the ALTER TABLE hits a duplicate-column error from a prior partial run, database.exec() aborts before CREATE TABLE kv_meta runs.
  • Catch duplicate-column errors in the migration loop, strip the already-applied ALTER statements, and re-exec the remaining SQL.
  • Add recoverMissingObjects() that idempotently creates kv_meta — runs both after fresh migrations and on already-at-latest DBs to heal existing broken databases.
  • Regression test: drops kv_meta, close/reopen DB, verifies recovery.

Follows up on #111 (singleton poisoning prevention). That fix prevents the cached handle from being stale, but doesn't help DBs that are already at version 11 with the table missing — this PR heals those.

All 583 tests pass.

Multi-statement migrations (e.g. migration 7: ALTER TABLE + CREATE TABLE
kv_meta) can partially fail when the ALTER TABLE hits a duplicate-column
error from a prior partial run. database.exec() stops at the first error,
so CREATE TABLE kv_meta never executes. The version still gets bumped to
latest, leaving a DB at version 11 with no kv_meta table.

Fix:
- Catch duplicate-column errors in the migration loop, strip the applied
  ALTER statements, and re-exec the remaining SQL so CREATE TABLE runs.
- Add recoverMissingObjects() that idempotently creates kv_meta — runs
  both after migrations and on already-at-latest DBs to heal existing
  broken databases.
- Regression test: drops kv_meta, close/reopen, verifies recovery.
@BYK BYK enabled auto-merge (squash) May 2, 2026 09:08
@BYK BYK merged commit b4194fc into main May 2, 2026
1 check passed
@BYK BYK deleted the fix/migrate-partial-failure-recovery branch May 2, 2026 09:08
@craft-deployer craft-deployer Bot mentioned this pull request May 2, 2026
5 tasks
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