File size: 1,941 Bytes
1e92f2d |
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 63 64 65 66 67 68 69 70 71 |
-- +migrate Up
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION update_notification_cycles() RETURNS VOID AS
$$
BEGIN
INSERT INTO user_notification_cycles (user_id, alert_id, escalation_level)
SELECT user_id, alert_id, escalation_level
FROM on_call_alert_users
WHERE status = 'triggered'
AND user_id IS NOT NULL
ON CONFLICT DO NOTHING;
UPDATE user_notification_cycles c
SET escalation_level = a.escalation_level
FROM
alerts a,
user_notification_cycle_state s
WHERE a.id = c.alert_id
AND s.user_id = c.user_id
AND s.alert_id = c.alert_id;
DELETE FROM user_notification_cycles c
WHERE (
SELECT count(notification_rule_id)
FROM user_notification_cycle_state s
WHERE s.alert_id = c.alert_id AND s.user_id = c.user_id
LIMIT 1
) = 0
AND c.escalation_level != (SELECT escalation_level FROM alerts WHERE id = c.alert_id);
END;
$$ LANGUAGE 'plpgsql';
-- +migrate StatementEnd
-- +migrate Down
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION update_notification_cycles() RETURNS VOID AS
$$
BEGIN
INSERT INTO user_notification_cycles (user_id, alert_id, escalation_level)
SELECT user_id, alert_id, escalation_level
FROM on_call_alert_users
WHERE status = 'triggered'
ON CONFLICT DO NOTHING;
UPDATE user_notification_cycles c
SET escalation_level = a.escalation_level
FROM
alerts a,
user_notification_cycle_state s
WHERE a.id = c.alert_id
AND s.user_id = c.user_id
AND s.alert_id = c.alert_id;
DELETE FROM user_notification_cycles c
WHERE (
SELECT count(notification_rule_id)
FROM user_notification_cycle_state s
WHERE s.alert_id = c.alert_id AND s.user_id = c.user_id
LIMIT 1
) = 0
AND c.escalation_level != (SELECT escalation_level FROM alerts WHERE id = c.alert_id);
END;
$$ LANGUAGE 'plpgsql';
-- +migrate StatementEnd
|