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 generatewrites 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
generate - Generate Migration Files
- Takes no name argument. File names are derived from the change, for example
20240116103045_create__posts.tsor20240116103046_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
runapplies pending migrations to the preset for the currentNODE_ENV. WithNODE_ENV=test, it applies to bothtestandfixture.- Without
--execute,runis a dry-run and changes nothing. Usemigrate preview TARGETto see the plan. rundoes not generate migrations. Runmigrate generatefirst.- Under
NODE_ENV=production,--force-reasonis required.
apply - Apply to Explicit Targets
shadow - Validate on a Temporary Copy
<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
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 thesrc/migrations/ directory.
src/migrations/20240116103045_alter_users_add1.ts
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
- Each DB preset (
development,staging,production,test,fixture) is a target *_readonlypresets are never migration targets- In local environments (
development,test), every target is available; instagingandproduction, only the preset for the currentNODE_ENVis
Applying to Several Databases
Practical Workflow
1. Change Entity
Addprofile_image to the User Entity in Sonamu UI or user.entity.json.
2. Check Status
3. Generate and Apply
4. Update Code
user.model.ts
Advanced Usage (Programmatic)
Use theMigrator 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
Troubleshooting
Migration Conflict
Problem: Multiple developers generated migrations for overlapping changes. Solution:Database Connection Failed
Problem: DB connection failedMigration Order Error
Problem: Applied in wrong orderBest Practices
1. Apply Frequently
Generate and apply migrations in small units instead of accumulating large changes.2. Test Before Production
3. Backup Required
4. Consider Rollback Possibility
5. Version Control with Git
Next Steps
fixture
Manage test data
Entity
Learn more about Entity definitions