about summary refs log tree commit diff
path: root/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2021-08-09 23:25:49 +0200
committerClaire <claire.github-309c@sitedethib.com>2021-08-09 23:25:49 +0200
commit8ec4be4233d2bfd9142860b25df639fc23655f0f (patch)
treee0b7f33c4ae22827a7a56aa9cac5fabdd77ee57f /db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
parenta61645ed30b854a1b4935026801eb5b287954095 (diff)
parent4ac78e2a066508a54de82f1d910ef2fd36c3d106 (diff)
Merge branch 'main' into glitch-soc/merge-upstream
Conflicts:
- `Gemfile.lock`:
  Not a real conflict, upstream-updated dependency (redis) textually too
  close to glitch-soc-only dependecy.
  Updated redis gem like upstream did.
Diffstat (limited to 'db/post_migrate/20210808071221_clear_orphaned_account_notes.rb')
-rw-r--r--db/post_migrate/20210808071221_clear_orphaned_account_notes.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb b/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
new file mode 100644
index 000000000..71171658a
--- /dev/null
+++ b/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class ClearOrphanedAccountNotes < ActiveRecord::Migration[5.2]
+  class Account < ApplicationRecord
+    # Dummy class, to make migration possible across version changes
+  end
+
+  class AccountNote < ApplicationRecord
+    # Dummy class, to make migration possible across version changes
+    belongs_to :account
+    belongs_to :target_account, class_name: 'Account'
+  end
+
+  def up
+    AccountNote.where('NOT EXISTS (SELECT * FROM users u WHERE u.account_id = account_notes.account_id)').in_batches.delete_all
+  end
+
+  def down
+    # nothing to do
+  end
+end