-- Operator alerts: things that need a human, one row per (org, kind, subject). -- Raising an open alert again bumps the row instead of adding one. A -- condition-backed alert resolves when its condition clears; an event alert -- stays until dismissed. Deliveries are the outbound notices (the notify -- webhook) each raise or resolve owes, with every attempt kept. -- +goose Up CREATE TABLE alerts ( id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, org_id text NOT NULL, kind text NOT NULL, subject text NOT NULL, severity text NOT NULL CHECK (severity IN ('info', 'warning', 'action')), title text NOT NULL, body text NOT NULL DEFAULT '', link text NOT NULL DEFAULT '', is_condition boolean NOT NULL, state text NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'resolved', 'dismissed')), first_raised timestamptz NOT NULL, last_seen timestamptz NOT NULL, count bigint NOT NULL DEFAULT 1, closed_by text NOT NULL DEFAULT '', closed_at timestamptz, UNIQUE (org_id, kind, subject) ); CREATE INDEX idx_alerts_org_state ON alerts (org_id, state, id); CREATE INDEX idx_alerts_closed ON alerts (closed_at) WHERE state <> 'open'; CREATE TABLE alert_deliveries ( id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, alert_id bigint NOT NULL REFERENCES alerts (id) ON DELETE CASCADE, event text NOT NULL CHECK (event IN ('raised', 'resolved')), state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending', 'delivered', 'failed', 'skipped')), created_at timestamptz NOT NULL DEFAULT now(), next_attempt_at timestamptz NOT NULL DEFAULT now(), attempts bigint NOT NULL DEFAULT 0 ); CREATE INDEX idx_alert_deliveries_due ON alert_deliveries (next_attempt_at) WHERE state = 'pending'; CREATE INDEX idx_alert_deliveries_alert ON alert_deliveries (alert_id, id); CREATE TABLE alert_delivery_attempts ( id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, delivery_id bigint NOT NULL REFERENCES alert_deliveries (id) ON DELETE CASCADE, attempt bigint NOT NULL, at timestamptz NOT NULL DEFAULT now(), status_code bigint NOT NULL DEFAULT 0, error text NOT NULL DEFAULT '' ); CREATE INDEX idx_alert_delivery_attempts_delivery ON alert_delivery_attempts (delivery_id, id); -- +goose Down DROP TABLE alert_delivery_attempts; DROP TABLE alert_deliveries; DROP TABLE alerts;