Skip to content

fix(update): save the rollback point without copying history - #1304

Merged
frahlg merged 2 commits into
masterfrom
1302-update-rollback-point
Sep 18, 2026
Merged

frahlg merged 2 commits into
masterfrom
1302-update-rollback-point

Conversation

@frahlg

@frahlg frahlg commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Refs #1302.

Problem

A Core update first writes a local rollback point. Until now that point exported every history row into state.db.gz through the paced live copy (1,024 rows or 256 KiB per batch, then a 100 ms pause, then a verification read), under a two-hour export deadline.

Observed tonight on the owner's Raspberry Pi 4 (v3.5.3-beta.1 → v3.6.0-beta.1), read-only measurements:

Item Value
history.db + WAL 6.73 GB + 0.77 GB
Raw export after 44 min 676 MB
Copy rate over the last 20 min about 0.25 MB/s
Time to finish at that rate about 8 h, against a 2 h deadline
Free disk 9.2 GB, against raw + gzip scratch of about 15 GB
Side effects while copying history commit failed … context deadline exceeded, SQLITE_BUSY on status writes, Core DP shadow plans rejected

So the update could not complete, and the box was degraded while it tried. Any box with a history above roughly 74 million rows hits the same wall on every schema-change update.

Why the copy bought nothing

  • ftw-updater restores only state.db (restoreSnapshotFiles). It never writes history.db.
  • handleVersionRollback refuses a point whose database_schema is older than the running Core's. The full copy was only usable through the offline ftw-backup path with the matching older Core.
  • History has lived in its own file since 3.x. The reason the old comment gave for including it ("the compact snapshot would erase recent time-series data") described the pre-3.x layout where history sat inside state.db.

Change

  • Store.BackupStateWithConfiguration copies the settings database and exports config from that copy. It skips exportHistoryToSQLite, keeps history_sqlite_generation so the restored state.db binds to the history.db that is still on disk, and preflights space against the settings file only. A store that still keeps legacy history inside state.db is copied whole, as before.
  • createPreUpdateSnapshot uses it and records history_in_place in meta.json. complete_database stays true; the point is complete for state.db.
  • Every update takes a rollback point when snapshots are enabled. The "schema unchanged; full history backup not needed" skip is gone because the point is now cheap.
  • Status and UI copy say what is copied: "Saving rollback point: settings database and config (history stays in place)".
  • Docs: backup-and-restore.md, self-update.md, operations.md.

Rollback across a history-format change is unchanged: the handler still refuses such a point and points to a full backup made before the update.

Evidence

  • go test ./internal/state/ ./internal/api/ focused runs pass, including three new tests:
    • the point holds no history tables, keeps the live history generation, and history.db keeps every row;
    • the space preflight is bounded by the settings file while a full backup still needs room for the history export;
    • a rollback done the way the updater does it (replace state.db, drop WAL/SHM, leave history.db) opens and keeps rows written before and after the point.
  • npm test: 638 pass.
  • Full go test ./... was still running when this PR was opened; CI will show the result.

What this does not fix

Boxes already on v3.5.3-beta.1 or older run the old code when they update, so their next schema-change update still tries the paced full export. The proposed transition is a documented one-time manual update (stop Core, copy state.db + config.yaml as the rollback point, bump the image tags, compose up). The alternative, declaring the old state schema in the next release's notes so old Cores skip the step, would refuse updates for any box already on 3.6.0-beta.1 and fails the stable release check; that is the owner's call. v3.6.0-beta.1 was converted to a draft tonight so no further box starts the doomed step; the beta channel resolves to v3.5.3-beta.1.

Follow-ups, proposed and not started:

🤖 Generated with Claude Code

Closes #1159: every update now takes a rollback point, including same-schema updates.

The pre-update snapshot exported every history row into state.db.gz
through the paced live copy. On a Raspberry Pi 4 with a 6.7 GB history
it ran at about 0.25 MB/s, could not meet the two-hour export deadline,
and made history commits time out while it ran. The updater only ever
restores state.db, and the UI rollback refuses a point that predates a
history-format change, so the copy bought nothing an operator could use
from the UI.

The rollback point now holds the settings database and config.yaml.
history.db stays in place through update and rollback. The step is
bounded by settings size, its space preflight ignores history, and every
update takes one. Going back across a history-format change still needs
a full backup made before that update; the rollback handler already
refuses such a point and says so.

Refs #1302

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>
@frahlg

frahlg commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

Full go test ./... passes after make drivers fetched the pinned driver snapshot into the worktree (the only failures before that were the tibber/zap driver tests reading the missing generated Lua files). npm test: 638 pass.

@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: 03cd846045

ℹ️ 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".

// hours on a Raspberry Pi for a file the UI rollback could not use
// (#1302). A store that still keeps legacy history inside state.db is
// copied whole.
stored, hasStored, err := s.deps.State.BackupStateWithConfiguration(filepath.Join(dir, "state.db.gz"), report)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve history when reverting legacy rollback points

When the selected target is one of the retained pre-change schema-v2 snapshots, it contains embedded history plus history_restore_generation; opening it renames the live history.db and binds a new generation. The pre-rollback safety point created here now contains only settings with the original history_sqlite_generation, so either automatic recovery after a failed health check or a later forward rollback fails with state and history generations differ instead of restoring the pre-rollback state. Keep a full safety copy when meta.HistoryInPlace is false, or reject/migrate these legacy targets.

AGENTS.md reference: AGENTS.md:L24-L25

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

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-09-17T18:30:57.267443Z 03cd846 PR opened
ℹ️ 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.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes the Core update rollback point to copy the settings database only, leaving history.db untouched — bounded by settings size instead of years of telemetry, and taken on every update (previously skipped whole when the schema was unchanged). #1302.

Correctness:

  • backupWithConfiguration now takes includeHistory; when false it skips exportHistoryToSQLite and bounds the disk-space preflight to stateSourceBytes() instead of BackupSourceBytes() — matches the stated goal of not waiting on a history export.
  • Legacy stores that still embed history inside state.db stay complete, since the whole-file copy captures it regardless of includeHistory — the comment on BackupStateWithConfiguration calls this out explicitly and stateSourceBytes() sizes off the same file, so the preflight stays accurate for that layout too.
  • TestRollbackPointRestoresBesideHistoryInPlace is the right test for the risky part (rollback replacing state.db while history.db survives untouched, before and after the point) — good that it checks rows written after the point too, not just before.
  • handleVersionRollback's use of the new HistoryInPlace meta field to refuse a cross-format rollback isn't in this diff, so I can't verify that gate directly, but it's referenced consistently across docs/comments/tests as already existing.

Security: no secrets, no new deps, no network destinations, no authz surface. Pure local-disk backup/restore logic.

Frontend + docs strings were updated in lockstep with the backend behavior change (four doc files, two web components, the progress test asserting the old "schema unchanged" message is gone). Safe to merge from my read.

@frahlg
frahlg merged commit dc728c4 into master Sep 18, 2026
16 checks passed
@frahlg
frahlg deleted the 1302-update-rollback-point branch September 18, 2026 04:06
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.

fix(selfupdate): same-schema Core updates skip the rollback snapshot

2 participants