Files
warmbly/internal/infrastructure/db/migrations/000149_managed_plans.up.sql
T
Matthew Meszaros 017f4cf60f feat: plans an operator can grant, visible in the admin panel (#467)
* feat: add operator-granted plans so a workspace can be paid without Stripe, surfaced in the admin panel as a badge, a filter and a card carrying who granted it and why, because the only alternative was writing a fake stripe subscription id into the database

* fix: hold a granted plan beside the paid one rather than over it so a Stripe workspace returns to the plan it pays for when the grant ends, route entitlement lookups through EffectivePlanID, separate a repository failure from an unknown plan, end a grant at local end of day, and drop an index that served no query
2026-09-12 09:45:31 -07:00

35 lines
1.6 KiB
SQL

-- A plan an operator granted rather than Stripe. Needed for internal
-- workspaces, design partners and support gestures; before this the only way
-- to make a workspace paid was a Stripe subscription id, so the alternative
-- was hand-writing a fake one into this table and hoping no webhook ever
-- reconciled against an id Stripe has never heard of.
--
-- Mirrors organizations.risk_override_*: who, why, when, so the grant is
-- answerable months later. managed_until NULL is an open-ended grant; a value
-- expires the grant without anyone having to remember to revoke it.
--
-- managed_plan_id is deliberately NOT plan_id. A workspace paying Stripe for
-- plan A that is granted plan B must go back to A when the grant ends, so the
-- grant is held beside the real plan rather than on top of it. Resolution is
-- models.Subscription.EffectivePlanID.
ALTER TABLE public.subscriptions
ADD COLUMN managed_at timestamptz,
ADD COLUMN managed_by uuid REFERENCES public.users(id) ON DELETE SET NULL,
ADD COLUMN managed_reason text,
ADD COLUMN managed_until timestamptz,
ADD COLUMN managed_plan_id uuid REFERENCES public.plans(id) ON DELETE SET NULL;
-- A grant is the fields together. Recording one without a reason or without a
-- plan leaves an unexplained or inert paid workspace, which is the thing this
-- is meant to prevent.
ALTER TABLE public.subscriptions
ADD CONSTRAINT subscriptions_managed_complete_check
CHECK (
managed_at IS NULL
OR (
managed_plan_id IS NOT NULL
AND managed_reason IS NOT NULL
AND btrim(managed_reason) <> ''
)
);