fix(core): auto-recover corrupted sqlite database on startup#37822
Open
leecoder wants to merge 1 commit into
Open
fix(core): auto-recover corrupted sqlite database on startup#37822leecoder wants to merge 1 commit into
leecoder wants to merge 1 commit into
Conversation
This was referenced Jul 20, 2026
leecoder
force-pushed
the
sqlite-auto-recovery
branch
from
July 20, 2026 02:46
bb3cb06 to
d1e3102
Compare
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.
Issue for this PR
Closes #37821
Type of change
What does this PR do?
When the SQLite database file is corrupted, opencode crashes on startup with
database disk image is malformedorfile is not a database. This happened to me after a power loss corrupted the WAL file.The fix wraps DB initialization with
Layer.catchCause. If the init fails with a corruption-related SQLite error, it:.db,-wal,-shmfiles with a.corrupt-<timestamp>suffixThe detection uses error message matching (
file is not a database/database disk image is malformed) rather than header-only inspection, so it catches both header corruption and internal page corruption.If the backup rename itself fails (permissions, file lock), it logs a distinct warning instead of silently claiming success.
How did you verify your code works?
database-migration.test.tsthat writes a file with valid SQLite header but corrupted internal pages, then verifies recovery creates a working database and the.corrupt-*backup existsbun typecheckpasses for bothpackages/coreandpackages/opencodeScreenshots / recordings
N/A - not a UI change
Checklist