correct unread resetting to handle nulls

Note, to avoid race conditions we are setting last_unread to 10 minutes ago
if there is nothing unread.

This is safer in case of in progress transactions
we don't want to lose unread for any window of time.
This commit is contained in:
Sam Saffron 2019-04-05 13:42:41 +11:00
parent 5f896ae8f7
commit 5b837f620b
1 changed files with 2 additions and 2 deletions

View File

@ -11,9 +11,9 @@ class UserStat < ActiveRecord::Base
end
def self.update_first_unread(last_seen, limit: 10_000)
DB.exec(<<~SQL, min_date: last_seen, limit: limit)
DB.exec(<<~SQL, min_date: last_seen, limit: limit, now: 10.minutes.ago)
UPDATE user_stats us
SET first_unread_at = Y.min_date
SET first_unread_at = COALESCE(Y.min_date, :now)
FROM (
SELECT u1.id user_id,
X.min min_date