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

20201014142521_schedule_sync_blocking_issues_count.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61b2b2aaad5ff22b1c3ad3c43d30b60a84ff468e (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
41
42
43
44
45
46
# frozen_string_literal: true

require 'set'

class ScheduleSyncBlockingIssuesCount < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  BATCH_SIZE = 50
  DELAY_INTERVAL = 120.seconds.to_i
  MIGRATION = 'SyncBlockingIssuesCount'.freeze

  disable_ddl_transaction!

  class TmpIssueLink < ActiveRecord::Base
    self.table_name = 'issue_links'

    include EachBatch
  end

  def up
    return unless Gitlab.ee?

    issue_link_ids = SortedSet.new

    TmpIssueLink.distinct.select(:source_id).where(link_type: 1).each_batch(of: 1000, column: :source_id) do |query|
      issue_link_ids.merge(query.pluck(:source_id))
    end

    TmpIssueLink.distinct.select(:target_id).where(link_type: 2).each_batch(of: 1000, column: :target_id) do |query|
      issue_link_ids.merge(query.pluck(:target_id))
    end

    issue_link_ids.each_slice(BATCH_SIZE).with_index do |items, index|
      start_id, *, end_id = items

      arguments = [start_id, end_id]

      final_delay = DELAY_INTERVAL * (index + 1)
      migrate_in(final_delay, MIGRATION, arguments)
    end
  end

  def down
    # NO OP
  end
end