30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
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')
|
|
const journal = path.join(process.cwd(), 'lib/db/migrations/meta/_journal.json')
|
|
|
|
if (!fs.existsSync(journal)) {
|
|
console.warn('[migrations] No journal found at', journal, '— skipping')
|
|
return
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|