-- Sharing pays in two steps: the first visit of a shared link earns a -- question (`share_reward`, see shares.rs), and someone who signs up from a -- shared link and makes their own chart earns the sharer a natal analysis -- (`referral_reward`, at most three per user). 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', 'question', 'share_reward', 'referral_reward')); -- Who came through whose shared link (one row per new user). CREATE TABLE referrals ( user_id uuid PRIMARY KEY REFERENCES users (id) ON DELETE CASCADE, referrer_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, share_id uuid REFERENCES shares (id) ON DELETE SET NULL, created_at timestamptz NOT NULL DEFAULT now(), -- Set when the new user made their own chart (rewarded or over the limit). rewarded_at timestamptz ); CREATE INDEX referrals_referrer_idx ON referrals (referrer_id);