fedibird-fe/db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
Claire 763ab0c7eb
Fix owned account notes not being deleted when an account is deleted (#16579)
* Add account_notes relationship

* Add tests

* Fix owned account notes not being deleted when an account is deleted

* Add post-migration to clean up orphaned account notes
2021-08-08 15:29:57 +02:00

21 lines
585 B
Ruby

# 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