diff --git a/instrumentation.ts b/instrumentation.ts index 6759892..66a7620 100644 --- a/instrumentation.ts +++ b/instrumentation.ts @@ -1,4 +1,5 @@ export async function register() { + console.log('[instrumentation] register() called, NEXT_RUNTIME =', process.env.NEXT_RUNTIME) if (process.env.NEXT_RUNTIME === 'nodejs') { const fs = await import('fs') const path = await import('path') @@ -9,10 +10,20 @@ export async function register() { return } - console.log('[migrations] Applying migrations from', path.dirname(journal)) - const { migrate } = await import('drizzle-orm/postgres-js/migrator') - const { db } = await import('@/lib/db') - await migrate(db, { migrationsFolder: './lib/db/migrations' }) - console.log('[migrations] Done') + console.log('[migrations] Applying migrations...') + try { + const postgres = (await import('postgres')).default + const { drizzle } = await import('drizzle-orm/postgres-js') + const { migrate } = await import('drizzle-orm/postgres-js/migrator') + + // Dedicated single connection for migrations — never share with the app pool + const client = postgres(process.env.DATABASE_URL!, { max: 1, connect_timeout: 10 }) + const db = drizzle(client) + await migrate(db, { migrationsFolder: './lib/db/migrations' }) + await client.end() + console.log('[migrations] Done') + } catch (err) { + console.error('[migrations] Failed:', err) + } } } diff --git a/lib/db/index.ts b/lib/db/index.ts index 6575765..30818f0 100644 --- a/lib/db/index.ts +++ b/lib/db/index.ts @@ -2,5 +2,5 @@ import { drizzle } from 'drizzle-orm/postgres-js' import postgres from 'postgres' import * as schema from './schema' -const client = postgres(process.env.DATABASE_URL!) +const client = postgres(process.env.DATABASE_URL!, { connect_timeout: 10 }) export const db = drizzle(client, { schema, logger: true })