Welcome to mirror list, hosted at ThFree Co, Russian Federation.

migrate_to_ghost_user.rb « users « concerns « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ecbbf3026a03705aa633ddab478c343cac5bbe81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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::MigrateToGhostUser
  extend ActiveSupport::Concern

  attr_reader :ghost_user

  def move_associated_records_to_ghost_user(user)
    # 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`
    # runs and before the user is destroyed, the destroy will fail with
    # an exception.
    user.block

    user.transaction do
      @ghost_user = User.ghost

      move_issues_to_ghost_user(user)
      move_merge_requests_to_ghost_user(user)
    end

    user.reload
  end

  private

  def move_issues_to_ghost_user(user)
    user.issues.update_all(author_id: ghost_user.id)
  end

  def move_merge_requests_to_ghost_user(user)
    user.merge_requests.update_all(author_id: ghost_user.id)
  end
end