feat(store): add nested monitor groups

A trigger rejects cycles and nesting deeper than eight levels; composite
foreign keys keep groups and their monitors in one organization.

Refs #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 09:19:55 +01:00
1 parent 6e2f8d0626
commit 2d7f326d38
7 files changed
+306 -11

No files matched your search

+10
View File
@@ -118,6 +118,16 @@ type Monitor struct {
CreatedAt time.Time
UpdatedAt time.Time
Confirmations int32
GroupID *uuid.UUID
}
type MonitorGroup struct {
ID uuid.UUID
OrgID uuid.UUID
ParentID *uuid.UUID
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
type MonitorRegion struct {
+169
View File
@@ -0,0 +1,169 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: monitor_groups.sql
package store
import (
"context"
"github.com/google/uuid"
)
const createGroup = `-- name: CreateGroup :one
INSERT INTO monitor_groups (org_id, parent_id, name) VALUES ($1, $2, $3)
RETURNING id, org_id, parent_id, name, created_at, updated_at
`
type CreateGroupParams struct {
OrgID uuid.UUID
ParentID *uuid.UUID
Name string
}
func (q *Queries) CreateGroup(ctx context.Context, arg CreateGroupParams) (MonitorGroup, error) {
row := q.db.QueryRow(ctx, createGroup, arg.OrgID, arg.ParentID, arg.Name)
var i MonitorGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.ParentID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const deleteAssignmentsForResource = `-- name: DeleteAssignmentsForResource :exec
DELETE FROM role_assignments WHERE resource_type = $1 AND resource_id = $2
`
type DeleteAssignmentsForResourceParams struct {
ResourceType *string
ResourceID *uuid.UUID
}
func (q *Queries) DeleteAssignmentsForResource(ctx context.Context, arg DeleteAssignmentsForResourceParams) error {
_, err := q.db.Exec(ctx, deleteAssignmentsForResource, arg.ResourceType, arg.ResourceID)
return err
}
const deleteGroup = `-- name: DeleteGroup :execrows
DELETE FROM monitor_groups WHERE id = $1
`
func (q *Queries) DeleteGroup(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, deleteGroup, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getGroup = `-- name: GetGroup :one
SELECT id, org_id, parent_id, name, created_at, updated_at FROM monitor_groups WHERE id = $1
`
func (q *Queries) GetGroup(ctx context.Context, id uuid.UUID) (MonitorGroup, error) {
row := q.db.QueryRow(ctx, getGroup, id)
var i MonitorGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.ParentID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const listGroups = `-- name: ListGroups :many
SELECT id, org_id, parent_id, name, created_at, updated_at FROM monitor_groups ORDER BY name
`
func (q *Queries) ListGroups(ctx context.Context) ([]MonitorGroup, error) {
rows, err := q.db.Query(ctx, listGroups)
if err != nil {
return nil, err
}
defer rows.Close()
items := []MonitorGroup{}
for rows.Next() {
var i MonitorGroup
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.ParentID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const regroupMonitors = `-- name: RegroupMonitors :exec
UPDATE monitors SET group_id = $1, updated_at = now()
WHERE group_id = $2
`
type RegroupMonitorsParams struct {
NewGroup *uuid.UUID
OldGroup *uuid.UUID
}
func (q *Queries) RegroupMonitors(ctx context.Context, arg RegroupMonitorsParams) error {
_, err := q.db.Exec(ctx, regroupMonitors, arg.NewGroup, arg.OldGroup)
return err
}
const reparentGroups = `-- name: ReparentGroups :exec
UPDATE monitor_groups SET parent_id = $1, updated_at = now()
WHERE parent_id = $2
`
type ReparentGroupsParams struct {
NewParent *uuid.UUID
OldParent *uuid.UUID
}
// Moves the subgroups of a group that is about to be deleted.
func (q *Queries) ReparentGroups(ctx context.Context, arg ReparentGroupsParams) error {
_, err := q.db.Exec(ctx, reparentGroups, arg.NewParent, arg.OldParent)
return err
}
const updateGroup = `-- name: UpdateGroup :one
UPDATE monitor_groups SET name = $2, parent_id = $3, updated_at = now()
WHERE id = $1
RETURNING id, org_id, parent_id, name, created_at, updated_at
`
type UpdateGroupParams struct {
ID uuid.UUID
Name string
ParentID *uuid.UUID
}
func (q *Queries) UpdateGroup(ctx context.Context, arg UpdateGroupParams) (MonitorGroup, error) {
row := q.db.QueryRow(ctx, updateGroup, arg.ID, arg.Name, arg.ParentID)
var i MonitorGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.ParentID,
&i.Name,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
+17 -8
View File
@@ -56,9 +56,9 @@ func (q *Queries) AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow
}
const createMonitor = `-- name: CreateMonitor :one
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations, group_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations, group_id
`
type CreateMonitorParams struct {
@@ -71,6 +71,7 @@ type CreateMonitorParams struct {
Config []byte
Enabled bool
Confirmations int32
GroupID *uuid.UUID
}
func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error) {
@@ -84,6 +85,7 @@ func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (M
arg.Config,
arg.Enabled,
arg.Confirmations,
arg.GroupID,
)
var i Monitor
err := row.Scan(
@@ -99,6 +101,7 @@ func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (M
&i.CreatedAt,
&i.UpdatedAt,
&i.Confirmations,
&i.GroupID,
)
return i, err
}
@@ -125,7 +128,7 @@ func (q *Queries) DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID)
}
const getMonitor = `-- name: GetMonitor :one
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations FROM monitors WHERE id = $1
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations, group_id FROM monitors WHERE id = $1
`
func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error) {
@@ -144,6 +147,7 @@ func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error)
&i.CreatedAt,
&i.UpdatedAt,
&i.Confirmations,
&i.GroupID,
)
return i, err
}
@@ -185,7 +189,7 @@ func (q *Queries) ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEna
}
const listMonitors = `-- name: ListMonitors :many
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations FROM monitors ORDER BY name
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations, group_id FROM monitors ORDER BY name
`
func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
@@ -210,6 +214,7 @@ func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
&i.CreatedAt,
&i.UpdatedAt,
&i.Confirmations,
&i.GroupID,
); err != nil {
return nil, err
}
@@ -246,7 +251,7 @@ func (q *Queries) MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]uu
}
const monitorsForJobs = `-- name: MonitorsForJobs :many
SELECT m.id, m.org_id, m.name, m.type, m.target, m.interval_seconds, m.timeout_ms, m.config, m.enabled, m.created_at, m.updated_at, m.confirmations FROM monitors m
SELECT m.id, m.org_id, m.name, m.type, m.target, m.interval_seconds, m.timeout_ms, m.config, m.enabled, m.created_at, m.updated_at, m.confirmations, m.group_id FROM monitors m
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region_id = $1
WHERE m.id = ANY ($2::uuid[]) AND m.enabled
AND ($3::uuid IS NULL OR m.org_id = $3)
@@ -283,6 +288,7 @@ func (q *Queries) MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams
&i.CreatedAt,
&i.UpdatedAt,
&i.Confirmations,
&i.GroupID,
); err != nil {
return nil, err
}
@@ -351,9 +357,9 @@ func (q *Queries) MonitorsForResults(ctx context.Context, arg MonitorsForResults
const updateMonitor = `-- name: UpdateMonitor :one
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, updated_at = now()
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, group_id = $10, updated_at = now()
WHERE id = $1
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations, group_id
`
type UpdateMonitorParams struct {
@@ -366,6 +372,7 @@ type UpdateMonitorParams struct {
Config []byte
Enabled bool
Confirmations int32
GroupID *uuid.UUID
}
func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error) {
@@ -379,6 +386,7 @@ func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (M
arg.Config,
arg.Enabled,
arg.Confirmations,
arg.GroupID,
)
var i Monitor
err := row.Scan(
@@ -394,6 +402,7 @@ func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (M
&i.CreatedAt,
&i.UpdatedAt,
&i.Confirmations,
&i.GroupID,
)
return i, err
}
+9
View File
@@ -29,6 +29,7 @@ type Querier interface {
CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error)
CreateAssignment(ctx context.Context, arg CreateAssignmentParams) (RoleAssignment, error)
CreateChannel(ctx context.Context, arg CreateChannelParams) (NotificationChannel, error)
CreateGroup(ctx context.Context, arg CreateGroupParams) (MonitorGroup, error)
CreateIdentity(ctx context.Context, arg CreateIdentityParams) error
CreateInvitation(ctx context.Context, arg CreateInvitationParams) (Invitation, error)
CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error)
@@ -43,7 +44,9 @@ type Querier interface {
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error)
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAssignmentsForResource(ctx context.Context, arg DeleteAssignmentsForResourceParams) error
DeleteChannel(ctx context.Context, id uuid.UUID) (int64, error)
DeleteGroup(ctx context.Context, id uuid.UUID) (int64, error)
DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error)
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
@@ -68,6 +71,7 @@ type Querier interface {
GetAgentByHash(ctx context.Context, tokenHash []byte) (GetAgentByHashRow, error)
GetAssignment(ctx context.Context, id uuid.UUID) (RoleAssignment, error)
GetChannel(ctx context.Context, id uuid.UUID) (NotificationChannel, error)
GetGroup(ctx context.Context, id uuid.UUID) (MonitorGroup, error)
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error)
GetInvitationByHash(ctx context.Context, tokenHash []byte) (GetInvitationByHashRow, error)
@@ -103,6 +107,7 @@ type Querier interface {
// schedule.
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
ListGroups(ctx context.Context) ([]MonitorGroup, error)
ListIncidents(ctx context.Context) ([]ListIncidentsRow, error)
ListMonitors(ctx context.Context) ([]Monitor, error)
ListOrgMembers(ctx context.Context, orgID uuid.UUID) ([]ListOrgMembersRow, error)
@@ -139,7 +144,10 @@ type Querier interface {
OpenIncidents(ctx context.Context) ([]Incident, error)
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
RegroupMonitors(ctx context.Context, arg RegroupMonitorsParams) error
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
// Moves the subgroups of a group that is about to be deleted.
ReparentGroups(ctx context.Context, arg ReparentGroupsParams) error
ResolveIncident(ctx context.Context, arg ResolveIncidentParams) (Incident, error)
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
// Changing the domain drops a previous verification.
@@ -156,6 +164,7 @@ type Querier interface {
TouchAgent(ctx context.Context, arg TouchAgentParams) error
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
UpdateChannel(ctx context.Context, arg UpdateChannelParams) (NotificationChannel, error)
UpdateGroup(ctx context.Context, arg UpdateGroupParams) (MonitorGroup, error)
UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error)
UpdateOrgPolicy(ctx context.Context, arg UpdateOrgPolicyParams) (Org, error)
UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error)
+29
View File
@@ -0,0 +1,29 @@
-- name: CreateGroup :one
INSERT INTO monitor_groups (org_id, parent_id, name) VALUES ($1, $2, $3)
RETURNING *;
-- name: UpdateGroup :one
UPDATE monitor_groups SET name = $2, parent_id = $3, updated_at = now()
WHERE id = $1
RETURNING *;
-- name: GetGroup :one
SELECT * FROM monitor_groups WHERE id = $1;
-- name: ListGroups :many
SELECT * FROM monitor_groups ORDER BY name;
-- name: ReparentGroups :exec
-- Moves the subgroups of a group that is about to be deleted.
UPDATE monitor_groups SET parent_id = sqlc.narg(new_parent), updated_at = now()
WHERE parent_id = sqlc.arg(old_parent);
-- name: RegroupMonitors :exec
UPDATE monitors SET group_id = sqlc.narg(new_group), updated_at = now()
WHERE group_id = sqlc.arg(old_group);
-- name: DeleteGroup :execrows
DELETE FROM monitor_groups WHERE id = $1;
-- name: DeleteAssignmentsForResource :exec
DELETE FROM role_assignments WHERE resource_type = $1 AND resource_id = $2;
+3 -3
View File
@@ -1,11 +1,11 @@
-- name: CreateMonitor :one
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations, group_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *;
-- name: UpdateMonitor :one
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, updated_at = now()
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, group_id = $10, updated_at = now()
WHERE id = $1
RETURNING *;