ai-chatbot/lib/db/migrations/0008_silent_harrier.sql
Your Name 53ddb7e585
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
Initial commit - clean repository
2025-12-17 20:15:18 +08:00

127 lines
4.5 KiB
SQL

-- Add User table columns if not exist (with permission handling)
DO $$
BEGIN
BEGIN
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "role" varchar DEFAULT 'regular' NOT NULL;
EXCEPTION
WHEN insufficient_privilege THEN
RAISE NOTICE 'Skipping User.role - insufficient privilege';
END;
BEGIN
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "isActive" boolean DEFAULT true NOT NULL;
EXCEPTION
WHEN insufficient_privilege THEN
RAISE NOTICE 'Skipping User.isActive - insufficient privilege';
END;
BEGIN
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "createdAt" timestamp DEFAULT now() NOT NULL;
EXCEPTION
WHEN insufficient_privilege THEN
RAISE NOTICE 'Skipping User.createdAt - insufficient privilege';
END;
BEGIN
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "updatedAt" timestamp DEFAULT now() NOT NULL;
EXCEPTION
WHEN insufficient_privilege THEN
RAISE NOTICE 'Skipping User.updatedAt - insufficient privilege';
END;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "AiProvider" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" varchar(128) NOT NULL,
"type" varchar DEFAULT 'custom' NOT NULL,
"baseUrl" text NOT NULL,
"apiKey" text NOT NULL,
"isEnabled" boolean DEFAULT true NOT NULL,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL,
"updatedBy" uuid
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "AiModel" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"providerId" uuid NOT NULL,
"modelId" varchar(128) NOT NULL,
"displayName" varchar(128) NOT NULL,
"description" text,
"supportsVision" boolean DEFAULT false NOT NULL,
"supportsImageGen" boolean DEFAULT false NOT NULL,
"isEnabled" boolean DEFAULT true NOT NULL,
"isDefault" boolean DEFAULT false NOT NULL,
"sortOrder" integer DEFAULT 0 NOT NULL,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "StorageConfig" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" varchar(128) NOT NULL,
"type" varchar DEFAULT 's3' NOT NULL,
"endpoint" text,
"region" varchar(64),
"bucket" varchar(128),
"accessKeyId" text,
"secretAccessKey" text,
"publicUrl" text,
"isDefault" boolean DEFAULT false NOT NULL,
"isEnabled" boolean DEFAULT true NOT NULL,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL,
"updatedBy" uuid
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "SystemSettings" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"key" varchar(128) NOT NULL,
"value" jsonb NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL,
"updatedBy" uuid,
CONSTRAINT "SystemSettings_key_unique" UNIQUE("key")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "ToolConfig" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"toolName" varchar(128) NOT NULL,
"isEnabled" boolean DEFAULT true NOT NULL,
"description" text,
"config" jsonb,
"updatedAt" timestamp DEFAULT now() NOT NULL,
"updatedBy" uuid,
CONSTRAINT "ToolConfig_toolName_unique" UNIQUE("toolName")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "AiModel" ADD CONSTRAINT "AiModel_providerId_AiProvider_id_fk" FOREIGN KEY ("providerId") REFERENCES "public"."AiProvider"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "AiProvider" ADD CONSTRAINT "AiProvider_updatedBy_User_id_fk" FOREIGN KEY ("updatedBy") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "StorageConfig" ADD CONSTRAINT "StorageConfig_updatedBy_User_id_fk" FOREIGN KEY ("updatedBy") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "SystemSettings" ADD CONSTRAINT "SystemSettings_updatedBy_User_id_fk" FOREIGN KEY ("updatedBy") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "ToolConfig" ADD CONSTRAINT "ToolConfig_updatedBy_User_id_fk" FOREIGN KEY ("updatedBy") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;