20220304195405_migrate_hide_network_preference.rb 1.0 KB

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