20220304195405_migrate_hide_network_preference.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. class MigrateHideNetworkPreference < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. # Dummy classes, to make migration possible across version changes
  5. class Account < ApplicationRecord
  6. has_one :user, inverse_of: :account
  7. scope :local, -> { where(domain: nil) }
  8. end
  9. class User < ApplicationRecord
  10. belongs_to :account
  11. end
  12. def up
  13. Account.reset_column_information
  14. Setting.unscoped.where(thing_type: 'User', var: 'hide_network').find_each do |setting|
  15. account = User.find(setting.thing_id).account
  16. ApplicationRecord.transaction do
  17. account.update(hide_collections: setting.value)
  18. setting.delete
  19. end
  20. rescue ActiveRecord::RecordNotFound
  21. next
  22. end
  23. end
  24. def down
  25. Account.local.where(hide_collections: true).includes(:user).find_each do |account|
  26. ApplicationRecord.transaction do
  27. Setting.create(thing_type: 'User', thing_id: account.user.id, var: 'hide_network', value: account.hide_collections?)
  28. account.update(hide_collections: nil)
  29. end
  30. end
  31. end
  32. end