This implementation adds automatic database schema validation on server startup to prevent the server from starting if the database schema is out of sync with the Prisma schema.
Created a comprehensive database validation utility that performs three checks:
- Prisma Schema Validation: Runs
prisma validateto ensure the schema file is syntactically correct - Database Connection Check: Verifies the database is accessible
- Migration Status Check: Runs
prisma migrate statusto detect pending migrations
The validator will:
- ✅ Pass if all checks succeed
- ❌ Fail and prevent server startup if:
- Prisma schema is invalid
- Database connection fails
- There are pending migrations
⚠️ Warn but continue if migration status check fails (e.g., command not found)
Added the validation check early in the startup sequence:
// [OPS] Validate database schema on startup
await validateDatabaseSchema();This runs after environment validation but before any database operations.
Created comprehensive unit tests covering:
- Successful validation scenario
- Prisma schema validation failure
- Database connection failure
- Pending migrations detection
- Migration status command failure (graceful degradation)
- No pending migrations scenario
When the database is in sync, you'll see:
🔍 Validating database schema...
✅ Prisma schema validation passed
✅ Database connection successful
✅ Database schema is up to date
✅ Database validation completed successfully
The server will fail to start with:
❌ Database has pending migrations
❌ Database validation failed!
The server cannot start because the database schema is not valid or out of sync.
To fix this issue:
1. Run: npm run db:migrate
2. Or run: npx prisma migrate deploy
3. Or run: npm run db:push (for development)
- Prevents Runtime Errors: Catches schema mismatches before the server starts accepting requests
- Clear Error Messages: Provides actionable guidance on how to fix issues
- Developer Experience: Saves debugging time by failing fast with clear instructions
- Production Safety: Ensures deployments don't proceed with incompatible schemas
Run the tests with:
npm run test:jest -- dbValidator.test.tsFor production deployments:
- Run migrations before starting the server:
npx prisma migrate deploy - Or include migration in your deployment pipeline
- The validation will catch any missed migrations and prevent startup
Potential improvements:
- Add option to auto-apply migrations in development mode
- Add schema version tracking in database
- Add Slack/webhook notifications for schema validation failures
- Add metrics/monitoring for validation checks