Use VACUUM INTO for consistent database snapshots in backups - #454
Merged
Conversation
Auto-backup, manual backup export, and vault move all copied the live, open SQLite file with plain filesystem calls, which can capture a torn database if a write is in flight. Add snapshotDatabase() (VACUUM INTO, via a bound parameter so paths with apostrophes work) and route all three call sites through it instead of fs.readFile/copyFile/cp. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Creating the snapshot temp dir outside the try meant an mkdtemp failure
escaped as a rejected promise rather than the documented
{ success: false, error } result, which the backup:run-auto IPC handler
returns directly to the renderer.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Closes #437
Three paths copied the live, open database with plain filesystem calls while writes could be in flight, risking a torn copy: hourly auto-backup,
backup:export, andsettings:move-vault. A corrupt backup is discovered exactly when it's needed most.Approach
Adds
snapshotDatabase(destPath)insrc/main/database/index.ts, which runsVACUUM INTOthrough the open connection, and routes all three call sites through it.Note on the issue's fix sketch: it suggested
db.backup(destPath)orVACUUM INTO.db.backup()does not work here — it throwsbackup is not supported with incompatible source and target databaseson a SQLCipher database. OnlyVACUUM INTOis viable.Verified empirically against the repo's native module before implementing:
user_versionis preserved (migrations depend on it)VACUUM INTOrefuses to write to an existing path, so every call site targets a fresh temp pathTom's Backupswould otherwise break or injectChanges
src/main/database/index.ts— newsnapshotDatabase()helpersrc/main/ipc/backup.ts—fs.copyFile→snapshotDatabase(existing temp-dir plumbing reused)src/main/ipc/auto-backup.ts— snapshots to a temp dir, cleaned up infinallysrc/main/ipc/settings.ts—fs.cp→snapshotDatabase; corrected the comment claiming DELETE journal mode makes a live copy safe at all times (it's only safe between transactions, which is the bug)src/test/database-snapshot.test.ts— new; pins readability with the key,user_versionpreservation, the not-plaintext property, and the apostrophe-path regressionThe not-plaintext assertion is the important one long-term: it guards against a future change silently producing an unencrypted backup.
Tradeoff worth knowing
VACUUM INTOis synchronous and briefly blocks the main process while it runs, where the oldfs.readFile/copyFilewas async. This is consistent with how the rest of the codebase does DB work on the main process, and the DB is small relative to screenshots (stored separately), but it does mean an hourly pause that scales with database size. Flagging rather than pre-optimising.Testing
npm run typecheckclean;npm test523 passed across 30 files.