Files
uptimesebi/internal/migrations/sql/00015_alerting.sql
Sebastian UnterschützandClaude Opus 5.5 ffef63e586 feat(store): add incidents, notification channels and delivery outbox
At most one open incident per monitor is enforced by a partial unique
index.

Refs #15, #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 01:23:37 +01:00

76 lines
3.4 KiB
SQL

-- +goose Up
-- How many consecutive failures a region needs before it counts as down.
ALTER TABLE monitors ADD COLUMN confirmations integer NOT NULL DEFAULT 2 CHECK (confirmations BETWEEN 1 AND 10);
CREATE TABLE incidents (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
monitor_id uuid NOT NULL REFERENCES monitors (id) ON DELETE CASCADE,
started_at timestamptz NOT NULL,
resolved_at timestamptz,
-- Last error per region when the incident opened.
cause jsonb NOT NULL DEFAULT '{}',
CHECK (resolved_at IS NULL OR resolved_at >= started_at)
);
-- Guards against duplicate incidents even if two evaluations race.
CREATE UNIQUE INDEX incidents_one_open_idx ON incidents (monitor_id) WHERE resolved_at IS NULL;
CREATE INDEX incidents_org_idx ON incidents (org_id, started_at DESC);
CREATE TABLE notification_channels (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
type text NOT NULL CHECK (type IN ('webhook', 'email')),
-- Type-specific settings; secrets inside are sealed by the application.
config jsonb NOT NULL,
enabled boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX notification_channels_org_idx ON notification_channels (org_id);
-- Outbox: a row per channel and incident event, delivered with retries by
-- any API replica.
CREATE TABLE notifications (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
channel_id uuid NOT NULL REFERENCES notification_channels (id) ON DELETE CASCADE,
incident_id uuid NOT NULL REFERENCES incidents (id) ON DELETE CASCADE,
event text NOT NULL CHECK (event IN ('opened', 'resolved')),
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
attempts integer NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL DEFAULT now(),
last_error text,
created_at timestamptz NOT NULL DEFAULT now(),
sent_at timestamptz,
UNIQUE (channel_id, incident_id, event)
);
CREATE INDEX notifications_due_idx ON notifications (next_attempt_at) WHERE status = 'pending';
ALTER TABLE incidents ENABLE ROW LEVEL SECURITY;
ALTER TABLE incidents FORCE ROW LEVEL SECURITY;
CREATE POLICY incidents_tenant ON incidents FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
ALTER TABLE notification_channels ENABLE ROW LEVEL SECURITY;
ALTER TABLE notification_channels FORCE ROW LEVEL SECURITY;
CREATE POLICY notification_channels_tenant ON notification_channels FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
ALTER TABLE notifications ENABLE ROW LEVEL SECURITY;
ALTER TABLE notifications FORCE ROW LEVEL SECURITY;
CREATE POLICY notifications_tenant ON notifications FOR ALL
USING (app_scope_system() OR org_id = app_org_id())
WITH CHECK (app_scope_system() OR org_id = app_org_id());
-- +goose Down
DROP TABLE notifications;
DROP TABLE notification_channels;
DROP TABLE incidents;
ALTER TABLE monitors DROP COLUMN confirmations;