Make CopyStatusStats migration use batches of 1000 to avoid locks (#8256)

master
Eugen Rochko 6 years ago committed by GitHub
parent d010816ba8
commit edb1de7800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      db/migrate/20180812173710_copy_status_stats.rb

@ -3,15 +3,18 @@ class CopyStatusStats < ActiveRecord::Migration[5.2]
def up def up
safety_assured do safety_assured do
Status.where.not(id: StatusStat.select('status_id')).select('id').find_in_batches do |statuses|
execute <<-SQL.squish execute <<-SQL.squish
INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at) INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
SELECT id, reblogs_count, favourites_count, created_at, updated_at SELECT id, reblogs_count, favourites_count, created_at, updated_at
FROM statuses FROM statuses
WHERE id IN (#{statuses.map(&:id).join(', ')})
ON CONFLICT (status_id) DO UPDATE ON CONFLICT (status_id) DO UPDATE
SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
SQL SQL
end end
end end
end
def down def down
# Nothing # Nothing

Loading…
Cancel
Save