forked from mtusi2026/mtusi2026lab5
ЛР 5 — асинхронная обработка и версии pipeline
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachment_variants.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createAttachmentVariant = `-- name: CreateAttachmentVariant :exec
|
||||
INSERT INTO attachment_variants (
|
||||
attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9
|
||||
)
|
||||
`
|
||||
|
||||
type CreateAttachmentVariantParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
Name string
|
||||
ObjectKey string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Width int32
|
||||
Height int32
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachmentVariant(ctx context.Context, arg CreateAttachmentVariantParams) error {
|
||||
_, err := q.db.Exec(ctx, createAttachmentVariant,
|
||||
arg.AttachmentID,
|
||||
arg.PipelineVersion,
|
||||
arg.Name,
|
||||
arg.ObjectKey,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Width,
|
||||
arg.Height,
|
||||
arg.Sha256,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listAttachmentVariants = `-- name: ListAttachmentVariants :many
|
||||
SELECT attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256 FROM attachment_variants
|
||||
WHERE attachment_id = $1 AND pipeline_version = $2
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
type ListAttachmentVariantsParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentVariants(ctx context.Context, arg ListAttachmentVariantsParams) ([]AttachmentVariant, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentVariants, arg.AttachmentID, arg.PipelineVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AttachmentVariant
|
||||
for rows.Next() {
|
||||
var i AttachmentVariant
|
||||
if err := rows.Scan(
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Name,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.Sha256,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachments.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const attachmentExistsByObjectKey = `-- name: AttachmentExistsByObjectKey :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM attachments
|
||||
WHERE object_key = $1::text OR staging_key = $1::text
|
||||
UNION ALL
|
||||
SELECT 1 FROM attachment_variants WHERE object_key = $1::text
|
||||
UNION ALL
|
||||
SELECT 1 FROM attachment_jobs WHERE published_original_key = $1::text
|
||||
)
|
||||
`
|
||||
|
||||
func (q *Queries) AttachmentExistsByObjectKey(ctx context.Context, objectKey string) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, attachmentExistsByObjectKey, objectKey)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const createAttachment = `-- name: CreateAttachment :one
|
||||
WITH legacy AS (
|
||||
INSERT INTO attachments
|
||||
(chat_id, owner_id, filename, object_key, staging_key, content_type, size_bytes, sha256)
|
||||
VALUES ($1, $2, $3,
|
||||
$4::text, $4::text,
|
||||
$5, $6, $7)
|
||||
RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
|
||||
), job AS (
|
||||
INSERT INTO attachment_jobs (attachment_id, pipeline_version)
|
||||
SELECT id, pipeline_version FROM legacy
|
||||
RETURNING id, attachment_id, pipeline_version, attempts_started, max_attempts, next_attempt_at, lease_generation, lease_until, created_at, updated_at, published_original_key
|
||||
), event AS (
|
||||
INSERT INTO outbox (job_id, payload, dedup_key)
|
||||
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
|
||||
'initial/' || id
|
||||
FROM job
|
||||
RETURNING job_id
|
||||
)
|
||||
SELECT legacy.id, legacy.chat_id, legacy.owner_id, legacy.filename, legacy.object_key, legacy.content_type, legacy.size_bytes, legacy.sha256, legacy.created_at, legacy.updated_at, legacy.status, legacy.error_code, legacy.staging_key, legacy.pipeline_version FROM legacy CROSS JOIN event
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
ChatID pgtype.UUID
|
||||
OwnerID pgtype.UUID
|
||||
Filename string
|
||||
ObjectKey string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
type CreateAttachmentRow struct {
|
||||
ID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
OwnerID pgtype.UUID
|
||||
Filename string
|
||||
ObjectKey pgtype.Text
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
StagingKey pgtype.Text
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
// Compatibility for the synchronous API until CreateQueuedAttachment is wired in.
|
||||
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (CreateAttachmentRow, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachment,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.Filename,
|
||||
arg.ObjectKey,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
)
|
||||
var i CreateAttachmentRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createQueuedAttachment = `-- name: CreateQueuedAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, filename, content_type, size_bytes, sha256, staging_key, status
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, 'queued'
|
||||
) RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
|
||||
`
|
||||
|
||||
type CreateQueuedAttachmentParams struct {
|
||||
ID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
OwnerID pgtype.UUID
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
StagingKey pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) CreateQueuedAttachment(ctx context.Context, arg CreateQueuedAttachmentParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, createQueuedAttachment,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.Filename,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
arg.StagingKey,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentByID = `-- name: GetAttachmentByID :one
|
||||
SELECT id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version FROM attachments WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetAttachmentByID(ctx context.Context, attachmentID pgtype.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentByID, attachmentID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentForUser = `-- name: GetAttachmentForUser :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.object_key, a.content_type, a.size_bytes, a.sha256, a.created_at, a.updated_at, a.status, a.error_code, a.staging_key, a.pipeline_version
|
||||
FROM attachments AS a
|
||||
JOIN chat_members AS cm ON cm.chat_id = a.chat_id
|
||||
WHERE a.id = $1
|
||||
AND cm.user_id = $2
|
||||
`
|
||||
|
||||
type GetAttachmentForUserParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentForUser(ctx context.Context, arg GetAttachmentForUserParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentForUser, arg.AttachmentID, arg.UserID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentsForMessages = `-- name: GetAttachmentsForMessages :many
|
||||
SELECT message_id, attachment_id, position
|
||||
FROM messages_attachments
|
||||
WHERE message_id = ANY($1::uuid[])
|
||||
ORDER BY message_id, position
|
||||
`
|
||||
|
||||
type GetAttachmentsForMessagesRow struct {
|
||||
MessageID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentsForMessages(ctx context.Context, messageIds []pgtype.UUID) ([]GetAttachmentsForMessagesRow, error) {
|
||||
rows, err := q.db.Query(ctx, getAttachmentsForMessages, messageIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetAttachmentsForMessagesRow
|
||||
for rows.Next() {
|
||||
var i GetAttachmentsForMessagesRow
|
||||
if err := rows.Scan(&i.MessageID, &i.AttachmentID, &i.Position); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listAttachmentKeys = `-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, created_at
|
||||
FROM attachments
|
||||
WHERE object_key > $1
|
||||
ORDER BY object_key
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListAttachmentKeysParams struct {
|
||||
AfterKey pgtype.Text
|
||||
PageSize int32
|
||||
}
|
||||
|
||||
type ListAttachmentKeysRow struct {
|
||||
ID pgtype.UUID
|
||||
ObjectKey pgtype.Text
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentKeys(ctx context.Context, arg ListAttachmentKeysParams) ([]ListAttachmentKeysRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentKeys, arg.AfterKey, arg.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListAttachmentKeysRow
|
||||
for rows.Next() {
|
||||
var i ListAttachmentKeysRow
|
||||
if err := rows.Scan(&i.ID, &i.ObjectKey, &i.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const tryLockAttachmentObjectKey = `-- name: TryLockAttachmentObjectKey :one
|
||||
SELECT pg_try_advisory_xact_lock(
|
||||
hashtextextended($1::text, 0)
|
||||
)::boolean AS locked
|
||||
`
|
||||
|
||||
func (q *Queries) TryLockAttachmentObjectKey(ctx context.Context, objectKey string) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, tryLockAttachmentObjectKey, objectKey)
|
||||
var locked bool
|
||||
err := row.Scan(&locked)
|
||||
return locked, err
|
||||
}
|
||||
@@ -0,0 +1,529 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachments_jobs.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const claimJob = `-- name: ClaimJob :one
|
||||
WITH claimed AS (
|
||||
UPDATE attachment_jobs AS j
|
||||
SET lease_generation = j.lease_generation + 1,
|
||||
lease_until = $1::timestamptz,
|
||||
attempts_started = j.attempts_started + 1,
|
||||
next_attempt_at = NULL,
|
||||
updated_at = clock_timestamp()
|
||||
FROM attachments AS a
|
||||
WHERE j.id = $2
|
||||
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
|
||||
AND j.attempts_started < j.max_attempts
|
||||
AND $1::timestamptz > clock_timestamp()
|
||||
AND (
|
||||
(a.status = 'queued' AND j.lease_until IS NULL AND j.next_attempt_at <= clock_timestamp())
|
||||
OR (a.status = 'processing' AND j.lease_until <= clock_timestamp())
|
||||
)
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
), expired AS (
|
||||
UPDATE attachment_attempts AS attempt
|
||||
SET outcome = 'lease_expired', error_code = 'lease_expired', finished_at = claimed.updated_at
|
||||
FROM claimed
|
||||
WHERE attempt.job_id = claimed.id
|
||||
AND attempt.lease_generation = claimed.lease_generation - 1 AND attempt.finished_at IS NULL
|
||||
), recorded AS (
|
||||
INSERT INTO attachment_attempts (job_id, lease_generation, attempt, started_at)
|
||||
SELECT id, lease_generation, attempts_started, updated_at FROM claimed
|
||||
RETURNING job_id
|
||||
), processing AS (
|
||||
UPDATE attachments AS a
|
||||
SET status = 'processing', updated_at = claimed.updated_at
|
||||
FROM claimed
|
||||
WHERE a.id = claimed.attachment_id
|
||||
RETURNING a.id
|
||||
)
|
||||
SELECT claimed.id, claimed.attachment_id, claimed.pipeline_version, claimed.attempts_started, claimed.max_attempts, claimed.next_attempt_at, claimed.lease_generation, claimed.lease_until, claimed.created_at, claimed.updated_at, claimed.published_original_key FROM claimed JOIN recorded ON recorded.job_id = claimed.id
|
||||
JOIN processing ON processing.id = claimed.attachment_id
|
||||
`
|
||||
|
||||
type ClaimJobParams struct {
|
||||
NewLeaseUntil pgtype.Timestamptz
|
||||
JobID pgtype.UUID
|
||||
}
|
||||
|
||||
type ClaimJobRow struct {
|
||||
ID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
AttemptsStarted int32
|
||||
MaxAttempts int32
|
||||
NextAttemptAt pgtype.Timestamptz
|
||||
LeaseGeneration int32
|
||||
LeaseUntil pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
PublishedOriginalKey pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) ClaimJob(ctx context.Context, arg ClaimJobParams) (ClaimJobRow, error) {
|
||||
row := q.db.QueryRow(ctx, claimJob, arg.NewLeaseUntil, arg.JobID)
|
||||
var i ClaimJobRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedOriginalKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const clearStagingReference = `-- name: ClearStagingReference :execrows
|
||||
UPDATE attachments
|
||||
SET object_key = CASE WHEN status = 'failed' AND object_key = staging_key THEN NULL ELSE object_key END,
|
||||
staging_key = NULL
|
||||
WHERE id = $1 AND staging_key = $2
|
||||
`
|
||||
|
||||
type ClearStagingReferenceParams struct {
|
||||
ID pgtype.UUID
|
||||
StagingKey pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) ClearStagingReference(ctx context.Context, arg ClearStagingReferenceParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, clearStagingReference, arg.ID, arg.StagingKey)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const completeJob = `-- name: CompleteJob :execrows
|
||||
WITH completed AS (
|
||||
UPDATE attachment_jobs AS j
|
||||
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp(),
|
||||
published_original_key = $1::text
|
||||
FROM attachments AS a
|
||||
WHERE j.id = $2
|
||||
AND j.lease_generation = $3 AND j.lease_until > clock_timestamp()
|
||||
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
|
||||
AND (SELECT count(*) FROM attachment_variants AS v
|
||||
WHERE v.attachment_id = a.id AND v.pipeline_version = j.pipeline_version) = 3
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
), recorded AS (
|
||||
UPDATE attachment_attempts AS attempt
|
||||
SET outcome = 'ready', finished_at = completed.updated_at
|
||||
FROM completed
|
||||
WHERE attempt.job_id = completed.id AND attempt.lease_generation = completed.lease_generation
|
||||
)
|
||||
UPDATE attachments AS a
|
||||
SET status = 'ready', object_key = $1::text, updated_at = completed.updated_at
|
||||
FROM completed
|
||||
WHERE a.id = completed.attachment_id
|
||||
`
|
||||
|
||||
type CompleteJobParams struct {
|
||||
ObjectKey string
|
||||
JobID pgtype.UUID
|
||||
LeaseGeneration int32
|
||||
}
|
||||
|
||||
func (q *Queries) CompleteJob(ctx context.Context, arg CompleteJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, completeJob, arg.ObjectKey, arg.JobID, arg.LeaseGeneration)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const createAttachmentJob = `-- name: CreateAttachmentJob :one
|
||||
INSERT INTO attachment_jobs (attachment_id, pipeline_version, next_attempt_at, max_attempts)
|
||||
VALUES ($1, $2,
|
||||
$3::timestamptz, $4)
|
||||
RETURNING id, attachment_id, pipeline_version, attempts_started, max_attempts, next_attempt_at, lease_generation, lease_until, created_at, updated_at, published_original_key
|
||||
`
|
||||
|
||||
type CreateAttachmentJobParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
AvailableAt pgtype.Timestamptz
|
||||
MaxAttempts int32
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachmentJob(ctx context.Context, arg CreateAttachmentJobParams) (AttachmentJob, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachmentJob,
|
||||
arg.AttachmentID,
|
||||
arg.PipelineVersion,
|
||||
arg.AvailableAt,
|
||||
arg.MaxAttempts,
|
||||
)
|
||||
var i AttachmentJob
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedOriginalKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const failJob = `-- name: FailJob :execrows
|
||||
WITH failed AS (
|
||||
UPDATE attachment_jobs AS j
|
||||
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
|
||||
FROM attachments AS a
|
||||
WHERE j.id = $2
|
||||
AND j.lease_generation = $3
|
||||
AND (
|
||||
j.lease_until > clock_timestamp()
|
||||
OR (j.lease_until <= clock_timestamp() AND j.attempts_started = j.max_attempts
|
||||
AND $1::text = 'retry_exhausted')
|
||||
)
|
||||
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
), recorded AS (
|
||||
UPDATE attachment_attempts AS attempt
|
||||
SET outcome = 'failed', error_code = $1::text, finished_at = failed.updated_at
|
||||
FROM failed
|
||||
WHERE attempt.job_id = failed.id AND attempt.lease_generation = failed.lease_generation
|
||||
)
|
||||
UPDATE attachments AS a
|
||||
SET status = 'failed', error_code = $1::text, updated_at = failed.updated_at
|
||||
FROM failed WHERE a.id = failed.attachment_id
|
||||
`
|
||||
|
||||
type FailJobParams struct {
|
||||
ErrorCode string
|
||||
JobID pgtype.UUID
|
||||
LeaseGeneration int32
|
||||
}
|
||||
|
||||
func (q *Queries) FailJob(ctx context.Context, arg FailJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, failJob, arg.ErrorCode, arg.JobID, arg.LeaseGeneration)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getAttachmentByStagingKey = `-- name: GetAttachmentByStagingKey :one
|
||||
SELECT id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version FROM attachments WHERE staging_key = $1::text
|
||||
`
|
||||
|
||||
func (q *Queries) GetAttachmentByStagingKey(ctx context.Context, stagingKey string) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentByStagingKey, stagingKey)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentJobState = `-- name: GetAttachmentJobState :one
|
||||
SELECT
|
||||
j.id, j.attachment_id, j.pipeline_version,
|
||||
CASE WHEN a.pipeline_version = j.pipeline_version THEN a.status ELSE 'superseded' END::text AS status,
|
||||
j.lease_generation,
|
||||
j.lease_until,
|
||||
coalesce(j.lease_until <= clock_timestamp(), false)::boolean AS lease_expired,
|
||||
j.attempts_started,
|
||||
j.max_attempts,
|
||||
j.next_attempt_at
|
||||
FROM attachment_jobs AS j
|
||||
JOIN attachments AS a ON a.id = j.attachment_id
|
||||
WHERE j.id = $1::uuid
|
||||
`
|
||||
|
||||
type GetAttachmentJobStateRow struct {
|
||||
ID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
Status string
|
||||
LeaseGeneration int32
|
||||
LeaseUntil pgtype.Timestamptz
|
||||
LeaseExpired bool
|
||||
AttemptsStarted int32
|
||||
MaxAttempts int32
|
||||
NextAttemptAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentJobState(ctx context.Context, jobID pgtype.UUID) (GetAttachmentJobStateRow, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentJobState, jobID)
|
||||
var i GetAttachmentJobStateRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Status,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.LeaseExpired,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listFailedAttachmentJobs = `-- name: ListFailedAttachmentJobs :many
|
||||
SELECT j.id, j.attachment_id, j.attempts_started, a.error_code, a.updated_at
|
||||
FROM attachment_jobs j
|
||||
JOIN attachments a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
|
||||
WHERE a.status = 'failed'
|
||||
ORDER BY a.updated_at DESC, j.id
|
||||
LIMIT $1
|
||||
`
|
||||
|
||||
type ListFailedAttachmentJobsRow struct {
|
||||
ID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
AttemptsStarted int32
|
||||
ErrorCode pgtype.Text
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) ListFailedAttachmentJobs(ctx context.Context, batchSize int32) ([]ListFailedAttachmentJobsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listFailedAttachmentJobs, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListFailedAttachmentJobsRow
|
||||
for rows.Next() {
|
||||
var i ListFailedAttachmentJobsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.AttemptsStarted,
|
||||
&i.ErrorCode,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockActiveAttachmentJob = `-- name: LockActiveAttachmentJob :one
|
||||
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
FROM attachment_jobs AS j JOIN attachments AS a ON a.id = j.attachment_id
|
||||
WHERE j.id = $1
|
||||
AND j.lease_generation = $2
|
||||
AND j.lease_until > clock_timestamp()
|
||||
AND a.status = 'processing' AND a.pipeline_version = j.pipeline_version
|
||||
FOR UPDATE OF j
|
||||
`
|
||||
|
||||
type LockActiveAttachmentJobParams struct {
|
||||
JobID pgtype.UUID
|
||||
LeaseGeneration int32
|
||||
}
|
||||
|
||||
// Hold this lock through variant insertion and CompleteJob. Roll back on a zero-row completion.
|
||||
func (q *Queries) LockActiveAttachmentJob(ctx context.Context, arg LockActiveAttachmentJobParams) (AttachmentJob, error) {
|
||||
row := q.db.QueryRow(ctx, lockActiveAttachmentJob, arg.JobID, arg.LeaseGeneration)
|
||||
var i AttachmentJob
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedOriginalKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockAttachmentJobForPipeline = `-- name: LockAttachmentJobForPipeline :one
|
||||
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
FROM attachment_jobs j
|
||||
WHERE j.attachment_id = $1 AND j.pipeline_version = $2
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
type LockAttachmentJobForPipelineParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) LockAttachmentJobForPipeline(ctx context.Context, arg LockAttachmentJobForPipelineParams) (AttachmentJob, error) {
|
||||
row := q.db.QueryRow(ctx, lockAttachmentJobForPipeline, arg.AttachmentID, arg.PipelineVersion)
|
||||
var i AttachmentJob
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedOriginalKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockAttachmentJobForReplay = `-- name: LockAttachmentJobForReplay :one
|
||||
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
FROM attachment_jobs j WHERE j.id = $1
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
func (q *Queries) LockAttachmentJobForReplay(ctx context.Context, id pgtype.UUID) (AttachmentJob, error) {
|
||||
row := q.db.QueryRow(ctx, lockAttachmentJobForReplay, id)
|
||||
var i AttachmentJob
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.AttemptsStarted,
|
||||
&i.MaxAttempts,
|
||||
&i.NextAttemptAt,
|
||||
&i.LeaseGeneration,
|
||||
&i.LeaseUntil,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PublishedOriginalKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const replayFailed = `-- name: ReplayFailed :execrows
|
||||
WITH replayed AS (
|
||||
UPDATE attachment_jobs AS j
|
||||
SET max_attempts = j.attempts_started + $1::int,
|
||||
next_attempt_at = clock_timestamp(), updated_at = clock_timestamp()
|
||||
FROM attachments AS a
|
||||
WHERE j.id = $2
|
||||
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
|
||||
AND a.status = 'failed' AND a.staging_key IS NOT NULL
|
||||
AND j.lease_until IS NULL AND j.next_attempt_at IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_replays AS r WHERE r.request_id = $3::uuid
|
||||
)
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
), recorded AS (
|
||||
INSERT INTO attachment_replays (request_id, job_id, operator_id, reason, extra_attempts)
|
||||
SELECT $3::uuid, id, $4, $5, $1
|
||||
FROM replayed
|
||||
), event AS (
|
||||
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
|
||||
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
|
||||
next_attempt_at, 'replay/' || id || '/' || $3::uuid
|
||||
FROM replayed
|
||||
)
|
||||
UPDATE attachments AS a
|
||||
SET status = 'queued', error_code = NULL, updated_at = replayed.updated_at
|
||||
FROM replayed WHERE a.id = replayed.attachment_id
|
||||
`
|
||||
|
||||
type ReplayFailedParams struct {
|
||||
ExtraAttempts int32
|
||||
JobID pgtype.UUID
|
||||
RequestID pgtype.UUID
|
||||
OperatorID string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (q *Queries) ReplayFailed(ctx context.Context, arg ReplayFailedParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, replayFailed,
|
||||
arg.ExtraAttempts,
|
||||
arg.JobID,
|
||||
arg.RequestID,
|
||||
arg.OperatorID,
|
||||
arg.Reason,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const scheduleRetry = `-- name: ScheduleRetry :execrows
|
||||
WITH scheduled AS (
|
||||
UPDATE attachment_jobs AS j
|
||||
SET lease_until = NULL, next_attempt_at = $1::timestamptz,
|
||||
updated_at = clock_timestamp()
|
||||
FROM attachments AS a
|
||||
WHERE j.id = $2
|
||||
AND j.lease_generation = $3 AND j.lease_until > clock_timestamp()
|
||||
AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing'
|
||||
AND j.attempts_started < j.max_attempts
|
||||
AND $1::timestamptz > clock_timestamp()
|
||||
RETURNING j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key
|
||||
), recorded AS (
|
||||
UPDATE attachment_attempts AS attempt
|
||||
SET outcome = 'retry', error_code = $4::text, finished_at = scheduled.updated_at
|
||||
FROM scheduled
|
||||
WHERE attempt.job_id = scheduled.id AND attempt.lease_generation = scheduled.lease_generation
|
||||
), event AS (
|
||||
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
|
||||
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at),
|
||||
next_attempt_at, 'retry/' || id || '/' || (attempts_started + 1)
|
||||
FROM scheduled
|
||||
)
|
||||
UPDATE attachments AS a
|
||||
SET status = 'queued', updated_at = scheduled.updated_at
|
||||
FROM scheduled WHERE a.id = scheduled.attachment_id
|
||||
`
|
||||
|
||||
type ScheduleRetryParams struct {
|
||||
AvailableAt pgtype.Timestamptz
|
||||
JobID pgtype.UUID
|
||||
LeaseGeneration int32
|
||||
ErrorCode string
|
||||
}
|
||||
|
||||
func (q *Queries) ScheduleRetry(ctx context.Context, arg ScheduleRetryParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, scheduleRetry,
|
||||
arg.AvailableAt,
|
||||
arg.JobID,
|
||||
arg.LeaseGeneration,
|
||||
arg.ErrorCode,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: chat_members.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createChatMember = `-- name: CreateChatMember :exec
|
||||
INSERT INTO chat_members(chat_id, user_id)
|
||||
VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type CreateChatMemberParams struct {
|
||||
ChatID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateChatMember(ctx context.Context, arg CreateChatMemberParams) error {
|
||||
_, err := q.db.Exec(ctx, createChatMember, arg.ChatID, arg.UserID)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: chats.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createChat = `-- name: CreateChat :one
|
||||
INSERT INTO chats (title)
|
||||
VALUES ($1)
|
||||
RETURNING id, title, created_at
|
||||
`
|
||||
|
||||
func (q *Queries) CreateChat(ctx context.Context, title string) (Chat, error) {
|
||||
row := q.db.QueryRow(ctx, createChat, title)
|
||||
var i Chat
|
||||
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getChatByID = `-- name: GetChatByID :one
|
||||
|
||||
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE c.id = $1
|
||||
GROUP BY c.id
|
||||
`
|
||||
|
||||
type GetChatByIDRow struct {
|
||||
ID pgtype.UUID
|
||||
Title string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
MemberIds []pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetChatByID(ctx context.Context, id pgtype.UUID) (GetChatByIDRow, error) {
|
||||
row := q.db.QueryRow(ctx, getChatByID, id)
|
||||
var i GetChatByIDRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getChatsByUserID = `-- name: GetChatsByUserID :many
|
||||
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE EXISTS (SELECT 1
|
||||
FROM chat_members membership
|
||||
WHERE membership.chat_id = c.id
|
||||
AND membership.user_id = $1)
|
||||
AND (
|
||||
$2::timestamptz IS NULL
|
||||
OR (c.created_at, c.id) >
|
||||
(
|
||||
$2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
)
|
||||
GROUP BY c.id
|
||||
ORDER BY c.created_at ASC, c.id ASC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type GetChatsByUserIDParams struct {
|
||||
UserID pgtype.UUID
|
||||
AfterCreatedAt pgtype.Timestamptz
|
||||
AfterID pgtype.UUID
|
||||
LimitCount int32
|
||||
}
|
||||
|
||||
type GetChatsByUserIDRow struct {
|
||||
ID pgtype.UUID
|
||||
Title string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
MemberIds []pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetChatsByUserID(ctx context.Context, arg GetChatsByUserIDParams) ([]GetChatsByUserIDRow, error) {
|
||||
rows, err := q.db.Query(ctx, getChatsByUserID,
|
||||
arg.UserID,
|
||||
arg.AfterCreatedAt,
|
||||
arg.AfterID,
|
||||
arg.LimitCount,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetChatsByUserIDRow
|
||||
for rows.Next() {
|
||||
var i GetChatsByUserIDRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: message_idempotence.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const deleteExpiredMessageIdempotency = `-- name: DeleteExpiredMessageIdempotency :exec
|
||||
DELETE FROM message_idempotency
|
||||
WHERE user_id = $1 AND chat_id = $2 AND idempotency_key = $3 AND expires_at <= now()
|
||||
`
|
||||
|
||||
type DeleteExpiredMessageIdempotencyParams struct {
|
||||
UserID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteExpiredMessageIdempotency(ctx context.Context, arg DeleteExpiredMessageIdempotencyParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteExpiredMessageIdempotency, arg.UserID, arg.ChatID, arg.IdempotencyKey)
|
||||
return err
|
||||
}
|
||||
|
||||
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM message_idempotency AS i
|
||||
JOIN messages AS m ON m.id = i.message_id
|
||||
WHERE i.user_id = $1
|
||||
AND i.chat_id = $2
|
||||
AND i.idempotency_key = $3
|
||||
`
|
||||
|
||||
type GetIdempotentMessageParams struct {
|
||||
UserID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (Message, error) {
|
||||
row := q.db.QueryRow(ctx, getIdempotentMessage, arg.UserID, arg.ChatID, arg.IdempotencyKey)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertMessageIdempotency = `-- name: InsertMessageIdempotency :execrows
|
||||
INSERT INTO message_idempotency(user_id, chat_id, idempotency_key, message_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING
|
||||
`
|
||||
|
||||
type InsertMessageIdempotencyParams struct {
|
||||
UserID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
IdempotencyKey string
|
||||
MessageID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) InsertMessageIdempotency(ctx context.Context, arg InsertMessageIdempotencyParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, insertMessageIdempotency,
|
||||
arg.UserID,
|
||||
arg.ChatID,
|
||||
arg.IdempotencyKey,
|
||||
arg.MessageID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const lockMessageRequest = `-- name: LockMessageRequest :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended(
|
||||
'message/' || ($1::uuid)::text || '/' ||
|
||||
($2::uuid)::text || '/' || $3::text, 0))
|
||||
`
|
||||
|
||||
type LockMessageRequestParams struct {
|
||||
UserID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
func (q *Queries) LockMessageRequest(ctx context.Context, arg LockMessageRequestParams) error {
|
||||
_, err := q.db.Exec(ctx, lockMessageRequest, arg.UserID, arg.ChatID, arg.IdempotencyKey)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: messages.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createMessage = `-- name: CreateMessage :one
|
||||
INSERT INTO messages (chat_id, sender_id, text)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, chat_id, sender_id, text, created_at
|
||||
`
|
||||
|
||||
type CreateMessageParams struct {
|
||||
ChatID pgtype.UUID
|
||||
SenderID pgtype.UUID
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
|
||||
row := q.db.QueryRow(ctx, createMessage, arg.ChatID, arg.SenderID, arg.Text)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMessagesByChatID = `-- name: GetMessagesByChatID :many
|
||||
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND (
|
||||
$2::timestamptz IS NULL
|
||||
OR (created_at, id) >
|
||||
($2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type GetMessagesByChatIDParams struct {
|
||||
ChatID pgtype.UUID
|
||||
AfterCreatedAt pgtype.Timestamptz
|
||||
AfterID pgtype.UUID
|
||||
LimitCount int32
|
||||
}
|
||||
|
||||
func (q *Queries) GetMessagesByChatID(ctx context.Context, arg GetMessagesByChatIDParams) ([]Message, error) {
|
||||
rows, err := q.db.Query(ctx, getMessagesByChatID,
|
||||
arg.ChatID,
|
||||
arg.AfterCreatedAt,
|
||||
arg.AfterID,
|
||||
arg.LimitCount,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Message
|
||||
for rows.Next() {
|
||||
var i Message
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: messages_attachments.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createMessagesAttachments = `-- name: CreateMessagesAttachments :execrows
|
||||
INSERT INTO messages_attachments (message_id, attachment_id, chat_id, position)
|
||||
SELECT $1, $2, $3, $4
|
||||
FROM attachments
|
||||
WHERE id = $2 AND chat_id = $3
|
||||
`
|
||||
|
||||
type CreateMessagesAttachmentsParams struct {
|
||||
MessageID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessagesAttachments(ctx context.Context, arg CreateMessagesAttachmentsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, createMessagesAttachments,
|
||||
arg.MessageID,
|
||||
arg.AttachmentID,
|
||||
arg.ChatID,
|
||||
arg.Position,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getMessagesAttachments = `-- name: GetMessagesAttachments :many
|
||||
SELECT attachment_id, position
|
||||
FROM messages_attachments
|
||||
WHERE message_id = $1
|
||||
ORDER BY position ASC
|
||||
`
|
||||
|
||||
type GetMessagesAttachmentsRow struct {
|
||||
AttachmentID pgtype.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
func (q *Queries) GetMessagesAttachments(ctx context.Context, messageID pgtype.UUID) ([]GetMessagesAttachmentsRow, error) {
|
||||
rows, err := q.db.Query(ctx, getMessagesAttachments, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetMessagesAttachmentsRow
|
||||
for rows.Next() {
|
||||
var i GetMessagesAttachmentsRow
|
||||
if err := rows.Scan(&i.AttachmentID, &i.Position); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockAttachmentsForMessage = `-- name: LockAttachmentsForMessage :many
|
||||
SELECT id, status FROM attachments
|
||||
WHERE chat_id = $1 AND id = ANY($2::uuid[])
|
||||
ORDER BY id FOR SHARE
|
||||
`
|
||||
|
||||
type LockAttachmentsForMessageParams struct {
|
||||
ChatID pgtype.UUID
|
||||
AttachmentIds []pgtype.UUID
|
||||
}
|
||||
|
||||
type LockAttachmentsForMessageRow struct {
|
||||
ID pgtype.UUID
|
||||
Status string
|
||||
}
|
||||
|
||||
func (q *Queries) LockAttachmentsForMessage(ctx context.Context, arg LockAttachmentsForMessageParams) ([]LockAttachmentsForMessageRow, error) {
|
||||
rows, err := q.db.Query(ctx, lockAttachmentsForMessage, arg.ChatID, arg.AttachmentIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []LockAttachmentsForMessageRow
|
||||
for rows.Next() {
|
||||
var i LockAttachmentsForMessageRow
|
||||
if err := rows.Scan(&i.ID, &i.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
OwnerID pgtype.UUID
|
||||
Filename string
|
||||
ObjectKey pgtype.Text
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
StagingKey pgtype.Text
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
type AttachmentAttempt struct {
|
||||
JobID pgtype.UUID
|
||||
LeaseGeneration int32
|
||||
Attempt int32
|
||||
StartedAt pgtype.Timestamptz
|
||||
FinishedAt pgtype.Timestamptz
|
||||
Outcome pgtype.Text
|
||||
ErrorCode pgtype.Text
|
||||
}
|
||||
|
||||
type AttachmentJob struct {
|
||||
ID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
AttemptsStarted int32
|
||||
MaxAttempts int32
|
||||
NextAttemptAt pgtype.Timestamptz
|
||||
LeaseGeneration int32
|
||||
LeaseUntil pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
PublishedOriginalKey pgtype.Text
|
||||
}
|
||||
|
||||
type AttachmentReplay struct {
|
||||
RequestID pgtype.UUID
|
||||
JobID pgtype.UUID
|
||||
OperatorID string
|
||||
Reason string
|
||||
ExtraAttempts int32
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type AttachmentVariant struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
Name string
|
||||
ObjectKey string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Width int32
|
||||
Height int32
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID pgtype.UUID
|
||||
Title string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type ChatMember struct {
|
||||
ChatID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
SenderID pgtype.UUID
|
||||
Text string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type MessageIdempotency struct {
|
||||
UserID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
IdempotencyKey string
|
||||
MessageID pgtype.UUID
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type MessagesAttachment struct {
|
||||
MessageID pgtype.UUID
|
||||
AttachmentID pgtype.UUID
|
||||
ChatID pgtype.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
type Outbox struct {
|
||||
ID pgtype.UUID
|
||||
JobID pgtype.UUID
|
||||
Payload []byte
|
||||
AvailableAt pgtype.Timestamptz
|
||||
SentAt pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamptz
|
||||
DedupKey string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID pgtype.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: outbox.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createInitialOutboxEvent = `-- name: CreateInitialOutboxEvent :exec
|
||||
INSERT INTO outbox (job_id, payload, available_at, dedup_key)
|
||||
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, 1, created_at),
|
||||
next_attempt_at, 'initial/' || id
|
||||
FROM attachment_jobs WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
func (q *Queries) CreateInitialOutboxEvent(ctx context.Context, jobID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, createInitialOutboxEvent, jobID)
|
||||
return err
|
||||
}
|
||||
|
||||
const enqueueExpiredLeases = `-- name: EnqueueExpiredLeases :many
|
||||
WITH expired AS (
|
||||
SELECT j.id, j.attachment_id, j.pipeline_version, j.attempts_started, j.max_attempts, j.next_attempt_at, j.lease_generation, j.lease_until, j.created_at, j.updated_at, j.published_original_key, 'recovery/' || j.id || '/' || j.lease_generation AS recovery_key
|
||||
FROM attachment_jobs AS j
|
||||
JOIN attachments AS a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version
|
||||
WHERE a.status = 'processing' AND j.lease_until <= clock_timestamp()
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM outbox AS o WHERE o.dedup_key = 'recovery/' || j.id || '/' || j.lease_generation
|
||||
)
|
||||
ORDER BY j.lease_until, j.id
|
||||
LIMIT $1
|
||||
FOR UPDATE OF j SKIP LOCKED
|
||||
)
|
||||
INSERT INTO outbox (job_id, payload, dedup_key)
|
||||
SELECT id, attachment_job_payload(id, attachment_id, pipeline_version,
|
||||
LEAST(attempts_started + 1, max_attempts), clock_timestamp()),
|
||||
recovery_key
|
||||
FROM expired
|
||||
ON CONFLICT (dedup_key) DO NOTHING
|
||||
RETURNING id, job_id, payload, available_at, sent_at, created_at, dedup_key
|
||||
`
|
||||
|
||||
func (q *Queries) EnqueueExpiredLeases(ctx context.Context, batchSize int32) ([]Outbox, error) {
|
||||
rows, err := q.db.Query(ctx, enqueueExpiredLeases, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Outbox
|
||||
for rows.Next() {
|
||||
var i Outbox
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.Payload,
|
||||
&i.AvailableAt,
|
||||
&i.SentAt,
|
||||
&i.CreatedAt,
|
||||
&i.DedupKey,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markOutboxEventSent = `-- name: MarkOutboxEventSent :execrows
|
||||
UPDATE outbox SET sent_at = clock_timestamp()
|
||||
WHERE id = $1 AND sent_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) MarkOutboxEventSent(ctx context.Context, id pgtype.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markOutboxEventSent, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const nextPendingOutboxEvent = `-- name: NextPendingOutboxEvent :one
|
||||
SELECT id, job_id, payload, available_at, sent_at, created_at, dedup_key
|
||||
FROM outbox
|
||||
WHERE sent_at IS NULL AND available_at <= statement_timestamp()
|
||||
ORDER BY available_at, id
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
`
|
||||
|
||||
func (q *Queries) NextPendingOutboxEvent(ctx context.Context) (Outbox, error) {
|
||||
row := q.db.QueryRow(ctx, nextPendingOutboxEvent)
|
||||
var i Outbox
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.Payload,
|
||||
&i.AvailableAt,
|
||||
&i.SentAt,
|
||||
&i.CreatedAt,
|
||||
&i.DedupKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: users.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, username, display_name, password_hash, created_at
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
row := q.db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByID = `-- name: GetUserByID :one
|
||||
|
||||
SELECT id, username, display_name, password_hash, created_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.Username,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByUsername = `-- name: GetUserByUsername :one
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users
|
||||
WHERE username = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUserByUsername, username)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: versioned_pipeline.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getPipelineJobID = `-- name: GetPipelineJobID :one
|
||||
SELECT id FROM attachment_jobs
|
||||
WHERE attachment_id = $1::uuid AND pipeline_version = $2::int
|
||||
`
|
||||
|
||||
type GetPipelineJobIDParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) GetPipelineJobID(ctx context.Context, arg GetPipelineJobIDParams) (pgtype.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, getPipelineJobID, arg.AttachmentID, arg.PipelineVersion)
|
||||
var id pgtype.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const getPublishedOriginalKey = `-- name: GetPublishedOriginalKey :one
|
||||
SELECT published_original_key FROM attachment_jobs
|
||||
WHERE attachment_id = $1::uuid AND pipeline_version = $2::int
|
||||
AND published_original_key IS NOT NULL
|
||||
`
|
||||
|
||||
type GetPublishedOriginalKeyParams struct {
|
||||
AttachmentID pgtype.UUID
|
||||
PipelineVersion int32
|
||||
}
|
||||
|
||||
func (q *Queries) GetPublishedOriginalKey(ctx context.Context, arg GetPublishedOriginalKeyParams) (pgtype.Text, error) {
|
||||
row := q.db.QueryRow(ctx, getPublishedOriginalKey, arg.AttachmentID, arg.PipelineVersion)
|
||||
var published_original_key pgtype.Text
|
||||
err := row.Scan(&published_original_key)
|
||||
return published_original_key, err
|
||||
}
|
||||
|
||||
const isPublishedOriginalKey = `-- name: IsPublishedOriginalKey :one
|
||||
SELECT EXISTS (SELECT 1 FROM attachment_jobs WHERE published_original_key = $1::text)
|
||||
`
|
||||
|
||||
func (q *Queries) IsPublishedOriginalKey(ctx context.Context, objectKey string) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, isPublishedOriginalKey, objectKey)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const queueAttachmentUpgrade = `-- name: QueueAttachmentUpgrade :one
|
||||
UPDATE attachments
|
||||
SET pipeline_version = 2, status = 'queued', staging_key = $1::text,
|
||||
object_key = NULL, error_code = NULL, updated_at = clock_timestamp()
|
||||
WHERE id = $2::uuid AND pipeline_version = 1
|
||||
RETURNING id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, created_at, updated_at, status, error_code, staging_key, pipeline_version
|
||||
`
|
||||
|
||||
type QueueAttachmentUpgradeParams struct {
|
||||
SourceKey string
|
||||
ID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) QueueAttachmentUpgrade(ctx context.Context, arg QueueAttachmentUpgradeParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, queueAttachmentUpgrade, arg.SourceKey, arg.ID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.Filename,
|
||||
&i.ObjectKey,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.StagingKey,
|
||||
&i.PipelineVersion,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const supersedeAttachmentJob = `-- name: SupersedeAttachmentJob :exec
|
||||
WITH superseded AS (
|
||||
UPDATE attachment_jobs
|
||||
SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp()
|
||||
WHERE id = $1::uuid
|
||||
RETURNING id, updated_at
|
||||
)
|
||||
UPDATE attachment_attempts AS attempt
|
||||
SET outcome = 'superseded', error_code = 'superseded', finished_at = superseded.updated_at
|
||||
FROM superseded
|
||||
WHERE attempt.job_id = superseded.id AND attempt.finished_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) SupersedeAttachmentJob(ctx context.Context, jobID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, supersedeAttachmentJob, jobID)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user