Files
member-console/internal/entitlements/queries/pool_provisions.sql
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

81 lines
3.2 KiB
SQL

-- Writes to core.pool_provisions go through the conferral functions
-- (queries/conferral.sql) alone; core_writer holds no direct DML on this
-- table (migration 00005 enclosure). Only read paths live here.
-- name: GetPoolProvisionsByPoolID :many
SELECT * FROM core.pool_provisions
WHERE pool_id = $1
ORDER BY created_at ASC;
-- name: GetActivePoolProvisionsByPoolID :many
SELECT * FROM core.pool_provisions
WHERE pool_id = $1 AND status = 'active'
ORDER BY created_at ASC;
-- name: GetPoolProvisionByGrantID :one
SELECT * FROM core.pool_provisions
WHERE grant_id = $1;
-- name: GetPoolProvisionByProvisionID :one
SELECT * FROM core.pool_provisions
WHERE provision_id = $1;
-- name: GetPoolProvisionBySubscriptionID :one
SELECT * FROM core.pool_provisions
WHERE subscription_id = $1;
-- name: GetLivePoolProvisionsBySubscriptionID :many
-- Live (non-ended) provisions of a subscription, one per product. Reconcile
-- diffs these against the subscription's desired-by-product line items.
SELECT * FROM core.pool_provisions
WHERE subscription_id = $1 AND status <> 'ended'
ORDER BY created_at ASC;
-- name: GetLivePoolProvisionsByProductID :many
-- Live (non-ended) provisions delivering a product. Tier add/remove aligns
-- each of these to the product's new conferral shape.
SELECT * FROM core.pool_provisions
WHERE product_id = $1 AND status <> 'ended'
ORDER BY created_at ASC;
-- name: ListPoolsCarryingSet :many
-- The carrying population of a set: pools with an active provision of it, one
-- row per pool, ascending by pool_id, which is both the predicate and the lock
-- order a commit takes them in (Decision 143).
SELECT DISTINCT pp.pool_id, rp.org_id, o.name AS org_name
FROM core.pool_provisions pp
JOIN core.resource_pools rp ON rp.pool_id = pp.pool_id
JOIN core.organizations o ON o.org_id = rp.org_id
WHERE pp.entitlement_set_id = $1 AND pp.status = 'active'
ORDER BY pp.pool_id ASC;
-- name: CountPoolsCarryingSet :one
SELECT COUNT(DISTINCT pp.pool_id)::BIGINT AS pool_count,
COUNT(DISTINCT rp.org_id)::BIGINT AS org_count
FROM core.pool_provisions pp
JOIN core.resource_pools rp ON rp.pool_id = pp.pool_id
WHERE pp.entitlement_set_id = $1 AND pp.status = 'active';
-- name: CountSuspendedOnlyPoolsCarryingSet :one
-- Pools holding the set only through suspended provisions: they carry nothing
-- today, so a change does not reach them until the provision resumes.
SELECT COUNT(*)::BIGINT AS pool_count FROM (
SELECT pp.pool_id
FROM core.pool_provisions pp
WHERE pp.entitlement_set_id = $1
GROUP BY pp.pool_id
HAVING BOOL_OR(pp.status = 'suspended') AND NOT BOOL_OR(pp.status = 'active')
) s;
-- name: CountCarryingPoolsAtOrOverLimit :one
-- Carrying pools whose recorded usage of one key has reached or passed the
-- pool's materialized limit: the aggregate-mode preview's one exact fact.
SELECT COUNT(DISTINCT pp.pool_id)::BIGINT AS pool_count
FROM core.pool_provisions pp
JOIN core.numeric_entitlements ne ON ne.pool_id = pp.pool_id
JOIN core.numeric_entitlement_usage u ON u.entitlement_id = ne.entitlement_id
WHERE pp.entitlement_set_id = sqlc.arg(set_id)
AND pp.status = 'active'
AND ne.resource_key = sqlc.arg(resource_key)
AND u.current_usage >= ne.resource_limit;