BEGIN; CREATE TABLE alert_routing_policy ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), public_id uuid NOT NULL UNIQUE DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenant(id), category varchar(20) NOT NULL CHECK (category IN ('BUSINESS','TECHNICAL','SECURITY','COST')), severity varchar(16) NOT NULL CHECK (severity IN ('CRITICAL','HIGH','MEDIUM','LOW')), version_no integer NOT NULL CHECK (version_no>0), status varchar(16) NOT NULL DEFAULT 'ACTIVE' CHECK (status IN ('ACTIVE','RETIRED')), receiver_group varchar(120) NOT NULL, response_minutes integer NOT NULL CHECK (response_minutes BETWEEN 1 AND 10080), escalation_after_minutes integer NOT NULL CHECK (escalation_after_minutes BETWEEN 1 AND 10080), escalation_receiver_group varchar(120) NOT NULL, runbook_ref varchar(300) NOT NULL, change_summary varchar(500) NOT NULL, created_by uuid NOT NULL REFERENCES user_account(id), retired_at timestamptz, row_version bigint NOT NULL DEFAULT 1 CHECK (row_version>0), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT alert_route_version_unique UNIQUE (tenant_id,category,severity,version_no) ); CREATE UNIQUE INDEX alert_route_active_unique ON alert_routing_policy(tenant_id,category,severity) WHERE status='ACTIVE'; CREATE TABLE alert_silence ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), public_id uuid NOT NULL UNIQUE DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenant(id), rule_code varchar(100), category varchar(20) CHECK (category IN ('BUSINESS','TECHNICAL','SECURITY','COST')), resource_type varchar(80), resource_public_id uuid, matcher jsonb NOT NULL DEFAULT '{}'::jsonb, reason varchar(500) NOT NULL, starts_at timestamptz NOT NULL, ends_at timestamptz NOT NULL, created_by uuid NOT NULL REFERENCES user_account(id), revoked_by uuid REFERENCES user_account(id), revoked_at timestamptz, row_version bigint NOT NULL DEFAULT 1 CHECK (row_version>0), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT alert_silence_scope CHECK (rule_code IS NOT NULL OR category IS NOT NULL OR resource_public_id IS NOT NULL), CONSTRAINT alert_silence_window CHECK (ends_at>starts_at) ); CREATE INDEX alert_silence_active_idx ON alert_silence(tenant_id,starts_at,ends_at) WHERE revoked_at IS NULL; CREATE TABLE alert_incident ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), public_id uuid NOT NULL UNIQUE DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenant(id), rule_code varchar(100) NOT NULL, category varchar(20) NOT NULL CHECK (category IN ('BUSINESS','TECHNICAL','SECURITY','COST')), severity varchar(16) NOT NULL CHECK (severity IN ('CRITICAL','HIGH','MEDIUM','LOW')), status varchar(24) NOT NULL DEFAULT 'OPEN' CHECK (status IN ('OPEN','ACKNOWLEDGED','MITIGATED','RESOLVED','SILENCED')), fingerprint varchar(200) NOT NULL, title varchar(200) NOT NULL, summary varchar(1000) NOT NULL, resource_type varchar(80), resource_public_id uuid, correlation_request_id varchar(128), routing_policy_id uuid REFERENCES alert_routing_policy(id), current_owner_id uuid REFERENCES user_account(id), first_detected_at timestamptz NOT NULL, last_detected_at timestamptz NOT NULL, occurrence_count bigint NOT NULL DEFAULT 1 CHECK (occurrence_count>0), acknowledged_at timestamptz, mitigated_at timestamptz, resolved_at timestamptz, silenced_until timestamptz, escalated_at timestamptz, row_version bigint NOT NULL DEFAULT 1 CHECK (row_version>0), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE UNIQUE INDEX alert_incident_active_fingerprint ON alert_incident(tenant_id,fingerprint) WHERE status<>'RESOLVED'; CREATE INDEX alert_incident_queue_idx ON alert_incident(tenant_id,status,severity,last_detected_at DESC); CREATE TABLE alert_action ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), public_id uuid NOT NULL UNIQUE DEFAULT gen_random_uuid(), tenant_id uuid NOT NULL REFERENCES tenant(id), incident_id uuid NOT NULL REFERENCES alert_incident(id) ON DELETE CASCADE, action_type varchar(24) NOT NULL CHECK (action_type IN ('DETECTED','ACKNOWLEDGED','ASSIGNED','SILENCED','MITIGATED','RESOLVED','REOPENED','ESCALATED','COMMENTED')), from_status varchar(24), to_status varchar(24), actor_type varchar(16) NOT NULL CHECK (actor_type IN ('system','user')), actor_user_id uuid REFERENCES user_account(id), note varchar(1000) NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX alert_action_incident_time_idx ON alert_action(incident_id,created_at,id); COMMIT;