feat(01-01): create monitoring tables migration

- Add service_health_checks table with status CHECK constraint, JSONB probe_details, checked_at column
- Add alert_events table with alert_type and status CHECK constraints, lifecycle timestamps
- Add created_at indexes on both tables (INFR-01 requirement)
- Add composite indexes for common query patterns
- Enable RLS on both tables (service role bypasses RLS per Supabase pattern)
This commit is contained in:
admin
2026-02-24 11:31:04 -05:00
parent fec5d0319e
commit 94d1c0adae

View File

@@ -0,0 +1,64 @@
-- Migration: Create monitoring tables for service health checks and alert events
-- Created: 2026-02-24
-- Purpose: Establish data foundation for the monitoring system.
-- Phase 1 of the monitoring feature — tables must exist before health probes
-- or alert services can write data.
-- =============================================================================
-- TABLE: service_health_checks
-- Records the result of each health probe run against a monitored service.
-- =============================================================================
CREATE TABLE IF NOT EXISTS service_health_checks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
service_name VARCHAR(100) NOT NULL,
status TEXT NOT NULL CHECK (status IN ('healthy', 'degraded', 'down')),
latency_ms INTEGER, -- nullable: not applicable for all probe types
checked_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, -- when the probe actually ran (distinct from created_at)
error_message TEXT, -- nullable: probe failure details
probe_details JSONB, -- nullable: flexible metadata (response codes, error specifics, etc.)
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Index required by INFR-01: supports 30-day retention queries
CREATE INDEX IF NOT EXISTS idx_service_health_checks_created_at
ON service_health_checks(created_at);
-- Composite index for dashboard "latest check per service" queries
CREATE INDEX IF NOT EXISTS idx_service_health_checks_service_created
ON service_health_checks(service_name, created_at);
-- Enable RLS (service role key bypasses RLS automatically — explicit policies added in Phase 3)
ALTER TABLE service_health_checks ENABLE ROW LEVEL SECURITY;
-- =============================================================================
-- TABLE: alert_events
-- Tracks alert lifecycle: creation, acknowledgement, and resolution.
-- =============================================================================
CREATE TABLE IF NOT EXISTS alert_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
service_name VARCHAR(100) NOT NULL,
alert_type TEXT NOT NULL CHECK (alert_type IN ('service_down', 'service_degraded', 'recovery')),
status TEXT NOT NULL CHECK (status IN ('active', 'acknowledged', 'resolved')),
message TEXT, -- nullable: human-readable alert description
details JSONB, -- nullable: structured metadata about the alert
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
acknowledged_at TIMESTAMP WITH TIME ZONE, -- nullable: set when status → acknowledged
resolved_at TIMESTAMP WITH TIME ZONE -- nullable: set when status → resolved
);
-- Index required by INFR-01: supports 30-day retention queries
CREATE INDEX IF NOT EXISTS idx_alert_events_created_at
ON alert_events(created_at);
-- Index for "active alerts" queries
CREATE INDEX IF NOT EXISTS idx_alert_events_status
ON alert_events(status);
-- Composite index for "active alerts per service" queries
CREATE INDEX IF NOT EXISTS idx_alert_events_service_status
ON alert_events(service_name, status);
-- Enable RLS (service role key bypasses RLS automatically — explicit policies added in Phase 3)
ALTER TABLE alert_events ENABLE ROW LEVEL SECURITY;