CREATE TABLE IF NOT EXISTS "VideoStoryboardTask" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "userId" uuid NOT NULL, "chatId" uuid, "messageId" uuid, "providerType" varchar(64) DEFAULT 'ruxa-video' NOT NULL, "modelRef" varchar(128), "sourceImageUrl" text NOT NULL, "isNineGrid" boolean DEFAULT false NOT NULL, "detectionConfidence" integer DEFAULT 0 NOT NULL, "normalizedScript" jsonb, "status" varchar(32) DEFAULT 'pending' NOT NULL, "currentShot" integer DEFAULT 0 NOT NULL, "totalShots" integer DEFAULT 9 NOT NULL, "error" text, "createdAt" timestamp DEFAULT now() NOT NULL, "updatedAt" timestamp DEFAULT now() NOT NULL, "startedAt" timestamp, "completedAt" timestamp ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "VideoStoryboardShotTask" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "parentTaskId" uuid NOT NULL, "shotIndex" integer NOT NULL, "tileIndex" integer, "tileImageUrl" text, "upscaledImageUrl" text, "prompt" text, "modelId" varchar(128), "fallbackChain" jsonb, "attemptCount" integer DEFAULT 0 NOT NULL, "status" varchar(32) DEFAULT 'pending' NOT NULL, "videoUrl" text, "errorCode" varchar(64), "errorMessage" text, "startedAt" timestamp, "completedAt" timestamp, "createdAt" timestamp DEFAULT now() NOT NULL, "updatedAt" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint DO $$ BEGIN ALTER TABLE "VideoStoryboardTask" ADD CONSTRAINT "VideoStoryboardTask_userId_User_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN ALTER TABLE "VideoStoryboardTask" ADD CONSTRAINT "VideoStoryboardTask_chatId_Chat_id_fk" FOREIGN KEY ("chatId") REFERENCES "public"."Chat"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN ALTER TABLE "VideoStoryboardTask" ADD CONSTRAINT "VideoStoryboardTask_messageId_Message_v2_id_fk" FOREIGN KEY ("messageId") REFERENCES "public"."Message_v2"("id") ON DELETE set null ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN ALTER TABLE "VideoStoryboardShotTask" ADD CONSTRAINT "VideoStoryboardShotTask_parentTaskId_VideoStoryboardTask_id_fk" FOREIGN KEY ("parentTaskId") REFERENCES "public"."VideoStoryboardTask"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint CREATE UNIQUE INDEX IF NOT EXISTS "VideoStoryboardShotTask_parent_shot_uidx" ON "VideoStoryboardShotTask"("parentTaskId", "shotIndex"); --> statement-breakpoint CREATE INDEX IF NOT EXISTS "VideoStoryboardTask_status_created_idx" ON "VideoStoryboardTask"("status", "createdAt"); --> statement-breakpoint CREATE INDEX IF NOT EXISTS "VideoStoryboardTask_chat_created_idx" ON "VideoStoryboardTask"("chatId", "createdAt"); --> statement-breakpoint CREATE INDEX IF NOT EXISTS "VideoStoryboardShotTask_parent_status_idx" ON "VideoStoryboardShotTask"("parentTaskId", "status");