fix(orgs): delete organizations at once and purge their check data later
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m28s
ci / test (pull_request) Successful in 12m39s
ci / build (pull_request) Successful in 16m10s
ci / image (pull_request) Successful in 5m20s

Deleting an organization removed its check results and aggregates in the
same transaction as the request. With gigabytes of results that took
longer than the request may last, it was cut off and rolled back, so a
large organization could not be deleted at all.

The organization and everything with a foreign key on it now go in the
request; its check data is queued in org_purges. Every API instance
purges the queue a chunk at a time, each chunk in a transaction of its
own, right after a deletion and every minute. Raw results go first, so
the aggregates' refresh does not rebuild what was purged. A lease lets
another instance take over a purge whose instance stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-28 14:50:18 +01:00
1 parent 9d01b62b98
commit 8ff4a86371
13 files changed
+393 -45

No files matched your search

+27 -5
View File
@@ -176,6 +176,12 @@ func TestDeleteOrganization(t *testing.T) {
})
keepOrg := uuid.MustParse(keep.orgID())
e.seedResults(t, keepOrg, uuid.MustParse(km["id"].(string)))
// Results older than a week are compressed; the purge goes through them
// as well.
if _, err := e.dbPool.Exec(context.Background(),
`SELECT compress_chunk(c, if_not_compressed => true) FROM show_chunks('check_results', older_than => now() - interval '1 day') c`); err != nil {
t.Fatalf("compressing chunks: %v", err)
}
admin := e.joinVia(t, owner.invite("a@"+uniq("d")+".test", owner.roleID("admin")))
if st, body := admin.do("POST", "/api/v1/org/delete", map[string]any{"slug": slug, "password": password}); st != http.StatusForbidden {
@@ -191,17 +197,33 @@ func TestDeleteOrganization(t *testing.T) {
t.Fatalf("owner deletes org = %d %v", st, body)
}
// The organization is gone at once, its check data is purged in the
// background, chunk by chunk.
for query, want := range map[string]int{
`SELECT count(*) FROM orgs WHERE id = $1`: 0,
`SELECT count(*) FROM monitors WHERE org_id = $1`: 0,
`SELECT count(*) FROM check_results WHERE org_id = $1`: 0,
`SELECT count(*) FROM check_results_1h WHERE org_id = $1`: 0,
`SELECT count(*) FROM audit_events WHERE org_id = $1`: 0,
`SELECT count(*) FROM orgs WHERE id = $1`: 0,
`SELECT count(*) FROM monitors WHERE org_id = $1`: 0,
`SELECT count(*) FROM org_purges WHERE org_id = $1`: 1,
} {
if got := e.count(t, query, org); got != want {
t.Errorf("%s = %d, want %d", query, got, want)
}
}
if err := e.api.PurgeDeletedOrgs(context.Background()); err != nil {
t.Fatalf("PurgeDeletedOrgs() = %v", err)
}
for query, want := range map[string]int{
`SELECT count(*) FROM check_results WHERE org_id = $1`: 0,
`SELECT count(*) FROM check_results_1h WHERE org_id = $1`: 0,
`SELECT count(*) FROM audit_events WHERE org_id = $1`: 0,
`SELECT count(*) FROM org_purges WHERE org_id = $1`: 0,
} {
if got := e.count(t, query, org); got != want {
t.Errorf("%s = %d, want %d", query, got, want)
}
}
if e.count(t, `SELECT count(*) FROM check_results WHERE org_id = $1`, keepOrg) != 30 {
t.Error("another organization's results were deleted")
}
if e.count(t, `SELECT count(*) FROM check_results_1h WHERE org_id = $1`, keepOrg) == 0 {
t.Error("another organization's aggregates were deleted")
}