-- Create synthetic_monitors table CREATE TABLE IF NOT EXISTS synthetic_monitors ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, type VARCHAR(50) NOT NULL CHECK (type IN ('http_status', 'ping', 'port_check')), target VARCHAR(500) NOT NULL, expected_status INTEGER, port INTEGER, interval INTEGER NOT NULL DEFAULT 60, enabled BOOLEAN DEFAULT TRUE, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -- Create monitor_results table CREATE TABLE IF NOT EXISTS monitor_results ( id SERIAL PRIMARY KEY, monitor_id INTEGER NOT NULL REFERENCES synthetic_monitors(id) ON DELETE CASCADE, status VARCHAR(20) NOT NULL CHECK (status IN ('success', 'failed')), response_time INTEGER, message TEXT, timestamp TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -- Create indexes for better query performance CREATE INDEX IF NOT EXISTS idx_monitor_results_monitor_id ON monitor_results(monitor_id); CREATE INDEX IF NOT EXISTS idx_monitor_results_timestamp ON monitor_results(timestamp DESC); CREATE INDEX IF NOT EXISTS idx_synthetic_monitors_enabled ON synthetic_monitors(enabled) WHERE enabled = TRUE; -- Create function to update updated_at timestamp CREATE OR REPLACE FUNCTION update_updated_at_column() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = NOW(); RETURN NEW; END; $$ LANGUAGE plpgsql; -- Create trigger to automatically update updated_at DROP TRIGGER IF EXISTS trigger_update_synthetic_monitors_updated_at ON synthetic_monitors; CREATE TRIGGER trigger_update_synthetic_monitors_updated_at BEFORE UPDATE ON synthetic_monitors FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();