feat(state): apply audit retention to observability events

This commit is contained in:
sam committed 2026-10-25 21:42:38 +11:00
1 parent 655068db70
commit 045f8c427d
9 files changed
+478 -2

No files matched your search

@@ -167,3 +167,43 @@ func (q *Queries) LockAuditRecord(ctx context.Context, arg LockAuditRecordParams
_, err := q.db.Exec(ctx, lockAuditRecord, arg.TableName, arg.RecordKey)
return err
}
const pruneAuditEventBatch = `-- name: PruneAuditEventBatch :execrows
DELETE FROM events
WHERE id IN (
SELECT id FROM events
WHERE kind = ANY($1::text[])
AND at < $2::timestamptz
ORDER BY at
LIMIT $3::bigint
)
`
type PruneAuditEventBatchParams struct {
Kinds []string
Cutoff time.Time
Batch int64
}
// One bounded delete of the observability kinds the sweep allows, oldest
// first. The kind list is code, so no stored setting can widen it.
func (q *Queries) PruneAuditEventBatch(ctx context.Context, arg PruneAuditEventBatchParams) (int64, error) {
result, err := q.db.Exec(ctx, pruneAuditEventBatch, arg.Kinds, arg.Cutoff, arg.Batch)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const tryAuditPruneLock = `-- name: TryAuditPruneLock :one
SELECT pg_try_advisory_xact_lock(hashtextextended('archie.audit.prune', 0))
`
// The retention sweep's transaction-scoped lock. A second sweep skips its
// tick rather than queuing deletes behind this one.
func (q *Queries) TryAuditPruneLock(ctx context.Context) (bool, error) {
row := q.db.QueryRow(ctx, tryAuditPruneLock)
var pg_try_advisory_xact_lock bool
err := row.Scan(&pg_try_advisory_xact_lock)
return pg_try_advisory_xact_lock, err
}