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:
parent
1f1083773d
commit
e72018ae44
|
|
@ -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) {
|
||||||
|
console.log('📖 Skipping migrations (LiteFS replica - read-only)')
|
||||||
|
} else {
|
||||||
|
console.log('🔄 Running database migrations...')
|
||||||
|
const { migrate } = require('drizzle-orm/better-sqlite3/migrator')
|
||||||
|
const { db } = require('./dist/db/index')
|
||||||
|
|
||||||
|
try {
|
||||||
migrate(db, { migrationsFolder: './drizzle' })
|
migrate(db, { migrationsFolder: './drizzle' })
|
||||||
console.log('✅ Migrations complete')
|
console.log('✅ Migrations complete')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Migration failed:', error)
|
console.error('❌ Migration failed:', error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.prepare().then(() => {
|
app.prepare().then(() => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue