Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Fix owned account notes not being deleted when an account is deleted (m…
Browse files Browse the repository at this point in the history
…astodon#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
  • Loading branch information
ClearlyClaire authored Aug 8, 2021
1 parent 818e0b3 commit 763ab0c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/models/concerns/account_interactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def follow_mapping(query, field)
has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account
has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account

# Account notes
has_many :account_notes, dependent: :destroy

# Block relationships
has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
Expand Down
2 changes: 2 additions & 0 deletions app/services/delete_account_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class DeleteAccountService < BaseService
include Payloadable

ASSOCIATIONS_ON_SUSPEND = %w(
account_notes
account_pins
active_relationships
aliases
Expand Down Expand Up @@ -34,6 +35,7 @@ class DeleteAccountService < BaseService
# by foreign keys, making them safe to delete without loading
# into memory
ASSOCIATIONS_WITHOUT_SIDE_EFFECTS = %w(
account_notes
account_pins
aliases
conversation_mutes
Expand Down
21 changes: 21 additions & 0 deletions db/post_migrate/20210808071221_clear_orphaned_account_notes.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_06_30_000137) do
ActiveRecord::Schema.define(version: 2021_08_08_071221) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
5 changes: 4 additions & 1 deletion spec/services/delete_account_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
let!(:favourite_notification) { Fabricate(:notification, account: local_follower, activity: favourite, type: :favourite) }
let!(:follow_notification) { Fabricate(:notification, account: local_follower, activity: active_relationship, type: :follow) }

let!(:account_note) { Fabricate(:account_note, account: account) }

subject do
-> { described_class.new.call(account) }
end
Expand All @@ -35,8 +37,9 @@
account.active_relationships,
account.passive_relationships,
account.polls,
account.account_notes,
].map(&:count)
}.from([2, 1, 1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0, 0, 0])
}.from([2, 1, 1, 1, 1, 1, 1, 1]).to([0, 0, 0, 0, 0, 0, 0, 0])
end

it 'deletes associated target records' do
Expand Down

0 comments on commit 763ab0c

Please sign in to comment.