blob: 40e9e3bddc8c6d5de9a3c2b2c9f5ff3f4f9d5b14 (
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 #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (
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
|