about summary refs log tree commit diff
path: root/lib/tasks
diff options
context:
space:
mode:
authorshel <Yiskah.Raphen@gmail.com>2017-03-31 10:34:14 -0400
committerGitHub <noreply@github.com>2017-03-31 10:34:14 -0400
commitf20f6b25b98efffca47caae713f8dc5e1d0d4bd1 (patch)
tree104152d4bd6f9889f38b2f46fee108185c3b29f7 /lib/tasks
parent76188d61f2c3bb5747208df92835e912db18d7b6 (diff)
parent680f9efe9c4aa7fce1f4dd6a35ef4aca7a80c1f3 (diff)
Merge branch 'master' into patch-2
Diffstat (limited to 'lib/tasks')
-rw-r--r--lib/tasks/mastodon.rake19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake
index bb10410b5..79dcb722a 100644
--- a/lib/tasks/mastodon.rake
+++ b/lib/tasks/mastodon.rake
@@ -62,4 +62,23 @@ namespace :mastodon do
       end
     end
   end
+
+  namespace :maintenance do
+    desc 'Update counter caches'
+    task update_counter_caches: :environment do
+      Rails.logger.debug 'Updating counter caches for accounts...'
+
+      Account.unscoped.select('id').find_in_batches do |batch|
+        Account.where(id: batch.map(&:id)).update_all('statuses_count = (select count(*) from statuses where account_id = accounts.id), followers_count = (select count(*) from follows where target_account_id = accounts.id), following_count = (select count(*) from follows where account_id = accounts.id)')
+      end
+
+      Rails.logger.debug 'Updating counter caches for statuses...'
+
+      Status.unscoped.select('id').find_in_batches do |batch|
+        Status.where(id: batch.map(&:id)).update_all('favourites_count = (select count(*) from favourites where favourites.status_id = statuses.id), reblogs_count = (select count(*) from statuses as reblogs where reblogs.reblog_of_id = statuses.id)')
+      end
+
+      Rails.logger.debug 'Done!'
+    end
+  end
 end