20210808071221_clear_orphaned_account_notes.rb 585 B

123456789101112131415161718192021
  1. # frozen_string_literal: true
  2. class ClearOrphanedAccountNotes < ActiveRecord::Migration[5.2]
  3. class Account < ApplicationRecord
  4. # Dummy class, to make migration possible across version changes
  5. end
  6. class AccountNote < ApplicationRecord
  7. # Dummy class, to make migration possible across version changes
  8. belongs_to :account
  9. belongs_to :target_account, class_name: 'Account'
  10. end
  11. def up
  12. AccountNote.where('NOT EXISTS (SELECT * FROM users u WHERE u.account_id = account_notes.account_id)').in_batches.delete_all
  13. end
  14. def down
  15. # nothing to do
  16. end
  17. end