Files
uptimesebi/internal/store/limits.sql.go
T
Sebastian UnterschützandClaude Opus 5.5 ca852c8299 feat(limits): limit the monitors shown on one status page
Platform admins set a default for how many monitors a status page may
show, and an override per organization, like the other limits. Saving a
page with more gives 422 page_monitor_limit; a page beyond a lowered
limit keeps its monitors but cannot grow. The page editor names the
limit and the organization's usage lists it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-25 23:21:26 +01:00

229 lines
6.3 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: limits.sql
package store
import (
"context"
"time"
"github.com/google/uuid"
)
const adminListOrgs = `-- name: AdminListOrgs :many
SELECT o.id, o.slug, o.name, o.created_at, o.suspended_at, o.suspended_reason,
o.limit_monitors, o.limit_status_pages, o.limit_regions_per_monitor, o.limit_own_regions_count,
o.limit_monitors_per_page,
(SELECT count(*) FROM monitors m WHERE m.org_id = o.id)::integer AS monitors,
(SELECT count(*) FROM status_pages p WHERE p.org_id = o.id)::integer AS status_pages,
coalesce(ow.id, '00000000-0000-0000-0000-000000000000'::uuid)::uuid AS owner_id,
coalesce(ow.email::text, '')::text AS owner_email, ow.disabled_at AS owner_disabled_at,
coalesce(ow.disabled_reason, '')::text AS owner_disabled_reason, coalesce(ow.is_admin, false)::boolean AS owner_is_admin,
ow.limit_orgs AS owner_limit_orgs,
coalesce(ow.owned, 0)::integer AS owner_orgs
FROM orgs o
LEFT JOIN LATERAL (
SELECT u.id, u.email, u.disabled_at, u.disabled_reason, u.is_admin, u.limit_orgs,
(SELECT count(*) FROM memberships m2 WHERE m2.user_id = u.id AND m2.is_owner) AS owned
FROM memberships m JOIN users u ON u.id = m.user_id
WHERE m.org_id = o.id AND m.is_owner ORDER BY m.created_at LIMIT 1
) ow ON true
WHERE ($1::text = ''
OR o.name ILIKE '%' || $1 || '%' OR o.slug::text ILIKE '%' || $1 || '%'
OR coalesce(ow.email::text, '') ILIKE '%' || $1 || '%')
AND ($2::timestamptz IS NULL OR (o.created_at, o.id) < ($2, $3::uuid))
AND ($4::timestamptz IS NULL OR (o.created_at, o.id) > ($4, $5::uuid))
ORDER BY
CASE WHEN $4::timestamptz IS NULL THEN o.created_at END DESC,
CASE WHEN $4::timestamptz IS NULL THEN o.id END DESC,
o.created_at, o.id
LIMIT $6
`
type AdminListOrgsParams struct {
Search string
AfterT *time.Time
AfterID *uuid.UUID
BeforeT *time.Time
BeforeID *uuid.UUID
MaxRows int32
}
type AdminListOrgsRow struct {
ID uuid.UUID
Slug string
Name string
CreatedAt time.Time
SuspendedAt *time.Time
SuspendedReason string
LimitMonitors *int32
LimitStatusPages *int32
LimitRegionsPerMonitor *int32
LimitOwnRegionsCount *bool
LimitMonitorsPerPage *int32
Monitors int32
StatusPages int32
OwnerID uuid.UUID
OwnerEmail string
OwnerDisabledAt *time.Time
OwnerDisabledReason string
OwnerIsAdmin bool
OwnerLimitOrgs *int32
OwnerOrgs int32
}
// System scope: organizations with their usage, limit overrides and owner,
// matching search in name, slug or owner email; newest first, a page from a
// keyset cursor.
func (q *Queries) AdminListOrgs(ctx context.Context, arg AdminListOrgsParams) ([]AdminListOrgsRow, error) {
rows, err := q.db.Query(ctx, adminListOrgs,
arg.Search,
arg.AfterT,
arg.AfterID,
arg.BeforeT,
arg.BeforeID,
arg.MaxRows,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []AdminListOrgsRow{}
for rows.Next() {
var i AdminListOrgsRow
if err := rows.Scan(
&i.ID,
&i.Slug,
&i.Name,
&i.CreatedAt,
&i.SuspendedAt,
&i.SuspendedReason,
&i.LimitMonitors,
&i.LimitStatusPages,
&i.LimitRegionsPerMonitor,
&i.LimitOwnRegionsCount,
&i.LimitMonitorsPerPage,
&i.Monitors,
&i.StatusPages,
&i.OwnerID,
&i.OwnerEmail,
&i.OwnerDisabledAt,
&i.OwnerDisabledReason,
&i.OwnerIsAdmin,
&i.OwnerLimitOrgs,
&i.OwnerOrgs,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const countMonitors = `-- name: CountMonitors :one
SELECT count(*) FROM monitors WHERE org_id = $1
`
func (q *Queries) CountMonitors(ctx context.Context, orgID uuid.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countMonitors, orgID)
var count int64
err := row.Scan(&count)
return count, err
}
const countStatusPageMonitors = `-- name: CountStatusPageMonitors :one
SELECT count(*) FROM status_page_monitors WHERE page_id = $1
`
func (q *Queries) CountStatusPageMonitors(ctx context.Context, pageID uuid.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countStatusPageMonitors, pageID)
var count int64
err := row.Scan(&count)
return count, err
}
const countStatusPages = `-- name: CountStatusPages :one
SELECT count(*) FROM status_pages WHERE org_id = $1
`
func (q *Queries) CountStatusPages(ctx context.Context, orgID uuid.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countStatusPages, orgID)
var count int64
err := row.Scan(&count)
return count, err
}
const lockOrgUsage = `-- name: LockOrgUsage :exec
SELECT pg_advisory_xact_lock(hashtextextended('usage:' || $1::uuid::text, 0))
`
// Serializes creations counted against org's limits until the transaction
// ends.
func (q *Queries) LockOrgUsage(ctx context.Context, orgID uuid.UUID) error {
_, err := q.db.Exec(ctx, lockOrgUsage, orgID)
return err
}
const setOrgLimits = `-- name: SetOrgLimits :one
UPDATE orgs SET limit_monitors = $1, limit_status_pages = $2,
limit_regions_per_monitor = $3, limit_own_regions_count = $4,
limit_monitors_per_page = $5
WHERE id = $6
RETURNING id, slug
`
type SetOrgLimitsParams struct {
Monitors *int32
StatusPages *int32
RegionsPerMonitor *int32
OwnRegionsCount *bool
MonitorsPerPage *int32
ID uuid.UUID
}
type SetOrgLimitsRow struct {
ID uuid.UUID
Slug string
}
func (q *Queries) SetOrgLimits(ctx context.Context, arg SetOrgLimitsParams) (SetOrgLimitsRow, error) {
row := q.db.QueryRow(ctx, setOrgLimits,
arg.Monitors,
arg.StatusPages,
arg.RegionsPerMonitor,
arg.OwnRegionsCount,
arg.MonitorsPerPage,
arg.ID,
)
var i SetOrgLimitsRow
err := row.Scan(&i.ID, &i.Slug)
return i, err
}
const setUserOrgLimit = `-- name: SetUserOrgLimit :one
UPDATE users SET limit_orgs = $1 WHERE id = $2
RETURNING id, email
`
type SetUserOrgLimitParams struct {
Orgs *int32
ID uuid.UUID
}
type SetUserOrgLimitRow struct {
ID uuid.UUID
Email string
}
func (q *Queries) SetUserOrgLimit(ctx context.Context, arg SetUserOrgLimitParams) (SetUserOrgLimitRow, error) {
row := q.db.QueryRow(ctx, setUserOrgLimit, arg.Orgs, arg.ID)
var i SetUserOrgLimitRow
err := row.Scan(&i.ID, &i.Email)
return i, err
}