about summary refs log tree commit diff
path: root/db
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2023-03-31 21:37:57 +0200
committerClaire <claire.github-309c@sitedethib.com>2023-03-31 22:50:57 +0200
commit3dcba94e68889f3d6603fa5416d54edd08dcf4bd (patch)
treefdcfb11945858d17d2a3998eee99daa2cc800e64 /db
parent01d6f7529faef97c0209ef11bbca2e856961bbab (diff)
Migrate glitch-soc's exclusive user settings
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20230215074424_move_glitch_user_settings.rb57
-rw-r--r--db/schema.rb2
2 files changed, 58 insertions, 1 deletions
diff --git a/db/migrate/20230215074424_move_glitch_user_settings.rb b/db/migrate/20230215074424_move_glitch_user_settings.rb
new file mode 100644
index 000000000..6b5a25925
--- /dev/null
+++ b/db/migrate/20230215074424_move_glitch_user_settings.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+class MoveGlitchUserSettings < ActiveRecord::Migration[6.1]
+  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
+      YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) if self[:value].present?
+    end
+  end
+
+  def up
+    User.find_each do |user|
+      previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var)
+
+      user_settings = Oj.load(user.settings || '{}')
+      user_settings.delete('theme')
+
+      MAPPING.each do |legacy_key, new_key|
+        value = previous_settings[legacy_key]&.value
+
+        next if value.blank?
+
+        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
+        end
+      end
+
+      user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
+    end
+  end
+
+  def down; end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5b03e24f5..7d894b1aa 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2023_02_15_074423) do
+ActiveRecord::Schema.define(version: 2023_02_15_074424) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"