Skip to main content

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.
Migration scripts are additional setup scripts that run after migrations to populate or transform data.

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:
  1. Compares your current entity definitions with the database schema
  2. Detects differences (new tables, columns, indexes, etc.)
  3. Generates TypeScript migration files with the necessary changes
  4. Saves the migration files in your module’s migrations directory

Synchronize database schema with the links defined by your application and Medusa core.
Links define relationships between modules. This command ensures the database schema matches your link definitions.

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 operations
  • MEDUSA_WORKER_MODE=server - Set to server mode
  • DB_MIGRATION_CONCURRENCY - Controlled by --concurrency option

Database Connection

Migration commands use the database connection configuration from your Medusa config file (typically medusa-config.js or medusa-config.ts). Example configuration:

Troubleshooting

”Unknown modules” Error

When running db: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:
  1. Check the error message for details
  2. Fix the underlying issue (e.g., database permissions, schema conflicts)
  3. Re-run the migration
With --all-or-nothing, failed migrations are automatically reverted:

Database Already Exists

If you run db: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