fix: preserve attachment pipeline invariants

This commit is contained in:
2026-10-30 13:45:58 +03:00
parent aa5d6a6e16
commit 25c80a0072
7 changed files with 353 additions and 60 deletions
+61 -5
View File
@@ -174,16 +174,51 @@ func (s *Store) AttachmentExistsByObjectKey(ctx context.Context, objectKey strin
return exists, nil
}
func (s *Store) AttachmentObjectKeys(ctx context.Context, attachmentID string) ([]string, error) {
const query = `
SELECT object_key
FROM (
SELECT object_key
FROM attachments
WHERE id = $1 AND object_key <> ''
UNION
SELECT object_key
FROM attachment_variants
WHERE attachment_id = $1
) objects
ORDER BY object_key`
rows, err := s.pool.Query(ctx, query, attachmentID)
if err != nil {
return nil, fmt.Errorf("list attachment object keys: %w", err)
}
defer rows.Close()
keys, err := pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
return nil, fmt.Errorf("scan attachment object keys: %w", err)
}
return keys, nil
}
func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limit int) ([]model.Attachment, string, error) {
if limit < 1 || limit > 1000 {
return nil, "", fmt.Errorf("attachment list limit must be between 1 and 1000")
}
query := `
SELECT ` + attachmentColumnsWithAlias("a") + `,
EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id)
FROM attachments a
WHERE a.object_key > $1
ORDER BY a.object_key
EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id),
objects.object_key
FROM (
SELECT id AS attachment_id, object_key
FROM attachments
WHERE object_key <> ''
UNION
SELECT attachment_id, object_key
FROM attachment_variants
) objects
JOIN attachments a ON a.id = objects.attachment_id
WHERE objects.object_key > $1
ORDER BY objects.object_key
LIMIT $2`
rows, err := s.pool.Query(ctx, query, afterObjectKey, limit+1)
if err != nil {
@@ -193,7 +228,9 @@ func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limi
attachments, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Attachment](func(row pgx.CollectableRow) (model.Attachment, error) {
var attachment model.Attachment
err := scanAttachmentWithReference(row, &attachment)
var objectKey string
err := scanAttachmentWithReferenceAndObjectKey(row, &attachment, &objectKey)
attachment.ObjectKey = objectKey
return publicAttachment(attachment), err
}))
if err != nil {
@@ -207,6 +244,25 @@ func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limi
return attachments, next, nil
}
func scanAttachmentWithReferenceAndObjectKey(
row rowScanner,
attachment *model.Attachment,
objectKey *string,
) error {
var stagingKey *string
err := row.Scan(
&attachment.ID, &attachment.ChatID, &attachment.OwnerID, &attachment.Filename,
&attachment.ContentType, &attachment.SizeBytes, &attachment.SHA256, &attachment.ObjectKey,
&attachment.Status, &attachment.CreatedAt, &attachment.UpdatedAt, &attachment.ReadyAt,
&attachment.PipelineVersion, &attachment.ErrorCode, &stagingKey,
&attachment.Referenced, objectKey,
)
if stagingKey != nil {
attachment.StagingKey = *stagingKey
}
return err
}
func scanAttachmentWithReference(row rowScanner, attachment *model.Attachment) error {
var stagingKey *string
err := row.Scan(