refactor(db): use int for attachment size and add lookup query

This commit is contained in:
kk10
2026-11-02 21:16:58 +03:00
parent 45c9f7731d
commit 983bbaa55e
9 changed files with 49 additions and 16 deletions
+1 -1
View File
@@ -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,
+5 -1
View File
@@ -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
+28 -3
View File
@@ -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
+2 -2
View File
@@ -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 {
+2 -2
View File
@@ -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) {
+2 -2
View File
@@ -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 {
+2 -2
View File
@@ -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
+3 -3
View File
@@ -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 {
+4
View File
@@ -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"