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

20220921144258_remove_orphan_group_token_users.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a2483f611a3273efb5d66aa5da759731cf7d7eda (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
39
40
# frozen_string_literal: true

class RemoveOrphanGroupTokenUsers < Gitlab::Database::Migration[2.0]
  restrict_gitlab_migration gitlab_schema: :gitlab_main

  disable_ddl_transaction!

  class MigrationUser < MigrationRecord
    include EachBatch

    self.table_name = 'users'

    scope :project_bots, -> { where(user_type: 6) }
    scope :without_memberships, -> { where("NOT EXISTS (SELECT 1 FROM members where members.user_id = users.id)") }
  end

  class MigrationPersonalAccessToken < MigrationRecord
    self.table_name = 'personal_access_tokens'
  end

  def up
    delete_worker = 'DeleteUserWorker'.safe_constantize

    MigrationUser.project_bots.each_batch(of: 1000) do |batch|
      bot_ids = batch.without_memberships.pluck(:id)

      MigrationPersonalAccessToken.where(user_id: bot_ids).delete_all

      next unless delete_worker && delete_worker.respond_to?(:perform_async)

      bot_ids.each do |bot_id|
        delete_worker.perform_async(bot_id, bot_id, skip_authorization: true)
      end
    end
  end

  def down
    # no-op
  end
end