Files
lincodex/backend/internal/adapter/secondary/postgres/sqlc/identity.sql.go
T

690 lines
16 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: identity.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const consumeEmailToken = `-- name: ConsumeEmailToken :one
UPDATE email_tokens AS token
SET consumed_at = $1
FROM users AS owner
WHERE
token.token_hash = $2
AND token.purpose = $3
AND token.consumed_at IS NULL
AND token.invalidated_at IS NULL
AND token.expires_at > $1
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
`
type ConsumeEmailTokenParams struct {
ConsumedAt pgtype.Timestamptz
TokenHash []byte
Purpose string
}
// 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.
func (q *Queries) ConsumeEmailToken(ctx context.Context, arg ConsumeEmailTokenParams) (EmailToken, error) {
row := q.db.QueryRow(ctx, consumeEmailToken, arg.ConsumedAt, arg.TokenHash, arg.Purpose)
var i EmailToken
err := row.Scan(
&i.ID,
&i.UserID,
&i.Purpose,
&i.TokenHash,
&i.RegistrationVersion,
&i.CreatedAt,
&i.ExpiresAt,
&i.ConsumedAt,
&i.InvalidatedAt,
)
return i, err
}
const deleteUnverifiedUsersRegisteredBefore = `-- 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 < $1
ORDER BY stale.last_registered_at
LIMIT $2
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
),
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
)
)
DELETE FROM users
USING deletable
WHERE users.id = deletable.id
`
type DeleteUnverifiedUsersRegisteredBeforeParams struct {
Cutoff pgtype.Timestamptz
BatchLimit int32
}
// 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.
// 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.
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 {
return 0, err
}
return result.RowsAffected(), nil
}
const getActiveSessionByTokenHash = `-- name: GetActiveSessionByTokenHash :one
SELECT
id,
user_id,
token_hash,
created_at,
last_seen_at,
expires_at,
revoked_at
FROM sessions
WHERE
token_hash = $1
AND revoked_at IS NULL
AND expires_at > $2
AND last_seen_at > $3
`
type GetActiveSessionByTokenHashParams struct {
TokenHash []byte
At pgtype.Timestamptz
IdleCutoff pgtype.Timestamptz
}
func (q *Queries) GetActiveSessionByTokenHash(ctx context.Context, arg GetActiveSessionByTokenHashParams) (Session, error) {
row := q.db.QueryRow(ctx, getActiveSessionByTokenHash, arg.TokenHash, arg.At, arg.IdleCutoff)
var i Session
err := row.Scan(
&i.ID,
&i.UserID,
&i.TokenHash,
&i.CreatedAt,
&i.LastSeenAt,
&i.ExpiresAt,
&i.RevokedAt,
)
return i, err
}
const getEmailTokenState = `-- 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 = $1
AND token.purpose = $2
`
type GetEmailTokenStateParams struct {
TokenHash []byte
Purpose string
}
type GetEmailTokenStateRow struct {
ConsumedAt pgtype.Timestamptz
InvalidatedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
CurrentRegistration bool
}
// Explains why ConsumeEmailToken matched no row.
func (q *Queries) GetEmailTokenState(ctx context.Context, arg GetEmailTokenStateParams) (GetEmailTokenStateRow, error) {
row := q.db.QueryRow(ctx, getEmailTokenState, arg.TokenHash, arg.Purpose)
var i GetEmailTokenStateRow
err := row.Scan(
&i.ConsumedAt,
&i.InvalidatedAt,
&i.ExpiresAt,
&i.CurrentRegistration,
)
return i, err
}
const getUserByEmailNormalized = `-- 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 = $1
`
func (q *Queries) GetUserByEmailNormalized(ctx context.Context, emailNormalized string) (User, error) {
row := q.db.QueryRow(ctx, getUserByEmailNormalized, emailNormalized)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.EmailNormalized,
&i.PasswordHash,
&i.Scenario,
&i.OnboardingStep,
&i.EmailVerifiedAt,
&i.RegistrationVersion,
&i.LastRegisteredAt,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getUserByID = `-- 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 = $1
`
func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (User, error) {
row := q.db.QueryRow(ctx, getUserByID, id)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.EmailNormalized,
&i.PasswordHash,
&i.Scenario,
&i.OnboardingStep,
&i.EmailVerifiedAt,
&i.RegistrationVersion,
&i.LastRegisteredAt,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertEmailToken = `-- name: InsertEmailToken :exec
INSERT INTO email_tokens (
id,
user_id,
purpose,
token_hash,
registration_version,
created_at,
expires_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7
)
`
type InsertEmailTokenParams struct {
ID pgtype.UUID
UserID pgtype.UUID
Purpose string
TokenHash []byte
RegistrationVersion int64
CreatedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
}
func (q *Queries) InsertEmailToken(ctx context.Context, arg InsertEmailTokenParams) error {
_, err := q.db.Exec(ctx, insertEmailToken,
arg.ID,
arg.UserID,
arg.Purpose,
arg.TokenHash,
arg.RegistrationVersion,
arg.CreatedAt,
arg.ExpiresAt,
)
return err
}
const insertSession = `-- name: InsertSession :exec
INSERT INTO sessions (
id,
user_id,
token_hash,
created_at,
last_seen_at,
expires_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6
)
`
type InsertSessionParams struct {
ID pgtype.UUID
UserID pgtype.UUID
TokenHash []byte
CreatedAt pgtype.Timestamptz
LastSeenAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz
}
func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) error {
_, err := q.db.Exec(ctx, insertSession,
arg.ID,
arg.UserID,
arg.TokenHash,
arg.CreatedAt,
arg.LastSeenAt,
arg.ExpiresAt,
)
return err
}
const insertUser = `-- 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 (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12
)
`
type InsertUserParams struct {
ID pgtype.UUID
Email string
EmailNormalized string
PasswordHash string
Scenario string
OnboardingStep string
EmailVerifiedAt pgtype.Timestamptz
RegistrationVersion int64
LastRegisteredAt pgtype.Timestamptz
Version int64
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
}
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) error {
_, err := q.db.Exec(ctx, insertUser,
arg.ID,
arg.Email,
arg.EmailNormalized,
arg.PasswordHash,
arg.Scenario,
arg.OnboardingStep,
arg.EmailVerifiedAt,
arg.RegistrationVersion,
arg.LastRegisteredAt,
arg.Version,
arg.CreatedAt,
arg.UpdatedAt,
)
return err
}
const invalidateActiveEmailTokens = `-- name: InvalidateActiveEmailTokens :execrows
UPDATE email_tokens
SET invalidated_at = $1
WHERE
user_id = $2
AND purpose = $3
AND consumed_at IS NULL
AND invalidated_at IS NULL
`
type InvalidateActiveEmailTokensParams struct {
InvalidatedAt pgtype.Timestamptz
UserID pgtype.UUID
Purpose string
}
func (q *Queries) InvalidateActiveEmailTokens(ctx context.Context, arg InvalidateActiveEmailTokensParams) (int64, error) {
result, err := q.db.Exec(ctx, invalidateActiveEmailTokens, arg.InvalidatedAt, arg.UserID, arg.Purpose)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const markUserEmailVerified = `-- name: MarkUserEmailVerified :one
UPDATE users
SET
email_verified_at = $1,
updated_at = $1,
version = version + 1
WHERE
id = $2
AND registration_version = $3
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
`
type MarkUserEmailVerifiedParams struct {
VerifiedAt pgtype.Timestamptz
ID pgtype.UUID
RegistrationVersion int64
}
// Verifies the email only for the registration version the consumed token
// was issued for.
func (q *Queries) MarkUserEmailVerified(ctx context.Context, arg MarkUserEmailVerifiedParams) (User, error) {
row := q.db.QueryRow(ctx, markUserEmailVerified, arg.VerifiedAt, arg.ID, arg.RegistrationVersion)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.EmailNormalized,
&i.PasswordHash,
&i.Scenario,
&i.OnboardingStep,
&i.EmailVerifiedAt,
&i.RegistrationVersion,
&i.LastRegisteredAt,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const revokeSession = `-- name: RevokeSession :execrows
UPDATE sessions
SET revoked_at = $1
WHERE
id = $2
AND revoked_at IS NULL
`
type RevokeSessionParams struct {
RevokedAt pgtype.Timestamptz
ID pgtype.UUID
}
func (q *Queries) RevokeSession(ctx context.Context, arg RevokeSessionParams) (int64, error) {
result, err := q.db.Exec(ctx, revokeSession, arg.RevokedAt, arg.ID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const revokeUserSessions = `-- name: RevokeUserSessions :execrows
UPDATE sessions
SET revoked_at = $1
WHERE
user_id = $2
AND revoked_at IS NULL
`
type RevokeUserSessionsParams struct {
RevokedAt pgtype.Timestamptz
UserID pgtype.UUID
}
func (q *Queries) RevokeUserSessions(ctx context.Context, arg RevokeUserSessionsParams) (int64, error) {
result, err := q.db.Exec(ctx, revokeUserSessions, arg.RevokedAt, arg.UserID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const touchSession = `-- name: TouchSession :one
WITH active_session AS (
SELECT candidate.id
FROM sessions AS candidate
WHERE
candidate.id = $1
AND candidate.revoked_at IS NULL
AND candidate.expires_at > $2
AND candidate.last_seen_at > $3
),
touched AS (
UPDATE sessions
SET last_seen_at = $2
WHERE
sessions.id = $1
AND sessions.revoked_at IS NULL
AND sessions.expires_at > $2
AND sessions.last_seen_at > $3
AND sessions.last_seen_at < $4
RETURNING sessions.id
)
SELECT
exists(SELECT 1 FROM active_session) AS active,
exists(SELECT 1 FROM touched) AS touched
`
type TouchSessionParams struct {
ID pgtype.UUID
At pgtype.Timestamptz
IdleCutoff pgtype.Timestamptz
TouchCutoff pgtype.Timestamptz
}
type TouchSessionRow struct {
Active bool
Touched bool
}
// 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.
func (q *Queries) TouchSession(ctx context.Context, arg TouchSessionParams) (TouchSessionRow, error) {
row := q.db.QueryRow(ctx, touchSession,
arg.ID,
arg.At,
arg.IdleCutoff,
arg.TouchCutoff,
)
var i TouchSessionRow
err := row.Scan(&i.Active, &i.Touched)
return i, err
}
const updateUserRegistration = `-- name: UpdateUserRegistration :one
UPDATE users
SET
email = $1,
password_hash = $2,
scenario = $3,
onboarding_step = $4,
registration_version = registration_version + 1,
last_registered_at = $5,
updated_at = $6,
version = version + 1
WHERE
id = $7
AND version = $8
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
`
type UpdateUserRegistrationParams struct {
Email string
PasswordHash string
Scenario string
OnboardingStep string
LastRegisteredAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
ID pgtype.UUID
ExpectedVersion int64
}
// 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.
func (q *Queries) UpdateUserRegistration(ctx context.Context, arg UpdateUserRegistrationParams) (User, error) {
row := q.db.QueryRow(ctx, updateUserRegistration,
arg.Email,
arg.PasswordHash,
arg.Scenario,
arg.OnboardingStep,
arg.LastRegisteredAt,
arg.UpdatedAt,
arg.ID,
arg.ExpectedVersion,
)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.EmailNormalized,
&i.PasswordHash,
&i.Scenario,
&i.OnboardingStep,
&i.EmailVerifiedAt,
&i.RegistrationVersion,
&i.LastRegisteredAt,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}