about summary refs log tree commit diff
path: root/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
diff options
context:
space:
mode:
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