Files
sorsfonalak/crates/db/migrations/0012_packages.sql
T

27 lines
1.4 KiB
SQL
Raw Normal View History

-- Packages: every purchase or admin grant of a package is an order; the units
-- it gives are credit_ledger rows pointing at it (reason 'package'), and the
-- unlimited package is a time-limited 'unlimited' entitlement.
CREATE TABLE package_orders (
id uuid PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE,
package text NOT NULL,
source text NOT NULL CHECK (source IN ('admin', 'mock', 'stripe')),
price_huf integer NOT NULL DEFAULT 0 CHECK (price_huf >= 0),
note text,
created_by uuid REFERENCES users ON DELETE SET NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX package_orders_user_idx ON package_orders (user_id, created_at DESC);
ALTER TABLE credit_ledger DROP CONSTRAINT credit_ledger_reason_check;
ALTER TABLE credit_ledger ADD CONSTRAINT credit_ledger_reason_check
CHECK (reason IN ('monthly_grant', 'analysis', 'chat', 'refund', 'admin_adjust', 'purchase', 'person', 'package'));
ALTER TABLE credit_ledger ADD COLUMN order_id uuid REFERENCES package_orders ON DELETE SET NULL;
-- 'unlimited': analyses and persons without limit until expires_at.
ALTER TABLE entitlements DROP CONSTRAINT entitlements_entitlement_check;
ALTER TABLE entitlements ADD CONSTRAINT entitlements_entitlement_check
CHECK (entitlement IN ('unlimited_persons', 'unlimited'));
ALTER TABLE entitlements ADD COLUMN expires_at timestamptz;