631 lines
18 KiB
SQL
631 lines
18 KiB
SQL
-- name: InsertUser :exec
|
|
INSERT INTO users (
|
|
id,
|
|
email,
|
|
email_normalized,
|
|
password_hash,
|
|
scenario,
|
|
onboarding_step,
|
|
email_verified_at,
|
|
registration_version,
|
|
last_registered_at,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(email),
|
|
sqlc.arg(email_normalized),
|
|
sqlc.arg(password_hash),
|
|
sqlc.arg(scenario),
|
|
sqlc.arg(onboarding_step),
|
|
sqlc.narg(email_verified_at),
|
|
sqlc.arg(registration_version),
|
|
sqlc.arg(last_registered_at),
|
|
sqlc.arg(version),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(updated_at)
|
|
);
|
|
|
|
-- name: GetUserByID :one
|
|
SELECT
|
|
id,
|
|
email,
|
|
email_normalized,
|
|
password_hash,
|
|
scenario,
|
|
onboarding_step,
|
|
email_verified_at,
|
|
registration_version,
|
|
last_registered_at,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE id = sqlc.arg(id);
|
|
|
|
-- name: GetUserByEmailNormalized :one
|
|
SELECT
|
|
id,
|
|
email,
|
|
email_normalized,
|
|
password_hash,
|
|
scenario,
|
|
onboarding_step,
|
|
email_verified_at,
|
|
registration_version,
|
|
last_registered_at,
|
|
version,
|
|
created_at,
|
|
updated_at
|
|
FROM users
|
|
WHERE email_normalized = sqlc.arg(email_normalized);
|
|
|
|
-- Replaces the registration of an unverified account and advances the
|
|
-- registration version in the same statement, so no caller can choose it.
|
|
-- The version check rejects a writer that read an older row; a verified
|
|
-- account never changes. The same statement deletes the C1 that the earlier
|
|
-- registration of this account recorded, so the consent of an earlier
|
|
-- registrant never stands for the new one; the new registration records its
|
|
-- own C1 in the same transaction. A version conflict deletes nothing.
|
|
-- name: UpdateUserRegistration :one
|
|
WITH updated AS (
|
|
UPDATE users
|
|
SET
|
|
email = sqlc.arg(email),
|
|
password_hash = sqlc.arg(password_hash),
|
|
scenario = sqlc.arg(scenario),
|
|
onboarding_step = sqlc.arg(onboarding_step),
|
|
registration_version = registration_version + 1,
|
|
last_registered_at = sqlc.arg(last_registered_at),
|
|
updated_at = sqlc.arg(updated_at),
|
|
version = version + 1
|
|
WHERE
|
|
users.id = sqlc.arg(id)
|
|
AND users.version = sqlc.arg(expected_version)
|
|
AND users.email_verified_at IS NULL
|
|
RETURNING
|
|
users.id,
|
|
users.email,
|
|
users.email_normalized,
|
|
users.password_hash,
|
|
users.scenario,
|
|
users.onboarding_step,
|
|
users.email_verified_at,
|
|
users.registration_version,
|
|
users.last_registered_at,
|
|
users.version,
|
|
users.created_at,
|
|
users.updated_at
|
|
),
|
|
|
|
discarded_consent AS (
|
|
DELETE FROM consent_records
|
|
USING updated
|
|
WHERE
|
|
consent_records.subject_id = updated.id
|
|
AND consent_records.purpose = 'c1'
|
|
AND consent_records.source = 'registration'
|
|
)
|
|
|
|
SELECT
|
|
updated.id,
|
|
updated.email,
|
|
updated.email_normalized,
|
|
updated.password_hash,
|
|
updated.scenario,
|
|
updated.onboarding_step,
|
|
updated.email_verified_at,
|
|
updated.registration_version,
|
|
updated.last_registered_at,
|
|
updated.version,
|
|
updated.created_at,
|
|
updated.updated_at
|
|
FROM updated;
|
|
|
|
-- Verifies the email only for the registration version the consumed token
|
|
-- was issued for.
|
|
-- name: MarkUserEmailVerified :one
|
|
UPDATE users
|
|
SET
|
|
email_verified_at = sqlc.arg(verified_at),
|
|
updated_at = sqlc.arg(verified_at),
|
|
version = version + 1
|
|
WHERE
|
|
id = sqlc.arg(id)
|
|
AND registration_version = sqlc.arg(registration_version)
|
|
AND email_verified_at IS NULL
|
|
RETURNING
|
|
id,
|
|
email,
|
|
email_normalized,
|
|
password_hash,
|
|
scenario,
|
|
onboarding_step,
|
|
email_verified_at,
|
|
registration_version,
|
|
last_registered_at,
|
|
version,
|
|
created_at,
|
|
updated_at;
|
|
|
|
-- Applies the registration carried by its token and verifies the address in
|
|
-- one optimistic update. The caller holds the token lock and checks the
|
|
-- matching C1 in the same transaction.
|
|
-- name: ConfirmUserEmail :one
|
|
UPDATE users
|
|
SET
|
|
password_hash = sqlc.arg(password_hash),
|
|
scenario = sqlc.arg(scenario),
|
|
onboarding_step = sqlc.arg(onboarding_step),
|
|
email_verified_at = sqlc.arg(verified_at),
|
|
updated_at = sqlc.arg(verified_at),
|
|
version = version + 1
|
|
WHERE
|
|
id = sqlc.arg(id)
|
|
AND registration_version = sqlc.arg(registration_version)
|
|
AND email_verified_at IS NULL
|
|
RETURNING
|
|
id,
|
|
email,
|
|
email_normalized,
|
|
password_hash,
|
|
scenario,
|
|
onboarding_step,
|
|
email_verified_at,
|
|
registration_version,
|
|
last_registered_at,
|
|
version,
|
|
created_at,
|
|
updated_at;
|
|
|
|
-- Deletes one batch of accounts that stayed unverified since before cutoff,
|
|
-- oldest first, with their tokens, sessions, mail requests, mail deliveries
|
|
-- (and through the deliveries their attempts), and consent records. The
|
|
-- consent records reference the account with ON DELETE RESTRICT, so they are
|
|
-- deleted explicitly in the same statement: a registration never confirmed
|
|
-- by its email owner is not kept as consent evidence. 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, a delivery
|
|
-- insert, or a consent insert, is skipped, and so is an account with any
|
|
-- token, session, mail request, delivery, or consent 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
|
|
WITH candidate AS (
|
|
SELECT stale.id
|
|
FROM users AS stale
|
|
WHERE
|
|
stale.email_verified_at IS NULL
|
|
AND stale.last_registered_at < sqlc.arg(cutoff)
|
|
ORDER BY stale.last_registered_at
|
|
LIMIT sqlc.arg(batch_limit)
|
|
FOR UPDATE SKIP LOCKED
|
|
),
|
|
|
|
locked_token AS (
|
|
SELECT token.user_id
|
|
FROM email_tokens AS token
|
|
WHERE token.user_id IN (SELECT candidate.id FROM candidate)
|
|
FOR UPDATE SKIP LOCKED
|
|
),
|
|
|
|
locked_session AS (
|
|
SELECT session.user_id
|
|
FROM sessions AS session
|
|
WHERE session.user_id IN (SELECT candidate.id FROM candidate)
|
|
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
|
|
),
|
|
|
|
locked_consent AS (
|
|
SELECT consent.subject_id
|
|
FROM consent_records AS consent
|
|
WHERE consent.subject_id IN (SELECT candidate.id FROM candidate)
|
|
FOR UPDATE SKIP LOCKED
|
|
),
|
|
|
|
-- An account qualifies only when every token, session, mail request,
|
|
-- delivery, and consent row visible to the statement was locked above.
|
|
deletable AS (
|
|
SELECT candidate.id
|
|
FROM candidate
|
|
WHERE
|
|
(
|
|
SELECT count(*) FROM email_tokens AS token
|
|
WHERE token.user_id = candidate.id
|
|
) = (
|
|
SELECT count(*) FROM locked_token
|
|
WHERE locked_token.user_id = candidate.id
|
|
)
|
|
AND (
|
|
SELECT count(*) FROM sessions AS session
|
|
WHERE session.user_id = candidate.id
|
|
) = (
|
|
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
|
|
)
|
|
AND (
|
|
SELECT count(*) FROM consent_records AS consent
|
|
WHERE consent.subject_id = candidate.id
|
|
) = (
|
|
SELECT count(*) FROM locked_consent
|
|
WHERE locked_consent.subject_id = candidate.id
|
|
)
|
|
),
|
|
|
|
-- The restricting foreign key is checked at the end of the statement, after
|
|
-- these rows are gone.
|
|
deleted_consent AS (
|
|
DELETE FROM consent_records
|
|
USING deletable
|
|
WHERE consent_records.subject_id = deletable.id
|
|
)
|
|
|
|
DELETE FROM users
|
|
USING deletable
|
|
WHERE users.id = deletable.id;
|
|
|
|
-- name: InsertEmailToken :exec
|
|
INSERT INTO email_tokens (
|
|
id,
|
|
user_id,
|
|
purpose,
|
|
token_hash,
|
|
registration_version,
|
|
created_at,
|
|
expires_at,
|
|
pending_password_hash,
|
|
pending_scenario,
|
|
pending_onboarding_step
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(purpose),
|
|
sqlc.arg(token_hash),
|
|
sqlc.arg(registration_version),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(expires_at),
|
|
sqlc.narg(pending_password_hash),
|
|
sqlc.narg(pending_scenario),
|
|
sqlc.narg(pending_onboarding_step)
|
|
);
|
|
|
|
-- The pending credentials of the newest token of a registration, for a
|
|
-- resend, which issues its token with the same credentials.
|
|
-- name: GetLatestPendingRegistration :one
|
|
SELECT
|
|
pending_password_hash,
|
|
pending_scenario,
|
|
pending_onboarding_step
|
|
FROM email_tokens
|
|
WHERE
|
|
user_id = sqlc.arg(user_id)
|
|
AND purpose = sqlc.arg(purpose)
|
|
AND registration_version = sqlc.arg(registration_version)
|
|
AND pending_password_hash IS NOT NULL
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT 1;
|
|
|
|
-- Reads a token before password verification; Consume rechecks it in the
|
|
-- confirmation transaction and clears pending credentials when it succeeds.
|
|
-- name: GetEmailTokenByHash :one
|
|
SELECT
|
|
id,
|
|
user_id,
|
|
purpose,
|
|
token_hash,
|
|
registration_version,
|
|
created_at,
|
|
expires_at,
|
|
consumed_at,
|
|
invalidated_at,
|
|
pending_password_hash,
|
|
pending_scenario,
|
|
pending_onboarding_step
|
|
FROM email_tokens
|
|
WHERE token_hash = sqlc.arg(token_hash)
|
|
AND purpose = sqlc.arg(purpose);
|
|
|
|
-- Starts a new registration of an unverified account without touching its
|
|
-- credentials, scenario, or onboarding step: the new registration carries
|
|
-- them on its verification token. Advances the registration version, which
|
|
-- ends every earlier token, and deletes the registration C1 of the earlier
|
|
-- registrant like UpdateUserRegistration does.
|
|
-- name: AdvanceUserRegistration :one
|
|
WITH updated AS (
|
|
UPDATE users
|
|
SET
|
|
registration_version = registration_version + 1,
|
|
last_registered_at = sqlc.arg(registered_at),
|
|
updated_at = sqlc.arg(registered_at),
|
|
version = version + 1
|
|
WHERE
|
|
users.id = sqlc.arg(id)
|
|
AND users.version = sqlc.arg(expected_version)
|
|
AND users.email_verified_at IS NULL
|
|
RETURNING
|
|
users.id,
|
|
users.email,
|
|
users.email_normalized,
|
|
users.password_hash,
|
|
users.scenario,
|
|
users.onboarding_step,
|
|
users.email_verified_at,
|
|
users.registration_version,
|
|
users.last_registered_at,
|
|
users.version,
|
|
users.created_at,
|
|
users.updated_at
|
|
),
|
|
|
|
discarded_consent AS (
|
|
DELETE FROM consent_records
|
|
USING updated
|
|
WHERE
|
|
consent_records.subject_id = updated.id
|
|
AND consent_records.purpose = 'c1'
|
|
AND consent_records.source = 'registration'
|
|
)
|
|
|
|
SELECT
|
|
updated.id,
|
|
updated.email,
|
|
updated.email_normalized,
|
|
updated.password_hash,
|
|
updated.scenario,
|
|
updated.onboarding_step,
|
|
updated.email_verified_at,
|
|
updated.registration_version,
|
|
updated.last_registered_at,
|
|
updated.version,
|
|
updated.created_at,
|
|
updated.updated_at
|
|
FROM updated;
|
|
|
|
-- name: InvalidateActiveEmailTokens :execrows
|
|
UPDATE email_tokens
|
|
SET
|
|
invalidated_at = sqlc.arg(invalidated_at),
|
|
-- An invalidated token keeps no password hash: it can never be confirmed.
|
|
pending_password_hash = NULL,
|
|
pending_scenario = NULL,
|
|
pending_onboarding_step = NULL
|
|
WHERE
|
|
user_id = sqlc.arg(user_id)
|
|
AND purpose = sqlc.arg(purpose)
|
|
AND consumed_at IS NULL
|
|
AND invalidated_at IS NULL;
|
|
|
|
-- Consumes a token in one statement. Concurrent callers queue on the token
|
|
-- row lock; under READ COMMITTED each waiter re-checks the WHERE clause
|
|
-- against the committed row, so exactly one of them updates it. The join
|
|
-- compares the registration version with the account row as the statement
|
|
-- reads it, without locking that row, so it rejects a token from an earlier
|
|
-- registration that already committed but not one that commits afterwards.
|
|
-- The confirmation guarantee therefore needs MarkUserEmailVerified with the
|
|
-- returned registration version in the same transaction, rolled back when it
|
|
-- matches no row.
|
|
-- name: ConsumeEmailToken :one
|
|
WITH candidate AS MATERIALIZED (
|
|
SELECT
|
|
token.id,
|
|
token.user_id,
|
|
token.purpose,
|
|
token.token_hash,
|
|
token.registration_version,
|
|
token.created_at,
|
|
token.expires_at,
|
|
token.pending_password_hash,
|
|
token.pending_scenario,
|
|
token.pending_onboarding_step
|
|
FROM email_tokens AS token
|
|
INNER JOIN users AS owner ON owner.id = token.user_id
|
|
WHERE
|
|
token.token_hash = sqlc.arg(token_hash)
|
|
AND token.purpose = sqlc.arg(purpose)
|
|
AND token.consumed_at IS NULL
|
|
AND token.invalidated_at IS NULL
|
|
AND token.expires_at > sqlc.arg(consumed_at)
|
|
AND owner.registration_version = token.registration_version
|
|
FOR UPDATE OF token
|
|
),
|
|
|
|
consumed AS (
|
|
UPDATE email_tokens AS token
|
|
SET
|
|
consumed_at = sqlc.arg(consumed_at),
|
|
pending_password_hash = NULL,
|
|
pending_scenario = NULL,
|
|
pending_onboarding_step = NULL
|
|
FROM candidate
|
|
WHERE token.id = candidate.id
|
|
RETURNING token.id, token.consumed_at, token.invalidated_at
|
|
)
|
|
|
|
SELECT
|
|
candidate.id,
|
|
candidate.user_id,
|
|
candidate.purpose,
|
|
candidate.token_hash,
|
|
candidate.registration_version,
|
|
candidate.created_at,
|
|
candidate.expires_at,
|
|
consumed.consumed_at,
|
|
consumed.invalidated_at,
|
|
candidate.pending_password_hash,
|
|
candidate.pending_scenario,
|
|
candidate.pending_onboarding_step
|
|
FROM candidate
|
|
INNER JOIN consumed ON consumed.id = candidate.id;
|
|
|
|
-- Explains why ConsumeEmailToken matched no row.
|
|
-- name: GetEmailTokenState :one
|
|
SELECT
|
|
token.consumed_at,
|
|
token.invalidated_at,
|
|
token.expires_at,
|
|
owner.registration_version = token.registration_version AS current_registration
|
|
FROM email_tokens AS token
|
|
INNER JOIN users AS owner ON token.user_id = owner.id
|
|
WHERE
|
|
token.token_hash = sqlc.arg(token_hash)
|
|
AND token.purpose = sqlc.arg(purpose);
|
|
|
|
-- name: InsertSession :exec
|
|
INSERT INTO sessions (
|
|
id,
|
|
user_id,
|
|
token_hash,
|
|
created_at,
|
|
last_seen_at,
|
|
expires_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(user_id),
|
|
sqlc.arg(token_hash),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(last_seen_at),
|
|
sqlc.arg(expires_at)
|
|
);
|
|
|
|
-- name: GetActiveSessionByTokenHash :one
|
|
SELECT
|
|
id,
|
|
user_id,
|
|
token_hash,
|
|
created_at,
|
|
last_seen_at,
|
|
expires_at,
|
|
revoked_at
|
|
FROM sessions
|
|
WHERE
|
|
token_hash = sqlc.arg(token_hash)
|
|
AND revoked_at IS NULL
|
|
AND expires_at > sqlc.arg(at)
|
|
AND last_seen_at > sqlc.arg(idle_cutoff);
|
|
|
|
-- Records activity on an active session and reports whether it is active.
|
|
-- The row is written only when last_seen_at is older than touch_cutoff, so
|
|
-- frequent requests do not rewrite it. last_seen_at never moves back, and a
|
|
-- revoked, expired, or idle session is not revived; the update re-checks
|
|
-- those conditions against a concurrent change.
|
|
-- name: TouchSession :one
|
|
WITH active_session AS (
|
|
SELECT candidate.id
|
|
FROM sessions AS candidate
|
|
WHERE
|
|
candidate.id = sqlc.arg(id)
|
|
AND candidate.revoked_at IS NULL
|
|
AND candidate.expires_at > sqlc.arg(at)
|
|
AND candidate.last_seen_at > sqlc.arg(idle_cutoff)
|
|
),
|
|
|
|
touched AS (
|
|
UPDATE sessions
|
|
SET last_seen_at = sqlc.arg(at)
|
|
WHERE
|
|
sessions.id = sqlc.arg(id)
|
|
AND sessions.revoked_at IS NULL
|
|
AND sessions.expires_at > sqlc.arg(at)
|
|
AND sessions.last_seen_at > sqlc.arg(idle_cutoff)
|
|
AND sessions.last_seen_at < sqlc.arg(touch_cutoff)
|
|
RETURNING sessions.id
|
|
)
|
|
|
|
SELECT
|
|
exists(SELECT 1 FROM active_session) AS active,
|
|
exists(SELECT 1 FROM touched) AS touched;
|
|
|
|
-- name: RevokeSession :execrows
|
|
UPDATE sessions
|
|
SET revoked_at = sqlc.arg(revoked_at)
|
|
WHERE
|
|
id = sqlc.arg(id)
|
|
AND revoked_at IS NULL;
|
|
|
|
-- name: RevokeUserSessions :execrows
|
|
UPDATE sessions
|
|
SET revoked_at = sqlc.arg(revoked_at)
|
|
WHERE
|
|
user_id = sqlc.arg(user_id)
|
|
AND revoked_at IS NULL;
|
|
|
|
-- Stores the replay record of a command. A record whose retention has passed
|
|
-- is replaced, so the key is accepted as new after the retention. A live
|
|
-- record is left as it is and nothing is returned; the caller then reads it
|
|
-- with GetLiveIdempotencyRecord. Concurrent callers with the same scope wait
|
|
-- on the row of the first one until its transaction ends.
|
|
-- name: ReserveIdempotencyRecord :one
|
|
INSERT INTO idempotency_records (
|
|
scope_hash,
|
|
fingerprint,
|
|
created_at,
|
|
expires_at
|
|
) VALUES (
|
|
sqlc.arg(scope_hash),
|
|
sqlc.arg(fingerprint),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(expires_at)
|
|
)
|
|
ON CONFLICT (scope_hash) DO UPDATE
|
|
SET
|
|
fingerprint = excluded.fingerprint,
|
|
created_at = excluded.created_at,
|
|
expires_at = excluded.expires_at
|
|
WHERE idempotency_records.expires_at <= excluded.created_at
|
|
RETURNING scope_hash, fingerprint, created_at, expires_at;
|
|
|
|
-- name: GetLiveIdempotencyRecord :one
|
|
SELECT
|
|
scope_hash,
|
|
fingerprint,
|
|
created_at,
|
|
expires_at
|
|
FROM idempotency_records
|
|
WHERE
|
|
scope_hash = sqlc.arg(scope_hash)
|
|
AND expires_at > sqlc.arg(at);
|
|
|
|
-- Deletes at most batch_limit expired records. Locked rows are skipped, so
|
|
-- the statement never waits for a concurrent reservation.
|
|
-- name: PurgeExpiredIdempotencyRecords :execrows
|
|
DELETE FROM idempotency_records
|
|
WHERE scope_hash IN (
|
|
SELECT stale.scope_hash
|
|
FROM idempotency_records AS stale
|
|
WHERE stale.expires_at <= sqlc.arg(at)
|
|
ORDER BY stale.expires_at
|
|
LIMIT sqlc.arg(batch_limit)
|
|
FOR UPDATE SKIP LOCKED
|
|
);
|