The hub agrees to remove a removed app's off-site copies only when its OWN record (offsite_end_notices) says the household was told at least 30 days ago, by the hub's clock — beside the 30 days of absence. The notice is sent from the box's report, at every report until the mail service accepts it; the box's event mails nobody. No address or a blocked household: the operator is mailed in its place and that mail starts the 30 days. A notice that cannot go out: no agreement, the window's answer names the reason, one line to the operator after 3 days. This commit also supplies the files that handler.go (c3fa43a4) already calls (store.NoteEndNotices, api.kickEndNotices) and the tests that replace TestD84_Receipt_* (their files left the tree inb8cf6c24). Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
59 lines
2.9 KiB
Go
59 lines
2.9 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)`},
|
|
{"offsite_large_requests", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
|
|
{"offsite_end_notices", `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()
|
|
}
|