about summary refs log tree commit diff
path: root/db/migrate
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-05-15 17:11:40 +0200
committerThibaut Girka <thib@sitedethib.com>2019-05-15 17:11:40 +0200
commit6badf2d252a980d496b894c7258a778c57639953 (patch)
treeb7c71885c413a8205dbe9d01bab7c60ea75c11bf /db/migrate
parentcd534830adab89300703904ffe87894dec8e2f95 (diff)
parentf4f22391b2d469bd1d5c29ec815daf020282f30c (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Diffstat (limited to 'db/migrate')
-rw-r--r--db/migrate/20190511134027_add_silenced_at_suspended_at_to_accounts.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/db/migrate/20190511134027_add_silenced_at_suspended_at_to_accounts.rb b/db/migrate/20190511134027_add_silenced_at_suspended_at_to_accounts.rb
new file mode 100644
index 000000000..1e5cd669c
--- /dev/null
+++ b/db/migrate/20190511134027_add_silenced_at_suspended_at_to_accounts.rb
@@ -0,0 +1,41 @@
+class AddSilencedAtSuspendedAtToAccounts < ActiveRecord::Migration[5.2]
+  class Account < ApplicationRecord
+    # Dummy class, to make migration possible across version changes
+  end
+
+  class DomainBlock < ApplicationRecord
+    # Dummy class, to make migration possible across version changes
+    enum severity: [:silence, :suspend, :noop]
+
+    has_many :accounts, foreign_key: :domain, primary_key: :domain
+  end
+
+  def up
+    add_column :accounts, :silenced_at, :datetime
+    add_column :accounts, :suspended_at, :datetime
+
+    # Record suspend date of blocks and silences for users whose limitations match
+    # a domain block
+    DomainBlock.where(severity: [:silence, :suspend]).find_each do |block|
+      scope = block.accounts
+      if block.suspend?
+        block.accounts.where(suspended: true).in_batches.update_all(suspended_at: block.created_at)
+      else
+        block.accounts.where(silenced: true).in_batches.update_all(silenced_at: block.created_at)
+      end
+    end
+
+    # Set dates for accounts which have limitations not related to a domain block
+    Account.where(suspended: true, suspended_at: nil).in_batches.update_all(suspended_at: Time.now.utc)
+    Account.where(silenced: true, silenced_at: nil).in_batches.update_all(silenced_at: Time.now.utc)
+  end
+
+  def down
+    # Block or silence accounts that have a date set
+    Account.where(suspended: false).where.not(suspended_at: nil).in_batches.update_all(suspended: true)
+    Account.where(silenced: false).where.not(silenced_at: nil).in_batches.update_all(silenced: true)
+
+    remove_column :accounts, :silenced_at
+    remove_column :accounts, :suspended_at
+  end
+end