Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement control over migration transaction #851

Closed
peter-episource opened this issue Sep 17, 2020 · 2 comments
Closed

Implement control over migration transaction #851

peter-episource opened this issue Sep 17, 2020 · 2 comments

Comments

@peter-episource
Copy link

Is your feature request related to a problem? Please describe.
I have a multiple tenant database structure where I would like to sync up migrations across tenant databases. For example, if I have two databases, A and B, this requires that if database A's migrations succeed, but database B's migrations fail, both database A and B's transactions rollback.

Describe the solution you'd like
In my scenario, I would have a MikroORM instance for each tenant database where I'd like to control commiting and rollback of the transactions.

The migration methods accept a transaction as a option:

migrator.up({ transaction: trx });

A way to pull a transaction from the orm. Currently I do a hacky workaround by doing:

const connection = orm.em.getDriver().getConnection();
// @ts-ignore
const trx = await connection.client.transaction();

This way, I can wait for all the migrations in the different databases to finish and commit the transactions only if all migration instances succeed.

@B4nan
Copy link
Member

B4nan commented Sep 18, 2020

In v4 there is em.getConnection().begin() that will give you the tx context, so the latter should be fine already.

@B4nan B4nan closed this as completed in 1089c86 Sep 18, 2020
@B4nan
Copy link
Member

B4nan commented Sep 18, 2020

await orm.em.transactional(async em => {
  await migrator.up({ transaction: em.getTransactionContext() });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants