Files
member-console/internal/entitlements/queries/pool_provision_ladders.sql
T
cgalo5758 dd3962990b Adopt entity keys and add invoice numbers
Replace the entity slugs on organizations, workspaces, resource pools,
and
plan ladders with nullable `key` columns and add keys to products,
prices,
and entitlement sets. Rename `providers.slug` to `provider` and add
partial
unique indexes for system and org role names.

Assign invoice numbers per billing account from a gapless transactional
counter; Stripe's number moves to the invoice mapping as an external
reference.

Seeds, fixtures, and the operator lookup address rows by key, and the
returning-login resync no longer blanks a display name when the IdP
sends
no `name` claim.
2026-08-29 20:12:04 -05:00

94 lines
4.2 KiB
SQL

-- Writes to core.pool_provision_ladders 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: GetActiveLadderAttachmentByPoolLadder :one
SELECT * FROM core.pool_provision_ladders
WHERE pool_id = $1 AND plan_ladder_id = $2 AND status = 'active'
LIMIT 1;
-- name: GetLadderAttachmentsByProvision :many
SELECT * FROM core.pool_provision_ladders
WHERE provision_id = $1
ORDER BY activated_at ASC;
-- name: GetLadderAttachmentsByPool :many
SELECT * FROM core.pool_provision_ladders
WHERE pool_id = $1
ORDER BY activated_at DESC;
-- name: GetActiveAttachmentsByPool :many
SELECT * FROM core.pool_provision_ladders
WHERE pool_id = $1 AND status = 'active'
ORDER BY activated_at DESC;
-- name: CountActiveAttachmentsByLadder :one
SELECT COUNT(*) FROM core.pool_provision_ladders
WHERE plan_ladder_id = $1 AND status = 'active';
-- name: ListLivePlanPositionsByOrgType :many
-- The whole population for the org-type default-change classification in one
-- round trip: one row per (org, live plan attachment), plus a row with NULL
-- pool for orgs lacking a default pool and a row with NULL attachment for
-- plan-less pools (LEFT JOINs). Replaces a per-org query pair, which does not
-- scale past a few hundred organizations of one type.
SELECT o.org_id, o.name,
rp.pool_id,
l.provision_ladder_id, l.plan_ladder_id, l.provision_id,
l.status AS attachment_status,
p.product_id, p.grant_id, p.subscription_id, p.purchase_id,
g.grant_reason
FROM core.organizations o
LEFT JOIN core.resource_pools rp
ON rp.org_id = o.org_id AND rp.pool_type = 'default' AND rp.status = 'active'
LEFT JOIN core.pool_provision_ladders l
ON l.pool_id = rp.pool_id AND l.status IN ('active', 'suspended')
LEFT JOIN core.pool_provisions p ON p.provision_id = l.provision_id
LEFT JOIN core.grants g ON g.grant_id = p.grant_id
WHERE o.org_type = $1
ORDER BY o.name, o.org_id;
-- name: ListLiveTierHoldersByLadderProduct :many
-- The tier-removal preview/commit population in one round trip: every live
-- (active|suspended) junction on this ladder whose provision delivers this
-- product, with the org name and grant provenance so holders classify
-- per position source (default-sourced needs a disposition; any other source
-- is align-shrunk and never force-ended).
SELECT l.provision_ladder_id, l.provision_id, l.status AS attachment_status,
p.product_id, p.grant_id, p.subscription_id, p.purchase_id,
g.grant_reason,
rp.pool_id, o.org_id, o.name
FROM core.pool_provision_ladders l
JOIN core.pool_provisions p ON p.provision_id = l.provision_id
JOIN core.resource_pools rp ON rp.pool_id = l.pool_id
JOIN core.organizations o ON o.org_id = rp.org_id
LEFT JOIN core.grants g ON g.grant_id = p.grant_id
WHERE l.plan_ladder_id = $1 AND p.product_id = $2
AND l.status IN ('active', 'suspended')
ORDER BY o.name, o.org_id;
-- name: GetLivePlanAttachmentsWithSourceByPool :many
-- Live (active|suspended) plan-ladder attachments for a pool together with
-- their source provenance, for the org-type default-change classification:
-- a grant-sourced row with grant_reason = 'default' marks the pool as holding
-- the outgoing default (bucket 2); any other live row is another source's
-- plan (bucket 3); no rows at all is a plan-less pool (bucket 1).
SELECT l.provision_ladder_id, l.plan_ladder_id, l.provision_id,
l.status AS attachment_status,
p.product_id, p.grant_id, p.subscription_id, p.purchase_id,
g.grant_reason
FROM core.pool_provision_ladders l
JOIN core.pool_provisions p ON p.provision_id = l.provision_id
LEFT JOIN core.grants g ON g.grant_id = p.grant_id
WHERE l.pool_id = $1 AND l.status IN ('active', 'suspended')
ORDER BY l.activated_at DESC;
-- name: CountActiveAttachmentsByTier :one
SELECT COUNT(*) FROM core.pool_provision_ladders
WHERE plan_ladder_id = $1 AND product_id = $2 AND status = 'active';
-- name: AnyActiveLadderAttachment :one
-- Empty-validation-domain probe: the topology health strip claims health
-- only when at least one active attachment existed for the checks to cover.
SELECT EXISTS(SELECT 1 FROM core.pool_provision_ladders WHERE status = 'active');