about summary refs log tree commit diff
path: root/db
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-08-18 03:44:50 +0200
committerGitHub <noreply@github.com>2018-08-18 03:44:50 +0200
commitedb1de780060a0d62bc57153d743002c0a8bde67 (patch)
tree467112b6e544efb46ae011283c3732eb2f3a7b13 /db
parentd010816ba876bf4f749749e9d7432a843e8a1ff9 (diff)
Make CopyStatusStats migration use batches of 1000 to avoid locks (#8256)
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20180812173710_copy_status_stats.rb17
1 files changed, 10 insertions, 7 deletions
diff --git a/db/migrate/20180812173710_copy_status_stats.rb b/db/migrate/20180812173710_copy_status_stats.rb
index 64a564ca0..0c5907c30 100644
--- a/db/migrate/20180812173710_copy_status_stats.rb
+++ b/db/migrate/20180812173710_copy_status_stats.rb
@@ -3,13 +3,16 @@ class CopyStatusStats < ActiveRecord::Migration[5.2]
 
   def up
     safety_assured do
-      execute <<-SQL.squish
-        INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
-        SELECT id, reblogs_count, favourites_count, created_at, updated_at
-        FROM statuses
-        ON CONFLICT (status_id) DO UPDATE
-        SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
-      SQL
+      Status.where.not(id: StatusStat.select('status_id')).select('id').find_in_batches do |statuses|
+        execute <<-SQL.squish
+          INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
+          SELECT id, reblogs_count, favourites_count, created_at, updated_at
+          FROM statuses
+          WHERE id IN (#{statuses.map(&:id).join(', ')})
+          ON CONFLICT (status_id) DO UPDATE
+          SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
+        SQL
+      end
     end
   end