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');
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { auth } from './routes/auth';
|
||||
import { savesRouter } from './routes/saves';
|
||||
import { leaderboardRouter } from './routes/leaderboard';
|
||||
import { invitesRouter } from './routes/invites';
|
||||
import { runMigrations } from './db';
|
||||
import { seedAdmin } from './db/seed';
|
||||
|
||||
if (!process.env.JWT_SECRET) {
|
||||
@@ -40,6 +41,7 @@ const port = Number(process.env.PORT) || 3001;
|
||||
|
||||
console.log(`AI Tycoon API server starting on port ${port}...`);
|
||||
|
||||
await runMigrations();
|
||||
await seedAdmin();
|
||||
|
||||
serve({ fetch: app.fetch, port });
|
||||
|
||||
Reference in New Issue
Block a user