45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { config } from "dotenv";
|
|
import { sql } from "drizzle-orm";
|
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
import postgres from "postgres";
|
|
|
|
config({
|
|
path: ".env.local",
|
|
});
|
|
|
|
const runMigrate = async () => {
|
|
if (!process.env.POSTGRES_URL) {
|
|
throw new Error("POSTGRES_URL is not defined");
|
|
}
|
|
|
|
const connection = postgres(process.env.POSTGRES_URL, { max: 1 });
|
|
const db = drizzle(connection);
|
|
|
|
console.log("⏳ Running migrations...");
|
|
|
|
const start = Date.now();
|
|
await migrate(db, { migrationsFolder: "./lib/db/migrations" });
|
|
await db.execute(sql`
|
|
ALTER TABLE "AiProvider" DROP CONSTRAINT IF EXISTS "AiProvider_type_check";
|
|
`);
|
|
await db.execute(sql`
|
|
ALTER TABLE "AiProvider"
|
|
ADD CONSTRAINT "AiProvider_type_check"
|
|
CHECK (type IN (
|
|
'alibaba', 'openai', 'custom', 'local', 'gemini',
|
|
'huanke-gemini', 'qianwen-video', 'volcano-vision', 'volcano-video', 'alibaba-image', 'ruxa-image'
|
|
));
|
|
`);
|
|
const end = Date.now();
|
|
|
|
console.log("✅ Migrations completed in", end - start, "ms");
|
|
process.exit(0);
|
|
};
|
|
|
|
runMigrate().catch((err) => {
|
|
console.error("❌ Migration failed");
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|