Skip to content

fix(db): fail closed when production database file goes missing (#278) - #610

Merged
itsbkm merged 2 commits into
mainfrom
fix/278-fail-closed-missing-db
Sep 2, 2026
Merged

itsbkm merged 2 commits into
mainfrom
fix/278-fail-closed-missing-db

Conversation

@itsbkm

@itsbkm itsbkm commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • initDatabase() previously opened new Database(dbPath) unconditionally, silently creating a blank database if a production install's flo.db was deleted, unmounted, or otherwise unreadable — a merchant could lose all data with no warning.
  • Adds a .flo-db-initialized marker written on every successful production boot, letting startup distinguish "database went missing" from "first-run install" (both look identical as an absent file).
  • When the marker exists but flo.db doesn't, startup now throws ERR_DATABASE_MISSING and refuses to create a new empty database, directing the user to restore from backup instead. Surfaces through the existing Initialization Error dialog.
  • Internal maintenance paths that deliberately recreate the db file (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 build
  • New regression test tests/issue-278-fail-closed-missing-db.test.ts (fresh install, missing-db fail-closed, E2E override bypass, internal reset flow) — registered as npm run test:issue-278-fail-closed-db
  • npm 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

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]>
@greptile-apps

greptile-apps Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds a production startup marker intended to distinguish first-run installation from a previously initialized installation whose database disappeared.

  • Refuses startup with ERR_DATABASE_MISSING when the database is absent but the marker exists.
  • Exempts E2E and internal database-recreation paths from the guard.
  • Adds regression coverage and registers it in the main test suite.

Confidence Score: 4/5

The 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

Important Files Changed

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]
Loading

Reviews (2): Last reviewed commit: "fix(db): retry and log marker-write fail..." | Re-trigger Greptile

Comment thread main/db.ts Resolved
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]>
@itsbkm
itsbkm merged commit 1d3d69a into main Sep 2, 2026
15 checks passed
@itsbkm
itsbkm deleted the fix/278-fail-closed-missing-db branch September 2, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant