Files
archie/internal/platform/pg/migrations/00011_alerts.sql
T

39 lines
1.8 KiB
SQL

-- +goose Up
CREATE SCHEMA alerts;
-- An alert is a condition a source reported, followed from when it opens until
-- it resolves. key is the source's own identifier for the condition, such as
-- Alertmanager's fingerprint, so a repeat or a resolve from the source finds
-- the open alert instead of opening another. run_id is the run working it.
CREATE TABLE alerts.alerts (
id uuid PRIMARY KEY,
org_id text NOT NULL,
source_id uuid NOT NULL,
key text NOT NULL,
state text NOT NULL CHECK (state IN ('open', 'acknowledged', 'resolved', 'suppressed')),
severity text NOT NULL CHECK (severity IN ('critical', 'high', 'medium', 'low', 'info')),
title text NOT NULL,
summary text NOT NULL DEFAULT '',
target_kind text NOT NULL,
target_id text NOT NULL,
labels jsonb NOT NULL DEFAULT '{}',
opened_event_id uuid NOT NULL,
resolved_event_id uuid,
run_id uuid,
opened_at timestamptz NOT NULL,
acknowledged_at timestamptz,
acknowledged_by_id text,
resolved_at timestamptz,
suppressed_until timestamptz,
updated_at timestamptz NOT NULL DEFAULT now(),
CHECK ((acknowledged_at IS NULL) = (acknowledged_by_id IS NULL)),
CHECK ((state <> 'resolved') OR (resolved_at IS NOT NULL)),
CHECK ((state <> 'suppressed') OR (suppressed_until IS NOT NULL))
);
CREATE UNIQUE INDEX alerts_one_unresolved ON alerts.alerts (org_id, source_id, key) WHERE (state <> 'resolved');
CREATE INDEX alerts_by_state ON alerts.alerts (org_id, state, opened_at DESC);
CREATE INDEX alerts_by_target ON alerts.alerts (org_id, target_kind, target_id, opened_at DESC);
-- +goose Down
DROP SCHEMA alerts CASCADE;