-- name: CreateResourcePool :one -- `key` is the optional declarative address (entity-keys §3), unique within -- the organization because a pool is a child of one. The console writes it -- for exactly one row it creates by design: every organization's default -- pool, keyed `default` (§4). Any other pool leaves it NULL unless a seed or -- an API client names one. INSERT INTO core.resource_pools (org_id, name, pool_type, is_auto_managed, key) VALUES ($1, $2, $3, $4, sqlc.narg(key)) RETURNING *; -- name: GetResourcePoolByOrgAndKey :one -- Child key resolver (entity-keys §5). uq_resource_pools_org_id_key -- guarantees at most one row matches; `default` resolves the pool the -- console creates for every organization. SELECT * FROM core.resource_pools WHERE org_id = $1 AND key = $2; -- name: GetResourcePoolByID :one SELECT * FROM core.resource_pools WHERE pool_id = $1; -- name: GetDefaultPoolByOrgID :one SELECT * FROM core.resource_pools WHERE org_id = $1 AND pool_type = 'default' AND status = 'active' LIMIT 1; -- name: GetResourcePoolsByOrgID :many SELECT * FROM core.resource_pools WHERE org_id = $1 AND status = 'active' ORDER BY pool_type ASC, name ASC; -- name: ListResourcePoolsByOrgIDAnyStatus :many -- Every pool row for the organization regardless of status -- unlike -- GetResourcePoolsByOrgID's active-only filter, this does not hide a -- suspended or archived pool from view. The org-detail composite uses -- this for its pools panel so an operator sees a pool's actual status -- (ux-honest-surfaces requirement: "pool status and usage are visible on -- the organization view") instead of the pool silently vanishing from the -- list, and so "this org has zero pool rows" (the pool-less breakage -- case) is distinguishable from "this org's only pool exists but is not -- active". SELECT * FROM core.resource_pools WHERE org_id = $1 ORDER BY pool_type ASC, name ASC; -- name: LockResourcePool :one -- The pool row taken FOR UPDATE: the lock a rule commit below the cap and -- each drained pool above it hold while they materialize, taken in ascending -- pool_id order (Decision 144). SELECT pool_id FROM core.resource_pools WHERE pool_id = $1 FOR UPDATE;