blob: a863fe8b7b8c05b7261d2b8dde1736e4fb7265e5 (
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
|
# frozen_string_literal: true
class AddUpdateVulnerabilityReadsLocationTrigger < Gitlab::Database::Migration[1.0]
include Gitlab::Database::SchemaHelpers
TRIGGER_NAME = 'trigger_update_location_on_vulnerability_occurrences_update'
FUNCTION_NAME = 'update_location_from_vulnerability_occurrences'
def up
create_trigger_function(FUNCTION_NAME, replace: true) do
<<~SQL
UPDATE
vulnerability_reads
SET
location_image = NEW.location->>'image',
cluster_agent_id = NEW.location->'kubernetes_resource'->>'agent_id'
WHERE
vulnerability_id = NEW.vulnerability_id;
RETURN NULL;
SQL
end
execute(<<~SQL)
CREATE TRIGGER #{TRIGGER_NAME}
AFTER UPDATE ON vulnerability_occurrences
FOR EACH ROW
WHEN (
NEW.report_type IN (2, 7) AND (
OLD.location->>'image' IS DISTINCT FROM NEW.location->>'image' OR
OLD.location->'kubernetes_resource'->>'agent_id' IS DISTINCT FROM NEW.location->'kubernetes_resource'->>'agent_id'
)
)
EXECUTE PROCEDURE #{FUNCTION_NAME}();
SQL
end
def down
drop_trigger(:vulnerability_occurrences, TRIGGER_NAME)
drop_function(FUNCTION_NAME)
end
end
|