Fix n+1 query in settings migration (#24783)

local
Eugen Rochko 1 year ago committed by GitHub
parent f1c1fa1411
commit 0ad2413b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      db/migrate/20230215074423_move_user_settings.rb

@ -59,9 +59,11 @@ class MoveUserSettings < ActiveRecord::Migration[6.1]
end
def up
User.find_each do |user|
previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var)
User.find_in_batches do |users|
previous_settings_for_batch = LegacySetting.where(thing_type: 'User', thing_id: users.map(&:id)).group_by(&:thing_id)
users.each do |user|
previous_settings = previous_settings_for_batch[user.id]&.index_by(&:var) || {}
user_settings = {}
MAPPING.each do |legacy_key, new_key|
@ -81,6 +83,7 @@ class MoveUserSettings < ActiveRecord::Migration[6.1]
user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
end
end
end
def down; end
end

Loading…
Cancel
Save