Use migration classes in migrations where current definition conflicts with older (#26390)

This commit is contained in:
Matt Jankowski 2023-08-09 05:26:42 -04:00 committed by GitHub
parent b12d75ef4f
commit 271d384fd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 21 deletions

View file

@ -3,6 +3,10 @@
class CopyAccountStats < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
class MigrationAccount < ApplicationRecord
self.table_name = :accounts
end
def up
safety_assured do
if supports_upsert?
@ -27,7 +31,7 @@ class CopyAccountStats < ActiveRecord::Migration[5.2]
def up_fast
say 'Upsert is available, importing counters using the fast method'
Account.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts|
MigrationAccount.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts|
execute <<-SQL.squish
INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at)
SELECT id, statuses_count, following_count, followers_count, created_at, updated_at
@ -44,7 +48,7 @@ class CopyAccountStats < ActiveRecord::Migration[5.2]
# We cannot use bulk INSERT or overarching transactions here because of possible
# uniqueness violations that we need to skip over
Account.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account|
MigrationAccount.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account|
params = [account.id, account[:statuses_count], account[:following_count], account[:followers_count], account.created_at, account.updated_at]
exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params)
rescue ActiveRecord::RecordNotUnique