diff --git a/backend/src/main/resources/db/migration/V50__backfill_comment_audit_events.sql b/backend/src/main/resources/db/migration/V50__backfill_comment_audit_events.sql new file mode 100644 index 00000000..839682a1 --- /dev/null +++ b/backend/src/main/resources/db/migration/V50__backfill_comment_audit_events.sql @@ -0,0 +1,39 @@ +-- Backfill COMMENT_ADDED and MENTION_CREATED audit events for comments +-- created before audit logging was added in commit 428c63a2. +-- Without these rows the Chronik activity feed (which reads exclusively from +-- audit_log) cannot surface pre-existing comments in "Für dich" or "Alle". + +INSERT INTO audit_log (id, happened_at, actor_id, kind, document_id, payload) +SELECT + gen_random_uuid(), + c.created_at, + c.author_id, + 'COMMENT_ADDED', + c.document_id, + jsonb_build_object('commentId', c.id::text) +FROM document_comments c +WHERE NOT EXISTS ( + SELECT 1 FROM audit_log a + WHERE a.kind = 'COMMENT_ADDED' + AND a.payload->>'commentId' = c.id::text +); + +INSERT INTO audit_log (id, happened_at, actor_id, kind, document_id, payload) +SELECT + gen_random_uuid(), + c.created_at, + c.author_id, + 'MENTION_CREATED', + c.document_id, + jsonb_build_object( + 'commentId', c.id::text, + 'mentionedUserId', m.user_id::text + ) +FROM comment_mentions m +JOIN document_comments c ON c.id = m.comment_id +WHERE NOT EXISTS ( + SELECT 1 FROM audit_log a + WHERE a.kind = 'MENTION_CREATED' + AND a.payload->>'commentId' = c.id::text + AND a.payload->>'mentionedUserId' = m.user_id::text +);