Skip to content

runMigrations does not respect migrationTransactionMode option #9695

@beeleebow

Description

@beeleebow

Issue description

DataSourceOptions.migrationTransactionMode not respected when calling DataSource.runMigrations

Expected Behavior

When the migrations are run, the migrationsTransactionMode present in the Connection/DataSource options is respected and you do not have to supply the migrationsTransactionMode as an argument to the runMigration method.

Actual Behavior

The default migrationTransactionMode of 'all' is used whenever you call DataSource.runMigrations without any arguments, even if you configured the options for the data source with a different transaction mode.

Steps to reproduce

Create a data source, providing a migrationsTransactionMode of 'each' in the options.

const dataSource = new DataSource({
    ...,
    migrationsTransactionMode: 'each'
})

Then have a migration which cannot run in a transaction, e.g. CREATE INDEX CONCURRENTLY for postgres. Use the transaction boolean on the MigrationInterface to opt out of transaction for this migration (which should be fine for the 'each' transaction mode).

import { MigrationInterface, QueryRunner } from 'typeorm'

export class ExampleMigration1672883829539 implements MigrationInterface {
  // Opt out of transaction for _this_ migration
  public transaction?: boolean | undefined = false

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `create index concurrently if not exists ...`,
    )
  }

  public async down(_: QueryRunner): Promise<void> {
    // corresponding down
  }
}

Then run the migrations using the DataSource. At this point you should see a failure reported because the default 'all' mode is being used, and the migration is trying to use the transaction boolean.

    const migrations = await dataSource.runMigrations()

Should see an error like this:
image

My Environment

Dependency Version
Operating System Mac OS Ventura
Node.js version 16.13.0
Typescript version 4.9.4
TypeORM version 0.3.11

Additional Context

I believe the fix would be to have the runMigrations method also consider this.options.migrationsTransactionMode.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions