debian-mirror-gitlab/app/services/users/migrate_to_ghost_user_service.rb

78 lines
2 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
# When a user is destroyed, some of their associated records are
# moved to a "Ghost User", to prevent these associated records from
# being destroyed.
#
# For example, all the issues/MRs a user has created are _not_ destroyed
# when the user is destroyed.
module Users
class MigrateToGhostUserService
extend ActiveSupport::Concern
attr_reader :ghost_user, :user
def initialize(user)
@user = user
end
def execute
transition = user.block_transition
user.transaction do
# Block the user before moving records to prevent a data race.
# For example, if the user creates an issue after `migrate_issues`
# runs and before the user is destroyed, the destroy will fail with
# an exception.
user.block
# Reverse the user block if record migration fails
2018-03-17 18:26:18 +05:30
if !migrate_records_in_transaction && transition
2017-08-17 22:00:37 +05:30
transition.rollback
user.save!
end
end
user.reload
end
private
2018-03-17 18:26:18 +05:30
def migrate_records_in_transaction
2017-08-17 22:00:37 +05:30
user.transaction(requires_new: true) do
@ghost_user = User.ghost
2018-03-17 18:26:18 +05:30
migrate_records
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
def migrate_records
migrate_issues
migrate_merge_requests
migrate_notes
migrate_abuse_reports
2018-10-15 14:42:47 +05:30
migrate_award_emoji
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
def migrate_issues
user.issues.update_all(author_id: ghost_user.id)
2017-09-10 17:25:29 +05:30
Issue.where(last_edited_by_id: user.id).update_all(last_edited_by_id: ghost_user.id)
2017-08-17 22:00:37 +05:30
end
def migrate_merge_requests
user.merge_requests.update_all(author_id: ghost_user.id)
2017-09-10 17:25:29 +05:30
MergeRequest.where(merge_user_id: user.id).update_all(merge_user_id: ghost_user.id)
2017-08-17 22:00:37 +05:30
end
def migrate_notes
user.notes.update_all(author_id: ghost_user.id)
end
def migrate_abuse_reports
user.reported_abuse_reports.update_all(reporter_id: ghost_user.id)
end
2018-10-15 14:42:47 +05:30
def migrate_award_emoji
2017-08-17 22:00:37 +05:30
user.award_emoji.update_all(user_id: ghost_user.id)
end
end
end