about summary refs log tree commit diff
path: root/app/models/account_stat.rb
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-10-05 18:04:36 +0200
committerGitHub <noreply@github.com>2019-10-05 18:04:36 +0200
commit3921125e5578fb3871fdcae0e8e8a77179f1ad72 (patch)
tree997c8ed2e7a6b8152266edc0c37ea8264c59d1ab /app/models/account_stat.rb
parent857c67f31b23b9c496e07eda41755ba449a37f17 (diff)
parentbc8543d9af0045fe2175f7e2234da9c77dc55023 (diff)
Merge pull request #1228 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/models/account_stat.rb')
-rw-r--r--app/models/account_stat.rb24
1 files changed, 23 insertions, 1 deletions
diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb
index 6d1097cec..c84e4217c 100644
--- a/app/models/account_stat.rb
+++ b/app/models/account_stat.rb
@@ -11,19 +11,36 @@
 #  created_at      :datetime         not null
 #  updated_at      :datetime         not null
 #  last_status_at  :datetime
+#  lock_version    :integer          default(0), not null
 #
 
 class AccountStat < ApplicationRecord
   belongs_to :account, inverse_of: :account_stat
 
-  update_index('accounts#account', :account) if Chewy.enabled?
+  update_index('accounts#account', :account)
 
   def increment_count!(key)
     update(attributes_for_increment(key))
+  rescue ActiveRecord::StaleObjectError
+    begin
+      reload_with_id
+    rescue ActiveRecord::RecordNotFound
+      # Nothing to do
+    else
+      retry
+    end
   end
 
   def decrement_count!(key)
     update(key => [public_send(key) - 1, 0].max)
+  rescue ActiveRecord::StaleObjectError
+    begin
+      reload_with_id
+    rescue ActiveRecord::RecordNotFound
+      # Nothing to do
+    else
+      retry
+    end
   end
 
   private
@@ -33,4 +50,9 @@ class AccountStat < ApplicationRecord
     attrs[:last_status_at] = Time.now.utc if key == :statuses_count
     attrs
   end
+
+  def reload_with_id
+    self.id = find_by!(account: account).id if new_record?
+    reload
+  end
 end