-- +goose Up -- Planned maintenance. starts_at is the first occurrence; repeats keep its -- wall-clock time in time_zone (see package maintenance). CREATE TABLE maintenance_windows ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, title text NOT NULL CHECK (length(title) BETWEEN 1 AND 200), description text NOT NULL DEFAULT '' CHECK (length(description) <= 20000), starts_at timestamptz NOT NULL, duration_seconds integer NOT NULL CHECK (duration_seconds BETWEEN 60 AND 2592000), time_zone text NOT NULL, recurrence text NOT NULL CHECK (recurrence IN ('none', 'daily', 'weekly', 'monthly')), every integer NOT NULL DEFAULT 1 CHECK (every BETWEEN 1 AND 365), weekdays smallint[] NOT NULL DEFAULT '{}', until timestamptz, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (id, org_id) ); -- A target is a monitor or a group; a group covers its subgroups. CREATE TABLE maintenance_targets ( window_id uuid NOT NULL, org_id uuid NOT NULL, monitor_id uuid, group_id uuid, CHECK ((monitor_id IS NULL) <> (group_id IS NULL)), FOREIGN KEY (window_id, org_id) REFERENCES maintenance_windows (id, org_id) ON DELETE CASCADE, FOREIGN KEY (monitor_id, org_id) REFERENCES monitors (id, org_id) ON DELETE CASCADE, FOREIGN KEY (group_id, org_id) REFERENCES monitor_groups (id, org_id) ON DELETE CASCADE, UNIQUE NULLS NOT DISTINCT (window_id, monitor_id, group_id) ); CREATE INDEX maintenance_targets_monitor_idx ON maintenance_targets (monitor_id) WHERE monitor_id IS NOT NULL; CREATE INDEX maintenance_targets_group_idx ON maintenance_targets (group_id) WHERE group_id IS NOT NULL; ALTER TABLE maintenance_windows ENABLE ROW LEVEL SECURITY; ALTER TABLE maintenance_windows FORCE ROW LEVEL SECURITY; CREATE POLICY maintenance_windows_tenant ON maintenance_windows FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); ALTER TABLE maintenance_targets ENABLE ROW LEVEL SECURITY; ALTER TABLE maintenance_targets FORCE ROW LEVEL SECURITY; CREATE POLICY maintenance_targets_tenant ON maintenance_targets FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); -- +goose Down DROP TABLE maintenance_targets; DROP TABLE maintenance_windows;