Files
member-console/internal/entitlements/numeric_entitlements.sql.go
T
cgalo5758 3727ff31d8 Add entitlement set rule change ledger and preview flow
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.
2026-09-15 03:53:28 -05:00

188 lines
5.5 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: numeric_entitlements.sql
package entitlements
import (
"context"
"github.com/lib/pq"
)
const createNumericEntitlement = `-- name: CreateNumericEntitlement :one
INSERT INTO core.numeric_entitlements (pool_id, resource_key, entitlement_type, resource_limit)
VALUES ($1, $2, $3, $4)
RETURNING entitlement_id, pool_id, resource_key, entitlement_type, resource_limit, reset_period, created_at, updated_at
`
type CreateNumericEntitlementParams struct {
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
EntitlementType string `json:"entitlement_type"`
ResourceLimit int64 `json:"resource_limit"`
}
func (q *Queries) CreateNumericEntitlement(ctx context.Context, arg CreateNumericEntitlementParams) (NumericEntitlement, error) {
row := q.db.QueryRowContext(ctx, createNumericEntitlement,
arg.PoolID,
arg.ResourceKey,
arg.EntitlementType,
arg.ResourceLimit,
)
var i NumericEntitlement
err := row.Scan(
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.EntitlementType,
&i.ResourceLimit,
&i.ResetPeriod,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getNumericEntitlementByPoolAndResource = `-- name: GetNumericEntitlementByPoolAndResource :one
SELECT entitlement_id, pool_id, resource_key, entitlement_type, resource_limit, reset_period, created_at, updated_at FROM core.numeric_entitlements
WHERE pool_id = $1 AND resource_key = $2
`
type GetNumericEntitlementByPoolAndResourceParams struct {
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
}
func (q *Queries) GetNumericEntitlementByPoolAndResource(ctx context.Context, arg GetNumericEntitlementByPoolAndResourceParams) (NumericEntitlement, error) {
row := q.db.QueryRowContext(ctx, getNumericEntitlementByPoolAndResource, arg.PoolID, arg.ResourceKey)
var i NumericEntitlement
err := row.Scan(
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.EntitlementType,
&i.ResourceLimit,
&i.ResetPeriod,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const listNumericEntitlementStateForPools = `-- name: ListNumericEntitlementStateForPools :many
SELECT ne.pool_id, ne.resource_key, ne.resource_limit,
COALESCE(u.current_usage, 0)::BIGINT AS current_usage
FROM core.numeric_entitlements ne
LEFT JOIN core.numeric_entitlement_usage u ON u.entitlement_id = ne.entitlement_id
WHERE ne.pool_id = ANY($1::uuid[])
ORDER BY ne.pool_id ASC, ne.resource_key ASC
`
type ListNumericEntitlementStateForPoolsRow struct {
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
ResourceLimit int64 `json:"resource_limit"`
CurrentUsage int64 `json:"current_usage"`
}
// Read-only per-pool numeric state for a batch of pools, each row carrying the
// usage the limit is read against. The preview folds these without locking.
func (q *Queries) ListNumericEntitlementStateForPools(ctx context.Context, poolIds []string) ([]ListNumericEntitlementStateForPoolsRow, error) {
rows, err := q.db.QueryContext(ctx, listNumericEntitlementStateForPools, pq.Array(poolIds))
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListNumericEntitlementStateForPoolsRow{}
for rows.Next() {
var i ListNumericEntitlementStateForPoolsRow
if err := rows.Scan(
&i.PoolID,
&i.ResourceKey,
&i.ResourceLimit,
&i.CurrentUsage,
); 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 listNumericEntitlementsByPoolID = `-- name: ListNumericEntitlementsByPoolID :many
SELECT entitlement_id, pool_id, resource_key, entitlement_type, resource_limit, reset_period, created_at, updated_at FROM core.numeric_entitlements
WHERE pool_id = $1
ORDER BY resource_key ASC
`
func (q *Queries) ListNumericEntitlementsByPoolID(ctx context.Context, poolID string) ([]NumericEntitlement, error) {
rows, err := q.db.QueryContext(ctx, listNumericEntitlementsByPoolID, poolID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []NumericEntitlement{}
for rows.Next() {
var i NumericEntitlement
if err := rows.Scan(
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.EntitlementType,
&i.ResourceLimit,
&i.ResetPeriod,
&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 updateNumericEntitlementLimit = `-- name: UpdateNumericEntitlementLimit :one
UPDATE core.numeric_entitlements
SET resource_limit = $2
WHERE entitlement_id = $1
RETURNING entitlement_id, pool_id, resource_key, entitlement_type, resource_limit, reset_period, created_at, updated_at
`
type UpdateNumericEntitlementLimitParams struct {
EntitlementID string `json:"entitlement_id"`
ResourceLimit int64 `json:"resource_limit"`
}
func (q *Queries) UpdateNumericEntitlementLimit(ctx context.Context, arg UpdateNumericEntitlementLimitParams) (NumericEntitlement, error) {
row := q.db.QueryRowContext(ctx, updateNumericEntitlementLimit, arg.EntitlementID, arg.ResourceLimit)
var i NumericEntitlement
err := row.Scan(
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.EntitlementType,
&i.ResourceLimit,
&i.ResetPeriod,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}