Files
felhom.eu/hub/internal/store/orphan_rows.go
T
admin 01a106b78c hub: the off-site copies of a removed app end after 30 days — the hub agrees first, the allowance is beside the cap, the household's mail (R-1374 part 1, D72, D84)
Built, not deployed. `07-backup-architecture.md` §8.2f parts 3-4 and decision 309.

- store: the hub's own record of which apps a box runs, by the hub's clock
  (offsite_app_presence, fed by every controller report); it goes with the household.
- offsitekeys: window-open takes the box's due end records and answers
  ended_allowed / ended_refused. An app is agreed only after 30 days of absence
  in the hub's record; an id only when the hub's own listing holds it, uploaded
  before that absence. The cap (max_remove) is unchanged; the close keeps only
  agreed ids from removed_for_ended_apps and allows them beside the cap.
- The 07:10 audit explains the ended apps' vanished files by name and still
  raises for every other vanished file.
- notify: offsite_end_scheduled is a household receipt — one mail per press to
  the household's own address, whatever its switches; the operator is not mailed.
- Two operator-only hub records: offsite_ended_app_removed (info),
  offsite_ended_app_refused (warning).

Tests TestD72_*, TestD84_*; 44 mutations seen red, 5 after a redo
(documentation/audits/build-release-4-2026-11-15/A/redproof-hub-*.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 13:26:13 +01:00

57 lines
2.6 KiB
Go

package store
import "fmt"
// OrphanRows counts what PurgeOrphanRows removed, per table.
type OrphanRows map[string]int64
// Total is the sum.
func (o OrphanRows) Total() int64 {
var n int64
for _, v := range o {
n += v
}
return n
}
// orphanTables are the tables D73 names (`09` §3 decision 293, R-1376) — the ones that had no delete statement — each
// with the column that names its owner and the query that says „the owner still exists”.
//
// A BOX exists while it has a `hosts` row. A HOUSEHOLD exists while it has a config row OR a box: a „ghost” (the
// config row gone, residue left) is exactly what this sweep is for, but a household in the middle of its delete
// cascade (boxes gone, config still there) is not an orphan.
var orphanTables = []struct{ table, where string }{
{"os_host_settings", `host_id NOT IN (SELECT host_id FROM hosts)`},
{"os_reports", `host_id NOT IN (SELECT host_id FROM hosts)`},
{"os_kernel_notices", `host_id NOT IN (SELECT host_id FROM hosts)`},
{"offsite_keys", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_windows", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_abandon_requests", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_app_presence", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
}
// PurgeOrphanRows removes, in ONE transaction, the rows of boxes and households that no longer exist from the six
// tables that had no delete statement (D73). It is the daily clean-up for what was left BEFORE DeleteHost and
// PurgeCustomerResidue learned those tables (Peti's box, the test boxes of 2026-11): from now on a delete leaves
// nothing for it. It never touches the delete journal (`host_deletions`, `customer_resets`), `events`,
// `notification_log` or `operator_actions` (those go by the one-year rule). Idempotent.
// Pinned by TestD73_PurgeOrphanRows_OnlyTheGone.
func (s *Store) PurgeOrphanRows() (OrphanRows, error) {
tx, err := s.db.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
out := OrphanRows{}
for _, t := range orphanTables {
res, err := tx.Exec(`DELETE FROM ` + t.table + ` WHERE ` + t.where)
if err != nil {
return nil, fmt.Errorf("PurgeOrphanRows %s: %w", t.table, err)
}
if n, _ := res.RowsAffected(); n > 0 {
out[t.table] = n
}
}
return out, tx.Commit()
}