Files
member-console/internal/entitlements/numeric_entitlement_usage.sql.go
T
cgalo5758 12f1d3fc00 Fix the four Slice 3 walk findings and page every operator list
Archives openspec change slice3-walk-fixes and syncs its five delta
specs (fedwiki-sites, entitlements, operator-panel-navigation,
operator-list-scale, ui-quality-gate).

- FedWiki site usage is read from active site rows in both quota
  readers; the reservation counter converges on the rows: raise-only
  after farm sync and inside the create quota check, exact at boot.
  The understated production counters repair on the first boot.
- The People tile caption excludes the reserved system person through
  the same query parameter the directory uses.
- The operator Domains live-claims list is a governed list: pages of
  50, true total, search over root name and organization, a
  pending/active facet.
- New lint rule table-without-list-controls refuses an unpaged
  page-body table unless it carries a list-scale exempt marker with a
  reason; six curated or detail tables carry one. Its first run caught
  the operator FedWiki sites list, which is now governed the same way.
- Entitlement-set rule copy: "Per unit", "Multiplied by the quantity
  purchased or granted."
2026-09-12 01:16:17 -05:00

242 lines
7.9 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_entitlement_usage.sql
package entitlements
import (
"context"
"database/sql"
)
const atomicDecrementUsage = `-- name: AtomicDecrementUsage :execresult
UPDATE core.numeric_entitlement_usage neu
SET current_usage = GREATEST(neu.current_usage - 1, 0)
WHERE neu.pool_id IN (
SELECT pa.pool_id FROM core.pool_assignments pa
WHERE pa.workspace_id = $1 AND pa.is_primary = TRUE AND pa.status = 'active'
)
AND neu.resource_key = $2
`
type AtomicDecrementUsageParams struct {
WorkspaceID string `json:"workspace_id"`
ResourceKey string `json:"resource_key"`
}
func (q *Queries) AtomicDecrementUsage(ctx context.Context, arg AtomicDecrementUsageParams) (sql.Result, error) {
return q.db.ExecContext(ctx, atomicDecrementUsage, arg.WorkspaceID, arg.ResourceKey)
}
const atomicIncrementUsage = `-- name: AtomicIncrementUsage :execresult
UPDATE core.numeric_entitlement_usage neu
SET current_usage = neu.current_usage + 1
WHERE neu.pool_id IN (
SELECT pa.pool_id FROM core.pool_assignments pa
WHERE pa.workspace_id = $1 AND pa.is_primary = TRUE AND pa.status = 'active'
)
AND neu.resource_key = $2
AND neu.current_usage < (
SELECT ne.resource_limit FROM core.numeric_entitlements ne
WHERE ne.entitlement_id = neu.entitlement_id
)
`
type AtomicIncrementUsageParams struct {
WorkspaceID string `json:"workspace_id"`
ResourceKey string `json:"resource_key"`
}
func (q *Queries) AtomicIncrementUsage(ctx context.Context, arg AtomicIncrementUsageParams) (sql.Result, error) {
return q.db.ExecContext(ctx, atomicIncrementUsage, arg.WorkspaceID, arg.ResourceKey)
}
const createNumericEntitlementUsage = `-- name: CreateNumericEntitlementUsage :one
INSERT INTO core.numeric_entitlement_usage (entitlement_id, pool_id, resource_key)
VALUES ($1, $2, $3)
RETURNING usage_id, entitlement_id, pool_id, resource_key, current_usage, current_period_start, current_period_end, last_reset_at, created_at, updated_at
`
type CreateNumericEntitlementUsageParams struct {
EntitlementID string `json:"entitlement_id"`
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
}
func (q *Queries) CreateNumericEntitlementUsage(ctx context.Context, arg CreateNumericEntitlementUsageParams) (NumericEntitlementUsage, error) {
row := q.db.QueryRowContext(ctx, createNumericEntitlementUsage, arg.EntitlementID, arg.PoolID, arg.ResourceKey)
var i NumericEntitlementUsage
err := row.Scan(
&i.UsageID,
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.CurrentUsage,
&i.CurrentPeriodStart,
&i.CurrentPeriodEnd,
&i.LastResetAt,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getUsageByPoolAndResource = `-- name: GetUsageByPoolAndResource :one
SELECT usage_id, entitlement_id, pool_id, resource_key, current_usage, current_period_start, current_period_end, last_reset_at, created_at, updated_at FROM core.numeric_entitlement_usage
WHERE pool_id = $1 AND resource_key = $2
`
type GetUsageByPoolAndResourceParams struct {
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
}
func (q *Queries) GetUsageByPoolAndResource(ctx context.Context, arg GetUsageByPoolAndResourceParams) (NumericEntitlementUsage, error) {
row := q.db.QueryRowContext(ctx, getUsageByPoolAndResource, arg.PoolID, arg.ResourceKey)
var i NumericEntitlementUsage
err := row.Scan(
&i.UsageID,
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.CurrentUsage,
&i.CurrentPeriodStart,
&i.CurrentPeriodEnd,
&i.LastResetAt,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const listNumericEntitlementUsageByPoolID = `-- name: ListNumericEntitlementUsageByPoolID :many
SELECT usage_id, entitlement_id, pool_id, resource_key, current_usage, current_period_start, current_period_end, last_reset_at, created_at, updated_at FROM core.numeric_entitlement_usage
WHERE pool_id = $1
ORDER BY resource_key ASC
`
func (q *Queries) ListNumericEntitlementUsageByPoolID(ctx context.Context, poolID string) ([]NumericEntitlementUsage, error) {
rows, err := q.db.QueryContext(ctx, listNumericEntitlementUsageByPoolID, poolID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []NumericEntitlementUsage{}
for rows.Next() {
var i NumericEntitlementUsage
if err := rows.Scan(
&i.UsageID,
&i.EntitlementID,
&i.PoolID,
&i.ResourceKey,
&i.CurrentUsage,
&i.CurrentPeriodStart,
&i.CurrentPeriodEnd,
&i.LastResetAt,
&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 listPoolUsageWithLimitsByPoolID = `-- name: ListPoolUsageWithLimitsByPoolID :many
SELECT
neu.resource_key,
neu.current_usage,
ne.resource_limit
FROM core.numeric_entitlement_usage neu
JOIN core.numeric_entitlements ne ON ne.entitlement_id = neu.entitlement_id
WHERE neu.pool_id = $1
ORDER BY neu.resource_key ASC
`
type ListPoolUsageWithLimitsByPoolIDRow struct {
ResourceKey string `json:"resource_key"`
CurrentUsage int64 `json:"current_usage"`
ResourceLimit int64 `json:"resource_limit"`
}
// Per-resource usage counters for a pool joined to their materialized
// limit (used vs limit per resource key), for the org-detail pools panel
// (ux-honest-surfaces: "pool status and usage are visible on the
// organization view"). A counter row exists only under a materialized
// numeric entitlement (invariant 9, resource-pools card), so the JOIN
// never orphans.
func (q *Queries) ListPoolUsageWithLimitsByPoolID(ctx context.Context, poolID string) ([]ListPoolUsageWithLimitsByPoolIDRow, error) {
rows, err := q.db.QueryContext(ctx, listPoolUsageWithLimitsByPoolID, poolID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListPoolUsageWithLimitsByPoolIDRow{}
for rows.Next() {
var i ListPoolUsageWithLimitsByPoolIDRow
if err := rows.Scan(&i.ResourceKey, &i.CurrentUsage, &i.ResourceLimit); 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 raiseNumericUsageTo = `-- name: RaiseNumericUsageTo :execresult
UPDATE core.numeric_entitlement_usage
SET current_usage = GREATEST(current_usage, $1)
WHERE pool_id = $2 AND resource_key = $3
`
type RaiseNumericUsageToParams struct {
Observed int64 `json:"observed"`
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
}
// Raise-only reconcile against the count of rows the owning integration holds.
// Never lowers: a create workflow holds a reservation between its gated
// increment and its row insert, and lowering here would erase it. Lowering on
// a real deletion stays with the decrement and drift paths.
// Creates no usage row; a missing counter remains the materializer's to create.
func (q *Queries) RaiseNumericUsageTo(ctx context.Context, arg RaiseNumericUsageToParams) (sql.Result, error) {
return q.db.ExecContext(ctx, raiseNumericUsageTo, arg.Observed, arg.PoolID, arg.ResourceKey)
}
const setNumericUsage = `-- name: SetNumericUsage :execresult
UPDATE core.numeric_entitlement_usage
SET current_usage = $1
WHERE pool_id = $2 AND resource_key = $3
`
type SetNumericUsageParams struct {
Observed int64 `json:"observed"`
PoolID string `json:"pool_id"`
ResourceKey string `json:"resource_key"`
}
// Exact set, for boot reconciliation only: at boot no create workflow holds an
// unmaterialized reservation, so the owned rows are the whole truth and an
// overstated counter (which refuses creates) can be brought down.
// Creates no usage row.
func (q *Queries) SetNumericUsage(ctx context.Context, arg SetNumericUsageParams) (sql.Result, error) {
return q.db.ExecContext(ctx, setNumericUsage, arg.Observed, arg.PoolID, arg.ResourceKey)
}