FIX: We don't want to update the post read count and user stats if the post timing wasn't created due to a conflict. (#8824)

This commit is contained in:
Roman Rizzi 2020-01-31 10:23:24 -03:00 committed by GitHub
parent 73b04976e5
commit df43ac901d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -31,12 +31,14 @@ class PostTiming < ActiveRecord::Base
end
def self.record_new_timing(args)
DB.exec("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
row_count = DB.exec("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
SELECT :topic_id, :user_id, :post_number, :msecs
ON CONFLICT DO NOTHING",
args)
# concurrency is hard, we are not running serialized so this can possibly
# still happen, if it happens we just don't care, its an invalid record anyway
return if row_count == 0
Post.where(['topic_id = :topic_id and post_number = :post_number', args]).update_all 'reads = reads + 1'
UserStat.where(user_id: args[:user_id]).update_all 'posts_read_count = posts_read_count + 1'
end