mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 19:13:38 +08:00
ae0625323a
Inab5361d69a
, we rescue from the PG error but the transaction is already aborted causing any DB query after to fail. As such, we avoid triggering the error in the first place by checking that we would not be insertin a negative number into the counter cache. Follow-up toab5361d69a
41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UserStatCountUpdater
|
|
class << self
|
|
def increment!(post, user_stat: nil)
|
|
update!(post, user_stat: user_stat)
|
|
end
|
|
|
|
def decrement!(post, user_stat: nil)
|
|
update!(post, user_stat: user_stat, action: :decrement!)
|
|
end
|
|
|
|
private
|
|
|
|
def update!(post, user_stat: nil, action: :increment!)
|
|
return if !post.topic
|
|
return if post.topic.private_message?
|
|
stat = user_stat || post.user.user_stat
|
|
|
|
column =
|
|
if post.is_first_post?
|
|
:topic_count
|
|
elsif post.post_type == Post.types[:regular]
|
|
:post_count
|
|
end
|
|
|
|
return if column.blank?
|
|
|
|
if action == :decrement! && stat.public_send(column) < 1
|
|
# There are still spots in the code base which results in the counter cache going out of sync. However,
|
|
# we have a job that runs on a daily basis which will correct the count. Therefore, we always check that we
|
|
# wouldn't end up with a negative count first before inserting.
|
|
Rails.logger.warn("Attempted to insert negative count into UserStat##{column}\n#{caller.join('\n')}")
|
|
return
|
|
end
|
|
|
|
stat.public_send(action, column)
|
|
end
|
|
end
|
|
end
|