feat: Auto Database Migration Plugin for Nitro #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces an automatic database migration system for our Nitro/Nuxt application using Drizzle ORM. The system automatically runs database migrations on application startup, eliminating the need for manual migration execution in both development and production environments.
Key Features
✅ Automatic Migration Execution: Runs migrations via Nitro plugin on startup
✅ Environment-Aware: Different behavior for development vs production
✅ Silent Mode: Reduces console noise in development environment
✅ Idempotent Migrations: Safe to run multiple times without errors
✅ PostgreSQL Notice Suppression: Clean output without unnecessary NOTICE messages
✅ Production Build Support: Automatically copies migration files to build output
Technical Implementation
1. Nitro Plugin (
server/plugins/01.database-migrate.ts)onnoticehandler for suppressing noticesAUTO_MIGRATEenvironment variable (defaults to enabled)2. Build Integration (
nuxt.config.ts)compiledhook to copy migration files to.output/server/migrations3. Idempotent Migration SQL
DO $$ ... IF NOT EXISTSblocksCREATE TABLE IF NOT EXISTSEnvironment Variables
AUTO_MIGRATE=false- Disable automatic migrations (default: enabled)DATABASE_URL- PostgreSQL connection string (required)Path Resolution
./server/database/migrations../migrationsMigration Behavior
Known Limitations & Future Improvements
This current approach requires manual modification of Drizzle-generated migration files to make them idempotent. While functional, we acknowledge this isn't the cleanest solution. We're actively researching better approaches in #20.
Alternative approaches being considered:
Feedback Requested
🔍 Community Input Welcome: Please share your thoughts on this auto-migration approach:
Testing
pnpm devpnpm build && pnpm previewMigration Impact
This change consolidates multiple migration files into a single idempotent migration (
0000_jazzy_jackpot.sql) that includes all schema changes with proper existence checks. The migration history has been reset to provide a clean foundation for the auto-migration system.Breaking Changes
None. This is an additive feature that can be disabled via environment variable.
Related Issues