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,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