From edb4e54df2fd1b8323b81cdb986c40cff0482fce Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 21 Apr 2026 12:08:45 +0200 Subject: [PATCH] fix(audit): backfill COMMENT_ADDED and MENTION_CREATED events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Comments created before audit logging was added in 428c63a2 have no corresponding audit_log rows, so the Chronik activity feed (which reads exclusively from audit_log) cannot surface them in "Alle" or "Für dich", even though the fix from #295 is wired up correctly. V50 inserts the missing events idempotently from document_comments and comment_mentions. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../V50__backfill_comment_audit_events.sql | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 backend/src/main/resources/db/migration/V50__backfill_comment_audit_events.sql 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 +);