-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
schema: Upgrade for rotation configuration
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
DO $$ BEGIN | ||
CREATE TYPE rotation_type AS ENUM ( '24-7', 'partial', 'multi' ); | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
|
||
CREATE TABLE IF NOT EXISTS rotation ( | ||
id bigserial, | ||
schedule_id bigint NOT NULL REFERENCES schedule(id), | ||
priority integer NOT NULL, -- the lower the higher the priority, starting at 0 | ||
name text NOT NULL, | ||
mode rotation_type NOT NULL, | ||
-- JSON with rotation-specific attributes | ||
-- Needed exclusively by Web to simplify editing and visualisation | ||
options text NOT NULL, | ||
|
||
CONSTRAINT pk_rotation PRIMARY KEY (id) | ||
); | ||
|
||
ALTER TABLE rule DROP COLUMN timeperiod_id; | ||
|
||
DROP TABLE IF EXISTS schedule_member; | ||
DROP TABLE IF EXISTS rotation_member; | ||
|
||
DROP TABLE IF EXISTS timeperiod_entry; | ||
|
||
DROP TABLE timeperiod; | ||
CREATE TABLE timeperiod ( | ||
id bigserial, | ||
owned_by_rotation_id bigint REFERENCES rotation(id), -- nullable for future standalone timeperiods | ||
|
||
CONSTRAINT pk_timeperiod PRIMARY KEY (id) | ||
); | ||
|
||
CREATE TABLE timeperiod_entry ( | ||
id bigserial, | ||
timeperiod_id bigint NOT NULL REFERENCES timeperiod(id), | ||
rotation_member_id bigint REFERENCES rotation_member(id), | ||
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. | ||
until_time bigint, | ||
timezone text NOT NULL, -- e.g. 'Europe/Berlin', relevant for evaluating rrule (DST changes differ between zones) | ||
rrule text, -- recurrence rule (RFC5545) | ||
|
||
CONSTRAINT pk_timeperiod_entry PRIMARY KEY (id) | ||
); | ||
|
||
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), | ||
position integer NOT NULL, | ||
|
||
-- 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), | ||
CHECK (num_nonnulls(contact_id, contactgroup_id) = 1), | ||
|
||
CONSTRAINT pk_rotation_member PRIMARY KEY (id) | ||
); | ||
|
||
ALTER TABLE rule ADD COLUMN timeperiod_id bigint REFERENCES timeperiod(id); | ||
|
||
DO $$ BEGIN | ||
DROP TYPE frequency_type; | ||
EXCEPTION | ||
WHEN undefined_object THEN null; | ||
END $$; |