-- Every failed attempt of an analysis, an answer or a prompt test run, for the -- admin error log. No foreign keys: the log outlives deleted items. Kept for -- 90 days (worker cleanup). CREATE TABLE generation_errors ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, created_at timestamptz NOT NULL DEFAULT now(), source text NOT NULL CHECK (source IN ('analysis', 'question', 'preview')), item_id uuid NOT NULL, kind text NOT NULL, model text NOT NULL, attempt integer NOT NULL, -- What happened next: tried again, the default model took over, or gave up. outcome text NOT NULL CHECK (outcome IN ('retry', 'fallback', 'failed')), error text NOT NULL, seconds integer ); CREATE INDEX generation_errors_created_idx ON generation_errors (created_at DESC); CREATE INDEX generation_errors_item_idx ON generation_errors (item_id); -- When the current text was asked for (a regeneration or an admin restart -- sets it again): the waiting page counts from here. ALTER TABLE analyses ADD COLUMN requested_at timestamptz NOT NULL DEFAULT now(); UPDATE analyses SET requested_at = created_at; ALTER TABLE questions ADD COLUMN requested_at timestamptz NOT NULL DEFAULT now(); UPDATE questions SET requested_at = created_at;