feat(statuspage): add nested status page groups and optional component details
This commit is contained in:
1 parent
1c31003baf
commit
9b26b4150e
26 files changed
+4566
-760
No files matched your search
@@ -0,0 +1,96 @@
|
||||
-- +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;
|
||||
@@ -0,0 +1,90 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func TestStatusPageGroupsBackfill(t *testing.T) {
|
||||
dsn := os.Getenv("UPTIME_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("UPTIME_TEST_DATABASE_URL not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
if _, err := Up(ctx, dsn); err != nil {
|
||||
t.Fatalf("Up() error = %v", err)
|
||||
}
|
||||
conn, err := pgx.Connect(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("pgx.Connect() error = %v", err)
|
||||
}
|
||||
defer func() { _ = conn.Close(ctx) }()
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("conn.Begin() error = %v", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
if _, err := tx.Exec(ctx, "SET LOCAL app.scope = 'system'"); err != nil {
|
||||
t.Fatalf("SET LOCAL app.scope error = %v", err)
|
||||
}
|
||||
raw, err := files.ReadFile("sql/00041_status_page_groups_and_details.sql")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(00041) error = %v", err)
|
||||
}
|
||||
up, down, ok := strings.Cut(string(raw), "-- +goose Down")
|
||||
if !ok {
|
||||
t.Fatal("00041_status_page_groups_and_details.sql missing -- +goose Down")
|
||||
}
|
||||
if _, err := tx.Exec(ctx, down); err != nil {
|
||||
t.Fatalf("down migration error = %v", err)
|
||||
}
|
||||
|
||||
orgID := uuid.New()
|
||||
pageID := uuid.New()
|
||||
m1, m2, m3 := uuid.New(), uuid.New(), uuid.New()
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO orgs (id, slug, name) VALUES ($1, $2, 'Migration Org')`, orgID, "sp-mig-"+orgID.String()); err != nil {
|
||||
t.Fatalf("insert org error = %v", err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO status_pages (id, org_id, slug, title, domain_token) VALUES ($1, $2, $3, 'Status', 'tok')`, pageID, orgID, "sp-"+pageID.String()[:12]); err != nil {
|
||||
t.Fatalf("insert status_page error = %v", err)
|
||||
}
|
||||
for i, id := range []uuid.UUID{m1, m2, m3} {
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO monitors (id, org_id, name, type, target, interval_seconds, timeout_ms) VALUES ($1, $2, $3, 'tcp', 'example.test:443', 60, 1000)`, id, orgID, "M"+string(rune('1'+i))); err != nil {
|
||||
t.Fatalf("insert monitor %d error = %v", i, err)
|
||||
}
|
||||
}
|
||||
rows := []struct {
|
||||
id uuid.UUID
|
||||
section string
|
||||
pos int
|
||||
}{
|
||||
{id: m1, section: "Frontend", pos: 0},
|
||||
{id: m2, section: "Backend", pos: 1},
|
||||
{id: m3, section: "", pos: 2},
|
||||
}
|
||||
for _, r := range rows {
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO status_page_monitors (page_id, monitor_id, org_id, section, position) VALUES ($1, $2, $3, $4, $5)`, pageID, r.id, orgID, r.section, r.pos); err != nil {
|
||||
t.Fatalf("insert status_page_monitors(%s) error = %v", r.section, err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, up); err != nil {
|
||||
t.Fatalf("up migration error = %v", err)
|
||||
}
|
||||
var groups, linked, ungrouped int
|
||||
err = tx.QueryRow(ctx, `SELECT
|
||||
(SELECT count(*) FROM status_page_groups WHERE page_id = $1),
|
||||
(SELECT count(*) FROM status_page_monitors WHERE page_id = $1 AND group_id IS NOT NULL),
|
||||
(SELECT count(*) FROM status_page_monitors WHERE page_id = $1 AND group_id IS NULL)`, pageID).Scan(&groups, &linked, &ungrouped)
|
||||
if err != nil {
|
||||
t.Fatalf("verify counts error = %v", err)
|
||||
}
|
||||
if groups != 2 || linked != 2 || ungrouped != 1 {
|
||||
t.Fatalf("backfilled groups/linked/ungrouped = %d/%d/%d, want 2/2/1", groups, linked, ungrouped)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user