365 lines
10 KiB
SQL
365 lines
10 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.
|
|
-- name: UpdateUserRegistration :one
|
|
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
|
|
id = sqlc.arg(id)
|
|
AND version = sqlc.arg(expected_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;
|
|
|
|
-- 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;
|
|
|
|
-- Deletes one batch of accounts that stayed unverified since before cutoff,
|
|
-- 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
|
|
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
|
|
),
|
|
|
|
-- 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
|
|
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
|
|
)
|
|
)
|
|
|
|
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
|
|
) 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)
|
|
);
|
|
|
|
-- name: InvalidateActiveEmailTokens :execrows
|
|
UPDATE email_tokens
|
|
SET invalidated_at = sqlc.arg(invalidated_at)
|
|
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
|
|
UPDATE email_tokens AS token
|
|
SET consumed_at = sqlc.arg(consumed_at)
|
|
FROM users AS owner
|
|
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.id = token.user_id
|
|
AND owner.registration_version = token.registration_version
|
|
RETURNING
|
|
token.id,
|
|
token.user_id,
|
|
token.purpose,
|
|
token.token_hash,
|
|
token.registration_version,
|
|
token.created_at,
|
|
token.expires_at,
|
|
token.consumed_at,
|
|
token.invalidated_at;
|
|
|
|
-- 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;
|