447 lines
9.5 KiB
Go
447 lines
9.5 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: delivery.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const deleteMailRequest = `-- name: DeleteMailRequest :execrows
|
|
DELETE FROM mail_requests
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteMailRequest(ctx context.Context, id pgtype.UUID) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteMailRequest, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const getDeliveryByID = `-- 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 = $1
|
|
`
|
|
|
|
func (q *Queries) GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error) {
|
|
row := q.db.QueryRow(ctx, getDeliveryByID, id)
|
|
var i Delivery
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.SourceEventID,
|
|
&i.RecipientUserID,
|
|
&i.RecipientEmail,
|
|
&i.Kind,
|
|
&i.TemplateVersion,
|
|
&i.Subject,
|
|
&i.TextBody,
|
|
&i.HtmlBody,
|
|
&i.ContentSha256,
|
|
&i.Provider,
|
|
&i.ProviderAccount,
|
|
&i.IdempotencyKey,
|
|
&i.Status,
|
|
&i.AttemptCount,
|
|
&i.NextAttemptAt,
|
|
&i.LastAttemptAt,
|
|
&i.ProviderMessageID,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertDelivery = `-- 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 (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11,
|
|
$12,
|
|
$13,
|
|
$14,
|
|
$15,
|
|
$16,
|
|
$17,
|
|
$18,
|
|
$19,
|
|
$20,
|
|
$21
|
|
)
|
|
ON CONFLICT (id) DO NOTHING
|
|
`
|
|
|
|
type InsertDeliveryParams struct {
|
|
ID pgtype.UUID
|
|
SourceEventID pgtype.UUID
|
|
RecipientUserID pgtype.UUID
|
|
RecipientEmail string
|
|
Kind string
|
|
TemplateVersion string
|
|
Subject string
|
|
TextBody string
|
|
HtmlBody string
|
|
ContentSha256 []byte
|
|
Provider string
|
|
ProviderAccount string
|
|
IdempotencyKey string
|
|
Status string
|
|
AttemptCount int32
|
|
NextAttemptAt pgtype.Timestamptz
|
|
LastAttemptAt pgtype.Timestamptz
|
|
ProviderMessageID pgtype.Text
|
|
Version int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) InsertDelivery(ctx context.Context, arg InsertDeliveryParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertDelivery,
|
|
arg.ID,
|
|
arg.SourceEventID,
|
|
arg.RecipientUserID,
|
|
arg.RecipientEmail,
|
|
arg.Kind,
|
|
arg.TemplateVersion,
|
|
arg.Subject,
|
|
arg.TextBody,
|
|
arg.HtmlBody,
|
|
arg.ContentSha256,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.IdempotencyKey,
|
|
arg.Status,
|
|
arg.AttemptCount,
|
|
arg.NextAttemptAt,
|
|
arg.LastAttemptAt,
|
|
arg.ProviderMessageID,
|
|
arg.Version,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertMailRequest = `-- name: InsertMailRequest :exec
|
|
INSERT INTO mail_requests (
|
|
id,
|
|
recipient_user_id,
|
|
kind,
|
|
subject,
|
|
body,
|
|
link_path,
|
|
created_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
`
|
|
|
|
type InsertMailRequestParams struct {
|
|
ID pgtype.UUID
|
|
RecipientUserID pgtype.UUID
|
|
Kind string
|
|
Subject string
|
|
Body string
|
|
LinkPath string
|
|
CreatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertMailRequest(ctx context.Context, arg InsertMailRequestParams) error {
|
|
_, err := q.db.Exec(ctx, insertMailRequest,
|
|
arg.ID,
|
|
arg.RecipientUserID,
|
|
arg.Kind,
|
|
arg.Subject,
|
|
arg.Body,
|
|
arg.LinkPath,
|
|
arg.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listDeliveryAttempts = `-- 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 = $1
|
|
ORDER BY attempt_number
|
|
`
|
|
|
|
func (q *Queries) ListDeliveryAttempts(ctx context.Context, deliveryID pgtype.UUID) ([]DeliveryAttempt, error) {
|
|
rows, err := q.db.Query(ctx, listDeliveryAttempts, deliveryID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []DeliveryAttempt
|
|
for rows.Next() {
|
|
var i DeliveryAttempt
|
|
if err := rows.Scan(
|
|
&i.DeliveryID,
|
|
&i.AttemptNumber,
|
|
&i.AttemptedAt,
|
|
&i.Outcome,
|
|
&i.Rejection,
|
|
&i.ProviderCode,
|
|
&i.ProviderMessageID,
|
|
&i.RetryAfterNs,
|
|
&i.StatusAfter,
|
|
&i.NextAttemptAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockMailRequest = `-- name: LockMailRequest :one
|
|
SELECT
|
|
id,
|
|
recipient_user_id,
|
|
kind,
|
|
subject,
|
|
body,
|
|
link_path,
|
|
created_at
|
|
FROM mail_requests
|
|
WHERE id = $1
|
|
FOR UPDATE
|
|
`
|
|
|
|
// 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.
|
|
func (q *Queries) LockMailRequest(ctx context.Context, id pgtype.UUID) (MailRequest, error) {
|
|
row := q.db.QueryRow(ctx, lockMailRequest, id)
|
|
var i MailRequest
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.RecipientUserID,
|
|
&i.Kind,
|
|
&i.Subject,
|
|
&i.Body,
|
|
&i.LinkPath,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const recordDeliveryAttempt = `-- name: RecordDeliveryAttempt :execrows
|
|
WITH advanced AS (
|
|
UPDATE deliveries
|
|
SET
|
|
status = $8,
|
|
attempt_count = $1,
|
|
next_attempt_at = $9,
|
|
last_attempt_at = $2,
|
|
provider_message_id = coalesce(
|
|
$6, deliveries.provider_message_id
|
|
),
|
|
version = deliveries.version + 1,
|
|
updated_at = $10
|
|
WHERE
|
|
deliveries.id = $11
|
|
AND deliveries.version = $12
|
|
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,
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9
|
|
FROM advanced
|
|
`
|
|
|
|
type RecordDeliveryAttemptParams struct {
|
|
AttemptNumber int32
|
|
AttemptedAt pgtype.Timestamptz
|
|
Outcome string
|
|
Rejection pgtype.Text
|
|
ProviderCode pgtype.Text
|
|
ProviderMessageID pgtype.Text
|
|
RetryAfterNs int64
|
|
Status string
|
|
NextAttemptAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
DeliveryID pgtype.UUID
|
|
ExpectedVersion int64
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) RecordDeliveryAttempt(ctx context.Context, arg RecordDeliveryAttemptParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, recordDeliveryAttempt,
|
|
arg.AttemptNumber,
|
|
arg.AttemptedAt,
|
|
arg.Outcome,
|
|
arg.Rejection,
|
|
arg.ProviderCode,
|
|
arg.ProviderMessageID,
|
|
arg.RetryAfterNs,
|
|
arg.Status,
|
|
arg.NextAttemptAt,
|
|
arg.UpdatedAt,
|
|
arg.DeliveryID,
|
|
arg.ExpectedVersion,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const updateDeliveryState = `-- name: UpdateDeliveryState :execrows
|
|
UPDATE deliveries
|
|
SET
|
|
status = $1,
|
|
next_attempt_at = $2,
|
|
provider = $3,
|
|
provider_account = $4,
|
|
version = deliveries.version + 1,
|
|
updated_at = $5
|
|
WHERE
|
|
deliveries.id = $6
|
|
AND deliveries.version = $7
|
|
`
|
|
|
|
type UpdateDeliveryStateParams struct {
|
|
Status string
|
|
NextAttemptAt pgtype.Timestamptz
|
|
Provider string
|
|
ProviderAccount string
|
|
UpdatedAt pgtype.Timestamptz
|
|
ID pgtype.UUID
|
|
ExpectedVersion int64
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) UpdateDeliveryState(ctx context.Context, arg UpdateDeliveryStateParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateDeliveryState,
|
|
arg.Status,
|
|
arg.NextAttemptAt,
|
|
arg.Provider,
|
|
arg.ProviderAccount,
|
|
arg.UpdatedAt,
|
|
arg.ID,
|
|
arg.ExpectedVersion,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|