mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 01:16:38 -06:00
FIX: handle concurrently creating post reply keys
In some very rare conditions this would be called concurrently and fail
This commit is contained in:
parent
8fa5dd4a1f
commit
ca5a6f0a9d
@ -262,7 +262,8 @@ module Email
|
||||
post_id &&
|
||||
header_value(Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER).present?
|
||||
|
||||
reply_key = PostReplyKey.find_or_create_by!(
|
||||
# use safe variant here cause we tend to see concurrency issue
|
||||
reply_key = PostReplyKey.find_or_create_by_safe!(
|
||||
post_id: post_id,
|
||||
user_id: user_id
|
||||
).reply_key
|
||||
|
@ -1,5 +1,17 @@
|
||||
class ActiveRecord::Base
|
||||
|
||||
# Handle PG::UniqueViolation as well due to concurrency
|
||||
# find_or_create does find_by(hash) || create!(hash)
|
||||
# in some cases find will not find and multiple creates will be called
|
||||
def self.find_or_create_by_safe!(hash)
|
||||
begin
|
||||
find_or_create_by!(hash)
|
||||
rescue PG::UniqueViolation
|
||||
# try again cause another transaction could have passed by now
|
||||
find_or_create_by!(hash)
|
||||
end
|
||||
end
|
||||
|
||||
# Execute SQL manually
|
||||
def self.exec_sql(*args)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user