Skip to content

feat: take a validated backup before an upgrade migrates the database - #106

Merged
TusanHomichi merged 17 commits into
mainfrom
claude/94-pre-migration-backup
Oct 14, 2026
Merged

TusanHomichi merged 17 commits into
mainfrom
claude/94-pre-migration-backup

Conversation

@TusanHomichi

@TusanHomichi TusanHomichi commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Primary Issue

Closes #94

Problem And Outcome

An upgrade applies forward-only migrations, and there is no downgrade (ADR 0023). Before this PR, the recovery point for a bad upgrade was the last scheduled backup, which could be up to a day old. Now any command that would migrate the database does three things first:

  • takes a validated snapshot of it as it was;
  • refuses to migrate without that snapshot;
  • logs the snapshot and the way back.

An older build that meets a database upgraded by a newer one says so plainly.

Changes

  • backup::open_migrated(data_dir, held_lock, off_server) is the one way the executable opens the database for use. serve, setup-code, and recover all go through it, and a guard test keeps storage::open out of main.rs. When a migration is pending, it:
    1. holds the serve lock: serve passes its own lock; other commands take it, or refuse with "stop the running service";
    2. checks for pending migrations again under the lock;
    3. takes consolebook-premigration-<stamp>.db with VACUUM INTO and validates it. Nothing is written to the old schema;
    4. keeps at most 3 pre-migration snapshots, so a migration that fails on every restart cannot fill the disk;
    5. copies the snapshot to the off-server directory when configured, best effort, before migrating, so a failed upgrade's recovery point is off the server too;
    6. migrates, then records the backup's audit event in the migrated database.
  • storage.rs:
    • pending_migrations() and newer_migrations();
    • open() refuses a database with migrations this build doesn't know, with a message naming the way back.
  • Docs:
    • docs/install.md "Upgrading": the pre-migration snapshot, which commands take it, how to find the version you upgraded from, and how to restore with that version;
    • ADR 0023 text and CHANGELOG entries.
  • Ported: fix(backups): order snapshots by their time, not their name #107 (backups: a pre-restore snapshot sorts as "newest" forever, masking copy failures #105 snapshot time ordering, which this PR's file naming relies on) and fix(lifecycle): read a phase event's "now" after taking the writer #109 (lifecycle: a phase event's "now" is read before it waits for the writer, so it can be refused as out of order #108 lifecycle race, which broke this PR's CI once). Both are now on main.

Correction rungs:

  • 1: one owning API; a #[must_use] pre-migration backup whose audit can only be recorded against a migrated pool.
  • 2: guard test and the tests below.

Verification

  • tests/pre_migration_backup.rs, 12 tests:
    • the old schema is snapshotted and nothing is written to it;
    • a database at migration 0001, which has no audit table, upgrades;
    • a new or current installation takes no extra backup;
    • an unwritable backups/ fails closed, with nothing migrated;
    • a newer database is refused in plain words and left unchanged;
    • the snapshot is the one catch-up copies, and a later backup is newer than it;
    • open_migrated backs up, migrates, and audits once;
    • pre-migration snapshots stay bounded at 3, and ordinary backups are untouched;
    • a running server blocks a migrating command but not an ordinary one;
    • the snapshot is copied off the server before migrating, and an unreachable share doesn't block;
    • guard: main.rs migrates only through open_migrated.
  • Negative controls, each failing with the fix removed:
    • audit-before-migrate (no such table: audit_event);
    • the guard;
    • the prune bound;
    • lock acquisition;
    • name ordering;
    • the off-server copy;
    • the newer-database refusal.
  • Recorded drill against real builds: the published v0.1.0 archive (checksum verified) and a scratch build of this branch with one drill-only migration standing in for the next release.
    1. The new build served 0.1.0's data, logged the snapshot, and migrated from schema 15 to 16.
    2. 0.1.0 then refused the migrated database (exit 1).
    3. 0.1.0 restored the pre-migration snapshot back to schema 15. doctor passed every check, and 0.1.0 served again under the same installation id.
  • Full local proof on e96eafc (scripts/proof.py, euid 0 with DAC capabilities dropped): passed, all 7 gates. Browser suite 59 expected, 0 unexpected, 0 flaky. Receipt .proof/20261014T225745Z-e96eafc5abfb/receipt.json, sha256 b24e2973b32ff910c111169e49c9463ef7e3d047c030e6e1bc225fe0e9d2e5da.
  • Codex: seven findings across rounds. Six were fixed as above. One was declined with reasoning on its thread: persisting a pending audit row across restarts if the post-migration audit insert fails.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Re-running the installer upgrades in place, and there is no downgrade:
an older executable refuses a database a newer one migrated (ADR 0023).
Nothing marked the moment before the change, so the recovery point was
whenever the last scheduled backup ran.

serve now checks, read-only, which of its migrations an existing
database lacks (storage::pending_migrations). When any are pending it
takes an ordinary validated snapshot first (backup::before_migrating),
logs its name with how to go back, and fails closed if the snapshot
cannot be written. A new or current database takes no extra snapshot.
Being an ordinary snapshot, it is retained and copied off the server
like any other.

Tests build a real older schema (every migration but the newest): the
snapshot holds the old schema and validates, the migration then runs,
nothing is pending afterwards, and an unwritable backups/ leaves the
database unmigrated. Making detection report nothing pending fails two
of them. The install guide and ADR 0023 describe going back.

Closes #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts
The ADR renumbering in #91 relabeled two links to ADR 0023 (installation
lifecycle) as ADR 0024 in the install guide and the development index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts
The upgrade drill showed 0.1.0 refusing a migrated database with
"migration 16 was previously applied but is missing". Startup now
checks for applied migrations this build does not know before
migrating, and names the way back.

Refs #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts
# Conflicts:
#	CHANGELOG.md
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T23:03:03.431100Z e96eafc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 74e78b1d6a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/backup.rs Resolved
backup::run records its audit event in the database it just copied. Before
migrating, that database has the old schema, which may lack the audit table
(migration 0002) or the current event kinds, so startup failed after taking
a good snapshot. The snapshot step now writes nothing; the pre-migration
backup comes back as a must_use PreMigrationBackup whose record() writes the
audit event into the migrated database.

Refs #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ad460a09fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/main.rs Resolved
setup-code and recover opened the database with storage::open, which
migrates, so running either with a new executable before serve upgraded
the installation without its recovery snapshot. backup::open_migrated is
now the one way the executable opens the database for use: backup, then
migrate, then audit. A guard test keeps storage::open out of main.rs.
Pre-migration backups no longer prune; the CLI does not know the
configured retention, and the next scheduled backup prunes with it.

Refs #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 096eb3f083

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/backup.rs Resolved
Comment thread crates/consolebook-server/src/backup.rs Resolved
…pshots

A command other than serve now takes the serve lock when it would migrate,
and refuses while a server holds it, so no running server commits records
after the snapshot or runs against the new schema; pending migrations are
checked again under the lock. Pre-migration snapshots are named
consolebook-premigration-<stamp>.db and at most three are kept, so a
migration that fails on every restart cannot fill the disk.

Refs #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

consolebook-prerestore-<stamp>.db sorted after every consolebook-<stamp>.db,
so after any restore the set-aside database stayed "newest" for good:
catch-up copied it off the server, copy status read current while later
copies failed, and retention pruned real backups ahead of it.

Closes #105

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e40a9c315d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/backup.rs Resolved

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7356d318f2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/install.md Resolved
record_phase_event read the implicit effective instant before waiting for
the write lock, then refused anything older than the newest committed event
as out of order. A request that waited across a second boundary while
another event committed got OutOfOrder instead of the real answer. It now
reads "now" (and checks for a future instant) under the lock.

Closes #108

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Copy link
Copy Markdown
Member Author

pr-gate failed on 7356d31 in write_transactions::training_writes::phase_events_validate_the_committed_pause_state with left: OutOfOrder, right: AlreadyPaused. That isn't this PR's bug. It's a pre-existing race in lifecycle::record_phase_event, which this PR doesn't touch: the implicit "now" was read before waiting for the writer, so a request that waited across a second boundary was refused as out of order.

Filed as #108 and fixed in #109, with a deterministic test whose negative control reproduces exactly this failure. The fix is ported here in e83cc88, together with a merge of main. CI is re-running on the new head.


Generated by Claude Code

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3be2551e52

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/main.rs Resolved
serve passes its off-server copy settings to open_migrated, which copies the
validated snapshot before migrating: when the migration then fails, the
scheduler that would have copied it never starts. The copy is best effort;
an unreachable share is logged and the next start's catch-up retries.

Refs #94

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TsvZjbz8dTRM8A1NqAP5ts

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e96eafc5ab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/consolebook-server/src/backup.rs Resolved
@TusanHomichi
TusanHomichi merged commit 8da788c into main Oct 14, 2026
4 checks passed
@TusanHomichi
TusanHomichi deleted the claude/94-pre-migration-backup branch October 14, 2026 23:12
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.

upgrade: take a validated backup before migrating the database

2 participants