question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to specify transaction property for individual migrations.

See original GitHub issue

Feature Description

Allow each individual migration to specify if it should be ran inside a transaction or not.

The Problem

Some operations cannot be ran inside a transaction (eg. CREATE INDEX CONCURRENTLY on postgresql). While the typeorm CLI does allow to specify a transaction argument on the migration:run command, this argument applies globally. This poses a problem when migrations are ran in a CI pipeline where we don’t want to completely abandon the guarantees that wrapping a migration in a transaction gives us (safe rollback if something shall go wrong) and leaves no other resource other than running some migrations manually

The Solution

When running a migration the MigrationExecutor checks if the migration class defines a value for the transaction property in order to decide if this particular migration should be wrapped in a transaction or not. This value should take precedence over the transaction property of the MigrationExecutor.

There is an issue if the transaction argument is 'all' ie. all migrations should be executed within a single transaction, as there is no clear cut way to conciliate it with some migrations wanting to be ran outside transactions, so maybe the Migration’s transaction property should only be honored if the MigrationExecutor’s transaction is either 'each' in which case if a migration has transaction = false it is not wrapped in a migration, or MigrationExecutor’s transaction is 'none' in which case only migrations which explicitly state it are wrapped in transactions.

Considered Alternatives

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
kjb085commented, Dec 22, 2021

@pedrolcn Has any progress been made on this?

2reactions
TimBeyercommented, Oct 11, 2022

Actually thinking about it again, if I have transaction: "each" on the overall config, then I have transaction: true as a default and transaction: false would override that.

If I have transaction: "none" on the overall config, then I have transaction: false as a default and transaction: true would override that.

That would be somewhat confusing, but not a lot more confusing than what we have currently.
It also would preserve the current logic.

I think I’ll go ahead and try to open a PR for this to see how it feels.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flyway by Redgate • Database Migrations Made Easy.
By default, Flyway always wraps the execution of an entire migration within a single transaction. Alternatively you can also configure Flyway to wrap...
Read more >
Managing Migrations - EF Core - Microsoft Learn
To manage migrations, you must first install the EF Core command-line ... and LastName properties with a single, new FullName property.
Read more >
Migrations - typeorm - GitBook
A migration is just a single file with sql queries to update a database schema and apply new changes to an existing database....
Read more >
How to run multiple Flyway migrations in a single transaction?
you can also configure Flyway to wrap the entire execution of all migrations of a single migration run within a single transaction by...
Read more >
Migrations - Django documentation
You can prevent a migration from running in a transaction by setting the atomic ... Restricting to a single app (either in makemigrations...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found