summaryrefslogtreecommitdiff
path: root/db/post_migrate/20201014142521_schedule_sync_blocking_issues_count.rb
blob: 30a8ea591dafd7693933ae2cfe5c723ef6b0f881 (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'

  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