Database Migration Commands
The Medusa CLI provides several commands for managing database migrations, including creating databases, running migrations, generating migration files, and rolling back changes.Overview
Medusa uses a migration system to manage database schema changes across modules. Each module can have its own migrations, and the CLI provides commands to coordinate these migrations across your entire application.Commands
medusa db:setup
Create the database, run all pending migrations, and sync links in a single command.Options
--db <name>
Specify the name of the database to create.
--interactive / --no-interactive
Display or suppress interactive prompts.
- Type:
boolean - Default:
true
--skip-links
Skip the link synchronization step.
--execute-all-links
Skip prompts and execute all actions from sync links, including unsafe operations.
--execute-safe-links
Skip prompts and execute only safe actions from sync links.
Example Output
medusa db:create
Create the database used by your Medusa application.Options
--db <name>
Specify the database name to create.
--interactive / --no-interactive
Control interactive prompts.
- Type:
boolean - Default:
true
medusa db:migrate
Execute all pending database migrations.Options
--skip-scripts
Skip running migration scripts.
--skip-links
Skip the link synchronization step.
--execute-all-links
Execute all link sync actions without prompting, including unsafe operations.
--execute-safe-links
Execute only safe link sync actions without prompting.
--concurrency <number>
Number of concurrent migrations to run.
--all-or-nothing
If any migration fails, revert all migrations that were applied.
- Type:
boolean - Default:
false
Example Output
medusa db:migrate:scripts
Run all migration scripts without running migrations.medusa db:rollback
Rollback the last batch of migrations for specified modules.Arguments
modules (required)
One or more module names to rollback migrations for.
Example Output
Error Handling
If you specify an invalid module name, you’ll see:medusa db:generate
Generate migration files for specified modules based on entity changes.Arguments
modules (required)
One or more module names to generate migrations for.
Example Output
How It Works
The command:- Compares your current entity definitions with the database schema
- Detects differences (new tables, columns, indexes, etc.)
- Generates TypeScript migration files with the necessary changes
- Saves the migration files in your module’s migrations directory
medusa db:sync-links
Synchronize database schema with the links defined by your application and Medusa core.Options
--execute-all
Execute all actions without prompts, including unsafe operations.
--execute-safe
Execute only safe actions without prompts.
--concurrency <number>
Number of concurrent operations to run.
Example Output
Common Workflows
Initial Setup
When setting up a new Medusa project:After Pulling Changes
When you pull changes that include new migrations:After Modifying Entities
When you modify entity definitions in a module:Rollback Last Changes
If you need to undo recent migrations:Production Deployment
For deploying to production:Environment Variables
All database commands set:NODE_ENV=development- Default environment for database operationsMEDUSA_WORKER_MODE=server- Set to server modeDB_MIGRATION_CONCURRENCY- Controlled by--concurrencyoption
Database Connection
Migration commands use the database connection configuration from your Medusa config file (typicallymedusa-config.js or medusa-config.ts).
Example configuration:
Troubleshooting
”Unknown modules” Error
When runningdb:generate or db:rollback, if you see an error about unknown modules, use the exact module name as defined in your configuration.
The error message shows available modules:
Migration Failures
If a migration fails:- Check the error message for details
- Fix the underlying issue (e.g., database permissions, schema conflicts)
- Re-run the migration
--all-or-nothing, failed migrations are automatically reverted:
Database Already Exists
If you rundb:create and the database already exists, you’ll see a message indicating the database exists. This is safe to ignore if you’re setting up an existing project.
See Also
- medusa db:setup - Complete database setup
- Database Configuration - Learn about database configuration options
- Deployment Overview - Production deployment guide