2023-03-31 22:37:57 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MoveGlitchUserSettings < ActiveRecord::Migration[6.1]
|
2023-05-01 18:45:18 +03:00
|
|
|
disable_ddl_transaction!
|
|
|
|
|
2023-03-31 22:37:57 +03:00
|
|
|
class User < ApplicationRecord; end
|
|
|
|
|
|
|
|
MAPPING = {
|
|
|
|
favourite_modal: 'web.favourite_modal',
|
|
|
|
system_emoji_font: 'web.use_system_emoji_font',
|
|
|
|
hide_followers_count: 'hide_followers_count',
|
|
|
|
default_content_type: 'default_content_type',
|
|
|
|
flavour: 'flavour',
|
|
|
|
skin: 'skin',
|
|
|
|
notification_emails: {
|
|
|
|
trending_link: 'notification_emails.link_trends',
|
|
|
|
trending_status: 'notification_emails.status_trends',
|
|
|
|
}.freeze,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
class LegacySetting < ApplicationRecord
|
|
|
|
self.table_name = 'settings'
|
|
|
|
|
|
|
|
def var
|
|
|
|
self[:var]&.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
2023-04-09 15:48:34 +03:00
|
|
|
YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
|
2023-03-31 22:37:57 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def up
|
2023-05-02 11:26:39 +03:00
|
|
|
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-03-31 22:37:57 +03:00
|
|
|
|
2023-05-02 11:26:39 +03: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-03-31 22:37:57 +03:00
|
|
|
|
2023-05-02 11:26:39 +03:00
|
|
|
MAPPING.each do |legacy_key, new_key|
|
|
|
|
value = previous_settings[legacy_key]&.value
|
2023-03-31 22:37:57 +03:00
|
|
|
|
2023-09-21 16:17:44 +03:00
|
|
|
next if value.nil?
|
2023-03-31 22:37:57 +03:00
|
|
|
|
2023-05-02 11:26:39 +03: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-03-31 22:37:57 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-21 20:25:12 +02:00
|
|
|
user.update_column('settings', Oj.dump(user_settings))
|
2023-05-02 11:26:39 +03:00
|
|
|
end
|
2023-03-31 22:37:57 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down; end
|
|
|
|
end
|