fix(cleanup): delete queued rows before their objects

This commit is contained in:
kk10
2026-11-03 23:45:45 +03:00
parent 1ee4f8ecec
commit 29f51bf0b3
3 changed files with 110 additions and 12 deletions
+22 -2
View File
@@ -64,12 +64,32 @@ ORDER BY message_id, position;
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: 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: MarkAttachmentReady :one
UPDATE attachments
SET status = 'ready', updated_at = now()
WHERE id = $1::uuid
RETURNING *;
-- name: DeleteAttachment :exec
-- name: DeleteQueuedAttachment :execrows
DELETE FROM attachments
WHERE id = $1::uuid;
WHERE id = sqlc.arg(id)::uuid AND status = 'queued';