Open MercatoDocsSearch docsAPI reference

CLI

yarn db:generate

Generate a migration after changing the entity model. Read the SQL diff before applying it, especially when a change removes or renames a field.

yarn db:generate executes mercato db generate, iterating through every enabled module and creating MikroORM migrations when entity metadata changes.

Usage

# Using yarn script
yarn db:generate

# Using CLI directly
yarn mercato db generate

The command requires a valid DATABASE_URL pointing to the Postgres database used for diffing.

What Happens

  1. Loads enabled modules via the CLI resolver (packages/cli/src/lib/resolver.ts).
  2. For each module (in alphabetical order), resolves entity definitions (data/entities.ts or schema.ts) and spins up a temporary MikroORM instance.
  3. Writes migrations into packages/<module>/src/modules/<module>/migrations. App overrides fall back to src/modules/<module>/migrations.
  4. Renames generated files and migration classes to include the module suffix (e.g. _auth) to keep names unique across modules.
  5. Updates the module snapshot file (.snapshot-<project>.json) used by MikroORM as the diff baseline for the next generation run.
  6. Prints a summary such as • auth: generated Migration20240214_auth or • directory: no changes.

Typical Workflow

Troubleshooting

Previous: mercato customer_accounts sync-customer-role-acls · Next: yarn db:greenfield