diff --git a/schema/pgsql/schema.sql b/schema/pgsql/schema.sql index 16518e490..211bfd72f 100644 --- a/schema/pgsql/schema.sql +++ b/schema/pgsql/schema.sql @@ -43,7 +43,7 @@ CREATE TABLE available_channel_type ( CREATE TABLE channel ( id bigserial, name citext NOT NULL, - type text NOT NULL REFERENCES available_channel_type(type), -- 'email', 'sms', ... + type text NOT NULL, -- 'email', 'sms', ... config text, -- JSON with channel-specific attributes -- for now type determines the implementation, in the future, this will need a reference to a concrete -- implementation to allow multiple implementations of a sms channel for example, probably even user-provided ones @@ -51,7 +51,8 @@ CREATE TABLE channel ( changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_channel PRIMARY KEY (id) + CONSTRAINT pk_channel PRIMARY KEY (id), + CONSTRAINT fk_channel_available_channel_type FOREIGN KEY (type) REFERENCES available_channel_type(type) ); CREATE INDEX idx_channel_changed_at ON channel(changed_at); @@ -60,7 +61,7 @@ CREATE TABLE contact ( id bigserial, full_name citext NOT NULL, username citext, -- reference to web user - default_channel_id bigint NOT NULL REFERENCES channel(id), + default_channel_id bigint NOT NULL, changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', @@ -68,21 +69,24 @@ CREATE TABLE contact ( CONSTRAINT pk_contact PRIMARY KEY (id), -- As the username is unique, it must be NULLed for deletion via "deleted = 'y'" - UNIQUE (username) + CONSTRAINT uk_contact_username UNIQUE (username), + + CONSTRAINT fk_contact_channel FOREIGN KEY (default_channel_id) REFERENCES channel(id) ); CREATE INDEX idx_contact_changed_at ON contact(changed_at); CREATE TABLE contact_address ( id bigserial, - contact_id bigint NOT NULL REFERENCES contact(id), + contact_id bigint NOT NULL, type text NOT NULL, -- 'phone', 'email', ... address text NOT NULL, -- phone number, email address, ... changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_contact_address PRIMARY KEY (id) + CONSTRAINT pk_contact_address PRIMARY KEY (id), + CONSTRAINT fk_contact_address_contact FOREIGN KEY (contact_id) REFERENCES contact(id) ); CREATE INDEX idx_contact_address_changed_at ON contact_address(changed_at); @@ -100,13 +104,15 @@ CREATE TABLE contactgroup ( CREATE INDEX idx_contactgroup_changed_at ON contactgroup(changed_at); CREATE TABLE contactgroup_member ( - contactgroup_id bigint NOT NULL REFERENCES contactgroup(id), - contact_id bigint NOT NULL REFERENCES contact(id), + contactgroup_id bigint NOT NULL, + contact_id bigint NOT NULL, changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_contactgroup_member PRIMARY KEY (contactgroup_id, contact_id) + CONSTRAINT pk_contactgroup_member PRIMARY KEY (contactgroup_id, contact_id), + CONSTRAINT fk_contactgroup_member_contactgroup FOREIGN KEY (contactgroup_id) REFERENCES contactgroup(id), + CONSTRAINT fk_contactgroup_member_contact FOREIGN KEY (contact_id) REFERENCES contact(id) ); CREATE INDEX idx_contactgroup_member_changed_at ON contactgroup_member(changed_at); @@ -125,7 +131,7 @@ CREATE INDEX idx_schedule_changed_at ON schedule(changed_at); CREATE TABLE rotation ( id bigserial, - schedule_id bigint NOT NULL REFERENCES schedule(id), + schedule_id bigint NOT NULL, -- the lower the more important, starting at 0, avoids the need to re-index upon addition priority integer, name text NOT NULL, @@ -146,33 +152,36 @@ CREATE TABLE rotation ( changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', + CONSTRAINT pk_rotation PRIMARY KEY (id), + -- Each schedule can only have one rotation with a given priority starting at a given date. -- Columns schedule_id, priority, first_handoff must be NULLed for deletion via "deleted = 'y'". - UNIQUE (schedule_id, priority, first_handoff), - CHECK (deleted = 'y' OR priority IS NOT NULL AND first_handoff IS NOT NULL), + CONSTRAINT uk_rotation_schedule_schedule_priority_first_handoff UNIQUE (schedule_id, priority, first_handoff), + CONSTRAINT ck_rotation_deleted_priority_first_handoff CHECK (deleted = 'y' OR priority IS NOT NULL AND first_handoff IS NOT NULL), - CONSTRAINT pk_rotation PRIMARY KEY (id) + CONSTRAINT fk_rotation_schedule FOREIGN KEY (schedule_id) REFERENCES schedule(id) ); CREATE INDEX idx_rotation_changed_at ON rotation(changed_at); CREATE TABLE timeperiod ( id bigserial, - owned_by_rotation_id bigint REFERENCES rotation(id), -- nullable for future standalone timeperiods + owned_by_rotation_id bigint, -- nullable for future standalone timeperiods changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_timeperiod PRIMARY KEY (id) + CONSTRAINT pk_timeperiod PRIMARY KEY (id), + CONSTRAINT fk_timeperiod_rotation FOREIGN KEY (owned_by_rotation_id) REFERENCES rotation(id) ); CREATE INDEX idx_timeperiod_changed_at ON timeperiod(changed_at); CREATE TABLE rotation_member ( id bigserial, - rotation_id bigint NOT NULL REFERENCES rotation(id), - contact_id bigint REFERENCES contact(id), - contactgroup_id bigint REFERENCES contactgroup(id), + rotation_id bigint NOT NULL, + contact_id bigint, + contactgroup_id bigint, position integer, changed_at bigint NOT NULL, @@ -180,27 +189,30 @@ CREATE TABLE rotation_member ( -- Each position in a rotation can only be used once. -- Column position must be NULLed for deletion via "deleted = 'y'". - UNIQUE (rotation_id, position), + CONSTRAINT uk_rotation_member_rotation_position UNIQUE (rotation_id, position), -- Two UNIQUE constraints prevent duplicate memberships of the same contact or contactgroup in a single rotation. -- Multiple NULLs are not considered to be duplicates, so rows with a contact_id but no contactgroup_id are -- basically ignored in the UNIQUE constraint over contactgroup_id and vice versa. The CHECK constraint below -- ensures that each row has only non-NULL values in one of these constraints. - UNIQUE (rotation_id, contact_id), - UNIQUE (rotation_id, contactgroup_id), + CONSTRAINT uk_rotation_member_rotation_contact UNIQUE (rotation_id, contact_id), + CONSTRAINT uk_rotation_member_rotation_contactgroup UNIQUE (rotation_id, contactgroup_id), CONSTRAINT ck_rotation_member_either_contact_id_or_contactgroup_id CHECK (num_nonnulls(contact_id, contactgroup_id) = 1), CONSTRAINT ck_rotation_member_non_deleted_needs_position CHECK (deleted = 'y' OR position IS NOT NULL), - CONSTRAINT pk_rotation_member PRIMARY KEY (id) + CONSTRAINT pk_rotation_member PRIMARY KEY (id), + CONSTRAINT fk_rotation_member_rotation FOREIGN KEY (rotation_id) REFERENCES rotation(id), + CONSTRAINT fk_rotation_member_contact FOREIGN KEY (contact_id) REFERENCES contact(id), + CONSTRAINT fk_rotation_member_contactgroup FOREIGN KEY (contactgroup_id) REFERENCES contactgroup(id) ); CREATE INDEX idx_rotation_member_changed_at ON rotation_member(changed_at); CREATE TABLE timeperiod_entry ( id bigserial, - timeperiod_id bigint NOT NULL REFERENCES timeperiod(id), - rotation_member_id bigint REFERENCES rotation_member(id), -- nullable for future standalone timeperiods + timeperiod_id bigint NOT NULL, + rotation_member_id bigint, -- nullable for future standalone timeperiods start_time bigint NOT NULL, end_time bigint NOT NULL, -- Is needed by icinga-notifications-web to prefilter entries, which matches until this time and should be ignored by the daemon. @@ -211,7 +223,9 @@ CREATE TABLE timeperiod_entry ( changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_timeperiod_entry PRIMARY KEY (id) + CONSTRAINT pk_timeperiod_entry PRIMARY KEY (id), + CONSTRAINT fk_timeperiod_entry_timeperiod FOREIGN KEY (timeperiod_id) REFERENCES timeperiod(id), + CONSTRAINT fk_timeperiod_entry_rotation_member FOREIGN KEY (rotation_member_id) REFERENCES rotation_member(id) ); CREATE INDEX idx_timeperiod_entry_changed_at ON timeperiod_entry(changed_at); @@ -247,8 +261,8 @@ CREATE TABLE source ( -- The hash is a PHP password_hash with PASSWORD_DEFAULT algorithm, defaulting to bcrypt. This check roughly ensures -- that listener_password_hash can only be populated with bcrypt hashes. -- https://icinga.com/docs/icinga-web/latest/doc/20-Advanced-Topics/#manual-user-creation-for-database-authentication-backend - CHECK (listener_password_hash IS NULL OR listener_password_hash LIKE '$2y$%'), - CHECK (type != 'icinga2' OR (icinga2_base_url IS NOT NULL AND icinga2_auth_user IS NOT NULL AND icinga2_auth_pass IS NOT NULL)), + CONSTRAINT ck_bcrypt_source_listener_password_hash CHECK (listener_password_hash IS NULL OR listener_password_hash LIKE '$2y$%'), + CONSTRAINT ck_source_icinga2_has_config CHECK (type != 'icinga2' OR (icinga2_base_url IS NOT NULL AND icinga2_auth_user IS NOT NULL AND icinga2_auth_pass IS NOT NULL)), CONSTRAINT pk_source PRIMARY KEY (id) ); @@ -257,32 +271,34 @@ CREATE INDEX idx_source_changed_at ON source(changed_at); CREATE TABLE object ( id bytea NOT NULL, -- SHA256 of identifying tags and the source.id - source_id bigint NOT NULL REFERENCES source(id), + source_id bigint NOT NULL, name text NOT NULL, url text, -- mute_reason indicates whether an object is currently muted by its source, and its non-zero value is mapped to true. mute_reason text, - CHECK (length(id) = 256/8), - - CONSTRAINT pk_object PRIMARY KEY (id) + CONSTRAINT pk_object PRIMARY KEY (id), + CONSTRAINT ck_object_id_is_sha256 CHECK (length(id) = 256/8), + CONSTRAINT fk_object_source FOREIGN KEY (source_id) REFERENCES source(id) ); CREATE TABLE object_id_tag ( - object_id bytea NOT NULL REFERENCES object(id), + object_id bytea NOT NULL, tag text NOT NULL, value text NOT NULL, - CONSTRAINT pk_object_id_tag PRIMARY KEY (object_id, tag) + CONSTRAINT pk_object_id_tag PRIMARY KEY (object_id, tag), + CONSTRAINT fk_object_id_tag_object FOREIGN KEY (object_id) REFERENCES object(id) ); CREATE TABLE object_extra_tag ( - object_id bytea NOT NULL REFERENCES object(id), + object_id bytea NOT NULL, tag text NOT NULL, value text NOT NULL, - CONSTRAINT pk_object_extra_tag PRIMARY KEY (object_id, tag) + CONSTRAINT pk_object_extra_tag PRIMARY KEY (object_id, tag), + CONSTRAINT fk_object_extra_tag_object FOREIGN KEY (object_id) REFERENCES object(id) ); CREATE TYPE event_type AS ENUM ( @@ -304,7 +320,7 @@ CREATE TYPE severity AS ENUM ('ok', 'debug', 'info', 'notice', 'warning', 'err', CREATE TABLE event ( id bigserial, time bigint NOT NULL, - object_id bytea NOT NULL REFERENCES object(id), + object_id bytea NOT NULL, type event_type NOT NULL, severity severity, message text, @@ -312,30 +328,32 @@ CREATE TABLE event ( mute boolenum, mute_reason text, - CONSTRAINT pk_event PRIMARY KEY (id) + CONSTRAINT pk_event PRIMARY KEY (id), + CONSTRAINT fk_event_object FOREIGN KEY (object_id) REFERENCES object(id) ); CREATE TABLE rule ( id bigserial, name citext NOT NULL, - timeperiod_id bigint REFERENCES timeperiod(id), + timeperiod_id bigint, object_filter text, changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', - CONSTRAINT pk_rule PRIMARY KEY (id) + CONSTRAINT pk_rule PRIMARY KEY (id), + CONSTRAINT fk_rule_timeperiod FOREIGN KEY (timeperiod_id) REFERENCES timeperiod(id) ); CREATE INDEX idx_rule_changed_at ON rule(changed_at); CREATE TABLE rule_escalation ( id bigserial, - rule_id bigint NOT NULL REFERENCES rule(id), + rule_id bigint NOT NULL, position integer, condition text, name citext, -- if not set, recipients are used as a fallback for display purposes - fallback_for bigint REFERENCES rule_escalation(id), + fallback_for bigint, changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', @@ -344,89 +362,106 @@ CREATE TABLE rule_escalation ( -- Each position in an escalation can only be used once. -- Column position must be NULLed for deletion via "deleted = 'y'" - UNIQUE (rule_id, position), + CONSTRAINT uk_rule_escalation_rule_position UNIQUE (rule_id, position), CONSTRAINT ck_rule_escalation_not_both_condition_and_fallback_for CHECK (NOT (condition IS NOT NULL AND fallback_for IS NOT NULL)), - CONSTRAINT ck_rule_escalation_non_deleted_needs_position CHECK (deleted = 'y' OR position IS NOT NULL) + CONSTRAINT ck_rule_escalation_non_deleted_needs_position CHECK (deleted = 'y' OR position IS NOT NULL), + CONSTRAINT fk_rule_escalation_rule FOREIGN KEY (rule_id) REFERENCES rule(id), + CONSTRAINT fk_rule_escalation_rule_escalation FOREIGN KEY (fallback_for) REFERENCES rule_escalation(id) ); CREATE INDEX idx_rule_escalation_changed_at ON rule_escalation(changed_at); CREATE TABLE rule_escalation_recipient ( id bigserial, - rule_escalation_id bigint NOT NULL REFERENCES rule_escalation(id), - contact_id bigint REFERENCES contact(id), - contactgroup_id bigint REFERENCES contactgroup(id), - schedule_id bigint REFERENCES schedule(id), - channel_id bigint REFERENCES channel(id), + rule_escalation_id bigint NOT NULL, + contact_id bigint, + contactgroup_id bigint, + schedule_id bigint, + channel_id bigint, changed_at bigint NOT NULL, deleted boolenum NOT NULL DEFAULT 'n', CONSTRAINT pk_rule_escalation_recipient PRIMARY KEY (id), - - CHECK (num_nonnulls(contact_id, contactgroup_id, schedule_id) = 1) + CONSTRAINT ck_rule_escalation_recipient_one_recipient CHECK (num_nonnulls(contact_id, contactgroup_id, schedule_id) = 1), + CONSTRAINT fk_rule_escalation_recipient_rule_escalation FOREIGN KEY (rule_escalation_id) REFERENCES rule_escalation(id), + CONSTRAINT fk_rule_escalation_recipient_contact FOREIGN KEY (contact_id) REFERENCES contact(id), + CONSTRAINT fk_rule_escalation_recipient_contactgroup FOREIGN KEY (contactgroup_id) REFERENCES contactgroup(id), + CONSTRAINT fk_rule_escalation_recipient_schedule FOREIGN KEY (schedule_id) REFERENCES schedule(id), + CONSTRAINT fk_rule_escalation_recipient_channel FOREIGN KEY (channel_id) REFERENCES channel(id) ); CREATE INDEX idx_rule_escalation_recipient_changed_at ON rule_escalation_recipient(changed_at); CREATE TABLE incident ( id bigserial, - object_id bytea NOT NULL REFERENCES object(id), + object_id bytea NOT NULL, started_at bigint NOT NULL, recovered_at bigint, severity severity NOT NULL, - CONSTRAINT pk_incident PRIMARY KEY (id) + CONSTRAINT pk_incident PRIMARY KEY (id), + CONSTRAINT fk_incident_object FOREIGN KEY (object_id) REFERENCES object(id) ); CREATE TABLE incident_event ( - incident_id bigint NOT NULL REFERENCES incident(id), - event_id bigint NOT NULL REFERENCES event(id), + incident_id bigint NOT NULL, + event_id bigint NOT NULL, - CONSTRAINT pk_incident_event PRIMARY KEY (incident_id, event_id) + CONSTRAINT pk_incident_event PRIMARY KEY (incident_id, event_id), + CONSTRAINT fk_incident_event_incident FOREIGN KEY (incident_id) REFERENCES incident(id), + CONSTRAINT fk_incident_event_event FOREIGN KEY (event_id) REFERENCES event(id) ); CREATE TYPE incident_contact_role AS ENUM ('recipient', 'subscriber', 'manager'); CREATE TABLE incident_contact ( - incident_id bigint NOT NULL REFERENCES incident(id), - contact_id bigint REFERENCES contact(id), - contactgroup_id bigint REFERENCES contactgroup(id), - schedule_id bigint REFERENCES schedule(id), + incident_id bigint NOT NULL, + contact_id bigint, + contactgroup_id bigint, + schedule_id bigint, role incident_contact_role NOT NULL, - CONSTRAINT key_incident_contact_contact UNIQUE (incident_id, contact_id), - CONSTRAINT key_incident_contact_contactgroup UNIQUE (incident_id, contactgroup_id), - CONSTRAINT key_incident_contact_schedule UNIQUE (incident_id, schedule_id), - CONSTRAINT nonnulls_incident_recipients_check CHECK (num_nonnulls(contact_id, contactgroup_id, schedule_id) = 1) + CONSTRAINT uk_incident_contact_contact UNIQUE (incident_id, contact_id), + CONSTRAINT uk_incident_contact_contactgroup UNIQUE (incident_id, contactgroup_id), + CONSTRAINT uk_incident_contact_schedule UNIQUE (incident_id, schedule_id), + CONSTRAINT ck_incident_contact_one_recipient CHECK (num_nonnulls(contact_id, contactgroup_id, schedule_id) = 1), + CONSTRAINT fk_incident_contact_incident FOREIGN KEY (incident_id) REFERENCES incident(id), + CONSTRAINT fk_incident_contact_contact FOREIGN KEY (contact_id) REFERENCES contact(id), + CONSTRAINT fk_incident_contact_contactgroup FOREIGN KEY (contactgroup_id) REFERENCES contactgroup(id), + CONSTRAINT fk_incident_contact_schedule FOREIGN KEY (schedule_id) REFERENCES schedule(id) ); CREATE TABLE incident_rule ( - incident_id bigint NOT NULL REFERENCES incident(id), - rule_id bigint NOT NULL REFERENCES rule(id), + incident_id bigint NOT NULL, + rule_id bigint NOT NULL, - CONSTRAINT pk_incident_rule PRIMARY KEY (incident_id, rule_id) + CONSTRAINT pk_incident_rule PRIMARY KEY (incident_id, rule_id), + CONSTRAINT fk_incident_rule_incident FOREIGN KEY (incident_id) REFERENCES incident(id), + CONSTRAINT fk_incident_rule_rule FOREIGN KEY (rule_id) REFERENCES rule(id) ); CREATE TABLE incident_rule_escalation_state ( - incident_id bigint NOT NULL REFERENCES incident(id), - rule_escalation_id bigint NOT NULL REFERENCES rule_escalation(id), + incident_id bigint NOT NULL, + rule_escalation_id bigint NOT NULL, triggered_at bigint NOT NULL, - CONSTRAINT pk_incident_rule_escalation_state PRIMARY KEY (incident_id, rule_escalation_id) + CONSTRAINT pk_incident_rule_escalation_state PRIMARY KEY (incident_id, rule_escalation_id), + CONSTRAINT fk_incident_rule_escalation_state_incident FOREIGN KEY (incident_id) REFERENCES incident(id), + CONSTRAINT fk_incident_rule_escalation_state_rule_escalation FOREIGN KEY (rule_escalation_id) REFERENCES rule_escalation(id) ); CREATE TABLE incident_history ( id bigserial, - incident_id bigint NOT NULL REFERENCES incident(id), - rule_escalation_id bigint REFERENCES rule_escalation(id), - event_id bigint REFERENCES event(id), - contact_id bigint REFERENCES contact(id), - contactgroup_id bigint REFERENCES contactgroup(id), - schedule_id bigint REFERENCES schedule(id), - rule_id bigint REFERENCES rule(id), - channel_id bigint REFERENCES channel(id), + incident_id bigint NOT NULL, + rule_escalation_id bigint, + event_id bigint, + contact_id bigint, + contactgroup_id bigint, + schedule_id bigint, + rule_id bigint, + channel_id bigint, time bigint NOT NULL, message text, type incident_history_event_type NOT NULL, @@ -438,7 +473,15 @@ CREATE TABLE incident_history ( sent_at bigint, CONSTRAINT pk_incident_history PRIMARY KEY (id), - FOREIGN KEY (incident_id, rule_escalation_id) REFERENCES incident_rule_escalation_state(incident_id, rule_escalation_id) + CONSTRAINT fk_incident_history_incident_rule_escalation_state FOREIGN KEY (incident_id, rule_escalation_id) REFERENCES incident_rule_escalation_state(incident_id, rule_escalation_id), + CONSTRAINT fk_incident_history_incident FOREIGN KEY (incident_id) REFERENCES incident(id), + CONSTRAINT fk_incident_history_rule_escalation FOREIGN KEY (rule_escalation_id) REFERENCES rule_escalation(id), + CONSTRAINT fk_incident_history_event FOREIGN KEY (event_id) REFERENCES event(id), + CONSTRAINT fk_incident_history_contact FOREIGN KEY (contact_id) REFERENCES contact(id), + CONSTRAINT fk_incident_history_contactgroup FOREIGN KEY (contactgroup_id) REFERENCES contactgroup(id), + CONSTRAINT fk_incident_history_schedule FOREIGN KEY (schedule_id) REFERENCES schedule(id), + CONSTRAINT fk_incident_history_rule FOREIGN KEY (rule_id) REFERENCES rule(id), + CONSTRAINT fk_incident_history_channel FOREIGN KEY (channel_id) REFERENCES channel(id) ); CREATE INDEX idx_incident_history_time_type ON incident_history(time, type); diff --git a/schema/pgsql/upgrades/033.sql b/schema/pgsql/upgrades/033.sql new file mode 100644 index 000000000..b074585da --- /dev/null +++ b/schema/pgsql/upgrades/033.sql @@ -0,0 +1,62 @@ +ALTER TABLE channel RENAME CONSTRAINT channel_type_fkey TO fk_channel_available_channel_type; +ALTER TABLE contact RENAME CONSTRAINT contact_username_key TO uk_contact_username; +ALTER TABLE contact RENAME CONSTRAINT contact_default_channel_id_fkey TO fk_contact_channel; +ALTER TABLE contact_address RENAME CONSTRAINT contact_address_contact_id_fkey TO fk_contact_address_contact; +ALTER TABLE contactgroup_member RENAME CONSTRAINT contactgroup_member_contact_id_fkey TO fk_contactgroup_member_contact; +ALTER TABLE contactgroup_member RENAME CONSTRAINT contactgroup_member_contactgroup_id_fkey TO fk_contactgroup_member_contactgroup; +ALTER TABLE rotation RENAME CONSTRAINT rotation_schedule_id_priority_first_handoff_key TO uk_rotation_schedule_schedule_priority_first_handoff; +ALTER TABLE rotation RENAME CONSTRAINT rotation_check TO ck_rotation_deleted_priority_first_handoff; +ALTER TABLE rotation RENAME CONSTRAINT rotation_schedule_id_fkey TO fk_rotation_schedule; +ALTER TABLE timeperiod RENAME CONSTRAINT timeperiod_owned_by_rotation_id_fkey TO fk_timeperiod_rotation; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_rotation_id_position_key TO uk_rotation_member_rotation_position; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_rotation_id_contact_id_key TO uk_rotation_member_rotation_contact; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_rotation_id_contactgroup_id_key TO uk_rotation_member_rotation_contactgroup; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_rotation_id_fkey TO fk_rotation_member_rotation; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_contact_id_fkey TO fk_rotation_member_contact; +ALTER TABLE rotation_member RENAME CONSTRAINT rotation_member_contactgroup_id_fkey TO fk_rotation_member_contactgroup; +ALTER TABLE timeperiod_entry RENAME CONSTRAINT timeperiod_entry_timeperiod_id_fkey TO fk_timeperiod_entry_timeperiod; +ALTER TABLE timeperiod_entry RENAME CONSTRAINT timeperiod_entry_rotation_member_id_fkey TO fk_timeperiod_entry_rotation_member; +ALTER TABLE source RENAME CONSTRAINT source_listener_password_hash_check TO ck_bcrypt_source_listener_password_hash; +ALTER TABLE source RENAME CONSTRAINT source_check TO ck_source_icinga2_has_config; +ALTER TABLE object RENAME CONSTRAINT object_id_check TO ck_object_id_is_sha256; +ALTER TABLE object RENAME CONSTRAINT object_source_id_fkey TO fk_object_source; +ALTER TABLE object_id_tag RENAME CONSTRAINT object_id_tag_object_id_fkey TO fk_object_id_tag_object; +ALTER TABLE object_extra_tag RENAME CONSTRAINT object_extra_tag_object_id_fkey TO fk_object_extra_tag_object; +ALTER TABLE event RENAME CONSTRAINT event_object_id_fkey TO fk_event_object; +ALTER TABLE rule RENAME CONSTRAINT rule_timeperiod_id_fkey TO fk_rule_timeperiod; +ALTER TABLE rule_escalation RENAME CONSTRAINT rule_escalation_rule_id_position_key TO uk_rule_escalation_rule_position; +ALTER TABLE rule_escalation RENAME CONSTRAINT rule_escalation_rule_id_fkey TO fk_rule_escalation_rule; +ALTER TABLE rule_escalation RENAME CONSTRAINT rule_escalation_fallback_for_fkey TO fk_rule_escalation_rule_escalation; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_check TO ck_rule_escalation_recipient_one_recipient; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_rule_escalation_id_fkey TO fk_rule_escalation_recipient_rule_escalation; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_contact_id_fkey TO fk_rule_escalation_recipient_contact; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_contactgroup_id_fkey TO fk_rule_escalation_recipient_contactgroup; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_schedule_id_fkey TO fk_rule_escalation_recipient_schedule; +ALTER TABLE rule_escalation_recipient RENAME CONSTRAINT rule_escalation_recipient_channel_id_fkey TO fk_rule_escalation_recipient_channel; +ALTER TABLE incident RENAME CONSTRAINT incident_object_id_fkey TO fk_incident_object; +ALTER TABLE incident_event RENAME CONSTRAINT incident_event_incident_id_fkey TO fk_incident_event_incident; +ALTER TABLE incident_event RENAME CONSTRAINT incident_event_event_id_fkey TO fk_incident_event_event; +ALTER TABLE incident_contact RENAME CONSTRAINT key_incident_contact_contact TO uk_incident_contact_contact; +ALTER TABLE incident_contact RENAME CONSTRAINT key_incident_contact_contactgroup TO uk_incident_contact_contactgroup; +ALTER TABLE incident_contact RENAME CONSTRAINT key_incident_contact_schedule TO uk_incident_contact_schedule; +ALTER TABLE incident_contact RENAME CONSTRAINT nonnulls_incident_recipients_check TO ck_incident_contact_one_recipient; +ALTER TABLE incident_contact RENAME CONSTRAINT incident_contact_incident_id_fkey TO fk_incident_contact_incident; +ALTER TABLE incident_contact RENAME CONSTRAINT incident_contact_contact_id_fkey TO fk_incident_contact_contact; +ALTER TABLE incident_contact RENAME CONSTRAINT incident_contact_contactgroup_id_fkey TO fk_incident_contact_contactgroup; +ALTER TABLE incident_contact RENAME CONSTRAINT incident_contact_schedule_id_fkey TO fk_incident_contact_schedule; +ALTER TABLE incident_rule RENAME CONSTRAINT incident_rule_incident_id_fkey TO fk_incident_rule_incident; +ALTER TABLE incident_rule RENAME CONSTRAINT incident_rule_rule_id_fkey TO fk_incident_rule_rule; +ALTER TABLE incident_rule_escalation_state RENAME CONSTRAINT incident_rule_escalation_state_incident_id_fkey TO fk_incident_rule_escalation_state_incident; +ALTER TABLE incident_rule_escalation_state RENAME CONSTRAINT incident_rule_escalation_state_rule_escalation_id_fkey TO fk_incident_rule_escalation_state_rule_escalation; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_incident_id_fkey TO fk_incident_history_incident; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_rule_escalation_id_fkey TO fk_incident_history_rule_escalation; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_event_id_fkey TO fk_incident_history_event; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_contact_id_fkey TO fk_incident_history_contact; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_contactgroup_id_fkey TO fk_incident_history_contactgroup; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_schedule_id_fkey TO fk_incident_history_schedule; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_rule_id_fkey TO fk_incident_history_rule; +ALTER TABLE incident_history RENAME CONSTRAINT incident_history_channel_id_fkey TO fk_incident_history_channel; + +ALTER TABLE incident_history DROP CONSTRAINT IF EXISTS incident_history_incident_id_fkey1; -- PostgreSQL 11 +ALTER TABLE incident_history DROP CONSTRAINT IF EXISTS incident_history_incident_id_rule_escalation_id_fkey; -- PostgreSQL 12 +ALTER TABLE incident_history ADD CONSTRAINT fk_incident_history_incident_rule_escalation_state FOREIGN KEY (incident_id, rule_escalation_id) REFERENCES incident_rule_escalation_state(incident_id, rule_escalation_id);