forked from mtusi2026/mtusi2026lab4
feat(cleanup): remove stale queued attachments after grace period
This commit is contained in:
@@ -7,6 +7,7 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -21,7 +22,6 @@ INSERT INTO attachments (
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
status,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
@@ -33,8 +33,7 @@ SELECT
|
||||
$6::attachment_content_type,
|
||||
$7::int,
|
||||
$8::text,
|
||||
$9::attachment_status,
|
||||
NULLIF($10::text, '')
|
||||
NULLIF($9::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $2
|
||||
@@ -52,7 +51,6 @@ type CreateAttachmentParams struct {
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Status AttachmentStatus
|
||||
ErrorCode string
|
||||
}
|
||||
|
||||
@@ -66,7 +64,6 @@ func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentPara
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
arg.Status,
|
||||
arg.ErrorCode,
|
||||
)
|
||||
var i Attachment
|
||||
@@ -110,6 +107,16 @@ func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessage
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteAttachment = `-- name: DeleteAttachment :exec
|
||||
DELETE FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteAttachment(ctx context.Context, dollar_1 uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteAttachment, dollar_1)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAttachmentByID = `-- name: GetAttachmentByID :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
@@ -225,3 +232,60 @@ func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messag
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listStaleAttachmentUploads = `-- name: ListStaleAttachmentUploads :many
|
||||
SELECT id, s3_key FROM attachments
|
||||
WHERE status = 'queued' AND created_at < $1::timestamptz
|
||||
`
|
||||
|
||||
type ListStaleAttachmentUploadsRow struct {
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
}
|
||||
|
||||
func (q *Queries) ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]ListStaleAttachmentUploadsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStaleAttachmentUploads, cutoff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListStaleAttachmentUploadsRow{}
|
||||
for rows.Next() {
|
||||
var i ListStaleAttachmentUploadsRow
|
||||
if err := rows.Scan(&i.ID, &i.S3Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markAttachmentReady = `-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
func (q *Queries) MarkAttachmentReady(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, markAttachmentReady, dollar_1)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user