ai-chatbot/lib/db/migrations/0052_add_asset_memory_snapshots.sql
Your Name c1fea459cd
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: cache asset visual memory summaries
2026-03-06 18:10:55 +08:00

57 lines
2.4 KiB
SQL

CREATE TABLE IF NOT EXISTS "AssetMemorySnapshot" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"userId" uuid NOT NULL,
"assetId" uuid,
"chatId" uuid,
"projectId" uuid,
"snapshotType" varchar(32) DEFAULT 'visual_summary' NOT NULL,
"status" varchar(32) DEFAULT 'ready' NOT NULL,
"normalizedUrl" text NOT NULL,
"summaryText" text,
"subjectTags" jsonb DEFAULT '[]'::jsonb NOT NULL,
"sceneTags" jsonb DEFAULT '[]'::jsonb NOT NULL,
"styleTags" jsonb DEFAULT '[]'::jsonb NOT NULL,
"lightingTags" jsonb DEFAULT '[]'::jsonb NOT NULL,
"confidence" integer DEFAULT 0 NOT NULL,
"sourceModelId" varchar(128),
"metadata" jsonb,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "AssetMemorySnapshot" ADD CONSTRAINT "AssetMemorySnapshot_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 "AssetMemorySnapshot" ADD CONSTRAINT "AssetMemorySnapshot_assetId_MediaAsset_id_fk" FOREIGN KEY ("assetId") REFERENCES "public"."MediaAsset"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "AssetMemorySnapshot" ADD CONSTRAINT "AssetMemorySnapshot_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 "AssetMemorySnapshot" ADD CONSTRAINT "AssetMemorySnapshot_projectId_CanvasProject_id_fk" FOREIGN KEY ("projectId") REFERENCES "public"."CanvasProject"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "AssetMemorySnapshot_user_url_type_uidx"
ON "AssetMemorySnapshot"("userId", "normalizedUrl", "snapshotType");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "AssetMemorySnapshot_asset_type_idx"
ON "AssetMemorySnapshot"("assetId", "snapshotType");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "AssetMemorySnapshot_chat_created_idx"
ON "AssetMemorySnapshot"("chatId", "createdAt");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "AssetMemorySnapshot_project_created_idx"
ON "AssetMemorySnapshot"("projectId", "createdAt");