Files
sorsfonalak/crates/db/migrations/0022_promo_codes.sql
T

24 lines
1.1 KiB
SQL
Raw Normal View History

2026-11-19 09:45:42 +00:00
-- Promo codes: an admin makes them for a package, a user redeems one once
-- and gets the package (a `package_orders` row with source `promo`).
CREATE TABLE promo_codes (
id uuid PRIMARY KEY,
-- Upper case, without separators (shown in groups of four).
code text NOT NULL UNIQUE CHECK (code ~ '^[2-9A-HJKMNP-Z]{8,32}$'),
package text NOT NULL,
-- Who it is for or which campaign (admin only).
note text,
created_by uuid REFERENCES users (id) ON DELETE SET NULL,
created_at timestamptz NOT NULL DEFAULT now(),
redeemed_by uuid REFERENCES users (id) ON DELETE SET NULL,
redeemed_at timestamptz,
order_id uuid REFERENCES package_orders (id) ON DELETE SET NULL,
-- An unused code can be withdrawn.
revoked_at timestamptz,
CHECK (redeemed_at IS NULL OR revoked_at IS NULL)
);
CREATE INDEX promo_codes_created_idx ON promo_codes (created_at DESC);
ALTER TABLE package_orders DROP CONSTRAINT package_orders_source_check;
ALTER TABLE package_orders ADD CONSTRAINT package_orders_source_check
CHECK (source IN ('admin', 'mock', 'stripe', 'promo'));