Files
archie/internal/platform/pg/migrations/00012_proposals.sql
T
sam 911152c9d1 merge: proposals module (#1995)
# Conflicts:
#	internal/platform/pg/migrations/00008_drafts.sql
#	modules/drafts/drafts.go
#	modules/drafts/internal/store/store.go
2026-11-24 03:11:07 +11:00

80 lines
4.1 KiB
SQL

-- +goose Up
CREATE SCHEMA proposals;
-- A proposal is a change Archie drafts with its evidence: a root-cause fix, a
-- new trigger and workflow, a workflow change or a raised ceiling. change holds
-- what publishing it writes, and base_version the version of the target it was
-- drafted against, so a target changed since is detected. A policy or ceiling
-- change needs a person with authority, and any other change needs the owner.
-- A rejection keeps its reason so the next proposal can take it into account.
-- prompted_by lists the events that led to it, which a published trigger runs
-- for. run_id is the run that takes the proposal's actions once it is approved.
CREATE TABLE proposals.proposals (
id uuid PRIMARY KEY,
org_id text NOT NULL,
kind text NOT NULL CHECK (kind IN ('root_cause_fix', 'trigger', 'workflow_change', 'ceiling_raise', 'policy_change')),
state text NOT NULL CHECK (state IN ('pending', 'approved', 'rejected', 'published', 'superseded', 'withdrawn')),
needs text NOT NULL CHECK (needs IN ('owner', 'authority')),
title text NOT NULL,
summary text NOT NULL,
evidence jsonb NOT NULL,
change jsonb NOT NULL,
target_kind text NOT NULL,
target_id text NOT NULL DEFAULT '',
base_version integer,
prompted_by uuid[] NOT NULL DEFAULT '{}',
origin_run_id uuid,
run_id uuid,
proposed_by_id text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
answered_by_id text,
answered_at timestamptz,
answer_reason text NOT NULL DEFAULT '',
published_at timestamptz,
CHECK ((answered_at IS NULL) = (answered_by_id IS NULL)),
CHECK ((state IN ('pending', 'withdrawn', 'superseded')) OR (answered_at IS NOT NULL)),
CHECK ((state = 'published') = (published_at IS NOT NULL))
);
CREATE INDEX proposals_by_state ON proposals.proposals (org_id, state, created_at DESC);
CREATE INDEX proposals_by_target ON proposals.proposals (org_id, target_kind, target_id, created_at DESC);
CREATE INDEX proposals_unfinished ON proposals.proposals (org_id, answered_at) WHERE state = 'approved';
-- A recurring cause on a target has at most one open root-cause proposal.
CREATE UNIQUE INDEX proposals_one_open_root_cause ON proposals.proposals (org_id, target_kind, target_id, (change ->> 'cause'))
WHERE kind = 'root_cause_fix' AND state IN ('pending', 'approved');
-- A group of unmatched events has at most one live trigger proposal, and an
-- open one holds its workflow name, so two proposals never publish the same
-- workflow.
CREATE UNIQUE INDEX proposals_one_trigger_per_group ON proposals.proposals (org_id, (change ->> 'group_id'))
WHERE kind = 'trigger' AND state IN ('pending', 'approved', 'published');
CREATE UNIQUE INDEX proposals_one_trigger_per_name ON proposals.proposals (org_id, (change ->> 'name'))
WHERE kind = 'trigger' AND state IN ('pending', 'approved');
-- The actions a proposal will take when it is approved, in order. Each is
-- judged when the proposal is made and shown with it, and approving the
-- proposal approves them. digest binds the approval to the exact action. The
-- run that takes the actions settles each one: ran, failed, unknown (started
-- and not confirmed, never repeated), refused (judged again and no longer
-- allowed) or skipped (an earlier action did not run).
CREATE TABLE proposals.actions (
proposal_id uuid NOT NULL REFERENCES proposals.proposals (id) ON DELETE CASCADE,
seq integer NOT NULL,
connector text NOT NULL,
installation_id uuid NOT NULL,
action text NOT NULL,
target_kind text NOT NULL,
target_id text NOT NULL,
input jsonb NOT NULL,
digest text NOT NULL,
decision_id uuid NOT NULL,
state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending', 'ran', 'failed', 'unknown', 'refused', 'skipped')),
outcome text NOT NULL DEFAULT '',
settled_at timestamptz,
CHECK ((state = 'pending') = (settled_at IS NULL)),
PRIMARY KEY (proposal_id, seq)
);
-- +goose Down
DROP SCHEMA proposals CASCADE;