Repository navigation
fix(db): fail closed when production database file goes missing (#278) - #610
Merged
Merged
Conversation
initDatabase() previously opened new Database(dbPath) unconditionally, silently creating a blank database if the real flo.db was deleted, unmounted, or unreadable in production — a merchant could lose all data with no warning. Now a marker file written on every successful production boot lets startup tell "database went missing" apart from "first-run install," and refuses to start with an empty database in the former case, directing the user to restore from backup instead. Internal maintenance paths (backup/restore/reset) that deliberately recreate the db file, dev mode, and the E2E db-path override are unaffected. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Contributor
Greptile SummaryThe PR adds a production startup marker intended to distinguish first-run installation from a previously initialized installation whose database disappeared.
Confidence Score: 4/5The PR is not yet safe to merge because marker persistence can still fail without aborting startup, leaving the missing-database guard ineffective. Both marker-write failures are caught and startup continues, while the missing-database check depends entirely on that marker and has no other persistence path; therefore the previously reported fail-open database-recreation path remains. Files Needing Attention: main/db.ts
|
| Filename | Overview |
|---|---|
| main/db.ts | Adds the missing-database guard and marker lifecycle, but the previously reported fail-open behavior remains when both marker writes fail. |
| tests/issue-278-fail-closed-missing-db.test.ts | Covers fresh installation, marker-based refusal, E2E bypass, and maintenance reset behavior, but not persistent marker-write failure. |
| package.json | Registers the new regression test and includes it in the aggregate test command. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Production startup] --> B{flo.db exists?}
B -->|Yes| C[Open and initialize database]
B -->|No| D{Initialization marker exists?}
D -->|Yes| E[Throw ERR_DATABASE_MISSING]
D -->|No| C
C --> F[Write initialization marker]
F -->|Success| G[Protected on later startup]
F -->|Both attempts fail| H[Log and continue without marker]
H --> I[Later missing database appears to be first run]
Reviews (2): Last reviewed commit: "fix(db): retry and log marker-write fail..." | Re-trigger Greptile
Greptile flagged (PR #610): if writing .flo-db-initialized failed, the error was only console.error'd and dropped, leaving a successfully initialized production install without the marker. If flo.db later went missing, the #278 fail-closed guard would then see no marker and wrongly treat it as a first run, recreating a blank database instead of raising ERR_DATABASE_MISSING — defeating the guard's whole purpose. Retry once immediately (most failures here are transient — AV scan lock, momentary ENOSPC), and if it still fails, log to the persistent app log via electron-log so the gap is visible to support/diagnostics rather than truly silent. Co-Authored-By: Claude Sonnet 5 <[email protected]>
4 tasks
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
initDatabase()previously openednew Database(dbPath)unconditionally, silently creating a blank database if a production install'sflo.dbwas deleted, unmounted, or otherwise unreadable — a merchant could lose all data with no warning..flo-db-initializedmarker written on every successful production boot, letting startup distinguish "database went missing" from "first-run install" (both look identical as an absent file).flo.dbdoesn't, startup now throwsERR_DATABASE_MISSINGand refuses to create a new empty database, directing the user to restore from backup instead. Surfaces through the existingInitialization Errordialog.resetDatabaseWithBackup, restore, interrupted-replacement recovery), dev mode, and the E2E db-path override are all unaffected — verified against the real reset/restore flows, not just by inspection.Addresses acceptance criterion 4 of #278 ("Fail-closed DB recovery guard"). The other three AC items in #278 (refunds/idempotency tables were already shipped separately; partial-refund math and cash-refund-into-day-close remain open, the latter blocked on #279) are out of scope for this PR.
Test plan
npm run buildtests/issue-278-fail-closed-missing-db.test.ts(fresh install, missing-db fail-closed, E2E override bypass, internal reset flow) — registered asnpm run test:issue-278-fail-closed-dbnpm run test:backup(production restore + interrupted-replacement recovery)npm run test:database-tools-api(65/65, including the real wipe-and-reinitialize flow)npx eslint main/db.ts— no new warnings/errors🤖 Generated with Claude Code