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