diff --git a/db/queries/attachments.sql b/db/queries/attachments.sql index fd8d6f9..9f6ce77 100644 --- a/db/queries/attachments.sql +++ b/db/queries/attachments.sql @@ -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'; diff --git a/internal/app/cleanup.go b/internal/app/cleanup.go index f0bbe51..c27528d 100644 --- a/internal/app/cleanup.go +++ b/internal/app/cleanup.go @@ -28,7 +28,7 @@ type idempotencyCleaner interface { type staleUploadLister interface { ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]db.ListStaleAttachmentUploadsRow, error) - DeleteAttachment(ctx context.Context, id uuid.UUID) error + DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) } type objectDeleter interface { @@ -128,12 +128,16 @@ func cleanupStaleUploads( deleted := 0 for _, row := range rows { - if err := store.DeleteAdmin(ctx, row.S3Key); err != nil { - log.Error("delete stale upload object", "key", row.S3Key, "error", err) + removed, err := q.DeleteQueuedAttachment(ctx, row.ID) + if err != nil { + log.Error("delete stale upload row", "id", row.ID, "error", err) continue } - if err := q.DeleteAttachment(ctx, row.ID); err != nil { - log.Error("delete stale upload row", "id", row.ID, "error", err) + if removed == 0 { + continue + } + if err := store.DeleteAdmin(ctx, row.S3Key); err != nil { + log.Error("delete stale upload object", "key", row.S3Key, "error", err) continue } deleted++ diff --git a/internal/db/attachments.sql.go b/internal/db/attachments.sql.go index 843941c..8103dd3 100644 --- a/internal/db/attachments.sql.go +++ b/internal/db/attachments.sql.go @@ -107,14 +107,17 @@ func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessage return err } -const deleteAttachment = `-- name: DeleteAttachment :exec +const deleteQueuedAttachment = `-- name: DeleteQueuedAttachment :execrows DELETE FROM attachments -WHERE id = $1::uuid +WHERE id = $1::uuid AND status = 'queued' ` -func (q *Queries) DeleteAttachment(ctx context.Context, dollar_1 uuid.UUID) error { - _, err := q.db.Exec(ctx, deleteAttachment, dollar_1) - return err +func (q *Queries) DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) { + result, err := q.db.Exec(ctx, deleteQueuedAttachment, id) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil } const getAttachmentByID = `-- name: GetAttachmentByID :one @@ -174,6 +177,56 @@ func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsB return items, nil } +const listAttachmentsForReconcile = `-- 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 +` + +type ListAttachmentsForReconcileRow struct { + ID uuid.UUID + S3Key string + Status AttachmentStatus + SizeBytes int + CreatedAt time.Time + Referenced bool +} + +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, + &i.CreatedAt, + &i.Referenced, + ); 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 @@ -263,6 +316,27 @@ func (q *Queries) ListStaleAttachmentUploads(ctx context.Context, cutoff time.Ti 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()