-- 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 sqlc.arg(id)::uuid, sqlc.arg(chat_id)::uuid, sqlc.arg(owner_id)::uuid, sqlc.arg(s3_key)::text, sqlc.narg(staging_key)::text, sqlc.narg(job_id)::uuid, sqlc.arg(filename)::text, sqlc.arg(content_type)::attachment_content_type, sqlc.arg(size_bytes)::int, sqlc.arg(sha256)::text, sqlc.arg(width)::int, sqlc.arg(height)::int, NULLIF(sqlc.arg(error_code)::text, '') WHERE EXISTS ( SELECT 1 FROM chat_members WHERE chat_id = sqlc.arg(chat_id) AND user_id = sqlc.arg(owner_id) ) RETURNING *; -- name: CreateMessageAttachment :exec INSERT INTO message_attachments ( message_id, attachment_id, position ) VALUES ( sqlc.arg(message_id), sqlc.arg(attachment_id), sqlc.arg(position) ); -- name: GetAttachmentByID :one SELECT * FROM attachments WHERE id = $1::uuid; -- name: ListAttachmentsByIDs :many SELECT status FROM attachments WHERE chat_id = sqlc.arg(chat_id)::uuid AND id = ANY(sqlc.arg(ids)::uuid[]); -- name: ListMessageAttachmentIDs :many SELECT attachment_id FROM message_attachments WHERE message_id = sqlc.arg(message_id)::uuid ORDER BY position; -- name: ListMessageAttachmentsByMessageIDs :many SELECT message_id, attachment_id FROM message_attachments WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[]) ORDER BY message_id, position; -- name: ListStaleAttachmentUploads :many SELECT id, s3_key FROM attachments WHERE status = 'queued' AND created_at < sqlc.arg(cutoff)::timestamptz; -- name: ListAttachmentsForReconcile :many SELECT a.id, a.s3_key, a.status, a.size_bytes, a.created_at, EXISTS ( SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id ) AS referenced FROM attachments a; -- name: LockAttachmentJob :one SELECT * FROM attachments WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND job_id = sqlc.arg(job_id)::uuid AND pipeline_version = sqlc.arg(pipeline_version)::int AND attempts = sqlc.arg(attempt)::int AND lease_until > now() FOR UPDATE; -- name: LockExpiredAttachmentJobs :many SELECT * FROM attachments WHERE status = 'processing' AND lease_until <= now() ORDER BY lease_until, id LIMIT sqlc.arg(batch_size)::int FOR UPDATE SKIP LOCKED; -- name: MarkAttachmentReady :one UPDATE attachments SET status = 'ready', updated_at = now() WHERE id = sqlc.arg(id)::uuid RETURNING *; -- name: MarkAttachmentFailedIfReady :execrows UPDATE attachments SET status = 'failed', error_code = sqlc.arg(error_code)::text, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'ready'; -- name: ClaimAttachmentJob :one UPDATE attachments SET status = 'processing', attempts = attempts + 1, lease_until = sqlc.arg(lease_until)::timestamptz, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND job_id = sqlc.arg(job_id)::uuid AND pipeline_version = sqlc.arg(pipeline_version)::int AND status = 'queued' AND attempts + 1 = sqlc.arg(attempt)::int RETURNING *; -- name: CompleteAttachmentJob :execrows UPDATE attachments SET status = 'ready', error_code = NULL, lease_until = NULL, width = sqlc.arg(width)::int, height = sqlc.arg(height)::int, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND attempts = sqlc.arg(attempt)::int AND lease_until > now(); -- name: RetryAttachmentJob :execrows UPDATE attachments SET status = 'queued', error_code = sqlc.arg(error_code)::text, lease_until = NULL, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND attempts = sqlc.arg(attempt)::int AND lease_until > now(); -- name: FailAttachmentJob :execrows UPDATE attachments SET status = 'failed', error_code = sqlc.arg(error_code)::text, lease_until = NULL, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND attempts = sqlc.arg(attempt)::int AND lease_until > now(); -- name: RecoverExpiredAttachmentJob :execrows UPDATE attachments SET status = 'queued', lease_until = NULL, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND lease_until <= now(); -- name: FailExpiredAttachmentJob :execrows UPDATE attachments SET status = 'failed', error_code = 'retry_exhausted', lease_until = NULL, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'processing' AND lease_until <= now(); -- name: DeleteQueuedAttachment :execrows DELETE FROM attachments WHERE id = sqlc.arg(id)::uuid AND status = 'queued';