Add auto-migration on server startup
Run Drizzle migrations before seeding admin user so tables exist on fresh database. Migration files generated from current schema. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||
import { migrate } from 'drizzle-orm/postgres-js/migrator';
|
||||
import postgres from 'postgres';
|
||||
import * as schema from './schema';
|
||||
|
||||
@@ -8,3 +11,13 @@ const client = postgres(connectionString);
|
||||
export const db = drizzle(client, { schema });
|
||||
|
||||
export type Database = typeof db;
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
export async function runMigrations() {
|
||||
const migrationClient = postgres(connectionString, { max: 1 });
|
||||
const migrationDb = drizzle(migrationClient);
|
||||
await migrate(migrationDb, { migrationsFolder: path.resolve(__dirname, '../../drizzle') });
|
||||
await migrationClient.end();
|
||||
console.log('Database migrations complete');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user