MikroORM has integrated support for migrations via umzug. It allows you to generate migrations with current schema differences.
By default, each migration will be all executed inside a transaction, and all of them will be wrapped in one master transaction, so if one of them fails, everything will be rolled back.
Migrations are classes that extend Migration abstract class:
To support undoing those changed, you can implement the
down method, which throws an error by default.
Migrations are by default wrapped in a transaction. You can override this behaviour on
per migration basis by implementing the
isTransactional(): boolean method.
Configuration object and driver instance are available in the
Migration class context.
Using via CLI
You can use it via CLI:
migration:down commands you can specify
-o) options to run only a subset of migrations:
To run TS migration files, you will need to enable
useTsNodeflag in your
Using the Migrator programmatically
Or you can create a simple script where you initialize MikroORM like this:
Then run this script via
ts-node (or compile it to plain JS and use
There is no way to rollback DDL changes in MySQL. An implicit commit is forced for those queries automatically, so transactions are not working as expected.