feat(notification): add durable mail delivery state

This commit is contained in:
shira committed 2026-10-06 10:52:47 +03:00
1 parent 391c8a6cec
commit 2db585fbff
41 files changed
+4903 -108

No files matched your search

@@ -93,6 +93,20 @@ locked_session AS (
FOR UPDATE SKIP LOCKED
),
locked_mail_request AS (
SELECT request.recipient_user_id
FROM mail_requests AS request
WHERE request.recipient_user_id IN (SELECT candidate.id FROM candidate)
FOR UPDATE SKIP LOCKED
),
locked_delivery AS (
SELECT delivery.recipient_user_id
FROM deliveries AS delivery
WHERE delivery.recipient_user_id IN (SELECT candidate.id FROM candidate)
FOR UPDATE SKIP LOCKED
),
deletable AS (
SELECT candidate.id
FROM candidate
@@ -111,6 +125,20 @@ deletable AS (
SELECT count(*) FROM locked_session
WHERE locked_session.user_id = candidate.id
)
AND (
SELECT count(*) FROM mail_requests AS request
WHERE request.recipient_user_id = candidate.id
) = (
SELECT count(*) FROM locked_mail_request
WHERE locked_mail_request.recipient_user_id = candidate.id
)
AND (
SELECT count(*) FROM deliveries AS delivery
WHERE delivery.recipient_user_id = candidate.id
) = (
SELECT count(*) FROM locked_delivery
WHERE locked_delivery.recipient_user_id = candidate.id
)
)
DELETE FROM users
@@ -124,15 +152,18 @@ type DeleteUnverifiedUsersRegisteredBeforeParams struct {
}
// Deletes one batch of accounts that stayed unverified since before cutoff,
// oldest first, with their tokens and sessions. Every lock is taken with SKIP
// LOCKED, so the statement never waits: an account whose row is locked, for
// example by a re-registration or a token insert, is skipped, and so is an
// account with any token or session row locked, for example by a
// confirmation that consumed a token. Skipped accounts stay for the next run.
// oldest first, with their tokens, sessions, mail requests, and mail
// deliveries (and through the deliveries their attempts). Every lock is taken
// with SKIP LOCKED, so the statement never waits: an account whose row is
// locked, for example by a re-registration, a token insert, or a delivery
// insert, is skipped, and so is an account with any token, session, mail
// request, or delivery row locked, for example by a confirmation that
// consumed a token, the delivery consumer, or a worker recording an attempt.
// Skipped accounts stay for the next run.
// Taking the account lock before the token locks is the reverse of the
// documented order, which is safe only because no lock here waits.
// An account qualifies only when every token and session row visible to the
// statement was locked above.
// An account qualifies only when every token, session, mail request, and
// delivery row visible to the statement was locked above.
func (q *Queries) DeleteUnverifiedUsersRegisteredBefore(ctx context.Context, arg DeleteUnverifiedUsersRegisteredBeforeParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteUnverifiedUsersRegisteredBefore, arg.Cutoff, arg.BatchLimit)
if err != nil {