Files
archie/internal/platform/pg/migrations/00003_events.sql
T

166 lines
7.7 KiB
SQL

-- +goose Up
CREATE SCHEMA events;
-- A source is owned by a scope: the organisation, or one person acting in it.
-- The label is the editable name people see, unique within its scope. The
-- webhook path is the public address deliveries post to, unique across the
-- instance, and changing it leaves every stored row alone because they refer to
-- the id.
CREATE TABLE events.sources (
id uuid PRIMARY KEY,
scope text NOT NULL CHECK (scope IN ('org', 'person')),
org_id text NOT NULL,
person_id text,
label text NOT NULL CHECK (label <> ''),
webhook_path text NOT NULL UNIQUE CHECK (webhook_path ~ '^[a-z][a-z0-9-]{0,62}$'),
connector text NOT NULL,
token_sha256 text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
cursor text NOT NULL DEFAULT '',
key_fields jsonb NOT NULL DEFAULT '[]',
verification text NOT NULL DEFAULT 'token' CHECK (verification IN ('token', 'hmac', 'none')),
hmac_header text NOT NULL DEFAULT '',
hmac_prefix text NOT NULL DEFAULT '',
hmac_secret_sealed bytea NOT NULL DEFAULT '\x'::bytea,
delivery_header text NOT NULL DEFAULT '',
unsigned_approved boolean NOT NULL DEFAULT false,
capture boolean NOT NULL DEFAULT true,
CHECK ((scope = 'person') = (person_id IS NOT NULL))
);
CREATE UNIQUE INDEX sources_org_label ON events.sources (org_id, label) WHERE scope = 'org';
CREATE UNIQUE INDEX sources_person_label ON events.sources (person_id, label) WHERE scope = 'person';
-- An event type identifies one kind of event from a source by its headers and
-- payload paths. schema lists the JSON paths of the example event it was
-- created from.
CREATE TABLE events.event_types (
id uuid PRIMARY KEY,
org_id text NOT NULL,
source_id uuid NOT NULL REFERENCES events.sources (id),
name text NOT NULL,
rule jsonb NOT NULL,
schema jsonb NOT NULL DEFAULT '{}',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (org_id, source_id, name)
);
-- An event is one delivered or scheduled occurrence. declared_event_type holds
-- the event kind declared by the connector (or a built-in kind), matched by
-- value, and is not a reference to events.event_types.
CREATE TABLE events.events (
id uuid PRIMARY KEY,
org_id text NOT NULL,
source_id uuid REFERENCES events.sources (id) ON DELETE SET NULL,
declared_event_type text NOT NULL,
source_event_id text NOT NULL,
link_key text,
linked_to uuid REFERENCES events.events (id),
fields jsonb NOT NULL,
occurred_at timestamptz,
received_at timestamptz NOT NULL DEFAULT now(),
dispatched_at timestamptz,
matched boolean NOT NULL DEFAULT false,
own boolean NOT NULL DEFAULT false,
origin_run_id uuid,
group_key text,
group_filter jsonb,
body bytea,
verification text NOT NULL DEFAULT '' CHECK (verification IN ('', 'signed', 'token', 'unsigned')),
headers jsonb,
remote_addr text NOT NULL DEFAULT '',
event_type_id uuid REFERENCES events.event_types (id) ON DELETE SET NULL,
UNIQUE (org_id, source_id, source_event_id)
);
-- A schedule tick has no source, and NULL ids never conflict in the unique
-- constraint above, so each slot is made unique here.
CREATE UNIQUE INDEX events_schedule_slot ON events.events (org_id, source_event_id)
WHERE source_id IS NULL AND declared_event_type = 'schedule.tick';
CREATE INDEX events_undispatched ON events.events (received_at) WHERE (dispatched_at IS NULL);
CREATE INDEX events_link ON events.events (org_id, source_id, link_key, received_at) WHERE (link_key IS NOT NULL);
-- A mapping lists the payload paths a workflow reads from events of one event
-- type.
CREATE TABLE events.mappings (
id uuid PRIMARY KEY,
org_id text NOT NULL,
name text NOT NULL,
source_id uuid REFERENCES events.sources (id) ON DELETE SET NULL,
event_type_id uuid NOT NULL REFERENCES events.event_types (id) ON DELETE RESTRICT,
fields jsonb NOT NULL,
match_count bigint NOT NULL DEFAULT 0,
last_matched_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
-- A trigger is identified by its id, fixed when it is created; its name is a
-- label that can change. A deleted trigger keeps its row, so its dispatches
-- still show what started them, and its name can be given to a new trigger.
-- Deleting it clears its source, event type and mapping, so it doesn't stop
-- them being deleted. declared_event_type holds the event kind declared by the
-- connector (or a built-in kind), matched by value, and is not a reference to
-- events.event_types.
CREATE TABLE events.triggers (
id uuid PRIMARY KEY,
org_id text NOT NULL,
name text NOT NULL,
source_id uuid REFERENCES events.sources (id),
declared_event_type text NOT NULL,
filter jsonb NOT NULL DEFAULT '{}',
workflow_id uuid NOT NULL,
enabled boolean NOT NULL DEFAULT true,
updated_at timestamptz NOT NULL DEFAULT now(),
schedule_every_s integer CHECK (schedule_every_s >= 60),
schedule_since timestamptz,
approved boolean NOT NULL DEFAULT true,
event_type_id uuid REFERENCES events.event_types (id) ON DELETE RESTRICT,
mapping_id uuid REFERENCES events.mappings (id) ON DELETE RESTRICT,
deleted_at timestamptz,
CHECK (deleted_at IS NOT NULL OR (source_id IS NULL) = (schedule_every_s IS NOT NULL))
);
CREATE UNIQUE INDEX triggers_live_name ON events.triggers (org_id, name) WHERE (deleted_at IS NULL);
CREATE TABLE events.dispatches (
event_id uuid NOT NULL REFERENCES events.events (id),
trigger_id uuid NOT NULL REFERENCES events.triggers (id),
run_id uuid,
created_at timestamptz NOT NULL DEFAULT now(),
outcome text NOT NULL DEFAULT 'started' CHECK (outcome IN ('started', 'failed', 'denied', 'disabled')),
reason jsonb,
PRIMARY KEY (event_id, trigger_id),
CHECK ((outcome = 'started') = (run_id IS NOT NULL))
);
-- A replay is a run started for a stored event outside dispatch: a test run of
-- a trigger proposal, or a run an approved proposal starts for the events that
-- prompted it. label is the proposed trigger's name when the run started.
CREATE TABLE events.replays (
run_id uuid PRIMARY KEY,
event_id uuid NOT NULL REFERENCES events.events (id),
label text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
-- A group collects the unmatched events that share a key. Its source,
-- declared event type and filter describe the trigger it suggests.
-- declared_event_type holds the event kind declared by the connector (or a
-- built-in kind), matched by value, and is not a reference to
-- events.event_types.
CREATE TABLE events.groups (
id uuid PRIMARY KEY,
org_id text NOT NULL,
key text NOT NULL,
source_id uuid NOT NULL REFERENCES events.sources (id) ON DELETE CASCADE,
declared_event_type text NOT NULL,
filter jsonb NOT NULL,
count bigint NOT NULL,
first_seen timestamptz NOT NULL,
last_seen timestamptz NOT NULL,
UNIQUE (org_id, key)
);
-- +goose Down
DROP SCHEMA events CASCADE;