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

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

class UpdateExternalProjectBots < ActiveRecord::Migration[6.1]
  include Gitlab::Database::MigrationHelpers

  class User < ActiveRecord::Base
    self.table_name = 'users'
  end

  disable_ddl_transaction!

  TMP_INDEX_NAME = 'tmp_idx_update_external_project_bots'

  def up
    add_concurrent_index('users', 'id', name: TMP_INDEX_NAME, where: 'external = true')

    ids = ActiveRecord::Base.connection
                            .execute("SELECT u.id FROM users u JOIN users u2 on u2.id = u.created_by_id WHERE u.user_type = 6 AND u2.external = true")
                            .map { |result| result['id'] }

    ids.each_slice(10) do |group|
      UpdateExternalProjectBots::User.where(id: group).update_all(external: true)
    end

    remove_concurrent_index_by_name('users', TMP_INDEX_NAME)
  end

  def down
    remove_concurrent_index_by_name('users', TMP_INDEX_NAME) if index_exists_by_name?('users', TMP_INDEX_NAME)

    # This migration is irreversible
  end
end