CREATE TABLE "alert_logs" ( "id" text PRIMARY KEY NOT NULL, "task_id" text NOT NULL, "user_id" text, "status" text NOT NULL, "error" text, "created_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "alert_tasks" ( "id" text PRIMARY KEY NOT NULL, "topic_slug" text, "sender_id" text, "status" text DEFAULT 'pending' NOT NULL, "recipient_count" integer DEFAULT 0, "success_count" integer DEFAULT 0, "payload" jsonb, "error" text, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "known_group_chats" ( "chat_id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "last_active_at" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "subscriptions" ( "user_id" text NOT NULL, "topic_id" text NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "subscriptions_user_id_topic_id_pk" PRIMARY KEY("user_id","topic_id") ); --> statement-breakpoint CREATE TABLE "topic_group_chats" ( "id" text PRIMARY KEY NOT NULL, "topic_id" text NOT NULL, "chat_id" text NOT NULL, "name" text NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "created_by" text ); --> statement-breakpoint CREATE TABLE "topics" ( "id" text PRIMARY KEY NOT NULL, "slug" text NOT NULL, "name" text NOT NULL, "description" text, "status" text DEFAULT 'approved' NOT NULL, "created_by" text, "approved_by" text, "created_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "topics_slug_unique" UNIQUE("slug") ); --> statement-breakpoint CREATE TABLE "users" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "feishu_user_id" text NOT NULL, "email" text, "is_admin" boolean DEFAULT false, "personal_token" text NOT NULL, CONSTRAINT "users_email_unique" UNIQUE("email"), CONSTRAINT "users_personal_token_unique" UNIQUE("personal_token") ); --> statement-breakpoint ALTER TABLE "alert_logs" ADD CONSTRAINT "alert_logs_task_id_alert_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."alert_tasks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "alert_tasks" ADD CONSTRAINT "alert_tasks_sender_id_users_id_fk" FOREIGN KEY ("sender_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_topic_id_topics_id_fk" FOREIGN KEY ("topic_id") REFERENCES "public"."topics"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "topic_group_chats" ADD CONSTRAINT "topic_group_chats_topic_id_topics_id_fk" FOREIGN KEY ("topic_id") REFERENCES "public"."topics"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "topic_group_chats" ADD CONSTRAINT "topic_group_chats_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "topics" ADD CONSTRAINT "topics_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "topics" ADD CONSTRAINT "topics_approved_by_users_id_fk" FOREIGN KEY ("approved_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;