20190511152737_remove_suspended_silenced_account_fields.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. class RemoveSuspendedSilencedAccountFields < ActiveRecord::Migration[5.2]
  3. class Account < ApplicationRecord
  4. # Dummy class, to make migration possible across version changes
  5. end
  6. class DomainBlock < ApplicationRecord
  7. # Dummy class, to make migration possible across version changes
  8. enum severity: [:silence, :suspend, :noop]
  9. has_many :accounts, foreign_key: :domain, primary_key: :domain
  10. end
  11. disable_ddl_transaction!
  12. def up
  13. # Record suspend date of blocks and silences for users whose limitations match
  14. # a domain block
  15. DomainBlock.where(severity: [:silence, :suspend]).find_each do |block|
  16. if block.suspend?
  17. block.accounts.where(suspended: true).in_batches.update_all(suspended_at: block.created_at)
  18. else
  19. block.accounts.where(silenced: true).in_batches.update_all(silenced_at: block.created_at)
  20. end
  21. end
  22. # Set dates for accounts which have limitations not related to a domain block
  23. Account.where(suspended: true, suspended_at: nil).in_batches.update_all(suspended_at: Time.now.utc)
  24. Account.where(silenced: true, silenced_at: nil).in_batches.update_all(silenced_at: Time.now.utc)
  25. safety_assured do
  26. remove_column :accounts, :suspended, :boolean, null: false, default: false
  27. remove_column :accounts, :silenced, :boolean, null: false, default: false
  28. end
  29. Account.reset_column_information
  30. end
  31. def down
  32. safety_assured do
  33. add_column :accounts, :suspended, :boolean, null: false, default: false
  34. add_column :accounts, :silenced, :boolean, null: false, default: false
  35. end
  36. end
  37. end