97 lines
3.7 KiB
PL/PgSQL
97 lines
3.7 KiB
PL/PgSQL
-- +goose Up
|
|
ALTER TABLE status_pages
|
|
ADD COLUMN show_component_details boolean NOT NULL DEFAULT false;
|
|
|
|
CREATE TABLE status_page_groups (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
page_id uuid NOT NULL,
|
|
org_id uuid NOT NULL,
|
|
parent_id uuid,
|
|
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
|
|
position integer NOT NULL DEFAULT 0,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
UNIQUE (id, page_id, org_id),
|
|
CHECK (parent_id IS DISTINCT FROM id),
|
|
FOREIGN KEY (page_id, org_id) REFERENCES status_pages (id, org_id) ON DELETE CASCADE,
|
|
FOREIGN KEY (parent_id, page_id, org_id) REFERENCES status_page_groups (id, page_id, org_id) ON DELETE SET NULL (parent_id)
|
|
);
|
|
|
|
CREATE INDEX status_page_groups_page_idx ON status_page_groups (page_id, position, name);
|
|
CREATE INDEX status_page_groups_parent_idx ON status_page_groups (parent_id);
|
|
|
|
-- +goose StatementBegin
|
|
-- Rejects cycles and nesting deeper than eight levels within a status page.
|
|
CREATE FUNCTION status_page_groups_tree_check() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
depth integer;
|
|
BEGIN
|
|
IF NEW.parent_id IS NULL THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
WITH RECURSIVE up AS (
|
|
SELECT id, parent_id, 1 AS depth FROM status_page_groups WHERE id = NEW.parent_id
|
|
UNION ALL
|
|
SELECT g.id, g.parent_id, up.depth + 1 FROM status_page_groups g JOIN up ON g.id = up.parent_id
|
|
WHERE up.depth < 20
|
|
)
|
|
SELECT max(up.depth) INTO depth FROM up;
|
|
IF EXISTS (
|
|
WITH RECURSIVE up AS (
|
|
SELECT id, parent_id FROM status_page_groups WHERE id = NEW.parent_id
|
|
UNION
|
|
SELECT g.id, g.parent_id FROM status_page_groups g JOIN up ON g.id = up.parent_id
|
|
)
|
|
SELECT 1 FROM up WHERE id = NEW.id
|
|
) THEN
|
|
RAISE EXCEPTION 'status page group cycle' USING ERRCODE = 'check_violation', CONSTRAINT = 'status_page_groups_cycle';
|
|
END IF;
|
|
IF depth >= 8 THEN
|
|
RAISE EXCEPTION 'status page groups nested too deeply' USING ERRCODE = 'check_violation', CONSTRAINT = 'status_page_groups_depth';
|
|
END IF;
|
|
RETURN NEW;
|
|
END
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER status_page_groups_tree BEFORE INSERT OR UPDATE OF parent_id ON status_page_groups
|
|
FOR EACH ROW EXECUTE FUNCTION status_page_groups_tree_check();
|
|
|
|
ALTER TABLE status_page_groups ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE status_page_groups FORCE ROW LEVEL SECURITY;
|
|
CREATE POLICY status_page_groups_tenant ON status_page_groups 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 status_page_monitors ADD COLUMN group_id uuid;
|
|
ALTER TABLE status_page_monitors
|
|
ADD FOREIGN KEY (group_id, page_id, org_id) REFERENCES status_page_groups (id, page_id, org_id) ON DELETE SET NULL (group_id);
|
|
CREATE INDEX status_page_monitors_group_idx ON status_page_monitors (group_id);
|
|
|
|
WITH existing_sections AS (
|
|
SELECT page_id, org_id, section, min(position) AS first_pos
|
|
FROM status_page_monitors
|
|
WHERE section <> ''
|
|
GROUP BY page_id, org_id, section
|
|
)
|
|
INSERT INTO status_page_groups (page_id, org_id, name, position)
|
|
SELECT page_id, org_id, section,
|
|
(row_number() OVER (PARTITION BY page_id ORDER BY first_pos, section) - 1)::integer
|
|
FROM existing_sections;
|
|
|
|
UPDATE status_page_monitors spm
|
|
SET group_id = g.id
|
|
FROM status_page_groups g
|
|
WHERE g.page_id = spm.page_id
|
|
AND g.org_id = spm.org_id
|
|
AND g.parent_id IS NULL
|
|
AND g.name = spm.section
|
|
AND spm.section <> '';
|
|
|
|
-- +goose Down
|
|
ALTER TABLE status_page_monitors DROP COLUMN group_id;
|
|
DROP TABLE status_page_groups;
|
|
DROP FUNCTION status_page_groups_tree_check();
|
|
ALTER TABLE status_pages DROP COLUMN show_component_details;
|