-- +goose Up CREATE SCHEMA approvals; -- A request is one judged action waiting for a person's answer. key identifies -- the run's wait that the answer resumes. The decision columns bind the answer -- to the judged action, so it can't be applied to a different action. CREATE TABLE approvals.requests ( id uuid PRIMARY KEY, org_id text NOT NULL, run_id uuid NOT NULL, key text NOT NULL, decision_id uuid NOT NULL, -- action_type_id names the judgement.action_types row for both action and -- autonomy requests, without a cross-module foreign key. action_type_id uuid NOT NULL, digest text NOT NULL, level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')), summary text NOT NULL, state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'discarded')), deadline timestamptz NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), answered_at timestamptz, answered_by text, answered_via text, connector text NOT NULL, installation_id uuid NOT NULL, installation_scope text NOT NULL CHECK (installation_scope IN ('instance', 'org', 'person')), action text NOT NULL, target_kind text NOT NULL, target_id text NOT NULL, kind text NOT NULL DEFAULT 'action' CHECK (kind IN ('action', 'autonomy')), step integer NOT NULL DEFAULT 0, step_deadline timestamptz, change_request boolean NOT NULL DEFAULT false, counted boolean NOT NULL DEFAULT false, discarded_because text, irreversible boolean NOT NULL DEFAULT false, answered_by_id text, UNIQUE (org_id, run_id, key) ); CREATE INDEX requests_by_state ON approvals.requests (org_id, state, created_at); -- An answer that was refused, and the reason it was refused, so a request that -- was never resolved can be explained. CREATE TABLE approvals.refusals ( id uuid PRIMARY KEY, org_id text NOT NULL, request_id uuid NOT NULL REFERENCES approvals.requests (id) ON DELETE CASCADE, identity text NOT NULL, via text NOT NULL, reason text NOT NULL, at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX refusals_by_request ON approvals.refusals (org_id, request_id, at); -- +goose Down DROP SCHEMA approvals CASCADE;