feat(db): add attachment content type and status enums

This commit is contained in:
kk10
2026-11-02 21:50:29 +03:00
parent 983bbaa55e
commit 42ddd2cad9
5 changed files with 108 additions and 15 deletions
+7 -2
View File
@@ -1,14 +1,17 @@
-- +goose Up -- +goose Up
CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg');
CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed');
CREATE TABLE attachments ( CREATE TABLE attachments (
id uuid PRIMARY KEY, id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT, owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
s3_key text NOT NULL UNIQUE, s3_key text NOT NULL UNIQUE,
filename text NOT NULL, filename text NOT NULL,
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), content_type attachment_content_type NOT NULL,
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760), size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status text NOT NULL DEFAULT 'ready' CHECK (status = 'ready'), status attachment_status NOT NULL DEFAULT 'ready',
error_code text, error_code text,
created_at timestamptz NOT NULL DEFAULT now(), created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now() updated_at timestamptz NOT NULL DEFAULT now()
@@ -30,3 +33,5 @@ CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attac
-- +goose Down -- +goose Down
DROP TABLE message_attachments; DROP TABLE message_attachments;
DROP TABLE attachments; DROP TABLE attachments;
DROP TYPE attachment_status;
DROP TYPE attachment_content_type;
+2 -2
View File
@@ -17,10 +17,10 @@ SELECT
sqlc.arg(owner_id)::uuid, sqlc.arg(owner_id)::uuid,
sqlc.arg(s3_key)::text, sqlc.arg(s3_key)::text,
sqlc.arg(filename)::text, sqlc.arg(filename)::text,
sqlc.arg(content_type)::text, sqlc.arg(content_type)::attachment_content_type,
sqlc.arg(size_bytes)::int, sqlc.arg(size_bytes)::int,
sqlc.arg(sha256)::text, sqlc.arg(sha256)::text,
sqlc.arg(status)::text, sqlc.arg(status)::attachment_status,
sqlc.arg(error_code)::text sqlc.arg(error_code)::text
WHERE EXISTS ( WHERE EXISTS (
SELECT 1 FROM chat_members SELECT 1 FROM chat_members
+7 -7
View File
@@ -30,10 +30,10 @@ SELECT
$3::uuid, $3::uuid,
$4::text, $4::text,
$5::text, $5::text,
$6::text, $6::attachment_content_type,
$7::int, $7::int,
$8::text, $8::text,
$9::text, $9::attachment_status,
$10::text $10::text
WHERE EXISTS ( WHERE EXISTS (
SELECT 1 FROM chat_members SELECT 1 FROM chat_members
@@ -49,10 +49,10 @@ type CreateAttachmentParams struct {
OwnerID uuid.UUID OwnerID uuid.UUID
S3Key string S3Key string
Filename string Filename string
ContentType string ContentType AttachmentContentType
SizeBytes int SizeBytes int
Sha256 string Sha256 string
Status string Status AttachmentStatus
ErrorCode string ErrorCode string
} }
@@ -147,15 +147,15 @@ type ListAttachmentsByIDsParams struct {
Ids []uuid.UUID Ids []uuid.UUID
} }
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]string, error) { func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) {
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids) rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
items := []string{} items := []AttachmentStatus{}
for rows.Next() { for rows.Next() {
var status string var status AttachmentStatus
if err := rows.Scan(&status); err != nil { if err := rows.Scan(&status); err != nil {
return nil, err return nil, err
} }
+90 -2
View File
@@ -5,21 +5,109 @@
package db package db
import ( import (
"database/sql/driver"
"fmt"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
) )
type AttachmentContentType string
const (
AttachmentContentTypeImagePng AttachmentContentType = "image/png"
AttachmentContentTypeImageJpeg AttachmentContentType = "image/jpeg"
)
func (e *AttachmentContentType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = AttachmentContentType(s)
case string:
*e = AttachmentContentType(s)
default:
return fmt.Errorf("unsupported scan type for AttachmentContentType: %T", src)
}
return nil
}
type NullAttachmentContentType struct {
AttachmentContentType AttachmentContentType
Valid bool // Valid is true if AttachmentContentType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullAttachmentContentType) Scan(value interface{}) error {
if value == nil {
ns.AttachmentContentType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.AttachmentContentType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullAttachmentContentType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.AttachmentContentType), nil
}
type AttachmentStatus string
const (
AttachmentStatusQueued AttachmentStatus = "queued"
AttachmentStatusProcessing AttachmentStatus = "processing"
AttachmentStatusReady AttachmentStatus = "ready"
AttachmentStatusFailed AttachmentStatus = "failed"
)
func (e *AttachmentStatus) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = AttachmentStatus(s)
case string:
*e = AttachmentStatus(s)
default:
return fmt.Errorf("unsupported scan type for AttachmentStatus: %T", src)
}
return nil
}
type NullAttachmentStatus struct {
AttachmentStatus AttachmentStatus
Valid bool // Valid is true if AttachmentStatus is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullAttachmentStatus) Scan(value interface{}) error {
if value == nil {
ns.AttachmentStatus, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.AttachmentStatus.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullAttachmentStatus) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.AttachmentStatus), nil
}
type Attachment struct { type Attachment struct {
ID uuid.UUID ID uuid.UUID
ChatID uuid.UUID ChatID uuid.UUID
OwnerID uuid.UUID OwnerID uuid.UUID
S3Key string S3Key string
Filename string Filename string
ContentType string ContentType AttachmentContentType
SizeBytes int SizeBytes int
Sha256 string Sha256 string
Status string Status AttachmentStatus
ErrorCode *string ErrorCode *string
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
+2 -2
View File
@@ -31,7 +31,7 @@ type messageQueries interface {
CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error
GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error) GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error)
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error) IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]string, error) ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]db.AttachmentStatus, error)
ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error)
ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error)
ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error) ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error)
@@ -79,7 +79,7 @@ func (s *MessageService) Create(
return model.Message{}, ErrAttachmentNotFound return model.Message{}, ErrAttachmentNotFound
} }
for _, status := range statuses { for _, status := range statuses {
if status != "ready" { if status != db.AttachmentStatusReady {
return model.Message{}, ErrAttachmentNotReady return model.Message{}, ErrAttachmentNotReady
} }
} }