Files
mtusi2026lab5/internal/db/attachments.sql.go
T

624 lines
14 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: attachments.sql
package db
import (
"context"
"time"
"github.com/google/uuid"
)
const claimAttachmentJob = `-- name: ClaimAttachmentJob :one
UPDATE attachments
SET status = 'processing',
attempts = attempts + 1,
lease_until = $1::timestamptz,
updated_at = now()
WHERE id = $2::uuid
AND job_id = $3::uuid
AND pipeline_version = $4::int
AND status = 'queued'
AND attempts + 1 = $5::int
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until
`
type ClaimAttachmentJobParams struct {
LeaseUntil time.Time
ID uuid.UUID
JobID uuid.UUID
PipelineVersion int
Attempt int
}
func (q *Queries) ClaimAttachmentJob(ctx context.Context, arg ClaimAttachmentJobParams) (Attachment, error) {
row := q.db.QueryRow(ctx, claimAttachmentJob,
arg.LeaseUntil,
arg.ID,
arg.JobID,
arg.PipelineVersion,
arg.Attempt,
)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const completeAttachmentJob = `-- name: CompleteAttachmentJob :execrows
UPDATE attachments
SET status = 'ready',
error_code = NULL,
lease_until = NULL,
width = $1::int,
height = $2::int,
updated_at = now()
WHERE id = $3::uuid
AND status = 'processing'
AND attempts = $4::int
AND lease_until > now()
`
type CompleteAttachmentJobParams struct {
Width int
Height int
ID uuid.UUID
Attempt int
}
func (q *Queries) CompleteAttachmentJob(ctx context.Context, arg CompleteAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, completeAttachmentJob,
arg.Width,
arg.Height,
arg.ID,
arg.Attempt,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const createAttachment = `-- name: CreateAttachment :one
INSERT INTO attachments (
id,
chat_id,
owner_id,
s3_key,
staging_key,
job_id,
filename,
content_type,
size_bytes,
sha256,
width,
height,
error_code
)
SELECT
$1::uuid,
$2::uuid,
$3::uuid,
$4::text,
$5::text,
$6::uuid,
$7::text,
$8::attachment_content_type,
$9::int,
$10::text,
$11::int,
$12::int,
NULLIF($13::text, '')
WHERE EXISTS (
SELECT 1 FROM chat_members
WHERE chat_id = $2
AND user_id = $3
)
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until
`
type CreateAttachmentParams struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
S3Key string
StagingKey string
JobID uuid.UUID
Filename string
ContentType AttachmentContentType
SizeBytes int
Sha256 string
Width int
Height int
ErrorCode string
}
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) {
row := q.db.QueryRow(ctx, createAttachment,
arg.ID,
arg.ChatID,
arg.OwnerID,
arg.S3Key,
arg.StagingKey,
arg.JobID,
arg.Filename,
arg.ContentType,
arg.SizeBytes,
arg.Sha256,
arg.Width,
arg.Height,
arg.ErrorCode,
)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const createMessageAttachment = `-- name: CreateMessageAttachment :exec
INSERT INTO message_attachments (
message_id,
attachment_id,
position
) VALUES (
$1,
$2,
$3
)
`
type CreateMessageAttachmentParams struct {
MessageID uuid.UUID
AttachmentID uuid.UUID
Position int
}
func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error {
_, err := q.db.Exec(ctx, createMessageAttachment, arg.MessageID, arg.AttachmentID, arg.Position)
return err
}
const failAttachmentJob = `-- name: FailAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = $1::text,
lease_until = NULL,
updated_at = now()
WHERE id = $2::uuid
AND status = 'processing'
AND attempts = $3::int
AND lease_until > now()
`
type FailAttachmentJobParams struct {
ErrorCode string
ID uuid.UUID
Attempt int
}
func (q *Queries) FailAttachmentJob(ctx context.Context, arg FailAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, failAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const failExpiredAttachmentJob = `-- name: FailExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = 'retry_exhausted',
lease_until = NULL,
updated_at = now()
WHERE id = $1::uuid
AND status = 'processing'
AND lease_until <= now()
`
func (q *Queries) FailExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, failExpiredAttachmentJob, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getAttachmentByID = `-- name: GetAttachmentByID :one
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
WHERE id = $1::uuid
`
func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, getAttachmentByID, dollar_1)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many
SELECT status
FROM attachments
WHERE chat_id = $1::uuid
AND id = ANY($2::uuid[])
`
type ListAttachmentsByIDsParams struct {
ChatID uuid.UUID
Ids []uuid.UUID
}
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) {
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids)
if err != nil {
return nil, err
}
defer rows.Close()
items := []AttachmentStatus{}
for rows.Next() {
var status AttachmentStatus
if err := rows.Scan(&status); err != nil {
return nil, err
}
items = append(items, status)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAttachmentsForReconcile = `-- name: ListAttachmentsForReconcile :many
SELECT id, s3_key, status, size_bytes
FROM attachments
`
type ListAttachmentsForReconcileRow struct {
ID uuid.UUID
S3Key string
Status AttachmentStatus
SizeBytes int
}
func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) {
rows, err := q.db.Query(ctx, listAttachmentsForReconcile)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListAttachmentsForReconcileRow{}
for rows.Next() {
var i ListAttachmentsForReconcileRow
if err := rows.Scan(
&i.ID,
&i.S3Key,
&i.Status,
&i.SizeBytes,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listMessageAttachmentIDs = `-- name: ListMessageAttachmentIDs :many
SELECT attachment_id
FROM message_attachments
WHERE message_id = $1::uuid
ORDER BY position
`
func (q *Queries) ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, listMessageAttachmentIDs, messageID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
for rows.Next() {
var attachment_id uuid.UUID
if err := rows.Scan(&attachment_id); err != nil {
return nil, err
}
items = append(items, attachment_id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listMessageAttachmentsByMessageIDs = `-- name: ListMessageAttachmentsByMessageIDs :many
SELECT message_id, attachment_id
FROM message_attachments
WHERE message_id = ANY($1::uuid[])
ORDER BY message_id, position
`
type ListMessageAttachmentsByMessageIDsRow struct {
MessageID uuid.UUID
AttachmentID uuid.UUID
}
func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIds []uuid.UUID) ([]ListMessageAttachmentsByMessageIDsRow, error) {
rows, err := q.db.Query(ctx, listMessageAttachmentsByMessageIDs, messageIds)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListMessageAttachmentsByMessageIDsRow{}
for rows.Next() {
var i ListMessageAttachmentsByMessageIDsRow
if err := rows.Scan(&i.MessageID, &i.AttachmentID); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockAttachmentJob = `-- name: LockAttachmentJob :one
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
WHERE id = $1::uuid
AND status = 'processing'
AND job_id = $2::uuid
AND pipeline_version = $3::int
AND attempts = $4::int
AND lease_until > now()
FOR UPDATE
`
type LockAttachmentJobParams struct {
ID uuid.UUID
JobID uuid.UUID
PipelineVersion int
Attempt int
}
func (q *Queries) LockAttachmentJob(ctx context.Context, arg LockAttachmentJobParams) (Attachment, error) {
row := q.db.QueryRow(ctx, lockAttachmentJob,
arg.ID,
arg.JobID,
arg.PipelineVersion,
arg.Attempt,
)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const lockExpiredAttachmentJobs = `-- name: LockExpiredAttachmentJobs :many
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
WHERE status = 'processing'
AND lease_until <= now()
ORDER BY lease_until, id
LIMIT $1::int
FOR UPDATE SKIP LOCKED
`
func (q *Queries) LockExpiredAttachmentJobs(ctx context.Context, batchSize int) ([]Attachment, error) {
rows, err := q.db.Query(ctx, lockExpiredAttachmentJobs, batchSize)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Attachment{}
for rows.Next() {
var i Attachment
if err := rows.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows
UPDATE attachments
SET status = 'failed',
error_code = $1::text,
updated_at = now()
WHERE id = $2::uuid AND status = 'ready'
`
type MarkAttachmentFailedIfReadyParams struct {
ErrorCode string
ID uuid.UUID
}
func (q *Queries) MarkAttachmentFailedIfReady(ctx context.Context, arg MarkAttachmentFailedIfReadyParams) (int64, error) {
result, err := q.db.Exec(ctx, markAttachmentFailedIfReady, arg.ErrorCode, arg.ID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const markAttachmentReady = `-- name: MarkAttachmentReady :one
UPDATE attachments
SET status = 'ready', updated_at = now()
WHERE id = $1::uuid
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until
`
func (q *Queries) MarkAttachmentReady(ctx context.Context, id uuid.UUID) (Attachment, error) {
row := q.db.QueryRow(ctx, markAttachmentReady, id)
var i Attachment
err := row.Scan(
&i.ID,
&i.ChatID,
&i.OwnerID,
&i.S3Key,
&i.Filename,
&i.ContentType,
&i.SizeBytes,
&i.Sha256,
&i.Status,
&i.ErrorCode,
&i.CreatedAt,
&i.UpdatedAt,
&i.StagingKey,
&i.JobID,
&i.PipelineVersion,
&i.Attempts,
&i.Width,
&i.Height,
&i.LeaseUntil,
)
return i, err
}
const recoverExpiredAttachmentJob = `-- name: RecoverExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
lease_until = NULL,
updated_at = now()
WHERE id = $1::uuid
AND status = 'processing'
AND lease_until <= now()
`
func (q *Queries) RecoverExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, recoverExpiredAttachmentJob, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const retryAttachmentJob = `-- name: RetryAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
error_code = $1::text,
lease_until = NULL,
updated_at = now()
WHERE id = $2::uuid
AND status = 'processing'
AND attempts = $3::int
AND lease_until > now()
`
type RetryAttachmentJobParams struct {
ErrorCode string
ID uuid.UUID
Attempt int
}
func (q *Queries) RetryAttachmentJob(ctx context.Context, arg RetryAttachmentJobParams) (int64, error) {
result, err := q.db.Exec(ctx, retryAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}