Files
member-console/internal/entitlements/queries/pool_provision_ladders.sql
T
cgalo5758 8608c871de Apply the acceptance fixes and archive the change (10k.4)
Six review rounds on the September walk's 40 findings, executed as the
acceptance-fixes change (design D1 to D31) and archived as
openspec/changes/archive/2026-09-03-acceptance-fixes/ with its 20
deltas synced into openspec/specs.

Shell and conventions: the location trail on every page rooted at the
surface; click-opened help popovers; buttons by role with the pressed
rule retired and tertiary openers with a rotating plus; the code chip
and white card headers; Remove versus Delete on row actions.

Operator surface: three overview tiles, one Integrations card, the
getting-started banner as the first instance setting
(core.instance_settings, migration 00015); record creation on its own
page for products, entitlement sets, and plan ladders; Visibility as
one Public checkbox; the Stripe provider page; the plan ladder
validation page retired and ranks ascending; org-types default change
with dispositions, Discard, and the settle-cloning fix; the rebuilt
composite billing card; hints instead of placeholders; the person page's
identity-provider glyph on its field labels; non-UUID ids answer 404.

Found by review and fixed: a session now ends when its person no longer
exists (the foreign-key failure on a ladder reorder after a snapshot
rebuild); the CSRF cookie lives as long as the session and its toast
says the page is out of date; htmx's settle delay is 0 app-wide.

Verification: unit suites, browser walkthroughs, the screens baseline
(31 screens, accepted), and Lighthouse at 100 on both widths, recorded
in docs/operator-a11y-baseline.md. Three stale issue entries archived.
2026-09-03 17:00:14 -05:00

102 lines
4.5 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');
-- name: CountMultiActiveLadderAttachments :many
-- Maintenance check, unexposed since acceptance-fixes round 2: the database's exclusion constraint makes a violation impossible.
SELECT pool_id, plan_ladder_id, COUNT(*) AS active_count
FROM core.pool_provision_ladders
WHERE status = 'active'
GROUP BY pool_id, plan_ladder_id
HAVING COUNT(*) > 1;