forked from mtusi2026/mtusi2026lab5
feat(attachments): accept uploads through staging and transactional outbox
This commit is contained in:
@@ -144,8 +144,8 @@ type CreateAttachmentParams struct {
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
StagingKey *string
|
||||
JobID *uuid.UUID
|
||||
StagingKey string
|
||||
JobID uuid.UUID
|
||||
Filename string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
@@ -219,19 +219,6 @@ func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessage
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteQueuedAttachment = `-- name: DeleteQueuedAttachment :execrows
|
||||
DELETE FROM attachments
|
||||
WHERE id = $1::uuid AND status = 'queued'
|
||||
`
|
||||
|
||||
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 failAttachmentJob = `-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
@@ -342,26 +329,15 @@ func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsB
|
||||
}
|
||||
|
||||
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
|
||||
SELECT id, s3_key, status, size_bytes
|
||||
FROM attachments
|
||||
`
|
||||
|
||||
type ListAttachmentsForReconcileRow struct {
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
Status AttachmentStatus
|
||||
SizeBytes int
|
||||
CreatedAt time.Time
|
||||
Referenced bool
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
Status AttachmentStatus
|
||||
SizeBytes int
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) {
|
||||
@@ -378,8 +354,6 @@ func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttach
|
||||
&i.S3Key,
|
||||
&i.Status,
|
||||
&i.SizeBytes,
|
||||
&i.CreatedAt,
|
||||
&i.Referenced,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -450,36 +424,6 @@ 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 lockAttachmentJob = `-- name: LockAttachmentJob :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
|
||||
Reference in New Issue
Block a user