Files
archie/internal/platform/pg/migrations/00004_runs.sql
T

94 lines
3.4 KiB
SQL

-- +goose Up
CREATE SCHEMA runs;
CREATE TABLE runs.runs (
id uuid PRIMARY KEY,
org_id text NOT NULL,
workflow text NOT NULL,
start_key text,
input jsonb NOT NULL,
state text NOT NULL CHECK (state IN ('queued', 'running', 'waiting', 'succeeded', 'failed', 'cancelled', 'parked', 'waiting_approval', 'declined')),
lease_token uuid,
lease_owner text,
lease_expires_at timestamptz,
wake_at timestamptz,
waiting_for text,
cancel_requested boolean NOT NULL DEFAULT false,
attempts integer NOT NULL DEFAULT 0,
result jsonb,
error text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
finished_at timestamptz,
workflow_version integer NOT NULL DEFAULT 1,
dry boolean NOT NULL DEFAULT false,
overlay jsonb,
detail text,
parent_run_id uuid REFERENCES runs.runs (id) ON DELETE CASCADE,
parent_signal text,
call_depth integer NOT NULL DEFAULT 0,
UNIQUE (org_id, start_key)
);
CREATE INDEX runs_claimable ON runs.runs (created_at) WHERE (state IN ('queued', 'waiting', 'running'));
CREATE INDEX runs_runs_parent ON runs.runs (parent_run_id) WHERE (parent_run_id IS NOT NULL);
CREATE TABLE runs.steps (
run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE,
seq integer NOT NULL,
name text NOT NULL,
outcome text NOT NULL CHECK (outcome IN ('done', 'failed', 'unknown')),
output json,
error text,
recorded_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (run_id, seq)
);
CREATE TABLE runs.signals (
run_id uuid NOT NULL REFERENCES runs.runs (id) ON DELETE CASCADE,
key text NOT NULL,
payload jsonb NOT NULL,
received_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (run_id, key)
);
-- A workflow is a document, stored by name and version. A run records the
-- version it started with and executes that version.
CREATE TABLE runs.workflows (
org_id text NOT NULL,
name text NOT NULL,
version integer NOT NULL,
body text NOT NULL,
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (org_id, name, version)
);
-- A draft is the unpublished edit of a workflow. It is stored so the edit
-- survives a page reload, and no run uses it until it is published as the next
-- version. revision increments on every write, and a write that specifies an
-- older revision is refused, so two editors can't overwrite each other's
-- changes. base_version is the published version the draft started from, or 0
-- for a new workflow.
CREATE TABLE runs.workflow_drafts (
org_id text NOT NULL,
name text NOT NULL,
body text NOT NULL,
base_version integer NOT NULL,
revision integer NOT NULL DEFAULT 1,
updated_by text NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (org_id, name)
);
-- A workflow with no row here is enabled. A disabled workflow can't be started
-- by a trigger, by another workflow or manually.
CREATE TABLE runs.workflow_settings (
org_id text NOT NULL,
name text NOT NULL,
enabled boolean NOT NULL,
PRIMARY KEY (org_id, name)
);
-- +goose Down
DROP SCHEMA runs CASCADE;