-- Migration: add client_alert_queries table for slow-query / client alert logging.
-- Run on existing databases that already have athena_clients / client_statistics.

CREATE TABLE IF NOT EXISTS client_alert_queries (
    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    created_at timestamptz NOT NULL DEFAULT now(),
    client_name text,
    summary text,
    db_statement text,
    elapsed_secs numeric,
    slow_threshold_secs numeric,
    rows_affected bigint,
    rows_returned bigint
);

SELECT force_column_as_primary_key('public', 'client_alert_queries', 'id');

CREATE INDEX IF NOT EXISTS idx_client_alert_queries_created_at ON client_alert_queries (created_at DESC);
CREATE INDEX IF NOT EXISTS idx_client_alert_queries_client_name ON client_alert_queries (client_name) WHERE client_name IS NOT NULL;
