chatbot-template/lib/db/migrate.ts

33 lines
813 B
TypeScript
Raw Normal View History

import { config } from 'dotenv';
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';
2024-10-11 18:00:22 +05:30
config({
path: '.env.local',
2024-10-11 18:00:22 +05:30
});
const runMigrate = async () => {
if (!process.env.POSTGRES_URL) {
throw new Error('POSTGRES_URL is not defined');
2024-10-11 18:00:22 +05:30
}
const connection = postgres(process.env.POSTGRES_URL, { max: 1 });
const db = drizzle(connection);
console.log('⏳ Running migrations...');
2024-10-11 18:00:22 +05:30
const start = Date.now();
await migrate(db, { migrationsFolder: './lib/db/migrations' });
2024-10-11 18:00:22 +05:30
const end = Date.now();
console.log('✅ Migrations completed in', end - start, 'ms');
2024-10-11 18:00:22 +05:30
process.exit(0);
};
runMigrate().catch((err) => {
console.error('❌ Migration failed');
2024-10-11 18:00:22 +05:30
console.error(err);
process.exit(1);
});