2018-07-18 16:03:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-06 17:03:07 +00:00
|
|
|
# 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
|
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
2017-04-21 05:55:24 +00:00
|
|
|
transition = user.block_transition
|
2017-04-06 17:03:07 +00:00
|
|
|
|
|
|
|
user.transaction do
|
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
2017-04-21 05:55:24 +00:00
|
|
|
# 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
|
2017-11-02 14:51:42 +00:00
|
|
|
if !migrate_records_in_transaction && transition
|
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
2017-04-21 05:55:24 +00:00
|
|
|
transition.rollback
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-08 13:33:36 +00:00
|
|
|
user.reset
|
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
2017-04-21 05:55:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-02 14:51:42 +00:00
|
|
|
def migrate_records_in_transaction
|
Move records to the ghost user in a transaction.
- While deleting a user, some of the user's associated records are moved to the
ghost user so they aren't deleted. The user is blocked before these records
are moved, to prevent the user from creating new records while the migration
is happening, and so preventing a data race.
- Previously, if the migration failed, the user would _remain_ blocked, which is
not the expected behavior. On the other hand, we can't just stick the block +
migration into a transaction, because we want the block to be committed before
the migration starts (for the data race reason mentioned above).
- One solution (implemented in this commit) is to block the user in a parent
transaction, migrate the associated records in a nested sub-transaction, and
then unblock the user in the parent transaction if the sub-transaction fails.
2017-04-21 05:55:24 +00:00
|
|
|
user.transaction(requires_new: true) do
|
2017-04-06 17:03:07 +00:00
|
|
|
@ghost_user = User.ghost
|
|
|
|
|
2017-11-02 14:51:42 +00:00
|
|
|
migrate_records
|
2017-04-06 17:03:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-02 14:51:42 +00:00
|
|
|
def migrate_records
|
|
|
|
migrate_issues
|
|
|
|
migrate_merge_requests
|
|
|
|
migrate_notes
|
|
|
|
migrate_abuse_reports
|
2018-05-08 03:34:19 +00:00
|
|
|
migrate_award_emoji
|
2017-11-02 14:51:42 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-06 17:03:07 +00:00
|
|
|
def migrate_issues
|
|
|
|
user.issues.update_all(author_id: ghost_user.id)
|
2017-07-17 14:38:43 +00:00
|
|
|
Issue.where(last_edited_by_id: user.id).update_all(last_edited_by_id: ghost_user.id)
|
2017-04-06 17:03:07 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-04-06 17:03:07 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-06 17:03:07 +00:00
|
|
|
def migrate_merge_requests
|
|
|
|
user.merge_requests.update_all(author_id: ghost_user.id)
|
2017-07-17 14:38:43 +00:00
|
|
|
MergeRequest.where(merge_user_id: user.id).update_all(merge_user_id: ghost_user.id)
|
2017-04-06 17:03:07 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-04-06 17:03:07 +00:00
|
|
|
|
|
|
|
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-05-08 03:34:19 +00:00
|
|
|
def migrate_award_emoji
|
2017-04-06 17:03:07 +00:00
|
|
|
user.award_emoji.update_all(user_id: ghost_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Users::MigrateToGhostUserService.prepend_if_ee('EE::Users::MigrateToGhostUserService')
|