CREATE EXTENSION IF NOT EXISTS "pgcrypto"; CREATE OR REPLACE FUNCTION require_scope(business_id uuid, tenant_id uuid) RETURNS boolean AS $$ BEGIN IF business_id IS NULL AND tenant_id IS NULL THEN RAISE EXCEPTION 'scope violation: no scope provided'; END IF; RETURN true; END; $$ LANGUAGE plpgsql IMMUTABLE; CREATE TABLE tenants ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name text NOT NULL ); CREATE TABLE businesses ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenants(id), name text NOT NULL ); CREATE TABLE users ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenants(id), business_id uuid NOT NULL REFERENCES businesses(id), name text NOT NULL, role text NOT NULL, created_at timestamptz NOT NULL DEFAULT now() );