From 5b837f620b05c5ec858accaf5b00f04466a4e357 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 5 Apr 2019 13:42:41 +1100 Subject: [PATCH] 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. --- app/models/user_stat.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 48940f948a2..e56ed2dba25 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -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