-- Tenant-bound, scoped API keys (ADR: prerequisite 1 of the Werkmeister plan). -- kind: user (portal, a person's key), ci (CLI licences in CI), integration -- (held by a product for a tenant, e.g. Werkmeister). New keys are stored as -- sha256 of the 192-bit random key (indexed lookup); rows created before this -- migration keep their argon2id hash. ALTER TABLE api_keys ADD COLUMN kind TEXT NOT NULL DEFAULT 'user' CHECK (kind IN ('user', 'ci', 'integration')), ADD COLUMN holder TEXT, ADD COLUMN expires_at TIMESTAMPTZ, ADD COLUMN hash_sha256 BYTEA; ALTER TABLE api_keys ALTER COLUMN hash DROP NOT NULL; ALTER TABLE api_keys ADD CONSTRAINT api_keys_hash_present CHECK (hash IS NOT NULL OR hash_sha256 IS NOT NULL), ADD CONSTRAINT api_keys_holder_kind CHECK ((kind = 'integration') = (holder IS NOT NULL)); CREATE UNIQUE INDEX api_keys_hash_sha256_idx ON api_keys (hash_sha256) WHERE hash_sha256 IS NOT NULL;