102 lines
4.1 KiB
SQL
102 lines
4.1 KiB
SQL
-- +goose Up
|
|
CREATE SCHEMA runs;
|
|
|
|
CREATE TABLE runs.runs (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
workflow_id uuid,
|
|
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),
|
|
-- Only a test run of a document that isn't a workflow yet has no workflow.
|
|
CHECK (dry OR workflow_id IS NOT NULL)
|
|
);
|
|
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 identified by its id, fixed when it is created; its name is a
|
|
-- label that can change. A deleted workflow keeps its row and versions, so its
|
|
-- runs still show what they ran, and its name can be given to a new workflow.
|
|
-- A disabled workflow can't be started by a trigger, by another workflow or
|
|
-- manually.
|
|
CREATE TABLE runs.workflows (
|
|
id uuid PRIMARY KEY,
|
|
org_id text NOT NULL,
|
|
name text NOT NULL,
|
|
enabled boolean NOT NULL DEFAULT true,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
deleted_at timestamptz
|
|
);
|
|
CREATE UNIQUE INDEX workflows_live_name ON runs.workflows (org_id, name) WHERE (deleted_at IS NULL);
|
|
|
|
-- A version is one published workflow document. A run records the version it
|
|
-- started with and executes that version. calls maps each workflow name the
|
|
-- document calls or hands off to onto the id it resolved to when the version
|
|
-- was published, so renaming the called workflow doesn't change what runs.
|
|
CREATE TABLE runs.workflow_versions (
|
|
workflow_id uuid NOT NULL REFERENCES runs.workflows (id),
|
|
version integer NOT NULL,
|
|
body text NOT NULL,
|
|
calls jsonb NOT NULL DEFAULT '{}',
|
|
created_by text NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (workflow_id, 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 workflow not yet published.
|
|
CREATE TABLE runs.workflow_drafts (
|
|
workflow_id uuid PRIMARY KEY REFERENCES runs.workflows (id),
|
|
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()
|
|
);
|
|
|
|
-- +goose Down
|
|
DROP SCHEMA runs CASCADE;
|