Skip to main content
Sonamu detects Entity changes and automatically generates Migration files.

Generation Workflow

Modify Entity

Edit JSON file Fields, types, indexes

Sonamu UI

Generate button Auto compare and generate

Review Migration

src/migrations/*.ts Review up/down

Execute

pnpm sonamu migrate Apply to DB

Generating in Sonamu UI

1. Modify Entity

First, modify the Entity JSON file:

2. Open Sonamu UI

Start the API dev server and open it in a browser. Sonamu UI is served from the API server’s /sonamu-ui path, without a separate port.
Sonamu UI Main Screen

3. Click Generate Button

Click the Generate button in the Entity tab.
Generate Button

4. Review Migration

Review the automatically generated Migration file:
Sonamu compares Entity with the current DB schema and generates migrations only for differences.

Generated Migration Types

New Table Creation

Adding Entity:
Generated Migration:

Adding Column

Entity Change:
Generated Migration:

Dropping Column

Entity Change:
Generated Migration:
Column deletion caution!Data cannot be recovered on rollback. Proceed carefully in production.

Changing Column Type

Entity Change:
Generated Migration:

Adding Index

Entity Change:
Generated Migration:
where is a raw SQL predicate used directly as a PostgreSQL partial index condition. Do not combine it with user input — only declare stable column conditions. Even when PostgreSQL introspection returns the predicate wrapped in parentheses, Sonamu treats it as the same condition in migration diffs.

Adding Foreign Key

Entity Change:
Generated Migration:

Composite Changes

Multiple changes result in multiple migrations: Entity Changes:
Generated Migrations:
Sonamu separates migrations by change type for easier management.

Migration Filename Convention

  • timestamp: YYYYMMDDHHmmss
  • action: create, alter, foreign, drop
  • table_name: underscore-separated
  • number: add1, add2, etc. when multiple of the same action
Examples:

Pre-generation Checklist

1. Verify DB Connection

2. Check Existing Migration Status

3. Sync Entities

Manual Modification

You can manually modify auto-generated migrations:
Manual modification cautions: - Also modify the down function - May be overwritten on next Generate - Separate complex logic into separate migrations

Special Cases

Generated Columns

Data Migration

Conditional Execution

Next Steps

Running Migrations

Apply with migrate run

Rolling Back

Undo changes

How It Works

Understanding auto-generation

Strategies

Safe migration practices