Skip to main content
The pnpm sonamu migrate commands manage database schema changes. Sonamu compares your Entity definitions with a database schema, generates Knex migration files from the difference, and applies them to the databases you choose.

Basic Concept

  • Entity-based: migrate generate writes migration code from the difference between Entities and the database
  • Files in Git: generated migrations are stored in src/migrations/ and committed like any other source file
  • Explicit execution: commands that change a database are dry-runs unless you pass --execute
  • Multiple databases: each Sonamu DB preset (development, staging, production, test, fixture) is a separate migration target

Commands

TARGET is one of development, staging, production, fixture, and test.

status - Check Status

For every connection, the result lists the current version and pending migration files. It also lists prepared migrations: the code Sonamu would generate for Entity changes that do not have a migration file yet.

generate - Generate Migration Files

  • Takes no name argument. File names are derived from the change, for example 20240116103045_create__posts.ts or 20240116103046_alter_users_add1.ts.
  • Files generated together are timestamped one second apart, so their order is stable.
  • Generation compares against a connection that has no pending migrations. If every connection has pending migrations, the command fails with MIGRATION_NOT_READY; apply them first.

run - Apply to the Current Environment

  • run applies pending migrations to the preset for the current NODE_ENV. With NODE_ENV=test, it applies to both test and fixture.
  • Without --execute, run is a dry-run and changes nothing. Use migrate preview TARGET to see the plan.
  • run does not generate migrations. Run migrate generate first.
  • Under NODE_ENV=production, --force-reason is required.

apply - Apply to Explicit Targets

In a TTY, omitting the targets opens a target selection prompt.

shadow - Validate on a Temporary Copy

Sonamu creates <database>__migration_shadow from the test (or fixture) database as a template, applies the pending migrations there, and drops it afterward. Only test and fixture are accepted.

rollback - Roll Back the Latest Batch

The target argument is required; NODE_ENV does not supply it. A production rollback also requires --force-reason.
Commands that change a database ask for confirmation in a TTY. In non-interactive runs, add --confirm (for example --execute --confirm). See the CLI reference for exit codes and JSON output.

Migration Generation

Change an Entity

Add a property to the Entity, either in Sonamu UI or in the .entity.json file:
src/application/user/user.entity.json

Migration Files

Generated migration files are stored in the src/migrations/ directory.
src/migrations/20240116103045_alter_users_add1.ts
Structure:
  • up(): Apply migration (create tables, add columns, etc.)
  • down(): Rollback migration (revert changes)

Supported Changes

Changes that Sonamu detects and generates migrations for:

Multi-Database Management

Database Configuration

sonamu.config.ts
Migration targets:
  • Each DB preset (development, staging, production, test, fixture) is a target
  • *_readonly presets are never migration targets
  • In local environments (development, test), every target is available; in staging and production, only the preset for the current NODE_ENV is

Applying to Several Databases

Practical Workflow

1. Change Entity

Add profile_image to the User Entity in Sonamu UI or user.entity.json.

2. Check Status

The prepared migrations include the new column.

3. Generate and Apply

4. Update Code

user.model.ts

Advanced Usage (Programmatic)

Use the Migrator class when you need migrations inside a script.

Using Migrator Class

src/scripts/migration-tools.ts
runAction("apply" | "rollback", targets) still exists for backward compatibility but is deprecated; call apply or rollback directly. new Migrator() handles every target the current environment allows. Use the constructor option excludedTargets to remove targets, or targetFilter to narrow them further (for example new Migrator({ excludedTargets: ["production"] })). Removed targets are left out of connection lists and status queries, and passing one to apply, rollback, and similar methods throws an error before connecting to the database.

Rollback Script

src/scripts/rollback-migration.ts
Data loss risk: Rollback can delete tables or columns, so use very carefully in production.

Troubleshooting

Migration Conflict

Problem: Multiple developers generated migrations for overlapping changes. Solution:

Database Connection Failed

Problem: DB connection failed
Solution:

Migration Order Error

Problem: Applied in wrong order
Solution: referenced tables must be created first. Roll back, adjust the file timestamps so the referenced table comes first, and apply again.

Best Practices

1. Apply Frequently

Generate and apply migrations in small units instead of accumulating large changes.

2. Test Before Production

See Production Approval for approval workflows.

3. Backup Required

4. Consider Rollback Possibility

5. Version Control with Git

Next Steps

fixture

Manage test data

Entity

Learn more about Entity definitions