Add database integrity check to detect and recover from corruption#2387
Add database integrity check to detect and recover from corruption#2387
Conversation
- Add PRAGMA quick_check query to detect database corruption at startup - Add check_integrity() method to BootstrapPipeline trait - Reset database automatically when corruption is detected - Handle both cases: non-"ok" response and query errors (severe corruption) - Add IntegrityCheckRow struct to parse JSON response from quick_check
WalkthroughThis PR adds database integrity checking functionality to detect and recover from corrupted databases. A new Changes
Sequence Diagram(s)sequenceDiagram
participant Runner as Runner
participant BootstrapPipeline as BootstrapPipeline
participant DB as Database
participant Reset as Reset Handler
rect rgba(100, 200, 100, 0.2)
Note over Runner,DB: Integrity Check Phase
Runner->>BootstrapPipeline: check_integrity(db)
BootstrapPipeline->>DB: Execute PRAGMA quick_check
DB-->>BootstrapPipeline: IntegrityCheckRow {quick_check}
end
alt Integrity OK (result == "ok")
rect rgba(150, 200, 150, 0.1)
BootstrapPipeline-->>Runner: Ok(true)
Runner->>Runner: Continue inspection & schema checks
end
else Integrity Failed or Error
rect rgba(200, 100, 100, 0.2)
BootstrapPipeline-->>Runner: Ok(false) or Err
Runner->>Reset: reset_db()
Reset->>DB: Clear/recreate metadata & views
Reset-->>Runner: Complete
Runner->>Runner: Exit early, skip further checks
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
📜 Recent review detailsConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (4)
🧰 Additional context used📓 Path-based instructions (3)crates/**/*.rs📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/crates/**📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.rs📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (11)📓 Common learnings📚 Learning: 2025-10-06T14:41:41.909ZApplied to files:
📚 Learning: 2025-10-28T14:11:56.648ZApplied to files:
📚 Learning: 2025-10-18T10:38:41.273ZApplied to files:
📚 Learning: 2025-11-25T16:50:31.752ZApplied to files:
📚 Learning: 2025-05-13T20:06:22.602ZApplied to files:
📚 Learning: 2025-05-20T10:20:08.206ZApplied to files:
📚 Learning: 2025-05-16T17:26:09.529ZApplied to files:
📚 Learning: 2025-12-03T10:40:25.429ZApplied to files:
📚 Learning: 2025-10-06T11:44:07.888ZApplied to files:
📚 Learning: 2025-10-06T11:13:29.956ZApplied to files:
🧬 Code graph analysis (1)crates/common/src/local_db/query/integrity_check.rs (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
🔇 Additional comments (11)
Comment |
|
@coderabbitai assess this PR size classification for the totality of the PR with the following criterias and report it in your comment: S/M/L PR Classification Guidelines:This guide helps classify merged pull requests by effort and complexity rather than just line count. The goal is to assess the difficulty and scope of changes after they have been completed. Small (S)Characteristics:
Review Effort: Would have taken 5-10 minutes Examples:
Medium (M)Characteristics:
Review Effort: Would have taken 15-30 minutes Examples:
Large (L)Characteristics:
Review Effort: Would have taken 45+ minutes Examples:
Additional Factors to ConsiderWhen deciding between sizes, also consider:
Notes:
|
|
The author of this PR is on the CodeRabbit Free Plan. In order to use the Chat feature, please upgrade the PR author to CodeRabbit Pro by visiting https://app.coderabbit.ai/login. |
Motivation
See issues:
When a browser tab is closed during a sync operation, the SQLite database can become corrupted with "database disk image is malformed" errors. Previously, this caused sync to fail repeatedly with no recovery mechanism, forcing users to manually clear their browser data.
Solution
Add a database integrity check at startup using SQLite's
PRAGMA quick_checkcommand:integrity_check.rs): Defines thePRAGMA quick_checkstatement andIntegrityCheckRowstruct to parse the JSON responsecheck_integrity()method: Added toBootstrapPipelinetrait to run the integrity check and return whether the database is healthyrunner_run(): Now checks integrity at startup before any other operationsquick_checkreturns non-"ok" → database is corrupted → reset and resync from dumpquick_checkquery fails entirely (severe corruption) → treat as corrupted → reset and resync from dumpThis ensures automatic recovery from database corruption without user intervention.
Checks
By submitting this for review, I'm confirming I've done the following:
fix #2385
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.