Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions drizzle/0043_lonely_rick_jones.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- Step 1: 创建枚举类型(幂等)
DO $$ BEGIN
CREATE TYPE "public"."notification_type" AS ENUM('circuit_breaker', 'daily_leaderboard', 'cost_alert');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint

DO $$ BEGIN
CREATE TYPE "public"."webhook_provider_type" AS ENUM('wechat', 'feishu', 'dingtalk', 'telegram', 'custom');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint

-- Step 2: 创建表(幂等)
CREATE TABLE IF NOT EXISTS "notification_target_bindings" (
"id" serial PRIMARY KEY NOT NULL,
"notification_type" "notification_type" NOT NULL,
"target_id" integer NOT NULL,
"is_enabled" boolean DEFAULT true NOT NULL,
"schedule_cron" varchar(100),
"schedule_timezone" varchar(50) DEFAULT 'Asia/Shanghai',
"template_override" jsonb,
"created_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "webhook_targets" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(100) NOT NULL,
"provider_type" "webhook_provider_type" NOT NULL,
"webhook_url" varchar(1024),
"telegram_bot_token" varchar(256),
"telegram_chat_id" varchar(64),
"dingtalk_secret" varchar(256),
"custom_template" jsonb,
"custom_headers" jsonb,
"proxy_url" varchar(512),
"proxy_fallback_to_direct" boolean DEFAULT false,
"is_enabled" boolean DEFAULT true NOT NULL,
"last_test_at" timestamp with time zone,
"last_test_result" jsonb,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone DEFAULT now()
);
--> statement-breakpoint

-- Step 3: 兼容旧配置(幂等)
ALTER TABLE "notification_settings" ADD COLUMN IF NOT EXISTS "use_legacy_mode" boolean DEFAULT true NOT NULL;--> statement-breakpoint

-- Step 4: 外键约束(幂等)
DO $$ BEGIN
ALTER TABLE "notification_target_bindings" ADD CONSTRAINT "notification_target_bindings_target_id_webhook_targets_id_fk" FOREIGN KEY ("target_id") REFERENCES "public"."webhook_targets"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint

-- Step 5: 索引(幂等)
CREATE UNIQUE INDEX IF NOT EXISTS "unique_notification_target_binding" ON "notification_target_bindings" USING btree ("notification_type","target_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_notification_bindings_type" ON "notification_target_bindings" USING btree ("notification_type","is_enabled");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_notification_bindings_target" ON "notification_target_bindings" USING btree ("target_id","is_enabled");--> statement-breakpoint
Loading
Loading