mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-08 01:16:45 +08:00
Deleting a comment removed every reply under it, through the parent_id ON DELETE CASCADE. Now delete removes only that comment: one with replies becomes a tombstone (deleted_at set, body/attachments/reactions/resolution cleared) so the replies keep their parent, and one without replies is removed along with any tombstone ancestor left empty.
Tombstones are read-only and are never agent input. Every write to a comment's children takes its owners first (issue -> comment -> child), and a comment commits together with the attachments it was posted with. Clients only promise that replies are kept when the server declares comment_delete_keep_replies_supported, and then delete through DELETE /api/comments/{id}/keep-replies, which older servers do not route.
1275 lines
37 KiB
Go
1275 lines
37 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: attachment.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const bindChatAttachmentsToMessage = `-- name: BindChatAttachmentsToMessage :many
|
|
UPDATE attachment
|
|
SET chat_message_id = $1
|
|
WHERE workspace_id = $2
|
|
AND task_id = $3
|
|
AND issue_id IS NULL
|
|
AND comment_id IS NULL
|
|
AND chat_message_id IS NULL
|
|
AND source_context_id IS NULL
|
|
RETURNING id
|
|
`
|
|
|
|
type BindChatAttachmentsToMessageParams struct {
|
|
ChatMessageID pgtype.UUID `json:"chat_message_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
}
|
|
|
|
// Bind a chat agent's task-scoped attachments to the assistant reply it just
|
|
// produced. Only rows still unclaimed by any owner (issue/comment/chat_message)
|
|
// are eligible, so an attachment already linked elsewhere is never stolen.
|
|
// Returns the bound ids for logging.
|
|
func (q *Queries) BindChatAttachmentsToMessage(ctx context.Context, arg BindChatAttachmentsToMessageParams) ([]pgtype.UUID, error) {
|
|
rows, err := q.db.Query(ctx, bindChatAttachmentsToMessage, arg.ChatMessageID, arg.WorkspaceID, arg.TaskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []pgtype.UUID{}
|
|
for rows.Next() {
|
|
var id pgtype.UUID
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const countUnboundChatAttachmentsForTask = `-- name: CountUnboundChatAttachmentsForTask :one
|
|
SELECT COUNT(*) FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND task_id = $2
|
|
AND issue_id IS NULL
|
|
AND comment_id IS NULL
|
|
AND chat_message_id IS NULL
|
|
AND source_context_id IS NULL
|
|
`
|
|
|
|
type CountUnboundChatAttachmentsForTaskParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
}
|
|
|
|
// How many attachments the agent produced for this chat task that are still
|
|
// unbound to any owner. Lets CompleteTask create an assistant message (and
|
|
// bind them) even when the agent's text output was empty but it uploaded files.
|
|
func (q *Queries) CountUnboundChatAttachmentsForTask(ctx context.Context, arg CountUnboundChatAttachmentsForTaskParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countUnboundChatAttachmentsForTask, arg.WorkspaceID, arg.TaskID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createAttachment = `-- name: CreateAttachment :one
|
|
WITH inserted AS (
|
|
INSERT INTO attachment (
|
|
id, workspace_id, issue_id, comment_id, chat_session_id, task_id, source_context_id,
|
|
uploader_type, uploader_id, filename, url, content_type, size_bytes
|
|
)
|
|
VALUES (
|
|
$1, $2, $9, $10, $11, $12, $13,
|
|
$3, $4, $5, $6, $7, $8
|
|
)
|
|
RETURNING id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id
|
|
), bumped_issue AS (
|
|
UPDATE issue
|
|
SET revision = revision + 1
|
|
WHERE id IN (SELECT issue_id FROM inserted WHERE issue_id IS NOT NULL)
|
|
RETURNING revision
|
|
), bumped_comment AS (
|
|
UPDATE comment
|
|
SET revision = revision + 1
|
|
WHERE id IN (SELECT comment_id FROM inserted WHERE comment_id IS NOT NULL)
|
|
RETURNING revision
|
|
)
|
|
SELECT inserted.id, inserted.workspace_id, inserted.issue_id, inserted.comment_id, inserted.uploader_type, inserted.uploader_id, inserted.filename, inserted.url, inserted.content_type, inserted.size_bytes, inserted.created_at, inserted.chat_session_id, inserted.chat_message_id, inserted.task_id, inserted.source_context_id,
|
|
COALESCE((SELECT revision FROM bumped_issue), 0)::bigint AS issue_revision,
|
|
COALESCE((SELECT revision FROM bumped_comment), 0)::bigint AS comment_revision
|
|
FROM inserted
|
|
`
|
|
|
|
type CreateAttachmentParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
UploaderType string `json:"uploader_type"`
|
|
UploaderID pgtype.UUID `json:"uploader_id"`
|
|
Filename string `json:"filename"`
|
|
Url string `json:"url"`
|
|
ContentType string `json:"content_type"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
ChatSessionID pgtype.UUID `json:"chat_session_id"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
SourceContextID pgtype.UUID `json:"source_context_id"`
|
|
}
|
|
|
|
type CreateAttachmentRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
UploaderType string `json:"uploader_type"`
|
|
UploaderID pgtype.UUID `json:"uploader_id"`
|
|
Filename string `json:"filename"`
|
|
Url string `json:"url"`
|
|
ContentType string `json:"content_type"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
ChatSessionID pgtype.UUID `json:"chat_session_id"`
|
|
ChatMessageID pgtype.UUID `json:"chat_message_id"`
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
SourceContextID pgtype.UUID `json:"source_context_id"`
|
|
IssueRevision int64 `json:"issue_revision"`
|
|
CommentRevision int64 `json:"comment_revision"`
|
|
}
|
|
|
|
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (CreateAttachmentRow, error) {
|
|
row := q.db.QueryRow(ctx, createAttachment,
|
|
arg.ID,
|
|
arg.WorkspaceID,
|
|
arg.UploaderType,
|
|
arg.UploaderID,
|
|
arg.Filename,
|
|
arg.Url,
|
|
arg.ContentType,
|
|
arg.SizeBytes,
|
|
arg.IssueID,
|
|
arg.CommentID,
|
|
arg.ChatSessionID,
|
|
arg.TaskID,
|
|
arg.SourceContextID,
|
|
)
|
|
var i CreateAttachmentRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
&i.IssueRevision,
|
|
&i.CommentRevision,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createSourceContextAttachment = `-- name: CreateSourceContextAttachment :one
|
|
INSERT INTO attachment (
|
|
id, workspace_id, source_context_id, uploader_type, uploader_id,
|
|
filename, url, content_type, size_bytes
|
|
) VALUES (
|
|
$1, $2, $3,
|
|
$4, $5, $6,
|
|
$7, $8, $9
|
|
)
|
|
RETURNING id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id
|
|
`
|
|
|
|
type CreateSourceContextAttachmentParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
SourceContextID pgtype.UUID `json:"source_context_id"`
|
|
UploaderType string `json:"uploader_type"`
|
|
UploaderID pgtype.UUID `json:"uploader_id"`
|
|
Filename string `json:"filename"`
|
|
Url string `json:"url"`
|
|
ContentType string `json:"content_type"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
}
|
|
|
|
func (q *Queries) CreateSourceContextAttachment(ctx context.Context, arg CreateSourceContextAttachmentParams) (Attachment, error) {
|
|
row := q.db.QueryRow(ctx, createSourceContextAttachment,
|
|
arg.ID,
|
|
arg.WorkspaceID,
|
|
arg.SourceContextID,
|
|
arg.UploaderType,
|
|
arg.UploaderID,
|
|
arg.Filename,
|
|
arg.Url,
|
|
arg.ContentType,
|
|
arg.SizeBytes,
|
|
)
|
|
var i Attachment
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteAttachment = `-- name: DeleteAttachment :one
|
|
WITH deleted AS (
|
|
DELETE FROM attachment
|
|
WHERE attachment.id = $1 AND attachment.workspace_id = $2
|
|
AND attachment.source_context_id IS NULL
|
|
RETURNING issue_id, comment_id
|
|
), bumped_issue AS (
|
|
UPDATE issue
|
|
SET revision = revision + 1
|
|
WHERE id IN (SELECT issue_id FROM deleted WHERE issue_id IS NOT NULL)
|
|
RETURNING revision
|
|
), bumped_comment AS (
|
|
UPDATE comment
|
|
SET revision = revision + 1
|
|
WHERE id IN (SELECT comment_id FROM deleted WHERE comment_id IS NOT NULL)
|
|
RETURNING revision
|
|
)
|
|
SELECT EXISTS(SELECT 1 FROM deleted) AS changed,
|
|
COALESCE((SELECT revision FROM bumped_issue), 0)::bigint AS issue_revision,
|
|
COALESCE((SELECT revision FROM bumped_comment), 0)::bigint AS comment_revision
|
|
`
|
|
|
|
type DeleteAttachmentParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
type DeleteAttachmentRow struct {
|
|
Changed bool `json:"changed"`
|
|
IssueRevision int64 `json:"issue_revision"`
|
|
CommentRevision int64 `json:"comment_revision"`
|
|
}
|
|
|
|
func (q *Queries) DeleteAttachment(ctx context.Context, arg DeleteAttachmentParams) (DeleteAttachmentRow, error) {
|
|
row := q.db.QueryRow(ctx, deleteAttachment, arg.ID, arg.WorkspaceID)
|
|
var i DeleteAttachmentRow
|
|
err := row.Scan(&i.Changed, &i.IssueRevision, &i.CommentRevision)
|
|
return i, err
|
|
}
|
|
|
|
const deleteAttachmentsBySourceContext = `-- name: DeleteAttachmentsBySourceContext :many
|
|
DELETE FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND source_context_id = $2
|
|
RETURNING id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id
|
|
`
|
|
|
|
type DeleteAttachmentsBySourceContextParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
SourceContextID pgtype.UUID `json:"source_context_id"`
|
|
}
|
|
|
|
func (q *Queries) DeleteAttachmentsBySourceContext(ctx context.Context, arg DeleteAttachmentsBySourceContextParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, deleteAttachmentsBySourceContext, arg.WorkspaceID, arg.SourceContextID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const deleteCommentAttachments = `-- name: DeleteCommentAttachments :many
|
|
DELETE FROM attachment
|
|
WHERE comment_id = $1 AND workspace_id = $2
|
|
RETURNING url
|
|
`
|
|
|
|
type DeleteCommentAttachmentsParams struct {
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
// Part of the comment delete transaction: removes the deleted comment's
|
|
// attachments and returns their storage URLs for cleanup after commit.
|
|
func (q *Queries) DeleteCommentAttachments(ctx context.Context, arg DeleteCommentAttachmentsParams) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, deleteCommentAttachments, arg.CommentID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var url string
|
|
if err := rows.Scan(&url); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, url)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const deleteSourceContextAttachmentsByWorkspace = `-- name: DeleteSourceContextAttachmentsByWorkspace :exec
|
|
DELETE FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND source_context_id IS NOT NULL
|
|
`
|
|
|
|
func (q *Queries) DeleteSourceContextAttachmentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteSourceContextAttachmentsByWorkspace, workspaceID)
|
|
return err
|
|
}
|
|
|
|
const detachAttachmentsFromUserChatMessageByTask = `-- name: DetachAttachmentsFromUserChatMessageByTask :many
|
|
UPDATE attachment
|
|
SET chat_message_id = NULL
|
|
WHERE chat_message_id IN (
|
|
SELECT id FROM chat_message WHERE chat_message.task_id = $1 AND role = 'user'
|
|
)
|
|
RETURNING id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id
|
|
`
|
|
|
|
// When an empty chat task is cancelled, its user message is deleted. The
|
|
// attachment FK is ON DELETE CASCADE, so without this the bound rows would be
|
|
// destroyed and a restored draft could never re-bind them. Detach first
|
|
// (chat_message_id -> NULL, keep chat_session_id) so the rows survive as
|
|
// workspace/session-scoped unattached attachments and re-send can re-link them.
|
|
func (q *Queries) DetachAttachmentsFromUserChatMessageByTask(ctx context.Context, taskID pgtype.UUID) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, detachAttachmentsFromUserChatMessageByTask, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getAttachment = `-- name: GetAttachment :one
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE id = $1 AND workspace_id = $2
|
|
`
|
|
|
|
type GetAttachmentParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) GetAttachment(ctx context.Context, arg GetAttachmentParams) (Attachment, error) {
|
|
row := q.db.QueryRow(ctx, getAttachment, arg.ID, arg.WorkspaceID)
|
|
var i Attachment
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAttachmentByIDOnly = `-- name: GetAttachmentByIDOnly :one
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE id = $1
|
|
`
|
|
|
|
// Used by the download endpoint, which derives workspace context from the
|
|
// attachment row itself rather than from request headers/query params. The
|
|
// caller still has to verify the requester is a member of the returned
|
|
// workspace_id before serving the bytes — this query is access-neutral on
|
|
// purpose so a self-contained URL like /api/attachments/{id}/download can
|
|
// work as a native <img>/<video> resource load (no header attachment).
|
|
func (q *Queries) GetAttachmentByIDOnly(ctx context.Context, id pgtype.UUID) (Attachment, error) {
|
|
row := q.db.QueryRow(ctx, getAttachmentByIDOnly, id)
|
|
var i Attachment
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const linkAttachmentsToChatMessage = `-- name: LinkAttachmentsToChatMessage :many
|
|
UPDATE attachment
|
|
SET chat_message_id = $1,
|
|
chat_session_id = $2
|
|
WHERE workspace_id = $3
|
|
AND issue_id IS NULL
|
|
AND comment_id IS NULL
|
|
AND chat_message_id IS NULL
|
|
AND source_context_id IS NULL
|
|
AND (
|
|
chat_session_id IS NULL
|
|
OR chat_session_id = $2
|
|
)
|
|
AND uploader_type = $4
|
|
AND uploader_id = $5
|
|
AND id = ANY($6::uuid[])
|
|
RETURNING id
|
|
`
|
|
|
|
type LinkAttachmentsToChatMessageParams struct {
|
|
ChatMessageID pgtype.UUID `json:"chat_message_id"`
|
|
ChatSessionID pgtype.UUID `json:"chat_session_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
UploaderType string `json:"uploader_type"`
|
|
UploaderID pgtype.UUID `json:"uploader_id"`
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
}
|
|
|
|
func (q *Queries) LinkAttachmentsToChatMessage(ctx context.Context, arg LinkAttachmentsToChatMessageParams) ([]pgtype.UUID, error) {
|
|
rows, err := q.db.Query(ctx, linkAttachmentsToChatMessage,
|
|
arg.ChatMessageID,
|
|
arg.ChatSessionID,
|
|
arg.WorkspaceID,
|
|
arg.UploaderType,
|
|
arg.UploaderID,
|
|
arg.AttachmentIds,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []pgtype.UUID{}
|
|
for rows.Next() {
|
|
var id pgtype.UUID
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const linkAttachmentsToComment = `-- name: LinkAttachmentsToComment :exec
|
|
UPDATE attachment
|
|
SET comment_id = $1
|
|
WHERE issue_id = $2
|
|
AND comment_id IS NULL
|
|
AND source_context_id IS NULL
|
|
AND id = ANY($3::uuid[])
|
|
`
|
|
|
|
type LinkAttachmentsToCommentParams struct {
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
Column3 []pgtype.UUID `json:"column_3"`
|
|
}
|
|
|
|
func (q *Queries) LinkAttachmentsToComment(ctx context.Context, arg LinkAttachmentsToCommentParams) error {
|
|
_, err := q.db.Exec(ctx, linkAttachmentsToComment, arg.CommentID, arg.IssueID, arg.Column3)
|
|
return err
|
|
}
|
|
|
|
const linkAttachmentsToIssue = `-- name: LinkAttachmentsToIssue :one
|
|
WITH linked AS (
|
|
UPDATE attachment
|
|
SET issue_id = $1
|
|
WHERE attachment.workspace_id = $2
|
|
AND attachment.issue_id IS NULL
|
|
AND attachment.source_context_id IS NULL
|
|
AND attachment.id = ANY($3::uuid[])
|
|
RETURNING attachment.issue_id
|
|
), bumped_issue AS (
|
|
UPDATE issue
|
|
SET revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
AND $4::boolean
|
|
AND EXISTS (SELECT 1 FROM linked)
|
|
RETURNING revision
|
|
)
|
|
SELECT COUNT(*)::bigint AS linked_count,
|
|
COALESCE((SELECT revision FROM bumped_issue), 0)::bigint AS issue_revision
|
|
FROM linked
|
|
`
|
|
|
|
type LinkAttachmentsToIssueParams struct {
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
BumpRevision bool `json:"bump_revision"`
|
|
}
|
|
|
|
type LinkAttachmentsToIssueRow struct {
|
|
LinkedCount int64 `json:"linked_count"`
|
|
IssueRevision int64 `json:"issue_revision"`
|
|
}
|
|
|
|
func (q *Queries) LinkAttachmentsToIssue(ctx context.Context, arg LinkAttachmentsToIssueParams) (LinkAttachmentsToIssueRow, error) {
|
|
row := q.db.QueryRow(ctx, linkAttachmentsToIssue,
|
|
arg.IssueID,
|
|
arg.WorkspaceID,
|
|
arg.AttachmentIds,
|
|
arg.BumpRevision,
|
|
)
|
|
var i LinkAttachmentsToIssueRow
|
|
err := row.Scan(&i.LinkedCount, &i.IssueRevision)
|
|
return i, err
|
|
}
|
|
|
|
const listAttachmentURLsByCommentID = `-- name: ListAttachmentURLsByCommentID :many
|
|
SELECT url FROM attachment
|
|
WHERE comment_id = $1
|
|
`
|
|
|
|
func (q *Queries) ListAttachmentURLsByCommentID(ctx context.Context, commentID pgtype.UUID) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentURLsByCommentID, commentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var url string
|
|
if err := rows.Scan(&url); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, url)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentURLsByIssueOrComments = `-- name: ListAttachmentURLsByIssueOrComments :many
|
|
SELECT a.url FROM attachment a
|
|
WHERE a.issue_id = $1
|
|
OR a.comment_id IN (SELECT c.id FROM comment c WHERE c.issue_id = $1)
|
|
`
|
|
|
|
func (q *Queries) ListAttachmentURLsByIssueOrComments(ctx context.Context, issueID pgtype.UUID) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentURLsByIssueOrComments, issueID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var url string
|
|
if err := rows.Scan(&url); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, url)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByChatMessage = `-- name: ListAttachmentsByChatMessage :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE chat_message_id = $1 AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByChatMessageParams struct {
|
|
ChatMessageID pgtype.UUID `json:"chat_message_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByChatMessage(ctx context.Context, arg ListAttachmentsByChatMessageParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByChatMessage, arg.ChatMessageID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByChatMessageIDs = `-- name: ListAttachmentsByChatMessageIDs :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE chat_message_id = ANY($1::uuid[]) AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByChatMessageIDsParams struct {
|
|
Column1 []pgtype.UUID `json:"column_1"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByChatMessageIDs(ctx context.Context, arg ListAttachmentsByChatMessageIDsParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByChatMessageIDs, arg.Column1, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByComment = `-- name: ListAttachmentsByComment :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE comment_id = $1 AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByCommentParams struct {
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByComment(ctx context.Context, arg ListAttachmentsByCommentParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByComment, arg.CommentID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByCommentIDs = `-- name: ListAttachmentsByCommentIDs :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE comment_id = ANY($1::uuid[]) AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByCommentIDsParams struct {
|
|
Column1 []pgtype.UUID `json:"column_1"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByCommentIDs(ctx context.Context, arg ListAttachmentsByCommentIDsParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByCommentIDs, arg.Column1, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE id = ANY($1::uuid[]) AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByIDsParams struct {
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.AttachmentIds, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsByIssue = `-- name: ListAttachmentsByIssue :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE issue_id = $1 AND workspace_id = $2
|
|
ORDER BY created_at ASC
|
|
`
|
|
|
|
type ListAttachmentsByIssueParams struct {
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsByIssue(ctx context.Context, arg ListAttachmentsByIssueParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsByIssue, arg.IssueID, arg.WorkspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listAttachmentsBySourceContext = `-- name: ListAttachmentsBySourceContext :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND source_context_id = $2
|
|
ORDER BY created_at ASC, id ASC
|
|
`
|
|
|
|
type ListAttachmentsBySourceContextParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
SourceContextID pgtype.UUID `json:"source_context_id"`
|
|
}
|
|
|
|
func (q *Queries) ListAttachmentsBySourceContext(ctx context.Context, arg ListAttachmentsBySourceContextParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listAttachmentsBySourceContext, arg.WorkspaceID, arg.SourceContextID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSourceContextAttachmentURLsByWorkspace = `-- name: ListSourceContextAttachmentURLsByWorkspace :many
|
|
SELECT url FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND source_context_id IS NOT NULL
|
|
ORDER BY id
|
|
`
|
|
|
|
func (q *Queries) ListSourceContextAttachmentURLsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listSourceContextAttachmentURLsByWorkspace, workspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var url string
|
|
if err := rows.Scan(&url); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, url)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSourceContextCommentAttachments = `-- name: ListSourceContextCommentAttachments :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND issue_id = $2
|
|
AND comment_id = ANY($3::uuid[])
|
|
AND source_context_id IS NULL
|
|
ORDER BY created_at ASC, id ASC
|
|
`
|
|
|
|
type ListSourceContextCommentAttachmentsParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
CommentIds []pgtype.UUID `json:"comment_ids"`
|
|
}
|
|
|
|
// The issue predicate is a defense-in-depth owner guard for damaged rows; the
|
|
// comment ids were already derived from the guarded thread history.
|
|
func (q *Queries) ListSourceContextCommentAttachments(ctx context.Context, arg ListSourceContextCommentAttachmentsParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listSourceContextCommentAttachments, arg.WorkspaceID, arg.IssueID, arg.CommentIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSourceContextIssueAttachments = `-- name: ListSourceContextIssueAttachments :many
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND issue_id = $2
|
|
AND comment_id IS NULL
|
|
AND source_context_id IS NULL
|
|
ORDER BY created_at ASC, id ASC
|
|
`
|
|
|
|
type ListSourceContextIssueAttachmentsParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
}
|
|
|
|
// Source snapshots keep issue-owned attachments separate from comment-owned
|
|
// attachments. In this schema a comment attachment retains issue_id, so the
|
|
// generic ListAttachmentsByIssue query is intentionally too broad here.
|
|
func (q *Queries) ListSourceContextIssueAttachments(ctx context.Context, arg ListSourceContextIssueAttachmentsParams) ([]Attachment, error) {
|
|
rows, err := q.db.Query(ctx, listSourceContextIssueAttachments, arg.WorkspaceID, arg.IssueID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Attachment{}
|
|
for rows.Next() {
|
|
var i Attachment
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockAttachmentRow = `-- name: LockAttachmentRow :one
|
|
SELECT id, workspace_id, issue_id, comment_id, uploader_type, uploader_id, filename, url, content_type, size_bytes, created_at, chat_session_id, chat_message_id, task_id, source_context_id FROM attachment
|
|
WHERE id = $1 AND workspace_id = $2
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockAttachmentRowParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
}
|
|
|
|
// Locks an attachment that has no owner to lock instead — a chat, avatar or
|
|
// still-unbound upload. Reading it under its own lock is what keeps it from
|
|
// gaining an owner between the read and the write, which would put the write
|
|
// back in the attachment -> issue order issue teardown deadlocks with.
|
|
func (q *Queries) LockAttachmentRow(ctx context.Context, arg LockAttachmentRowParams) (Attachment, error) {
|
|
row := q.db.QueryRow(ctx, lockAttachmentRow, arg.ID, arg.WorkspaceID)
|
|
var i Attachment
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.IssueID,
|
|
&i.CommentID,
|
|
&i.UploaderType,
|
|
&i.UploaderID,
|
|
&i.Filename,
|
|
&i.Url,
|
|
&i.ContentType,
|
|
&i.SizeBytes,
|
|
&i.CreatedAt,
|
|
&i.ChatSessionID,
|
|
&i.ChatMessageID,
|
|
&i.TaskID,
|
|
&i.SourceContextID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const lockAttachmentsForCommentLink = `-- name: LockAttachmentsForCommentLink :many
|
|
SELECT id FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND issue_id = $2
|
|
AND comment_id IS NULL
|
|
AND source_context_id IS NULL
|
|
AND id = ANY($3::uuid[])
|
|
ORDER BY id
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockAttachmentsForCommentLinkParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
}
|
|
|
|
// CreateComment binds attachments in the transaction that created the comment,
|
|
// after the CreateComment statement has taken the issue row: issue -> comment
|
|
// -> child, the order every owner-first mutation here uses. This pins the
|
|
// requested set under that lock and returns the ids still eligible, so the
|
|
// caller can refuse before the comment is committed when a requested
|
|
// attachment was deleted while the issue lock was contended.
|
|
func (q *Queries) LockAttachmentsForCommentLink(ctx context.Context, arg LockAttachmentsForCommentLinkParams) ([]pgtype.UUID, error) {
|
|
rows, err := q.db.Query(ctx, lockAttachmentsForCommentLink, arg.WorkspaceID, arg.IssueID, arg.AttachmentIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []pgtype.UUID{}
|
|
for rows.Next() {
|
|
var id pgtype.UUID
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockAttachmentsForIssueLink = `-- name: LockAttachmentsForIssueLink :many
|
|
SELECT id FROM attachment
|
|
WHERE workspace_id = $1
|
|
AND issue_id IS NULL
|
|
AND source_context_id IS NULL
|
|
AND id = ANY($2::uuid[])
|
|
ORDER BY id
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockAttachmentsForIssueLinkParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
}
|
|
|
|
// Issue updates bind attachments and then touch the owner row. Only rows that
|
|
// belong to no issue yet are eligible, and nothing reaches those through an
|
|
// issue — not teardown's cascade, not DeleteAttachment, which takes the owning
|
|
// issue first — so locking them before the owner cannot deadlock.
|
|
// Attachments that DO belong to the issue are locked after it; see
|
|
// LockAttachmentsForCommentLink.
|
|
func (q *Queries) LockAttachmentsForIssueLink(ctx context.Context, arg LockAttachmentsForIssueLinkParams) ([]pgtype.UUID, error) {
|
|
rows, err := q.db.Query(ctx, lockAttachmentsForIssueLink, arg.WorkspaceID, arg.AttachmentIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []pgtype.UUID{}
|
|
for rows.Next() {
|
|
var id pgtype.UUID
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const replaceCommentAttachments = `-- name: ReplaceCommentAttachments :execrows
|
|
UPDATE attachment
|
|
SET comment_id = CASE
|
|
WHEN id = ANY($3::uuid[]) THEN $1
|
|
ELSE NULL
|
|
END
|
|
WHERE issue_id = $2
|
|
AND source_context_id IS NULL
|
|
AND (
|
|
(comment_id = $1 AND NOT id = ANY($3::uuid[]))
|
|
OR (comment_id IS NULL AND id = ANY($3::uuid[]))
|
|
)
|
|
`
|
|
|
|
type ReplaceCommentAttachmentsParams struct {
|
|
CommentID pgtype.UUID `json:"comment_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
AttachmentIds []pgtype.UUID `json:"attachment_ids"`
|
|
}
|
|
|
|
func (q *Queries) ReplaceCommentAttachments(ctx context.Context, arg ReplaceCommentAttachmentsParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, replaceCommentAttachments, arg.CommentID, arg.IssueID, arg.AttachmentIds)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|