The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

42 lines
1.6 KiB

  1. class AddSilencedAtSuspendedAtToAccounts < ActiveRecord::Migration[5.2]
  2. class Account < ApplicationRecord
  3. # Dummy class, to make migration possible across version changes
  4. end
  5. class DomainBlock < ApplicationRecord
  6. # Dummy class, to make migration possible across version changes
  7. enum severity: [:silence, :suspend, :noop]
  8. has_many :accounts, foreign_key: :domain, primary_key: :domain
  9. end
  10. def up
  11. add_column :accounts, :silenced_at, :datetime
  12. add_column :accounts, :suspended_at, :datetime
  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. end
  27. def down
  28. # Block or silence accounts that have a date set
  29. Account.where(suspended: false).where.not(suspended_at: nil).in_batches.update_all(suspended: true)
  30. Account.where(silenced: false).where.not(silenced_at: nil).in_batches.update_all(silenced: true)
  31. remove_column :accounts, :silenced_at
  32. remove_column :accounts, :suspended_at
  33. end
  34. end