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

20191105094625_set_report_type_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: 5a8529c24d7c14cb885f99656900868fe51a55ec (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
# frozen_string_literal: true

class SetReportTypeForVulnerabilities < ActiveRecord::Migration[5.2]
  DOWNTIME = false

  def up
    # set report_type based on vulnerability_occurrences from which the vulnerabilities were promoted,
    # that is, first vulnerability_occurrences among those having the same vulnerability_id
    execute <<~SQL
    WITH first_findings_for_vulnerabilities AS (
      SELECT MIN(id) AS id, vulnerability_id
      FROM vulnerability_occurrences
      WHERE vulnerability_id IS NOT NULL
      GROUP BY vulnerability_id
    )
    UPDATE vulnerabilities
    SET report_type = vulnerability_occurrences.report_type
    FROM vulnerability_occurrences, first_findings_for_vulnerabilities
    WHERE vulnerability_occurrences.id = first_findings_for_vulnerabilities.id
    AND vulnerabilities.id = vulnerability_occurrences.vulnerability_id
    SQL

    # set default report_type for orphan vulnerabilities (there should be none but...)
    execute 'UPDATE vulnerabilities SET report_type = 0 WHERE report_type IS NULL'

    change_column_null :vulnerabilities, :report_type, false
  end

  def down
    change_column_null :vulnerabilities, :report_type, true

    execute 'UPDATE vulnerabilities SET report_type = NULL'
  end
end