chatbot-template/lib/db/migrate.ts

33 lines
807 B
TypeScript
Raw Normal View History

2024-11-14 12:16:05 -05:00
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({
2024-11-14 12:16:05 -05:00
path: '.env.local',
2024-10-11 18:00:22 +05:30
});
const runMigrate = async () => {
if (!process.env.POSTGRES_URL) {
2024-11-14 12:16:05 -05:00
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);
2024-11-14 12:16:05 -05:00
console.log('⏳ Running migrations...');
2024-10-11 18:00:22 +05:30
const start = Date.now();
2024-11-14 12:16:05 -05:00
await migrate(db, { migrationsFolder: './lib/drizzle' });
2024-10-11 18:00:22 +05:30
const end = Date.now();
2024-11-14 12:16:05 -05:00
console.log('✅ Migrations completed in', end - start, 'ms');
2024-10-11 18:00:22 +05:30
process.exit(0);
};
runMigrate().catch((err) => {
2024-11-14 12:16:05 -05:00
console.error('❌ Migration failed');
2024-10-11 18:00:22 +05:30
console.error(err);
process.exit(1);
});