fix(db): prevent singleton poisoning when migrate() throws#111
Merged
Conversation
If migrate() threw (SQLITE_BUSY, partial prior run, disk error), the module-level `instance` was already assigned to the un-migrated Database handle. Every subsequent db() call returned this stale handle via the `if (instance) return instance` short-circuit, causing 'no such table' errors (e.g. kv_meta) for the rest of the process lifetime. Fix: use a local variable for the Database handle and only promote it to the module-level singleton after migrate() succeeds. If migrate() throws, `instance` remains undefined and the next db() call retries from scratch.
5 tasks
BYK
added a commit
that referenced
this pull request
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.
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
db()singleton poisoning: the module-levelinstancewas assigned beforemigrate()ran, so if migration threw (SQLITE_BUSY from lock contention, partial prior run with duplicate ALTER TABLE, disk error), every subsequentdb()call returned the un-migrated handle — causing "no such table: kv_meta" errors for the rest of the process lifetime.migrate()succeeds, so a failed init is retried on the next call.kv_metatable existence anddb()re-initialization afterclose().All 552 existing tests pass.