about summary refs log tree commit diff
path: root/db/migrate/20220304195405_migrate_hide_network_preference.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-03-08 22:40:21 +0100
committerGitHub <noreply@github.com>2022-03-08 22:40:21 +0100
commit02133866e6915e37431298b396e1aded1e4c44c5 (patch)
tree4ec43c5d1269ef7d5a3816a4d000bb7129bf81bd /db/migrate/20220304195405_migrate_hide_network_preference.rb
parentf03148f441d8dfc1856451c4faa00b5e26b6e199 (diff)
parent481f7c8c3850a5d38e92222ab14e5229c49c2812 (diff)
Merge pull request #1713 from ClearlyClaire/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'db/migrate/20220304195405_migrate_hide_network_preference.rb')
-rw-r--r--db/migrate/20220304195405_migrate_hide_network_preference.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/db/migrate/20220304195405_migrate_hide_network_preference.rb b/db/migrate/20220304195405_migrate_hide_network_preference.rb
new file mode 100644
index 000000000..102ee46d6
--- /dev/null
+++ b/db/migrate/20220304195405_migrate_hide_network_preference.rb
@@ -0,0 +1,37 @@
+class MigrateHideNetworkPreference < ActiveRecord::Migration[6.1]
+  disable_ddl_transaction!
+
+  # Dummy classes, to make migration possible across version changes
+  class Account < ApplicationRecord
+    has_one :user, inverse_of: :account
+    scope :local, -> { where(domain: nil) }
+  end
+
+  class User < ApplicationRecord
+    belongs_to :account
+  end
+
+  def up
+    Account.reset_column_information
+
+    Setting.unscoped.where(thing_type: 'User', var: 'hide_network').find_each do |setting|
+      account = User.find(setting.thing_id).account
+
+      ApplicationRecord.transaction do
+        account.update(hide_collections: setting.value)
+        setting.delete
+      end
+    rescue ActiveRecord::RecordNotFound
+      next
+    end
+  end
+
+  def down
+    Account.local.where(hide_collections: true).includes(:user).find_each do |account|
+      ApplicationRecord.transaction do
+        Setting.create(thing_type: 'User', thing_id: account.user.id, var: 'hide_network', value: account.hide_collections?)
+        account.update(hide_collections: nil)
+      end
+    end
+  end
+end