Add an append-only ledger of entitlement set rule changes with per-pool effect rows, a preview-and-commit rule change flow, and an automatic drain that settles deferred recomputations. Rules gain a tier reduction policy, resource keys declare over-limit behavior, and the materializer now lowers limits when a rule stops applying. Add entitlement set rule change ledger and preview flow Add an append-only ledger of entitlement set rule changes with a preview-and-commit operator flow. Rule writes now go through an enclosed `core.commit_rule_change` function that files an act row and one obligation per carrying pool, with a drain workflow settling deferred recomputations. The preview dry-runs the materializer with a rule overlay and renders per-pool buckets, reduction-policy disclosures, and provider over-limit consequences. Materializing transactions take a shared advisory rendezvous that rule changes hold exclusively, enforced by a possession assertion. Add History and Entitlement changes surfaces, a rule-less warning on five product-selection surfaces, and a `tier_reduction_policy` column that gates FedWiki parking.
253 lines
7.1 KiB
Go
253 lines
7.1 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: resource_pools.sql
|
|
|
|
package entitlements
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const createResourcePool = `-- name: CreateResourcePool :one
|
|
INSERT INTO core.resource_pools (org_id, name, pool_type, is_auto_managed, key)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at
|
|
`
|
|
|
|
type CreateResourcePoolParams struct {
|
|
OrgID string `json:"org_id"`
|
|
Name string `json:"name"`
|
|
PoolType string `json:"pool_type"`
|
|
IsAutoManaged bool `json:"is_auto_managed"`
|
|
Key sql.NullString `json:"key"`
|
|
}
|
|
|
|
// `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.
|
|
func (q *Queries) CreateResourcePool(ctx context.Context, arg CreateResourcePoolParams) (ResourcePool, error) {
|
|
row := q.db.QueryRowContext(ctx, createResourcePool,
|
|
arg.OrgID,
|
|
arg.Name,
|
|
arg.PoolType,
|
|
arg.IsAutoManaged,
|
|
arg.Key,
|
|
)
|
|
var i ResourcePool
|
|
err := row.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getDefaultPoolByOrgID = `-- name: GetDefaultPoolByOrgID :one
|
|
SELECT pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at FROM core.resource_pools
|
|
WHERE org_id = $1 AND pool_type = 'default' AND status = 'active'
|
|
LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetDefaultPoolByOrgID(ctx context.Context, orgID string) (ResourcePool, error) {
|
|
row := q.db.QueryRowContext(ctx, getDefaultPoolByOrgID, orgID)
|
|
var i ResourcePool
|
|
err := row.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getResourcePoolByID = `-- name: GetResourcePoolByID :one
|
|
SELECT pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at FROM core.resource_pools
|
|
WHERE pool_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetResourcePoolByID(ctx context.Context, poolID string) (ResourcePool, error) {
|
|
row := q.db.QueryRowContext(ctx, getResourcePoolByID, poolID)
|
|
var i ResourcePool
|
|
err := row.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getResourcePoolByOrgAndKey = `-- name: GetResourcePoolByOrgAndKey :one
|
|
SELECT pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at FROM core.resource_pools
|
|
WHERE org_id = $1 AND key = $2
|
|
`
|
|
|
|
type GetResourcePoolByOrgAndKeyParams struct {
|
|
OrgID string `json:"org_id"`
|
|
Key sql.NullString `json:"key"`
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) GetResourcePoolByOrgAndKey(ctx context.Context, arg GetResourcePoolByOrgAndKeyParams) (ResourcePool, error) {
|
|
row := q.db.QueryRowContext(ctx, getResourcePoolByOrgAndKey, arg.OrgID, arg.Key)
|
|
var i ResourcePool
|
|
err := row.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getResourcePoolsByOrgID = `-- name: GetResourcePoolsByOrgID :many
|
|
SELECT pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at FROM core.resource_pools
|
|
WHERE org_id = $1 AND status = 'active'
|
|
ORDER BY pool_type ASC, name ASC
|
|
`
|
|
|
|
func (q *Queries) GetResourcePoolsByOrgID(ctx context.Context, orgID string) ([]ResourcePool, error) {
|
|
rows, err := q.db.QueryContext(ctx, getResourcePoolsByOrgID, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ResourcePool{}
|
|
for rows.Next() {
|
|
var i ResourcePool
|
|
if err := rows.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listResourcePoolsByOrgIDAnyStatus = `-- name: ListResourcePoolsByOrgIDAnyStatus :many
|
|
SELECT pool_id, org_id, name, key, pool_type, is_auto_managed, description, status, suspended_at, archived_at, created_at, updated_at FROM core.resource_pools
|
|
WHERE org_id = $1
|
|
ORDER BY pool_type ASC, name ASC
|
|
`
|
|
|
|
// 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".
|
|
func (q *Queries) ListResourcePoolsByOrgIDAnyStatus(ctx context.Context, orgID string) ([]ResourcePool, error) {
|
|
rows, err := q.db.QueryContext(ctx, listResourcePoolsByOrgIDAnyStatus, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ResourcePool{}
|
|
for rows.Next() {
|
|
var i ResourcePool
|
|
if err := rows.Scan(
|
|
&i.PoolID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Key,
|
|
&i.PoolType,
|
|
&i.IsAutoManaged,
|
|
&i.Description,
|
|
&i.Status,
|
|
&i.SuspendedAt,
|
|
&i.ArchivedAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockResourcePool = `-- name: LockResourcePool :one
|
|
SELECT pool_id FROM core.resource_pools
|
|
WHERE pool_id = $1
|
|
FOR UPDATE
|
|
`
|
|
|
|
// 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).
|
|
func (q *Queries) LockResourcePool(ctx context.Context, poolID string) (string, error) {
|
|
row := q.db.QueryRowContext(ctx, lockResourcePool, poolID)
|
|
var pool_id string
|
|
err := row.Scan(&pool_id)
|
|
return pool_id, err
|
|
}
|