Skip to content

Commit

Permalink
Make CopyStatusStats migration use batches of 1000 to avoid locks (#8256
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Gargron committed Aug 18, 2018
1 parent d010816 commit edb1de7
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions db/migrate/20180812173710_copy_status_stats.rb
Expand Up @@ -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

Expand Down

0 comments on commit edb1de7

Please sign in to comment.