Commit 049a428c authored by Pavel Shutsin's avatar Pavel Shutsin

Update data in batches while deleting a user

User pending deletion can have plenty of
associated data. We need to update that data
to a ghost user. To do it without query timeouts
we update it in batches.

Changelog: other
EE: true
parent 62ad1a2f
......@@ -57,20 +57,20 @@ module Users
# rubocop: disable CodeReuse/ActiveRecord
def migrate_issues
user.issues.update_all(author_id: ghost_user.id)
Issue.where(last_edited_by_id: user.id).update_all(last_edited_by_id: ghost_user.id)
batched_migrate(Issue, :author_id)
batched_migrate(Issue, :last_edited_by_id)
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def migrate_merge_requests
user.merge_requests.update_all(author_id: ghost_user.id)
MergeRequest.where(merge_user_id: user.id).update_all(merge_user_id: ghost_user.id)
batched_migrate(MergeRequest, :author_id)
batched_migrate(MergeRequest, :merge_user_id)
end
# rubocop: enable CodeReuse/ActiveRecord
def migrate_notes
user.notes.update_all(author_id: ghost_user.id)
batched_migrate(Note, :author_id)
end
def migrate_abuse_reports
......@@ -87,8 +87,17 @@ module Users
end
def migrate_reviews
user.reviews.update_all(author_id: ghost_user.id)
batched_migrate(Review, :author_id)
end
# rubocop:disable CodeReuse/ActiveRecord
def batched_migrate(base_scope, column)
loop do
update_count = base_scope.where(column => user.id).limit(100).update_all(column => ghost_user.id)
break if update_count == 0
end
end
# rubocop:enable CodeReuse/ActiveRecord
end
end
......
......@@ -14,8 +14,8 @@ module EE
# rubocop: disable CodeReuse/ActiveRecord
def migrate_epics
user.epics.update_all(author_id: ghost_user.id)
::Epic.where(last_edited_by_id: user.id).update_all(last_edited_by_id: ghost_user.id)
batched_migrate(::Epic, :author_id)
batched_migrate(::Epic, :last_edited_by_id)
end
# rubocop: enable CodeReuse/ActiveRecord
......
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