Files
archie/internal/platform/pg/migrations/00005_judgement.sql
T

104 lines
4.5 KiB
SQL

-- +goose Up
CREATE SCHEMA judgement;
CREATE TABLE judgement.action_types (
id uuid PRIMARY KEY,
org_id text NOT NULL,
connector text NOT NULL,
action text NOT NULL,
target_kind text NOT NULL,
environment text NOT NULL,
read_only boolean NOT NULL,
level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')),
created_at timestamptz NOT NULL DEFAULT now(),
reset_at timestamptz,
tightened_at timestamptz,
tightened_because text,
UNIQUE (org_id, connector, action, target_kind, environment)
);
-- A decision is the judgement of one concrete action, with every verdict in
-- its body. key identifies the attempt to run the action, so a replayed step
-- reads the existing decision instead of judging the action again.
-- action_type_id identifies the action type the decision counts towards;
-- connector, action, target_kind and environment are point-in-time snapshots
-- for the audit trail.
CREATE TABLE judgement.decisions (
id uuid PRIMARY KEY,
org_id text NOT NULL,
run_id uuid NOT NULL,
key text NOT NULL,
action_type_id uuid NOT NULL REFERENCES judgement.action_types (id),
connector text NOT NULL,
-- installation_id and installation_scope name the installation the action
-- resolved to, so an audit can say which configuration acted and whose it was.
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,
environment text NOT NULL,
level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')),
body json NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
dry boolean NOT NULL DEFAULT false,
UNIQUE (org_id, key)
);
CREATE INDEX decisions_by_run ON judgement.decisions (org_id, run_id, created_at);
CREATE INDEX decisions_by_action_type ON judgement.decisions (org_id, action_type_id, created_at);
CREATE TABLE judgement.action_type_resets (
id uuid PRIMARY KEY,
org_id text NOT NULL,
action_type_id uuid NOT NULL REFERENCES judgement.action_types (id),
from_level text NOT NULL,
to_level text NOT NULL,
by text NOT NULL,
at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX action_type_resets_by_action_type ON judgement.action_type_resets (action_type_id, at);
CREATE TABLE judgement.action_type_approvals (
id uuid PRIMARY KEY,
org_id text NOT NULL,
action_type_id uuid NOT NULL REFERENCES judgement.action_types (id),
decision_id uuid NOT NULL UNIQUE,
by text NOT NULL,
by_id text NOT NULL DEFAULT '',
at timestamptz NOT NULL,
counted boolean NOT NULL DEFAULT true
);
CREATE INDEX action_type_approvals_counted ON judgement.action_type_approvals (action_type_id, at) WHERE counted;
CREATE TABLE judgement.autonomy_proposals (
id uuid PRIMARY KEY,
org_id text NOT NULL,
action_type_id uuid NOT NULL REFERENCES judgement.action_types (id),
from_level text NOT NULL,
to_level text NOT NULL,
evidence jsonb NOT NULL,
state text NOT NULL CHECK (state IN ('pending', 'approved', 'denied', 'expired', 'superseded')),
deadline timestamptz NOT NULL,
proposed_at timestamptz NOT NULL,
answered_by text,
answered_at timestamptz,
answered_by_id text
);
CREATE UNIQUE INDEX autonomy_proposals_one_pending ON judgement.autonomy_proposals (action_type_id) WHERE (state = 'pending');
CREATE INDEX autonomy_proposals_by_action_type ON judgement.autonomy_proposals (action_type_id, proposed_at);
-- Hard rules and change-request rules are Cedar policy sets, one per kind per
-- organisation. Every version is kept, and the highest version is in force.
CREATE TABLE judgement.policy_sets (
id uuid PRIMARY KEY,
org_id text NOT NULL,
kind text NOT NULL CHECK (kind IN ('hard_rules', 'change_request')),
version integer NOT NULL,
body text NOT NULL,
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (org_id, kind, version)
);
-- +goose Down
DROP SCHEMA judgement CASCADE;