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

126 lines
4.8 KiB
SQL

-- +goose Up
CREATE SCHEMA events;
CREATE TABLE events.sources (
org_id text NOT NULL,
name text NOT NULL,
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,
display_name text NOT NULL DEFAULT '',
delivery_header text NOT NULL DEFAULT '',
unsigned_approved boolean NOT NULL DEFAULT false,
capture boolean NOT NULL DEFAULT true,
PRIMARY KEY (org_id, name)
);
-- 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 text NOT NULL,
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, name)
);
CREATE TABLE events.events (
id uuid PRIMARY KEY,
org_id text NOT NULL,
source text NOT 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, source_event_id)
);
CREATE INDEX events_undispatched ON events.events (received_at) WHERE (dispatched_at IS NULL);
CREATE INDEX events_link ON events.events (org_id, source, 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_hint text NOT NULL DEFAULT '',
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 text NOT NULL,
event_type text NOT NULL,
filter jsonb NOT NULL DEFAULT '{}',
workflow text 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)
);
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 text NOT NULL,
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;