Files
Bohan Jiang a9e82c7973 MUL-7282: fix(comments): delete only the selected comment and keep its replies (#8323)
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.
2026-09-13 19:13:43 +08:00

48 lines
1.5 KiB
SQL

-- name: AddReaction :one
WITH inserted AS (
INSERT INTO comment_reaction (comment_id, workspace_id, actor_type, actor_id, emoji)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (comment_id, actor_type, actor_id, emoji) DO NOTHING
RETURNING *
), bumped AS (
UPDATE comment
SET revision = revision + 1
WHERE id IN (SELECT comment_id FROM inserted)
RETURNING revision
)
SELECT reaction.*, COALESCE((SELECT revision FROM bumped), 0)::bigint AS comment_revision
FROM inserted reaction
UNION ALL
SELECT reaction.*, 0::bigint AS comment_revision
FROM comment_reaction reaction
WHERE reaction.comment_id = $1
AND reaction.actor_type = $3
AND reaction.actor_id = $4
AND reaction.emoji = $5
AND NOT EXISTS (SELECT 1 FROM inserted)
LIMIT 1;
-- name: RemoveReaction :one
WITH deleted AS (
DELETE FROM comment_reaction
WHERE comment_id = $1 AND actor_type = $2 AND actor_id = $3 AND emoji = $4
RETURNING comment_id
), bumped AS (
UPDATE comment
SET revision = revision + 1
WHERE id IN (SELECT comment_id FROM deleted)
RETURNING revision
)
SELECT EXISTS(SELECT 1 FROM deleted) AS changed,
COALESCE((SELECT revision FROM bumped), 0)::bigint AS comment_revision;
-- name: ListReactionsByCommentIDs :many
SELECT * FROM comment_reaction
WHERE comment_id = ANY($1::uuid[])
ORDER BY created_at ASC;
-- name: DeleteCommentReactions :exec
-- Part of the comment delete transaction: reactions go with the comment.
DELETE FROM comment_reaction
WHERE comment_id = @comment_id AND workspace_id = @workspace_id;