-- 015_user_ai_config.sql — per-user BYO AI provider settings. -- -- Additive and idempotent; schema.sql carries the identical table, so this -- is a structural no-op on a fresh database (db-converge proves both paths -- land the same). No existing table gains a constraint here, so converge -- pass 2 has nothing external to repair. -- -- Separates AI CONFIG (this row: provider, model, encrypted key) from the -- AI IMPLEMENTATION (internal/ai adapters). Resolution order per request: -- this row when enabled, else the server env key when the tenant's plan -- allows it (entitlements.AllowServerAI: premium/core/selfhosted), else -- "configure AI in Settings" (428). CREATE TABLE IF NOT EXISTS "UserAiConfig" ( id text PRIMARY KEY, "ownerId" text NOT NULL UNIQUE REFERENCES "User"(id) ON DELETE CASCADE, provider text NOT NULL CHECK (provider IN ('openai-compatible','anthropic','gemini')), "baseUrl" text NOT NULL DEFAULT '', model text NOT NULL, "apiKey" text NOT NULL, enabled boolean NOT NULL DEFAULT true, version integer NOT NULL DEFAULT 1, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now() );