-- 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;