@@ -18,5 +18,91 @@ ALTER TABLE checkcommand_envvar MODIFY COLUMN envvar_key varchar(255) NOT NULL;
1818ALTER TABLE eventcommand_envvar MODIFY COLUMN envvar_key varchar (255 ) NOT NULL ;
1919ALTER TABLE notificationcommand_envvar MODIFY COLUMN envvar_key varchar (255 ) NOT NULL ;
2020
21+ CREATE TABLE sla_lifecycle (
22+ id binary(20 ) NOT NULL COMMENT ' host.id if service_id is NULL otherwise service.id' ,
23+ environment_id binary(20 ) NOT NULL COMMENT ' environment.id' ,
24+ host_id binary(20 ) NOT NULL COMMENT ' host.id (may reference already deleted hosts)' ,
25+ service_id binary(20 ) DEFAULT NULL COMMENT ' service.id (may reference already deleted services)' ,
26+
27+ -- These columns are nullable, but as we're using the delete_time to build the composed primary key, we have to set
28+ -- this to `0` instead, since it's not allowed to use a nullable column as part of the primary key.
29+ create_time bigint unsigned NOT NULL DEFAULT 0 COMMENT ' unix timestamp the event occurred' ,
30+ delete_time bigint unsigned NOT NULL DEFAULT 0 COMMENT ' unix timestamp the delete event occurred' ,
31+
32+ PRIMARY KEY (id, delete_time)
33+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_bin ROW_FORMAT= DYNAMIC;
34+
35+ -- Insert a sla lifecycle create_time entry for all existing hosts with the LEAST timestamp found in either
36+ -- the sla_history_state or sla_history_downtime table, otherwise fallback to the current Unix timestamp.
37+ INSERT INTO sla_lifecycle (id, environment_id, host_id, create_time)
38+ SELECT host .id ,
39+ host .environment_id ,
40+ host .id ,
41+ -- In MySQL/MariaDB, LEAST() returns NULL if either event_time or downtime_start is NULL, which is not
42+ -- desirable for our use cases. So we need to work around this behaviour by nesting some COALESCE() calls.
43+ COALESCE(LEAST(COALESCE(MIN (event_time), MIN (downtime_start)), COALESCE(MIN (downtime_start), MIN (event_time))), UNIX_TIMESTAMP() * 1000 ) AS create_time
44+ FROM host
45+ LEFT JOIN sla_history_state shs on host .id = shs .host_id AND shs .service_id IS NULL
46+ LEFT JOIN sla_history_downtime shd on host .id = shd .host_id AND shd .service_id IS NULL
47+ GROUP BY host .id , host .environment_id
48+ ON DUPLICATE KEY UPDATE sla_lifecycle .id = sla_lifecycle .id ;
49+
50+ -- Insert a sla lifecycle deleted entry for all not existing hosts with the GREATEST timestamp
51+ -- found in either the sla_history_state or sla_history_downtime table.
52+ INSERT INTO sla_lifecycle (id, environment_id, host_id, delete_time)
53+ SELECT host_id AS id,
54+ environment_id,
55+ host_id,
56+ MAX (event_time) AS delete_time
57+ FROM (SELECT host_id, environment_id, MAX (event_time) AS event_time
58+ FROM sla_history_state
59+ WHERE service_id IS NULL AND NOT EXISTS(SELECT 1 FROM host WHERE id = host_id)
60+ GROUP BY host_id, environment_id
61+ UNION ALL
62+ SELECT host_id, environment_id, MAX (downtime_end) AS event_time
63+ FROM sla_history_downtime
64+ WHERE service_id IS NULL AND NOT EXISTS(SELECT 1 FROM host WHERE id = host_id)
65+ GROUP BY host_id, environment_id
66+ ) AS deleted_hosts
67+ GROUP BY host_id, environment_id HAVING MAX (event_time) IS NOT NULL
68+ ON DUPLICATE KEY UPDATE sla_lifecycle .id = sla_lifecycle .id ;
69+
70+ -- Insert a sla lifecycle create_time entry for all existing services with the LEAST timestamp found in either
71+ -- the sla_history_state or sla_history_downtime table, otherwise fallback to the current Unix timestamp.
72+ INSERT INTO sla_lifecycle (id, environment_id, host_id, service_id, create_time)
73+ SELECT service .id ,
74+ service .environment_id ,
75+ service .host_id ,
76+ service .id ,
77+ -- In MySQL/MariaDB, LEAST() returns NULL if either event_time or downtime_start is NULL, which is not
78+ -- desirable for our use cases. So we need to work around this behaviour by nesting some COALESCE() calls.
79+ COALESCE(LEAST(COALESCE(MIN (event_time), MIN (downtime_start)), COALESCE(MIN (downtime_start), MIN (event_time))), UNIX_TIMESTAMP() * 1000 ) AS create_time
80+ FROM service
81+ LEFT JOIN sla_history_state shs on service .id = shs .service_id
82+ LEFT JOIN sla_history_downtime shd on service .id = shd .service_id
83+ GROUP BY service .id , service .host_id , service .environment_id
84+ ON DUPLICATE KEY UPDATE sla_lifecycle .id = sla_lifecycle .id ;
85+
86+ -- Insert a sla lifecycle deleted entry for all not existing hosts with the GREATEST timestamp
87+ -- found in either the sla_history_state or sla_history_downtime table.
88+ INSERT INTO sla_lifecycle (id, environment_id, host_id, service_id, delete_time)
89+ SELECT service_id AS id,
90+ environment_id,
91+ host_id,
92+ service_id,
93+ MAX (event_time) AS delete_time
94+ FROM (SELECT service_id, environment_id, host_id, MAX (event_time) AS event_time
95+ FROM sla_history_state
96+ WHERE service_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service WHERE id = service_id)
97+ GROUP BY service_id, environment_id, host_id
98+ UNION ALL
99+ SELECT service_id, environment_id, host_id, MAX (downtime_end) AS event_time
100+ FROM sla_history_downtime
101+ WHERE service_id IS NOT NULL AND NOT EXISTS(SELECT 1 FROM service WHERE id = service_id)
102+ GROUP BY service_id, environment_id, host_id
103+ ) AS deleted_services
104+ GROUP BY service_id, environment_id, host_id HAVING MAX (event_time) IS NOT NULL
105+ ON DUPLICATE KEY UPDATE sla_lifecycle .id = sla_lifecycle .id ;
106+
21107INSERT INTO icingadb_schema (version, timestamp )
22108 VALUES (6 , UNIX_TIMESTAMP() * 1000 );
0 commit comments