fix(db): recover missing kv_meta from partial migration failure#114
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kv_metais 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 beforeCREATE TABLE kv_metaruns.recoverMissingObjects()that idempotently createskv_meta— runs both after fresh migrations and on already-at-latest DBs to heal existing broken databases.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.