Skip to content
Snippets Groups Projects
Unverified Commit edb1de78 authored by Eugen Rochko's avatar Eugen Rochko Committed by GitHub
Browse files

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

parent d010816b
No related branches found
No related tags found
No related merge requests found
...@@ -3,13 +3,16 @@ class CopyStatusStats < ActiveRecord::Migration[5.2] ...@@ -3,13 +3,16 @@ class CopyStatusStats < ActiveRecord::Migration[5.2]
def up def up
safety_assured do safety_assured do
execute <<-SQL.squish Status.where.not(id: StatusStat.select('status_id')).select('id').find_in_batches do |statuses|
INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at) execute <<-SQL.squish
SELECT id, reblogs_count, favourites_count, created_at, updated_at INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at)
FROM statuses SELECT id, reblogs_count, favourites_count, created_at, updated_at
ON CONFLICT (status_id) DO UPDATE FROM statuses
SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count WHERE id IN (#{statuses.map(&:id).join(', ')})
SQL ON CONFLICT (status_id) DO UPDATE
SET reblogs_count = EXCLUDED.reblogs_count, favourites_count = EXCLUDED.favourites_count
SQL
end
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment