-- Migration 014: v0.22.2 — Routing Policies -- Adds routing_policies table for rules-based provider routing. CREATE TABLE IF NOT EXISTS routing_policies ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(200) NOT NULL, scope VARCHAR(10) NOT NULL DEFAULT 'global' CHECK (scope IN ('global', 'team')), team_id UUID REFERENCES teams(id) ON DELETE CASCADE, priority INTEGER NOT NULL DEFAULT 100, policy_type VARCHAR(30) NOT NULL CHECK (policy_type IN ('provider_prefer', 'team_route', 'cost_limit', 'model_alias')), config JSONB NOT NULL DEFAULT '{}'::jsonb, is_active BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_routing_policies_active ON routing_policies(is_active, priority) WHERE is_active = true; CREATE INDEX IF NOT EXISTS idx_routing_policies_team ON routing_policies(team_id) WHERE team_id IS NOT NULL; -- Trigger for updated_at CREATE TRIGGER routing_policies_updated_at BEFORE UPDATE ON routing_policies FOR EACH ROW EXECUTE FUNCTION update_updated_at(); -- Add routing_decision column to usage_log for observability. ALTER TABLE usage_log ADD COLUMN IF NOT EXISTS routing_decision JSONB;