From 796c7a711be46d941389e67cb9a6bcfe9a2d69fe Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 13 Oct 2026 16:19:12 +1100 Subject: [PATCH] fix(postgres): age retention spares captures still waiting to be dispatched Issue: archie-core-jrh2m --- .../postgres/postgresdb/edastore.sql.go | 13 ++++++++++++- .../infrastructure/postgres/queries/edastore.sql | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index aa20db00a..3e538eef0 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -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 diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index af9cc1c75..50b29be2d 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -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