diff --git a/db/migrations/00003_attachments.sql b/db/migrations/00003_attachments.sql index 118c286..4a7b1af 100644 --- a/db/migrations/00003_attachments.sql +++ b/db/migrations/00003_attachments.sql @@ -1,14 +1,17 @@ -- +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 ( id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT, s3_key text NOT NULL UNIQUE, 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), 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, created_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 DROP TABLE message_attachments; DROP TABLE attachments; +DROP TYPE attachment_status; +DROP TYPE attachment_content_type; diff --git a/db/queries/attachments.sql b/db/queries/attachments.sql index 4d011db..4c949a9 100644 --- a/db/queries/attachments.sql +++ b/db/queries/attachments.sql @@ -17,10 +17,10 @@ SELECT sqlc.arg(owner_id)::uuid, sqlc.arg(s3_key)::text, sqlc.arg(filename)::text, - sqlc.arg(content_type)::text, + sqlc.arg(content_type)::attachment_content_type, sqlc.arg(size_bytes)::int, sqlc.arg(sha256)::text, - sqlc.arg(status)::text, + sqlc.arg(status)::attachment_status, sqlc.arg(error_code)::text WHERE EXISTS ( SELECT 1 FROM chat_members diff --git a/internal/db/attachments.sql.go b/internal/db/attachments.sql.go index fb4e5e2..c14e39d 100644 --- a/internal/db/attachments.sql.go +++ b/internal/db/attachments.sql.go @@ -30,10 +30,10 @@ SELECT $3::uuid, $4::text, $5::text, - $6::text, + $6::attachment_content_type, $7::int, $8::text, - $9::text, + $9::attachment_status, $10::text WHERE EXISTS ( SELECT 1 FROM chat_members @@ -49,10 +49,10 @@ type CreateAttachmentParams struct { OwnerID uuid.UUID S3Key string Filename string - ContentType string + ContentType AttachmentContentType SizeBytes int Sha256 string - Status string + Status AttachmentStatus ErrorCode string } @@ -147,15 +147,15 @@ type ListAttachmentsByIDsParams struct { 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) if err != nil { return nil, err } defer rows.Close() - items := []string{} + items := []AttachmentStatus{} for rows.Next() { - var status string + var status AttachmentStatus if err := rows.Scan(&status); err != nil { return nil, err } diff --git a/internal/db/models.go b/internal/db/models.go index e54b10d..5d69de1 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -5,21 +5,109 @@ package db import ( + "database/sql/driver" + "fmt" "time" "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 { ID uuid.UUID ChatID uuid.UUID OwnerID uuid.UUID S3Key string Filename string - ContentType string + ContentType AttachmentContentType SizeBytes int Sha256 string - Status string + Status AttachmentStatus ErrorCode *string CreatedAt time.Time UpdatedAt time.Time diff --git a/internal/service/message.go b/internal/service/message.go index 2a44ff9..a993d59 100644 --- a/internal/service/message.go +++ b/internal/service/message.go @@ -31,7 +31,7 @@ type messageQueries interface { CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, 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) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error) ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error) @@ -79,7 +79,7 @@ func (s *MessageService) Create( return model.Message{}, ErrAttachmentNotFound } for _, status := range statuses { - if status != "ready" { + if status != db.AttachmentStatusReady { return model.Message{}, ErrAttachmentNotReady } }