feat: refactor stage 2

This commit is contained in:
2026-04-08 13:00:52 +04:00
parent 1f01a00ad6
commit 6adeeeb6ea
15 changed files with 551 additions and 16 deletions
+40 -2
View File
@@ -72,10 +72,48 @@ export async function migrate(sql: any) {
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS max_retries INTEGER NOT NULL DEFAULT 0`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS retry_interval_s INTEGER NOT NULL DEFAULT 30`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS resend_interval INTEGER NOT NULL DEFAULT 0`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS consecutive_down INTEGER NOT NULL DEFAULT 0`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS last_state TEXT`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS cert_alert_days INTEGER NOT NULL DEFAULT 14`;
await sql`ALTER TABLE pings ADD COLUMN IF NOT EXISTS important BOOLEAN NOT NULL DEFAULT false`;
// Per-region transition state. region='' for unspecified/single-region monitors.
await sql`
CREATE TABLE IF NOT EXISTS monitor_region_state (
monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
region TEXT NOT NULL,
last_state TEXT,
consecutive_down INTEGER NOT NULL DEFAULT 0,
cert_alert_sent BOOLEAN NOT NULL DEFAULT false,
updated_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (monitor_id, region)
)
`;
// Drop the now-stale per-monitor state columns from the previous slice.
await sql`ALTER TABLE monitors DROP COLUMN IF EXISTS last_state`;
await sql`ALTER TABLE monitors DROP COLUMN IF EXISTS consecutive_down`;
// Notifications: modular providers, webhook only for now.
await sql`
CREATE TABLE IF NOT EXISTS notification_channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
account_id UUID NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
name TEXT NOT NULL,
kind TEXT NOT NULL,
config JSONB NOT NULL,
enabled BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ DEFAULT now()
)
`;
await sql`CREATE INDEX IF NOT EXISTS idx_notification_channels_account ON notification_channels(account_id)`;
await sql`
CREATE TABLE IF NOT EXISTS monitor_notifications (
monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
channel_id UUID NOT NULL REFERENCES notification_channels(id) ON DELETE CASCADE,
PRIMARY KEY (monitor_id, channel_id)
)
`;
await sql`CREATE INDEX IF NOT EXISTS idx_monitor_notifications_channel ON monitor_notifications(channel_id)`;
await sql`CREATE INDEX IF NOT EXISTS idx_pings_monitor ON pings(monitor_id, checked_at DESC)`;
await sql`CREATE INDEX IF NOT EXISTS idx_pings_checked_at ON pings(checked_at)`;