2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SetReportTypeForVulnerabilities < ActiveRecord::Migration[5.2]
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
def up
|
2020-01-01 13:55:28 +05:30
|
|
|
# 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
|
2019-12-26 22:10:19 +05:30
|
|
|
execute <<~SQL
|
2020-01-01 13:55:28 +05:30
|
|
|
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
|
|
|
|
)
|
2019-12-26 22:10:19 +05:30
|
|
|
UPDATE vulnerabilities
|
|
|
|
SET report_type = vulnerability_occurrences.report_type
|
2020-01-01 13:55:28 +05:30
|
|
|
FROM vulnerability_occurrences, first_findings_for_vulnerabilities
|
|
|
|
WHERE vulnerability_occurrences.id = first_findings_for_vulnerabilities.id
|
|
|
|
AND vulnerabilities.id = vulnerability_occurrences.vulnerability_id
|
2019-12-26 22:10:19 +05:30
|
|
|
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
|