fix(cleanup): delete queued rows before their objects
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user