Skip to content

Consistent migration lifecycle to ensure beforeMigration and afterMigration are always called #16

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

netroy
Copy link
Member

@netroy netroy commented Apr 29, 2025

typeorm#7922 added the code to disable/enable foreign key checks for sqlite when running migrations individually, but is missing the code do so when the migrations are run via connection.runMigrations().
This change ensures a consistent behavior between all the code paths.

Copy link

@despairblue despairblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, but I'm wondering about one thing.

If this.transaction is all wouldn't we commit outside of a transaction?

First this runs:

if (this.transaction === "all" && !queryRunner.isTransactionActive) {
await queryRunner.beforeMigration()
await queryRunner.startTransaction()
transactionStartedByUs = true
}

Then let's assume there is only one migration.
This is skipped, because queryRunner.isTransactionActive is true:

if (migration.transaction && !queryRunner.isTransactionActive) {
await queryRunner.beforeMigration()
await queryRunner.startTransaction()
transactionStartedByUs = true
}

Then we commit, since transactionStartedByUs is true:

if (migration.transaction && transactionStartedByUs) {
await queryRunner.commitTransaction()
}

Then this runs, because transactionStartedByUs is still true:

if (this.transaction === "all" && transactionStartedByUs) {
await queryRunner.commitTransaction()
}

So we're

  1. starting transaction
  2. don't start another one
  3. commit the transaction
  4. try to commit again

Or am I missing something?

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

Successfully merging this pull request may close these issues.

2 participants