Skip to content

Commit

Permalink
schema: Upgrade for rotation configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed May 7, 2024
1 parent 06fcf5e commit 4ba93ab
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions schema/pgsql/upgrades/025.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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),
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 (
rotation_id bigint NOT NULL REFERENCES rotation(id),
contact_id bigint REFERENCES contact(id),
contactgroup_id bigint REFERENCES contactgroup(id),
timeperiod_entry_id bigint NOT NULL REFERENCES timeperiod_entry(id),
position integer NOT NULL,

-- There is no PRIMARY KEY in that table as either contact_id or contactgroup_id should be allowed to be NULL.
-- Instead, there are two UNIQUE constraints that prevent duplicate entries. 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)
);

ALTER TABLE rule ADD COLUMN timeperiod_id bigint REFERENCES timeperiod(id);

DO $$ BEGIN
DROP TYPE frequency_type;
EXCEPTION
WHEN undefined_object THEN null;
END $$;

0 comments on commit 4ba93ab

Please sign in to comment.