-- 017_api_keys.sql — personal API keys for machine clients (MCP). -- -- 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). -- -- Long-lived Bearer credentials ("sb_...") for non-browser clients — the -- superbrain-mcp server the federated mcpserver gateway spawns, or any REST -- client. Only the SHA-256 hash is stored; the plaintext is shown exactly -- once at creation. Revocation is deletion of the row (there is no soft -- state); keys are deliberately independent of User.tokenVersion — "sign -- out everywhere" targets browser sessions, machine credentials must be -- revoked explicitly so a gateway link survives a password change. -- Issuance AND use are gated on the tenant's mcp-core connector -- (entitlements catalog: core plan), so a downgrade stops existing keys. CREATE TABLE IF NOT EXISTS "ApiKey" ( id text PRIMARY KEY, "ownerId" text NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, name text NOT NULL, prefix text NOT NULL, "keyHash" text NOT NULL UNIQUE, "lastUsedAt" timestamptz, "createdAt" timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS "ApiKey_ownerId_idx" ON "ApiKey"("ownerId");