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

+38 -7
View File
@@ -121,11 +121,14 @@ RETURNING
updated_at;
-- 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.
-- name: DeleteUnverifiedUsersRegisteredBefore :execrows
@@ -154,8 +157,22 @@ locked_session AS (
FOR UPDATE SKIP LOCKED
),
-- An account qualifies only when every token and session row visible to the
-- statement was locked above.
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
),
-- An account qualifies only when every token, session, mail request, and
-- delivery row visible to the statement was locked above.
deletable AS (
SELECT candidate.id
FROM candidate
@@ -174,6 +191,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