forked from mtusi2026/mtusi2026lab5
lab5: удаление неиспользуемых sqlc-запросов
Only the message_attachments queries remain after the legacy attachment repository was removed.
This commit is contained in:
@@ -7,10 +7,8 @@ package sqlcgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const attachToMessage = `-- name: AttachToMessage :exec
|
||||
@@ -29,273 +27,6 @@ func (q *Queries) AttachToMessage(ctx context.Context, arg AttachToMessageParams
|
||||
return err
|
||||
}
|
||||
|
||||
const createAttachment = `-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, status
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
'pending'
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
type CreateAttachmentRow struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (CreateAttachmentRow, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachment,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.ObjectKey,
|
||||
arg.Filename,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
)
|
||||
var i CreateAttachmentRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deletePendingAttachment = `-- name: DeletePendingAttachment :exec
|
||||
DELETE FROM attachments
|
||||
WHERE id = $1
|
||||
AND status = 'pending'
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePendingAttachment(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deletePendingAttachment, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAttachmentForMember = `-- name: GetAttachmentForMember :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = $1
|
||||
WHERE a.id = $2
|
||||
`
|
||||
|
||||
type GetAttachmentForMemberParams struct {
|
||||
UserID uuid.UUID
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
type GetAttachmentForMemberRow struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentForMember(ctx context.Context, arg GetAttachmentForMemberParams) (GetAttachmentForMemberRow, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentForMember, arg.UserID, arg.ID)
|
||||
var i GetAttachmentForMemberRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentForOwner = `-- name: GetAttachmentForOwner :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = a.owner_id
|
||||
WHERE a.id = $1
|
||||
AND a.owner_id = $2
|
||||
`
|
||||
|
||||
type GetAttachmentForOwnerParams struct {
|
||||
ID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
}
|
||||
|
||||
type GetAttachmentForOwnerRow struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentForOwner(ctx context.Context, arg GetAttachmentForOwnerParams) (GetAttachmentForOwnerRow, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentForOwner, arg.ID, arg.OwnerID)
|
||||
var i GetAttachmentForOwnerRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAttachmentKeys = `-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, status, created_at
|
||||
FROM attachments
|
||||
ORDER BY id
|
||||
LIMIT $2 OFFSET $1
|
||||
`
|
||||
|
||||
type ListAttachmentKeysParams struct {
|
||||
RowOffset int32
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type ListAttachmentKeysRow struct {
|
||||
ID uuid.UUID
|
||||
ObjectKey string
|
||||
Status string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentKeys(ctx context.Context, arg ListAttachmentKeysParams) ([]ListAttachmentKeysRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentKeys, arg.RowOffset, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentKeysRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentKeysRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ObjectKey,
|
||||
&i.Status,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listAttachmentStatesForChat = `-- name: ListAttachmentStatesForChat :many
|
||||
SELECT id, status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1
|
||||
AND id = ANY($2::uuid[])
|
||||
`
|
||||
|
||||
type ListAttachmentStatesForChatParams struct {
|
||||
ChatID uuid.UUID
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type ListAttachmentStatesForChatRow struct {
|
||||
ID uuid.UUID
|
||||
Status string
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentStatesForChat(ctx context.Context, arg ListAttachmentStatesForChatParams) ([]ListAttachmentStatesForChatRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentStatesForChat, arg.ChatID, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentStatesForChatRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentStatesForChatRow
|
||||
if err := rows.Scan(&i.ID, &i.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachments = `-- name: ListMessageAttachments :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
@@ -327,117 +58,3 @@ func (q *Queries) ListMessageAttachments(ctx context.Context, messageIds []uuid.
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listStalePendingAttachments = `-- name: ListStalePendingAttachments :many
|
||||
SELECT id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'pending'
|
||||
AND created_at < $1
|
||||
ORDER BY created_at
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListStalePendingAttachmentsParams struct {
|
||||
CreatedBefore time.Time
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type ListStalePendingAttachmentsRow struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListStalePendingAttachments(ctx context.Context, arg ListStalePendingAttachmentsParams) ([]ListStalePendingAttachmentsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStalePendingAttachments, arg.CreatedBefore, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListStalePendingAttachmentsRow{}
|
||||
for rows.Next() {
|
||||
var i ListStalePendingAttachmentsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markAttachmentReady = `-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
object_key = $1,
|
||||
updated_at = now()
|
||||
WHERE id = $2
|
||||
AND status = 'pending'
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
type MarkAttachmentReadyParams struct {
|
||||
ObjectKey string
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
type MarkAttachmentReadyRow struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) MarkAttachmentReady(ctx context.Context, arg MarkAttachmentReadyParams) (MarkAttachmentReadyRow, error) {
|
||||
row := q.db.QueryRow(ctx, markAttachmentReady, arg.ObjectKey, arg.ID)
|
||||
var i MarkAttachmentReadyRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -1,56 +1,3 @@
|
||||
-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, status
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(chat_id),
|
||||
sqlc.arg(owner_id),
|
||||
sqlc.arg(object_key),
|
||||
sqlc.arg(filename),
|
||||
sqlc.arg(content_type),
|
||||
sqlc.arg(size_bytes),
|
||||
sqlc.arg(sha256),
|
||||
'pending'
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at;
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
object_key = sqlc.arg(object_key),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND status = 'pending'
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at;
|
||||
|
||||
-- name: GetAttachmentForMember :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = sqlc.arg(user_id)
|
||||
WHERE a.id = sqlc.arg(id);
|
||||
|
||||
-- name: GetAttachmentForOwner :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = a.owner_id
|
||||
WHERE a.id = sqlc.arg(id)
|
||||
AND a.owner_id = sqlc.arg(owner_id);
|
||||
|
||||
-- name: ListAttachmentStatesForChat :many
|
||||
SELECT id, status
|
||||
FROM attachments
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND id = ANY(sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: AttachToMessage :exec
|
||||
INSERT INTO message_attachments (message_id, attachment_id, position)
|
||||
VALUES (sqlc.arg(message_id), sqlc.arg(attachment_id), sqlc.arg(position));
|
||||
@@ -60,23 +7,3 @@ SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
||||
ORDER BY message_id, position;
|
||||
|
||||
-- name: DeletePendingAttachment :exec
|
||||
DELETE FROM attachments
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND status = 'pending';
|
||||
|
||||
-- name: ListStalePendingAttachments :many
|
||||
SELECT id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'pending'
|
||||
AND created_at < sqlc.arg(created_before)
|
||||
ORDER BY created_at
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
|
||||
-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, status, created_at
|
||||
FROM attachments
|
||||
ORDER BY id
|
||||
LIMIT sqlc.arg(row_limit) OFFSET sqlc.arg(row_offset);
|
||||
|
||||
Reference in New Issue
Block a user