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. scope = block.accounts
  17. if block.suspend?
  18. block.accounts.where(suspended: true).in_batches.update_all(suspended_at: block.created_at)
  19. else
  20. block.accounts.where(silenced: true).in_batches.update_all(silenced_at: block.created_at)
  21. end
  22. end
  23. # Set dates for accounts which have limitations not related to a domain block
  24. Account.where(suspended: true, suspended_at: nil).in_batches.update_all(suspended_at: Time.now.utc)
  25. Account.where(silenced: true, silenced_at: nil).in_batches.update_all(silenced_at: Time.now.utc)
  26. safety_assured do
  27. remove_column :accounts, :suspended, :boolean, null: false, default: false
  28. remove_column :accounts, :silenced, :boolean, null: false, default: false
  29. end
  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