-- name: CreateMaintenance :one INSERT INTO maintenance_windows (org_id, title, description, starts_at, duration_seconds, time_zone, recurrence, every, weekdays, until) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *; -- name: UpdateMaintenance :one UPDATE maintenance_windows SET title = $2, description = $3, starts_at = $4, duration_seconds = $5, time_zone = $6, recurrence = $7, every = $8, weekdays = $9, until = $10, updated_at = now() WHERE id = $1 RETURNING *; -- name: DeleteMaintenance :execrows DELETE FROM maintenance_windows WHERE id = $1; -- name: GetMaintenance :one SELECT * FROM maintenance_windows WHERE id = $1; -- name: ListMaintenance :many SELECT * FROM maintenance_windows ORDER BY starts_at, title; -- name: ClearMaintenanceTargets :exec DELETE FROM maintenance_targets WHERE window_id = $1; -- name: AddMaintenanceTarget :exec INSERT INTO maintenance_targets (window_id, org_id, monitor_id, group_id) VALUES ($1, $2, $3, $4); -- name: ListMaintenanceTargets :many SELECT * FROM maintenance_targets; -- name: MaintenanceForMonitors :many -- System scope: windows covering any of the monitors, directly or through -- their group or one of its ancestors. WITH RECURSIVE chain AS ( SELECT m.id AS monitor_id, m.group_id FROM monitors m WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.group_id IS NOT NULL UNION ALL SELECT c.monitor_id, g.parent_id FROM chain c JOIN monitor_groups g ON g.id = c.group_id WHERE g.parent_id IS NOT NULL ), covered AS ( SELECT t.window_id, t.monitor_id FROM maintenance_targets t WHERE t.monitor_id = ANY (sqlc.arg(ids)::uuid[]) UNION SELECT t.window_id, c.monitor_id FROM maintenance_targets t JOIN chain c ON c.group_id = t.group_id ) SELECT c.monitor_id, w.id, w.title, w.description, w.starts_at, w.duration_seconds, w.time_zone, w.recurrence, w.every, w.weekdays, w.until FROM covered c JOIN maintenance_windows w ON w.id = c.window_id; -- name: ListMaintenancePage :many -- Latest start first, a page from a keyset cursor (see paging.go). SELECT * FROM maintenance_windows WHERE true AND (sqlc.narg(after_t)::timestamptz IS NULL OR (starts_at, id) < (sqlc.narg(after_t), sqlc.narg(after_id)::uuid)) AND (sqlc.narg(before_t)::timestamptz IS NULL OR (starts_at, id) > (sqlc.narg(before_t), sqlc.narg(before_id)::uuid)) ORDER BY CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN starts_at END DESC, CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN id END DESC, starts_at, id LIMIT sqlc.arg(max_rows);