Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/migration/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,16 @@ export class Migrator {
}
}

if (adapter.supportsTransactionalDdl && !this.#props.disableTransactions) {
if (this.#props.db.isTransaction) {
if (!adapter.supportsTransactionalDdl) {
throw new Error('Transactional DDL is not supported in this dialect. Passing a transaction to this migrator would result in failure or unexpected behavior.')
}

return run(this.#props.db)
} else if (
adapter.supportsTransactionalDdl &&
!this.#props.disableTransactions
) {
return this.#props.db.transaction().execute(run)
} else {
return this.#props.db.connection().execute(run)
Expand Down
37 changes: 37 additions & 0 deletions test/node/src/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,43 @@
expect(transactionSpy.called).to.be.false
}
})

it('should run migrations using a provided transaction', async () => {
const migrationName = 'trx_connection_method_fail_case'
const executedUpMethodsForThisTest: string[] = []

const testSpecificProvider: MigrationProvider = {

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (20.x)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (20.x) /w transformer

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (22.x) /w transformer

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (22.x)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (24.x)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (24.x) /w transformer

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.7)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.9)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.0)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.8)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.6)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.3)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.4)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?

Check failure on line 833 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.2)

Cannot find name 'MigrationProvider'. Did you mean 'FileMigrationProvider'?
getMigrations: async () => ({
[migrationName]: {
async up(_db: Kysely<any>): Promise<void> {
executedUpMethodsForThisTest.push(migrationName)
},
async down(_db: Kysely<any>): Promise<void> {},
},
}),
}

const trx = await db.startTransaction().execute()

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (20.x)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (20.x) /w transformer

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (22.x) /w transformer

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (22.x)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (24.x)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Node.js (24.x) /w transformer

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.7)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.9)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.0)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.8)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^4.6)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.3)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.4)

Cannot find name 'db'.

Check failure on line 844 in test/node/src/migration.test.ts

View workflow job for this annotation

GitHub Actions / Older TypeScript version (^5.2)

Cannot find name 'db'.

const migratorInTransaction = new Migrator({
db: trx,
provider: testSpecificProvider,
})

const { results, error } = await migratorInTransaction.migrateUp()

await trx.rollback().execute().catch(() => {})

expect(error).to.be.undefined
expect(results).to.eql([
{ migrationName, direction: 'Up', status: 'Success' },
])

expect(executedUpMethodsForThisTest).to.eql(
[migrationName],
"The migration's 'up' method should have been executed.",
)
})
})

describe('migrateDown', () => {
Expand Down
Loading