From 330c831ba4fb4c5a0034fbf3d493106d5bbc4954 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Mon, 29 Mar 2021 12:43:24 -0500 Subject: [PATCH] FIX: Prevent UniqueViolation exceptions when syncing group mentions (#12543) --- app/services/post_alerter.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index fc6c74bed88..c062bb6b448 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -156,9 +156,20 @@ class PostAlerter GroupMention.where(post_id: post.id).destroy_all return if mentioned_groups.blank? - mentioned_groups.each do |group| - GroupMention.create(post_id: post.id, group_id: group.id) - end + now = Time.zone.now + + # insert_all instead of insert_all! since multiple post_alert jobs might be + # running concurrently + GroupMention.insert_all( + mentioned_groups.map do |group| + { + post_id: post.id, + group_id: group.id, + created_at: now, + updated_at: now, + } + end + ) end def unread_posts(user, topic)