[Glitch] Fix n+1 query in settings migration
Port 0ad2413b35
to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
75e28731a5
commit
8bf9bd5ac8
|
@ -31,27 +31,30 @@ class MoveGlitchUserSettings < ActiveRecord::Migration[6.1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
User.find_each do |user|
|
User.find_in_batches do |users|
|
||||||
previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var)
|
previous_settings_for_batch = LegacySetting.where(thing_type: 'User', thing_id: users.map(&:id)).group_by(&:thing_id)
|
||||||
|
|
||||||
user_settings = Oj.load(user.settings || '{}')
|
users.each do |user|
|
||||||
user_settings.delete('theme')
|
previous_settings = previous_settings_for_batch[user.id]&.index_by(&:var) || {}
|
||||||
|
user_settings = Oj.load(user.settings || '{}')
|
||||||
|
user_settings.delete('theme')
|
||||||
|
|
||||||
MAPPING.each do |legacy_key, new_key|
|
MAPPING.each do |legacy_key, new_key|
|
||||||
value = previous_settings[legacy_key]&.value
|
value = previous_settings[legacy_key]&.value
|
||||||
|
|
||||||
next if value.blank?
|
next if value.blank?
|
||||||
|
|
||||||
if value.is_a?(Hash)
|
if value.is_a?(Hash)
|
||||||
value.each do |nested_key, nested_value|
|
value.each do |nested_key, nested_value|
|
||||||
user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
|
user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
|
||||||
|
end
|
||||||
|
else
|
||||||
|
user_settings[new_key] = value
|
||||||
end
|
end
|
||||||
else
|
|
||||||
user_settings[new_key] = value
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
|
user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Reference in New Issue