From 983bbaa55ecd38ea40a92a7636e02a52b6e13819 Mon Sep 17 00:00:00 2001 From: kk10 Date: Mon, 2 Nov 2026 21:16:58 +0300 Subject: [PATCH] refactor(db): use int for attachment size and add lookup query --- db/migrations/00003_attachments.sql | 2 +- db/queries/attachments.sql | 6 +++++- internal/db/attachments.sql.go | 31 ++++++++++++++++++++++++++--- internal/db/chats.sql.go | 4 ++-- internal/db/messages.sql.go | 4 ++-- internal/db/models.go | 4 ++-- internal/service/chat.go | 4 ++-- internal/service/message.go | 6 +++--- sqlc.yaml | 4 ++++ 9 files changed, 49 insertions(+), 16 deletions(-) diff --git a/db/migrations/00003_attachments.sql b/db/migrations/00003_attachments.sql index e777337..118c286 100644 --- a/db/migrations/00003_attachments.sql +++ b/db/migrations/00003_attachments.sql @@ -6,7 +6,7 @@ CREATE TABLE attachments ( s3_key text NOT NULL UNIQUE, filename text NOT NULL, content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), - size_bytes bigint 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}$'), status text NOT NULL DEFAULT 'ready' CHECK (status = 'ready'), error_code text, diff --git a/db/queries/attachments.sql b/db/queries/attachments.sql index 5b9495b..4d011db 100644 --- a/db/queries/attachments.sql +++ b/db/queries/attachments.sql @@ -18,7 +18,7 @@ SELECT sqlc.arg(s3_key)::text, sqlc.arg(filename)::text, sqlc.arg(content_type)::text, - sqlc.arg(size_bytes)::bigint, + sqlc.arg(size_bytes)::int, sqlc.arg(sha256)::text, sqlc.arg(status)::text, sqlc.arg(error_code)::text @@ -40,6 +40,10 @@ INSERT INTO message_attachments ( sqlc.arg(position) ); +-- name: GetAttachmentByID :one +SELECT * FROM attachments +WHERE id = $1::uuid; + -- name: ListAttachmentsByIDs :many SELECT status FROM attachments diff --git a/internal/db/attachments.sql.go b/internal/db/attachments.sql.go index 85ebc97..fb4e5e2 100644 --- a/internal/db/attachments.sql.go +++ b/internal/db/attachments.sql.go @@ -31,7 +31,7 @@ SELECT $4::text, $5::text, $6::text, - $7::bigint, + $7::int, $8::text, $9::text, $10::text @@ -50,7 +50,7 @@ type CreateAttachmentParams struct { S3Key string Filename string ContentType string - SizeBytes int64 + SizeBytes int Sha256 string Status string ErrorCode string @@ -102,7 +102,7 @@ INSERT INTO message_attachments ( type CreateMessageAttachmentParams struct { MessageID uuid.UUID AttachmentID uuid.UUID - Position int32 + Position int } func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error { @@ -110,6 +110,31 @@ func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessage 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 +` + +func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) { + row := q.db.QueryRow(ctx, getAttachmentByID, 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 +} + const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many SELECT status FROM attachments diff --git a/internal/db/chats.sql.go b/internal/db/chats.sql.go index c061eaf..c2794cc 100644 --- a/internal/db/chats.sql.go +++ b/internal/db/chats.sql.go @@ -139,7 +139,7 @@ type ListChatsAfterParams struct { UserID uuid.UUID AfterCreatedAt time.Time AfterID uuid.UUID - Lim int32 + Lim int } type ListChatsAfterRow struct { @@ -196,7 +196,7 @@ LIMIT $2 type ListChatsFirstPageParams struct { UserID uuid.UUID - Lim int32 + Lim int } type ListChatsFirstPageRow struct { diff --git a/internal/db/messages.sql.go b/internal/db/messages.sql.go index c599fb0..cb579fd 100644 --- a/internal/db/messages.sql.go +++ b/internal/db/messages.sql.go @@ -72,7 +72,7 @@ type ListMessagesAfterParams struct { ChatID uuid.UUID AfterCreatedAt time.Time AfterID uuid.UUID - Lim int32 + Lim int } func (q *Queries) ListMessagesAfter(ctx context.Context, arg ListMessagesAfterParams) ([]Message, error) { @@ -115,7 +115,7 @@ LIMIT $2 type ListMessagesFirstPageParams struct { ChatID uuid.UUID - Lim int32 + Lim int } func (q *Queries) ListMessagesFirstPage(ctx context.Context, arg ListMessagesFirstPageParams) ([]Message, error) { diff --git a/internal/db/models.go b/internal/db/models.go index c02477d..e54b10d 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -17,7 +17,7 @@ type Attachment struct { S3Key string Filename string ContentType string - SizeBytes int64 + SizeBytes int Sha256 string Status string ErrorCode *string @@ -57,7 +57,7 @@ type Message struct { type MessageAttachment struct { MessageID uuid.UUID AttachmentID uuid.UUID - Position int32 + Position int } type Session struct { diff --git a/internal/service/chat.go b/internal/service/chat.go index 15b1d36..c00caa8 100644 --- a/internal/service/chat.go +++ b/internal/service/chat.go @@ -143,7 +143,7 @@ func (s *ChatService) List( if cursor == nil { rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{ UserID: userID, - Lim: int32(limit + 1), + Lim: limit + 1, }) if err != nil { return nil, nil, err @@ -168,7 +168,7 @@ func (s *ChatService) List( UserID: userID, AfterCreatedAt: decoded.CreatedAt, AfterID: decoded.ID, - Lim: int32(limit + 1), + Lim: limit + 1, }) if err != nil { return nil, nil, err diff --git a/internal/service/message.go b/internal/service/message.go index 7a723e8..2a44ff9 100644 --- a/internal/service/message.go +++ b/internal/service/message.go @@ -167,7 +167,7 @@ func (s *MessageService) Create( if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{ MessageID: id, AttachmentID: attachmentID, - Position: int32(i), + Position: i, }); err != nil { return model.Message{}, err } @@ -210,7 +210,7 @@ func (s *MessageService) List( if cursor == nil { rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{ ChatID: chatID, - Lim: int32(limit + 1), + Lim: limit + 1, }) } else { decoded, derr := decodeCursor(*cursor, chatID) @@ -222,7 +222,7 @@ func (s *MessageService) List( ChatID: chatID, AfterCreatedAt: decoded.CreatedAt, AfterID: decoded.ID, - Lim: int32(limit + 1), + Lim: limit + 1, }) } if err != nil { diff --git a/sqlc.yaml b/sqlc.yaml index 0e83411..8743b61 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -11,6 +11,10 @@ sql: emit_empty_slices: true emit_pointers_for_null_types: true overrides: + - db_type: "integer" + go_type: "int" + - db_type: "pg_catalog.int4" + go_type: "int" - db_type: "uuid" go_type: "github.com/google/uuid.UUID" - db_type: "uuid"