fix(postgres): age retention spares captures still waiting to be dispatched

Issue: archie-core-jrh2m
This commit is contained in:
sam committed 2026-10-13 16:19:12 +11:00
1 parent 4ac9fd852f
commit 796c7a711b
2 files changed
+24 -2

No files matched your search

@@ -189,9 +189,20 @@ func (q *Queries) DeleteCapturesBeyondCount(ctx context.Context, arg DeleteCaptu
}
const deleteCapturesOlderThan = `-- name: DeleteCapturesOlderThan :exec
DELETE FROM captures WHERE received_at < $1
DELETE FROM captures AS c
WHERE c.received_at < $1
AND NOT (
c.event_type <> '' AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
)
`
// Age retention spares what DeleteCapturesBeyondCount spares: a capture
// ListUndispatchedCaptures would still offer is accepted work.
func (q *Queries) DeleteCapturesOlderThan(ctx context.Context, receivedAt time.Time) error {
_, err := q.db.Exec(ctx, deleteCapturesOlderThan, receivedAt)
return err
@@ -31,7 +31,18 @@ ORDER BY c.received_at, c.id
LIMIT @entry_limit;
-- name: DeleteCapturesOlderThan :exec
DELETE FROM captures WHERE received_at < $1;
-- Age retention spares what DeleteCapturesBeyondCount spares: a capture
-- ListUndispatchedCaptures would still offer is accepted work.
DELETE FROM captures AS c
WHERE c.received_at < $1
AND NOT (
c.event_type <> '' AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
);
-- name: DeleteCapturesBeyondCount :exec
-- Count retention stays within the just-inserted capture's org: one org's