-- 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.arg(staging_key)::text, sqlc.arg(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: LockAttachmentByStagingKey :one SELECT * FROM attachments WHERE staging_key = sqlc.arg(staging_key)::text FOR UPDATE; -- 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: ListAttachmentsForReconcile :many SELECT id, s3_key, staging_key, status, size_bytes, updated_at FROM attachments; -- name: ListFailedAttachments :many SELECT id, job_id, error_code, attempts, updated_at FROM attachments WHERE status = 'failed' ORDER BY updated_at, id LIMIT sqlc.arg(batch_size)::int; -- 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: LockFailedAttachmentForReplay :one SELECT * FROM attachments WHERE id = sqlc.arg(id)::uuid AND status = 'failed' AND staging_key IS NOT NULL AND NOT EXISTS ( SELECT 1 FROM attachment_variants WHERE attachment_id = attachments.id ) FOR UPDATE; -- name: ClearAttachmentStagingKey :exec UPDATE attachments SET staging_key = NULL WHERE id = sqlc.arg(id)::uuid; -- 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, 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: ReplayAttachmentJob :execrows UPDATE attachments SET status = 'queued', retry_base = attempts, error_code = NULL, lease_until = NULL, updated_at = now() WHERE id = sqlc.arg(id)::uuid AND status = 'failed' AND staging_key IS NOT NULL AND NOT EXISTS ( SELECT 1 FROM attachment_variants WHERE attachment_id = attachments.id ); -- 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();