238 lines
6.3 KiB
SQL
238 lines
6.3 KiB
SQL
-- Stores a new delivery. A delivery with the same ID already exists when the
|
|
-- same request is handled again; the statement then inserts nothing and
|
|
-- returns 0 rows, and the stored content stays as it was. While another
|
|
-- transaction holds an uncommitted row with the same ID, the insert waits
|
|
-- for it.
|
|
-- name: InsertDelivery :execrows
|
|
INSERT INTO deliveries (
|
|
id,
|
|
source_event_id,
|
|
recipient_user_id,
|
|
recipient_email,
|
|
kind,
|
|
template_version,
|
|
subject,
|
|
text_body,
|
|
html_body,
|
|
content_sha256,
|
|
provider,
|
|
provider_account,
|
|
idempotency_key,
|
|
status,
|
|
attempt_count,
|
|
next_attempt_at,
|
|
last_attempt_at,
|
|
provider_message_id,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.narg(source_event_id),
|
|
sqlc.arg(recipient_user_id),
|
|
sqlc.arg(recipient_email),
|
|
sqlc.arg(kind),
|
|
sqlc.arg(template_version),
|
|
sqlc.arg(subject),
|
|
sqlc.arg(text_body),
|
|
sqlc.arg(html_body),
|
|
sqlc.arg(content_sha256),
|
|
sqlc.arg(provider),
|
|
sqlc.arg(provider_account),
|
|
sqlc.arg(idempotency_key),
|
|
sqlc.arg(status),
|
|
sqlc.arg(attempt_count),
|
|
sqlc.narg(next_attempt_at),
|
|
sqlc.narg(last_attempt_at),
|
|
sqlc.narg(provider_message_id),
|
|
sqlc.arg(version),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(updated_at)
|
|
)
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
-- name: GetDeliveryByID :one
|
|
SELECT
|
|
id,
|
|
source_event_id,
|
|
recipient_user_id,
|
|
recipient_email,
|
|
kind,
|
|
template_version,
|
|
subject,
|
|
text_body,
|
|
html_body,
|
|
content_sha256,
|
|
provider,
|
|
provider_account,
|
|
idempotency_key,
|
|
status,
|
|
attempt_count,
|
|
next_attempt_at,
|
|
last_attempt_at,
|
|
provider_message_id,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
FROM deliveries
|
|
WHERE id = sqlc.arg(id);
|
|
|
|
-- Advances a delivery past one attempt and appends the attempt in one
|
|
-- statement, so both happen or neither does, inside or outside a
|
|
-- transaction. The update applies only while the stored version still
|
|
-- equals expected_version; otherwise nothing is written and the statement
|
|
-- returns 0 rows. Two workers that record the same attempt number therefore
|
|
-- cannot both succeed. Content columns are not touched; a trigger refuses
|
|
-- any change to them.
|
|
-- name: RecordDeliveryAttempt :execrows
|
|
WITH advanced AS (
|
|
UPDATE deliveries
|
|
SET
|
|
status = sqlc.arg(status),
|
|
attempt_count = sqlc.arg(attempt_number),
|
|
next_attempt_at = sqlc.narg(next_attempt_at),
|
|
last_attempt_at = sqlc.arg(attempted_at),
|
|
provider_message_id = coalesce(
|
|
sqlc.narg(provider_message_id), deliveries.provider_message_id
|
|
),
|
|
version = deliveries.version + 1,
|
|
updated_at = sqlc.arg(updated_at)
|
|
WHERE
|
|
deliveries.id = sqlc.arg(delivery_id)
|
|
AND deliveries.version = sqlc.arg(expected_version)
|
|
RETURNING deliveries.id
|
|
)
|
|
|
|
INSERT INTO delivery_attempts (
|
|
delivery_id,
|
|
attempt_number,
|
|
attempted_at,
|
|
outcome,
|
|
rejection,
|
|
provider_code,
|
|
provider_message_id,
|
|
retry_after_ns,
|
|
status_after,
|
|
next_attempt_at
|
|
)
|
|
SELECT
|
|
advanced.id,
|
|
sqlc.arg(attempt_number),
|
|
sqlc.arg(attempted_at),
|
|
sqlc.arg(outcome),
|
|
sqlc.narg(rejection),
|
|
sqlc.narg(provider_code),
|
|
sqlc.narg(provider_message_id),
|
|
sqlc.arg(retry_after_ns),
|
|
sqlc.arg(status),
|
|
sqlc.narg(next_attempt_at)
|
|
FROM advanced;
|
|
|
|
-- Stores a change of the delivery state that is not an attempt: a quota wait
|
|
-- or a new provider account before the first attempt. It applies only while
|
|
-- the stored version still equals expected_version and returns 0 rows
|
|
-- otherwise. The trigger refuses a provider change after an attempt.
|
|
-- name: UpdateDeliveryState :execrows
|
|
UPDATE deliveries
|
|
SET
|
|
status = sqlc.arg(status),
|
|
next_attempt_at = sqlc.narg(next_attempt_at),
|
|
provider = sqlc.arg(provider),
|
|
provider_account = sqlc.arg(provider_account),
|
|
version = deliveries.version + 1,
|
|
updated_at = sqlc.arg(updated_at)
|
|
WHERE
|
|
deliveries.id = sqlc.arg(id)
|
|
AND deliveries.version = sqlc.arg(expected_version);
|
|
|
|
-- name: ListDeliveryAttempts :many
|
|
SELECT
|
|
delivery_id,
|
|
attempt_number,
|
|
attempted_at,
|
|
outcome,
|
|
rejection,
|
|
provider_code,
|
|
provider_message_id,
|
|
retry_after_ns,
|
|
status_after,
|
|
next_attempt_at
|
|
FROM delivery_attempts
|
|
WHERE delivery_id = sqlc.arg(delivery_id)
|
|
ORDER BY attempt_number;
|
|
|
|
-- name: InsertMailRequest :exec
|
|
INSERT INTO mail_requests (
|
|
id,
|
|
recipient_user_id,
|
|
kind,
|
|
subject,
|
|
body,
|
|
link_path,
|
|
created_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(recipient_user_id),
|
|
sqlc.arg(kind),
|
|
sqlc.arg(subject),
|
|
sqlc.arg(body),
|
|
sqlc.arg(link_path),
|
|
sqlc.arg(created_at)
|
|
);
|
|
|
|
-- Locks a request for the consumer transaction that turns it into a
|
|
-- delivery. A concurrent consumer of the same request waits here and then
|
|
-- finds the row deleted. The account cleanup and the cascade from users take
|
|
-- the same row lock, so a request is never converted for an account whose
|
|
-- deletion is in progress without one of the two transactions waiting.
|
|
-- name: LockMailRequest :one
|
|
SELECT
|
|
id,
|
|
recipient_user_id,
|
|
kind,
|
|
subject,
|
|
body,
|
|
link_path,
|
|
created_at
|
|
FROM mail_requests
|
|
WHERE id = sqlc.arg(id)
|
|
FOR UPDATE;
|
|
|
|
-- name: DeleteMailRequest :execrows
|
|
DELETE FROM mail_requests
|
|
WHERE id = sqlc.arg(id);
|
|
|
|
-- Reads the status of the newest delivery of a kind addressed to an account,
|
|
-- for the account's own view of its mail. It selects no address and no
|
|
-- content. The recipient index narrows the scan to the account's rows.
|
|
-- name: GetLatestDeliveryStatus :one
|
|
SELECT
|
|
id,
|
|
kind,
|
|
status,
|
|
next_attempt_at,
|
|
created_at,
|
|
updated_at
|
|
FROM deliveries
|
|
WHERE
|
|
recipient_user_id = sqlc.arg(recipient_user_id)
|
|
AND kind = sqlc.arg(kind)
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT 1;
|
|
|
|
-- Aggregates the deliveries by kind and status for the metrics, at most once
|
|
-- per metrics interval. It reads only kind, status, and next_attempt_at,
|
|
-- never an address, a body, or an ID. No index covers status, so it scans
|
|
-- the table; add an index on (status) or keep per-status counters before
|
|
-- the table holds millions of rows.
|
|
-- name: GetDeliveryStatistics :many
|
|
SELECT
|
|
kind,
|
|
status,
|
|
count(*) AS deliveries,
|
|
min(next_attempt_at) FILTER (
|
|
WHERE status = 'pending'
|
|
)::timestamptz AS earliest_pending_attempt_at
|
|
FROM deliveries
|
|
GROUP BY kind, status;
|