51 lines
2.0 KiB
TypeScript
51 lines
2.0 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' })
|
|
console.log('[migrations] Done')
|
|
|
|
// Seed admin user from ADMIN_EMAIL if not already present
|
|
const adminEmail = process.env.ADMIN_EMAIL
|
|
if (adminEmail) {
|
|
const { user } = await import('./lib/db/schema')
|
|
const { eq } = await import('drizzle-orm')
|
|
const existing = await db.select({ id: user.id }).from(user).where(eq(user.email, adminEmail)).limit(1)
|
|
if (existing.length === 0) {
|
|
const { default: crypto } = await import('crypto')
|
|
await db.insert(user).values({
|
|
id: crypto.randomUUID(),
|
|
email: adminEmail,
|
|
name: adminEmail.split('@')[0],
|
|
emailVerified: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
})
|
|
console.log('[seed] Admin user created:', adminEmail)
|
|
}
|
|
}
|
|
|
|
await client.end()
|
|
} catch (err) {
|
|
console.error('[migrations] Failed:', err)
|
|
}
|
|
}
|
|
}
|