diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 629f40be6..57d059e7f 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -1269,7 +1269,12 @@ CREATE TABLE history ( flapping_history_id binary(20) DEFAULT NULL COMMENT 'flapping_history.id', acknowledgement_history_id binary(20) DEFAULT NULL COMMENT 'acknowledgement_history.id', - event_type enum('notification','state_change','downtime_start', 'downtime_end','comment_add','comment_remove','flapping_start','flapping_end','ack_set','ack_clear') NOT NULL, + -- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with + -- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes, + -- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result + -- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping + -- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts. + event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL, event_time bigint unsigned NOT NULL, PRIMARY KEY (id), diff --git a/schema/mysql/upgrades/1.2.0.sql b/schema/mysql/upgrades/1.2.0.sql index c7168d4ad..fb34f8c60 100644 --- a/schema/mysql/upgrades/1.2.0.sql +++ b/schema/mysql/upgrades/1.2.0.sql @@ -14,3 +14,17 @@ ALTER TABLE servicegroup ADD INDEX idx_servicegroup_name_ci (name_ci) COMMENT 'Servicegroup list filtered using quick search', DROP INDEX idx_servicegroup_name, ADD INDEX idx_servicegroup_name (name) COMMENT 'Host/service/service group list filtered by service group name; Servicegroup detail filter'; + +-- The following sequence of statements changes the type of history.event_type like the following statement would: +-- +-- ALTER TABLE history MODIFY event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL; +-- +-- It's just much faster to add a second column, copy the column using an UPDATE statement and then replace the +-- old column with the one just generated. Table locking ensures that no other connection inserts data in the meantime. +LOCK TABLES history WRITE; +ALTER TABLE history ADD COLUMN event_type_new enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL AFTER event_type; +UPDATE history SET event_type_new = event_type; +ALTER TABLE history + DROP COLUMN event_type, + CHANGE COLUMN event_type_new event_type enum('state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification') NOT NULL; +UNLOCK TABLES; diff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql index 53c34a703..3ca9daf86 100644 --- a/schema/pgsql/schema.sql +++ b/schema/pgsql/schema.sql @@ -19,7 +19,13 @@ CREATE TYPE state_type AS ENUM ( 'hard', 'soft' ); CREATE TYPE checkable_type AS ENUM ( 'host', 'service' ); CREATE TYPE comment_type AS ENUM ( 'comment', 'ack' ); CREATE TYPE notification_type AS ENUM ( 'downtime_start', 'downtime_end', 'downtime_removed', 'custom', 'acknowledgement', 'problem', 'recovery', 'flapping_start', 'flapping_end' ); -CREATE TYPE history_type AS ENUM ( 'notification', 'state_change', 'downtime_start', 'downtime_end', 'comment_add', 'comment_remove', 'flapping_start', 'flapping_end', 'ack_set', 'ack_clear' ); + +-- The enum values are ordered in a way that event_type provides a meaningful sort order for history entries with +-- the same event_time. state_change comes first as it can cause many of the other events like trigger downtimes, +-- remove acknowledgements and send notifications. Similarly, notification comes last as any other event can result +-- in a notification. End events sort before the corresponding start events as any ack/comment/downtime/flapping +-- period should last for more than a millisecond, therefore, the old period ends first and then the new one starts. +CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification' ); CREATE OR REPLACE FUNCTION get_sla_ok_percent( in_host_id bytea20, @@ -2040,7 +2046,7 @@ CREATE TABLE history ( flapping_history_id bytea20 DEFAULT NULL, acknowledgement_history_id bytea20 DEFAULT NULL, - event_type history_type NOT NULL DEFAULT 'notification', + event_type history_type NOT NULL DEFAULT 'state_change', event_time biguint NOT NULL, CONSTRAINT pk_history PRIMARY KEY (id), diff --git a/schema/pgsql/upgrades/1.2.0.sql b/schema/pgsql/upgrades/1.2.0.sql index e65bf6759..244e566d8 100644 --- a/schema/pgsql/upgrades/1.2.0.sql +++ b/schema/pgsql/upgrades/1.2.0.sql @@ -14,3 +14,11 @@ CREATE INDEX idx_servicegroup_name_ci ON servicegroup(name_ci); COMMENT ON INDEX idx_servicegroup_display_name IS 'Servicegroup list filtered/ordered by display_name'; COMMENT ON INDEX idx_servicegroup_name_ci IS 'Servicegroup list filtered using quick search'; COMMENT ON INDEX idx_servicegroup_name IS 'Host/service/service group list filtered by service group name; Servicegroup detail filter'; + +ALTER TYPE history_type RENAME TO history_type_old; +CREATE TYPE history_type AS ENUM ( 'state_change', 'ack_clear', 'comment_remove', 'downtime_end', 'flapping_end', 'ack_set', 'comment_add', 'downtime_start', 'flapping_start', 'notification' ); +ALTER TABLE history + ALTER COLUMN event_type DROP DEFAULT, + ALTER COLUMN event_type TYPE history_type USING event_type::text::history_type, + ALTER COLUMN event_type SET DEFAULT 'state_change'::history_type; +DROP TYPE history_type_old;