fix(server): skip migrations on LiteFS replicas

LiteFS replicas are read-only, so migrations fail with "read only replica"
error. Check LITEFS_CANDIDATE env var and skip migrations on replicas.
The primary (pod-0) will run migrations and replicate the changes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Thomas Hallock 2026-01-23 14:31:19 -06:00
parent 1f1083773d
commit e72018ae44
1 changed files with 17 additions and 10 deletions

View File

@ -9,17 +9,24 @@ const port = parseInt(process.env.PORT || '3000', 10)
const app = next({ dev, hostname, port }) const app = next({ dev, hostname, port })
const handle = app.getRequestHandler() const handle = app.getRequestHandler()
// Run migrations before starting server // Run migrations before starting server (only on primary/candidate nodes)
console.log('🔄 Running database migrations...') // LiteFS replicas are read-only, so migrations must run on the primary
const { migrate } = require('drizzle-orm/better-sqlite3/migrator') const isLiteFSReplica = process.env.LITEFS_CANDIDATE === 'false'
const { db } = require('./dist/db/index')
try { if (isLiteFSReplica) {
migrate(db, { migrationsFolder: './drizzle' }) console.log('📖 Skipping migrations (LiteFS replica - read-only)')
console.log('✅ Migrations complete') } else {
} catch (error) { console.log('🔄 Running database migrations...')
console.error('❌ Migration failed:', error) const { migrate } = require('drizzle-orm/better-sqlite3/migrator')
process.exit(1) const { db } = require('./dist/db/index')
try {
migrate(db, { migrationsFolder: './drizzle' })
console.log('✅ Migrations complete')
} catch (error) {
console.error('❌ Migration failed:', error)
process.exit(1)
}
} }
app.prepare().then(() => { app.prepare().then(() => {