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

20201130103926_schedule_populate_dismissed_state_for_vulnerabilities.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e8da532251fd9600710098c649a8d3b3aa9b9fd (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

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

  TMP_INDEX_NAME = 'tmp_index_on_vulnerabilities_non_dismissed'

  DOWNTIME = false
  BATCH_SIZE = 1_000
  VULNERABILITY_BATCH_SIZE = 5_000
  DELAY_INTERVAL = 3.minutes.to_i
  MIGRATION_CLASS = 'PopulateDismissedStateForVulnerabilities'

  VULNERABILITY_JOIN_CONDITION = 'JOIN "vulnerability_occurrences" ON "vulnerability_occurrences"."vulnerability_id" = "vulnerabilities"."id"'
  FEEDBACK_WHERE_CONDITION = <<~SQL
    EXISTS (SELECT 1 FROM vulnerability_feedback
      WHERE "vulnerability_occurrences"."project_id" = "vulnerability_feedback"."project_id"
      AND "vulnerability_occurrences"."report_type" = "vulnerability_feedback"."category"
      AND ENCODE("vulnerability_occurrences"."project_fingerprint", 'hex') = "vulnerability_feedback"."project_fingerprint"
      AND "vulnerability_feedback"."feedback_type" = 0
    )
  SQL

  class Vulnerability < ActiveRecord::Base # rubocop:disable Style/Documentation
    include EachBatch

    self.table_name = 'vulnerabilities'
  end

  disable_ddl_transaction!

  def up
    add_concurrent_index(:vulnerabilities, :id, where: 'state <> 2', name: TMP_INDEX_NAME)

    batch = []
    index = 1

    Vulnerability.where('state <> 2').each_batch(of: VULNERABILITY_BATCH_SIZE) do |relation|
      ids = relation
        .joins(VULNERABILITY_JOIN_CONDITION)
        .where(FEEDBACK_WHERE_CONDITION)
        .pluck('vulnerabilities.id')

      ids.each do |id|
        batch << id

        if batch.size == BATCH_SIZE
          migrate_in(index * DELAY_INTERVAL, MIGRATION_CLASS, batch)
          index += 1

          batch.clear
        end
      end
    end

    migrate_in(index * DELAY_INTERVAL, MIGRATION_CLASS, batch) unless batch.empty?
  end

  def down
    remove_concurrent_index_by_name(:vulnerabilities, TMP_INDEX_NAME)
  end
end