47 lines
1.9 KiB
SQL
47 lines
1.9 KiB
SQL
-- +goose Up
|
|
CREATE SCHEMA deliveries;
|
|
|
|
-- A message is one notification on one channel. The composer reads its
|
|
-- messages from this table. A channel that sends messages, such as Telegram,
|
|
-- records each delivery attempt in deliveries.attempts.
|
|
CREATE TABLE deliveries.messages (
|
|
seq bigserial PRIMARY KEY,
|
|
id uuid NOT NULL UNIQUE,
|
|
org_id text NOT NULL,
|
|
channel text NOT NULL CHECK (channel ~ '^[a-z][a-z0-9-]{0,62}$'),
|
|
kind text NOT NULL CHECK (kind IN ('notification', 'approval_request', 'approval_answer')),
|
|
run_id uuid,
|
|
title text NOT NULL,
|
|
body text NOT NULL,
|
|
key text NOT NULL,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
request_id uuid,
|
|
expires_at timestamptz,
|
|
attempts integer NOT NULL DEFAULT 0,
|
|
next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
|
sent_at timestamptz,
|
|
external_id text,
|
|
last_error text,
|
|
failed_at timestamptz,
|
|
UNIQUE (org_id, channel, key)
|
|
);
|
|
CREATE INDEX messages_external ON deliveries.messages (org_id, channel, external_id) WHERE (external_id IS NOT NULL);
|
|
CREATE INDEX messages_due ON deliveries.messages (next_attempt_at) WHERE ((sent_at IS NULL) AND (failed_at IS NULL));
|
|
|
|
-- One row per delivery attempt, so every channel response and failure is kept,
|
|
-- not only the last error. Rows are never pruned: the volume is low, and the
|
|
-- attempts are the audit record of a delivery.
|
|
CREATE TABLE deliveries.attempts (
|
|
message_seq bigint NOT NULL REFERENCES deliveries.messages (seq) ON DELETE CASCADE,
|
|
attempt integer NOT NULL,
|
|
at timestamptz NOT NULL DEFAULT now(),
|
|
ok boolean NOT NULL,
|
|
response text NOT NULL DEFAULT '',
|
|
error text NOT NULL DEFAULT '',
|
|
duration_ms bigint NOT NULL,
|
|
PRIMARY KEY (message_seq, attempt)
|
|
);
|
|
|
|
-- +goose Down
|
|
DROP SCHEMA deliveries CASCADE;
|