fix: recover from corrupt SQLite database files on startup#1077
Merged
Conversation
LeafWiki failed to start after an upgrade if a previous run left a stale SQLite journal file (search.db-journal), causing disk I/O error 3338. On startup, all four derived SQLite stores (search, links, tags, properties) now detect genuine I/O and corruption errors (SQLITE_IOERR, SQLITE_CORRUPT, SQLITE_NOTADB), delete the database and any sidecar files, and retry initialization once. Since these stores are always rebuilt from the Markdown files on disk, the delete is safe. Transient errors (SQLITE_BUSY, SQLITE_LOCKED) and resource errors (SQLITE_IOERR_NOMEM) are not treated as recoverable — they are returned immediately without touching the database files. Shared helpers (IsSQLiteRecoverableError, RemoveSQLiteFiles) live in internal/core/shared/sqliteutil to avoid coupling non-database packages to the SQLite module. The auth stores (users.db, sessions.db) are intentionally excluded from auto-recovery as they hold primary data. Fixes #1072
Contributor
There was a problem hiding this comment.
Pull request overview
Recovers from corrupt or stale SQLite database files for the four derived stores (search, links, tags, properties) on startup by detecting a narrow set of recoverable SQLite errors, deleting the DB plus sidecar files (-journal, -wal, -shm), and retrying initialization once. This addresses the upgrade failure in #1072 where a stale search.db-journal produced "disk I/O error (3338)" and prevented startup. The auth stores (users.db, sessions.db) are intentionally left untouched because they hold primary data.
Changes:
- Adds
internal/core/shared/sqliteutilwithIsSQLiteRecoverableError(matchesSQLITE_IOERR=10,SQLITE_CORRUPT=11,SQLITE_NOTADB=26; explicitly excludesSQLITE_IOERR_NOMEM=3082) andRemoveSQLiteFiles. - Wraps
ensureSchemafailures inNewSQLiteIndex,NewLinksStore,NewTagsStore, andNewPropertiesStorewith a one-shot delete-and-retry recovery path. - Misc cleanup in
internal/search/sqlite_index.go(removes a straylog.Printfper-row inSearch, drops unused"log"import).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/core/shared/sqliteutil/sqlite.go | New shared helpers: classify recoverable SQLite errors and remove DB + sidecar files. |
| internal/search/sqlite_index.go | Adds recovery around ensureSchema; removes per-row log.Printf and unused import; stray extra blank line. |
| internal/links/links_store.go | Adds recovery around ensureSchema, resetting s.db and re-Connect-ing on retry. |
| internal/tags/tags_store.go | Adds recovery: closes db, removes files, reopens and re-runs ensureSchema. |
| internal/properties/properties_store.go | Adds recovery: closes db, removes files, reopens and re-runs ensureSchema. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
LeafWiki failed to start after an upgrade if a previous run left a stale SQLite journal file (search.db-journal), causing disk I/O error 3338.
On startup, all four derived SQLite stores (search, links, tags, properties) now detect genuine I/O and corruption errors (SQLITE_IOERR, SQLITE_CORRUPT, SQLITE_NOTADB), delete the database and any sidecar files, and retry initialization once. Since these stores are always rebuilt from the Markdown files on disk, the delete is safe.
Transient errors (SQLITE_BUSY, SQLITE_LOCKED) and resource errors (SQLITE_IOERR_NOMEM) are not treated as recoverable — they are returned immediately without touching the database files.
Shared helpers (IsSQLiteRecoverableError, RemoveSQLiteFiles) live in internal/core/shared/sqliteutil to avoid coupling non-database packages to the SQLite module. The auth stores (users.db, sessions.db) are intentionally excluded from auto-recovery as they hold primary data.
Fixes #1072