BigW Consortium Gitlab

Move a user's abuse reports to the ghost user

... when the user is destroyed. To clarify, this regards abuse reports that the to-be-deleted user has _reported_.
parent 6a065074
...@@ -11,7 +11,7 @@ module Users::MigrateToGhostUser ...@@ -11,7 +11,7 @@ module Users::MigrateToGhostUser
def move_associated_records_to_ghost_user(user) def move_associated_records_to_ghost_user(user)
# Block the user before moving records to prevent a data race. # Block the user before moving records to prevent a data race.
# For example, if the user creates an issue after `move_issues_to_ghost_user` # For example, if the user creates an issue after `migrate_issues`
# runs and before the user is destroyed, the destroy will fail with # runs and before the user is destroyed, the destroy will fail with
# an exception. # an exception.
user.block user.block
...@@ -19,9 +19,10 @@ module Users::MigrateToGhostUser ...@@ -19,9 +19,10 @@ module Users::MigrateToGhostUser
user.transaction do user.transaction do
@ghost_user = User.ghost @ghost_user = User.ghost
move_issues_to_ghost_user(user) migrate_issues(user)
move_merge_requests_to_ghost_user(user) migrate_merge_requests(user)
move_notes_to_ghost_user(user) migrate_notes(user)
migrate_abuse_reports(user)
end end
user.reload user.reload
...@@ -29,15 +30,19 @@ module Users::MigrateToGhostUser ...@@ -29,15 +30,19 @@ module Users::MigrateToGhostUser
private private
def move_issues_to_ghost_user(user) def migrate_issues(user)
user.issues.update_all(author_id: ghost_user.id) user.issues.update_all(author_id: ghost_user.id)
end end
def move_merge_requests_to_ghost_user(user) def migrate_merge_requests(user)
user.merge_requests.update_all(author_id: ghost_user.id) user.merge_requests.update_all(author_id: ghost_user.id)
end end
def move_notes_to_ghost_user(user) def migrate_notes(user)
user.notes.update_all(author_id: ghost_user.id) user.notes.update_all(author_id: ghost_user.id)
end end
def migrate_abuse_reports(user)
AbuseReport.where(reporter_id: user.id).update_all(reporter_id: ghost_user.id)
end
end end
...@@ -162,6 +162,13 @@ describe Users::DestroyService, services: true do ...@@ -162,6 +162,13 @@ describe Users::DestroyService, services: true do
let(:created_record) { create(:note, project: project, author: user) } let(:created_record) { create(:note, project: project, author: user) }
end end
end end
context 'abuse reports' do
include_examples "migrating a deleted user's associated records to the ghost user", AbuseReport, { skip_assignee_specs: true } do
let(:created_record) { create(:abuse_report, reporter: user, user: create(:user)) }
let(:author_method) { :reporter }
end
end
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment