refactor(db): use int for attachment size and add lookup query
This commit is contained in:
@@ -6,7 +6,7 @@ CREATE TABLE attachments (
|
|||||||
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 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}$'),
|
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||||
status text NOT NULL DEFAULT 'ready' CHECK (status = 'ready'),
|
status text NOT NULL DEFAULT 'ready' CHECK (status = 'ready'),
|
||||||
error_code text,
|
error_code text,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ SELECT
|
|||||||
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)::text,
|
||||||
sqlc.arg(size_bytes)::bigint,
|
sqlc.arg(size_bytes)::int,
|
||||||
sqlc.arg(sha256)::text,
|
sqlc.arg(sha256)::text,
|
||||||
sqlc.arg(status)::text,
|
sqlc.arg(status)::text,
|
||||||
sqlc.arg(error_code)::text
|
sqlc.arg(error_code)::text
|
||||||
@@ -40,6 +40,10 @@ INSERT INTO message_attachments (
|
|||||||
sqlc.arg(position)
|
sqlc.arg(position)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- name: GetAttachmentByID :one
|
||||||
|
SELECT * FROM attachments
|
||||||
|
WHERE id = $1::uuid;
|
||||||
|
|
||||||
-- name: ListAttachmentsByIDs :many
|
-- name: ListAttachmentsByIDs :many
|
||||||
SELECT status
|
SELECT status
|
||||||
FROM attachments
|
FROM attachments
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ SELECT
|
|||||||
$4::text,
|
$4::text,
|
||||||
$5::text,
|
$5::text,
|
||||||
$6::text,
|
$6::text,
|
||||||
$7::bigint,
|
$7::int,
|
||||||
$8::text,
|
$8::text,
|
||||||
$9::text,
|
$9::text,
|
||||||
$10::text
|
$10::text
|
||||||
@@ -50,7 +50,7 @@ type CreateAttachmentParams struct {
|
|||||||
S3Key string
|
S3Key string
|
||||||
Filename string
|
Filename string
|
||||||
ContentType string
|
ContentType string
|
||||||
SizeBytes int64
|
SizeBytes int
|
||||||
Sha256 string
|
Sha256 string
|
||||||
Status string
|
Status string
|
||||||
ErrorCode string
|
ErrorCode string
|
||||||
@@ -102,7 +102,7 @@ INSERT INTO message_attachments (
|
|||||||
type CreateMessageAttachmentParams struct {
|
type CreateMessageAttachmentParams struct {
|
||||||
MessageID uuid.UUID
|
MessageID uuid.UUID
|
||||||
AttachmentID uuid.UUID
|
AttachmentID uuid.UUID
|
||||||
Position int32
|
Position int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error {
|
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
|
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
|
const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many
|
||||||
SELECT status
|
SELECT status
|
||||||
FROM attachments
|
FROM attachments
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ type ListChatsAfterParams struct {
|
|||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
AfterCreatedAt time.Time
|
AfterCreatedAt time.Time
|
||||||
AfterID uuid.UUID
|
AfterID uuid.UUID
|
||||||
Lim int32
|
Lim int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListChatsAfterRow struct {
|
type ListChatsAfterRow struct {
|
||||||
@@ -196,7 +196,7 @@ LIMIT $2
|
|||||||
|
|
||||||
type ListChatsFirstPageParams struct {
|
type ListChatsFirstPageParams struct {
|
||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
Lim int32
|
Lim int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListChatsFirstPageRow struct {
|
type ListChatsFirstPageRow struct {
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ type ListMessagesAfterParams struct {
|
|||||||
ChatID uuid.UUID
|
ChatID uuid.UUID
|
||||||
AfterCreatedAt time.Time
|
AfterCreatedAt time.Time
|
||||||
AfterID uuid.UUID
|
AfterID uuid.UUID
|
||||||
Lim int32
|
Lim int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) ListMessagesAfter(ctx context.Context, arg ListMessagesAfterParams) ([]Message, error) {
|
func (q *Queries) ListMessagesAfter(ctx context.Context, arg ListMessagesAfterParams) ([]Message, error) {
|
||||||
@@ -115,7 +115,7 @@ LIMIT $2
|
|||||||
|
|
||||||
type ListMessagesFirstPageParams struct {
|
type ListMessagesFirstPageParams struct {
|
||||||
ChatID uuid.UUID
|
ChatID uuid.UUID
|
||||||
Lim int32
|
Lim int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) ListMessagesFirstPage(ctx context.Context, arg ListMessagesFirstPageParams) ([]Message, error) {
|
func (q *Queries) ListMessagesFirstPage(ctx context.Context, arg ListMessagesFirstPageParams) ([]Message, error) {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type Attachment struct {
|
|||||||
S3Key string
|
S3Key string
|
||||||
Filename string
|
Filename string
|
||||||
ContentType string
|
ContentType string
|
||||||
SizeBytes int64
|
SizeBytes int
|
||||||
Sha256 string
|
Sha256 string
|
||||||
Status string
|
Status string
|
||||||
ErrorCode *string
|
ErrorCode *string
|
||||||
@@ -57,7 +57,7 @@ type Message struct {
|
|||||||
type MessageAttachment struct {
|
type MessageAttachment struct {
|
||||||
MessageID uuid.UUID
|
MessageID uuid.UUID
|
||||||
AttachmentID uuid.UUID
|
AttachmentID uuid.UUID
|
||||||
Position int32
|
Position int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ func (s *ChatService) List(
|
|||||||
if cursor == nil {
|
if cursor == nil {
|
||||||
rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{
|
rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Lim: int32(limit + 1),
|
Lim: limit + 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -168,7 +168,7 @@ func (s *ChatService) List(
|
|||||||
UserID: userID,
|
UserID: userID,
|
||||||
AfterCreatedAt: decoded.CreatedAt,
|
AfterCreatedAt: decoded.CreatedAt,
|
||||||
AfterID: decoded.ID,
|
AfterID: decoded.ID,
|
||||||
Lim: int32(limit + 1),
|
Lim: limit + 1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ func (s *MessageService) Create(
|
|||||||
if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{
|
if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{
|
||||||
MessageID: id,
|
MessageID: id,
|
||||||
AttachmentID: attachmentID,
|
AttachmentID: attachmentID,
|
||||||
Position: int32(i),
|
Position: i,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return model.Message{}, err
|
return model.Message{}, err
|
||||||
}
|
}
|
||||||
@@ -210,7 +210,7 @@ func (s *MessageService) List(
|
|||||||
if cursor == nil {
|
if cursor == nil {
|
||||||
rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{
|
rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{
|
||||||
ChatID: chatID,
|
ChatID: chatID,
|
||||||
Lim: int32(limit + 1),
|
Lim: limit + 1,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
decoded, derr := decodeCursor(*cursor, chatID)
|
decoded, derr := decodeCursor(*cursor, chatID)
|
||||||
@@ -222,7 +222,7 @@ func (s *MessageService) List(
|
|||||||
ChatID: chatID,
|
ChatID: chatID,
|
||||||
AfterCreatedAt: decoded.CreatedAt,
|
AfterCreatedAt: decoded.CreatedAt,
|
||||||
AfterID: decoded.ID,
|
AfterID: decoded.ID,
|
||||||
Lim: int32(limit + 1),
|
Lim: limit + 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ sql:
|
|||||||
emit_empty_slices: true
|
emit_empty_slices: true
|
||||||
emit_pointers_for_null_types: true
|
emit_pointers_for_null_types: true
|
||||||
overrides:
|
overrides:
|
||||||
|
- db_type: "integer"
|
||||||
|
go_type: "int"
|
||||||
|
- db_type: "pg_catalog.int4"
|
||||||
|
go_type: "int"
|
||||||
- db_type: "uuid"
|
- db_type: "uuid"
|
||||||
go_type: "github.com/google/uuid.UUID"
|
go_type: "github.com/google/uuid.UUID"
|
||||||
- db_type: "uuid"
|
- db_type: "uuid"
|
||||||
|
|||||||
Reference in New Issue
Block a user