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.
67 lines
2.7 KiB
SQL
67 lines
2.7 KiB
SQL
-- name: GetPrice :one
|
|
SELECT * FROM core.prices
|
|
WHERE price_id = $1;
|
|
|
|
-- name: ListPricesByProduct :many
|
|
SELECT * FROM core.prices
|
|
WHERE product_id = $1 AND is_active = TRUE
|
|
ORDER BY created_at ASC, price_id ASC;
|
|
|
|
-- name: GetDefaultPriceByProduct :one
|
|
SELECT * FROM core.prices
|
|
WHERE product_id = $1 AND is_default = TRUE;
|
|
|
|
-- name: CreatePrice :one
|
|
-- The first price a product gets becomes its default automatically (there is
|
|
-- nothing else to offer members); subsequent prices are created non-default
|
|
-- and promoted explicitly via Clear/MarkDefaultPrice. The NOT EXISTS probe
|
|
-- also self-heals a product that somehow lost its default: the next price
|
|
-- created becomes it. The partial unique index
|
|
-- (idx_prices_one_default_per_product) backstops concurrent creation races.
|
|
--
|
|
-- `key` is the optional declarative address (entity-keys §3), unique within
|
|
-- the product because a price is a child of one. Stripe's `lookup_key` is
|
|
-- the same idea on the same entity. The operator create form does not
|
|
-- collect one, so a UI-created price leaves it NULL.
|
|
INSERT INTO core.prices (product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, is_default, key)
|
|
VALUES ($1, $2, $3, $4, $5, TRUE,
|
|
NOT EXISTS (SELECT 1 FROM core.prices p WHERE p.product_id = $1 AND p.is_default),
|
|
sqlc.narg(key))
|
|
RETURNING *;
|
|
|
|
-- name: GetPriceByProductAndKey :one
|
|
-- Child key resolver (entity-keys §5): the declarative address of a price is
|
|
-- its product's key plus its own. uq_prices_product_id_key guarantees at
|
|
-- most one row matches.
|
|
SELECT * FROM core.prices
|
|
WHERE product_id = $1 AND key = $2;
|
|
|
|
-- name: ClearDefaultPrice :exec
|
|
-- First half of the make-default handshake; run in the same transaction as
|
|
-- MarkDefaultPrice (clear before set, so the partial unique index never sees
|
|
-- two defaults for one product).
|
|
UPDATE core.prices
|
|
SET is_default = FALSE
|
|
WHERE product_id = $1 AND is_default;
|
|
|
|
-- name: MarkDefaultPrice :one
|
|
-- Second half of the make-default handshake. Scoped to (price_id, product_id)
|
|
-- so a stale/forged price ID from another product cannot be promoted; the
|
|
-- is_active guard keeps a deactivated price from becoming the default.
|
|
UPDATE core.prices
|
|
SET is_default = TRUE
|
|
WHERE price_id = $1 AND product_id = $2 AND is_active = TRUE
|
|
RETURNING *;
|
|
|
|
-- name: DeactivatePrice :one
|
|
-- Handlers must refuse to deactivate the default price (members would lose
|
|
-- the purchase path); the is_default guard here backstops that rule.
|
|
UPDATE core.prices
|
|
SET is_active = FALSE
|
|
WHERE price_id = $1 AND is_default = FALSE
|
|
RETURNING *;
|
|
|
|
-- name: AnyActivePrice :one
|
|
-- Setup-checklist existence probe.
|
|
SELECT EXISTS(SELECT 1 FROM core.prices WHERE is_active = TRUE);
|