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

141 lines
6.0 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)
);
CREATE TABLE events.events (
id uuid PRIMARY KEY,
org_id text NOT NULL,
source_id uuid REFERENCES events.sources (id) ON DELETE SET NULL,
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 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()
);
CREATE TABLE events.triggers (
org_id text NOT NULL,
name text NOT NULL,
source_id uuid REFERENCES events.sources (id),
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,
PRIMARY KEY (org_id, name),
CHECK ((source_id IS NULL) = (schedule_every_s IS NOT NULL))
);
CREATE TABLE events.dispatches (
event_id uuid NOT NULL REFERENCES events.events (id),
trigger text NOT NULL,
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),
CHECK ((outcome = 'started') = (run_id IS NOT NULL))
);
-- A group collects the unmatched events that share a key. Its source, event
-- type and filter describe the trigger it suggests.
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,
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;