feat: Auto Database Migration Plugin for Nitro#19
Closed
productdevbook wants to merge 1 commit intomainfrom
Closed
feat: Auto Database Migration Plugin for Nitro#19productdevbook wants to merge 1 commit intomainfrom
productdevbook wants to merge 1 commit intomainfrom
Conversation
This commit introduces an automatic database migration system that runs on application startup, eliminating the need for manual migration execution. Key Features: - Automatic migration execution via Nitro plugin - Environment-aware path resolution for dev/production - Silent mode in development to reduce noise - Idempotent migrations that handle "already exists" errors - PostgreSQL notice suppression for cleaner output - Build-time migration file copying for production deployments Technical Implementation: - Added server/plugins/01.database-migrate.ts Nitro plugin - Integrated Nitro build hook to copy migration files to .output - Modified migration SQL files to use IF NOT EXISTS patterns - Added AUTO_MIGRATE environment variable for control (default: enabled) The plugin automatically detects development vs production environments and adjusts paths accordingly. In development, it runs silently to avoid console clutter. All migration operations are idempotent, preventing errors when running multiple times. Custom migration modifications were made to handle PostgreSQL enum types, table creation, and foreign key constraints with proper existence checks.
5 tasks
Owner
Author
|
Replaced by #27 with a better approach using Drizzle's built-in migration tracking instead of manual IF NOT EXISTS modifications. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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