This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon/db/migrate/20230215074425_move_emoji_r...

50 lines
1.4 KiB
Ruby
Raw Normal View History

2023-04-03 13:16:35 -07:00
# frozen_string_literal: true
class MoveEmojiReactionSettings < ActiveRecord::Migration[6.1]
class User < ApplicationRecord; end
MAPPING = {
setting_visible_reactions: 'visible_reactions',
}.freeze
class LegacySetting < ApplicationRecord
self.table_name = 'settings'
def var
self[:var]&.to_sym
end
def value
YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) if self[:value].present?
end
end
def up
User.find_in_batches do |users|
previous_settings_for_batch = LegacySetting.where(thing_type: 'User', thing_id: users.map(&:id)).group_by(&:thing_id)
2023-04-03 13:16:35 -07:00
users.each do |user|
previous_settings = previous_settings_for_batch[user.id]&.index_by(&:var) || {}
user_settings = Oj.load(user.settings || '{}')
user_settings.delete('theme')
2023-04-03 13:16:35 -07:00
MAPPING.each do |legacy_key, new_key|
value = previous_settings[legacy_key]&.value
2023-04-03 13:16:35 -07:00
next if value.blank?
2023-04-03 13:16:35 -07:00
if value.is_a?(Hash)
value.each do |nested_key, nested_value|
user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
end
else
user_settings[new_key] = value
2023-04-03 13:16:35 -07:00
end
end
user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
end
2023-04-03 13:16:35 -07:00
end
end
end