feat(cleanup): remove stale queued attachments after grace period

This commit is contained in:
kk10
2026-11-03 15:12:55 +03:00
parent fbd9c56b1f
commit 1ee4f8ecec
7 changed files with 189 additions and 36 deletions
+2 -1
View File
@@ -11,7 +11,7 @@ CREATE TABLE attachments (
content_type attachment_content_type NOT NULL,
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status attachment_status NOT NULL DEFAULT 'ready',
status attachment_status NOT NULL DEFAULT 'queued',
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
@@ -19,6 +19,7 @@ CREATE TABLE attachments (
CREATE INDEX idx_attachments_chat_id ON attachments (chat_id, created_at);
CREATE INDEX idx_attachments_owner_id ON attachments (owner_id);
CREATE INDEX idx_attachments_queued_created_at ON attachments (created_at) WHERE status = 'queued';
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
+14 -2
View File
@@ -8,7 +8,6 @@ INSERT INTO attachments (
content_type,
size_bytes,
sha256,
status,
error_code
)
SELECT
@@ -20,7 +19,6 @@ SELECT
sqlc.arg(content_type)::attachment_content_type,
sqlc.arg(size_bytes)::int,
sqlc.arg(sha256)::text,
sqlc.arg(status)::attachment_status,
NULLIF(sqlc.arg(error_code)::text, '')
WHERE EXISTS (
SELECT 1 FROM chat_members
@@ -61,3 +59,17 @@ 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: MarkAttachmentReady :one
UPDATE attachments
SET status = 'ready', updated_at = now()
WHERE id = $1::uuid
RETURNING *;
-- name: DeleteAttachment :exec
DELETE FROM attachments
WHERE id = $1::uuid;