From a7823367f13c3dba0c339eaafaad0199bd524b13 Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:43:09 +0300 Subject: [PATCH] fix: Fix transaction handling for 'revert' command (#11145) --- .../src/commands/db/__tests__/revert.test.ts | 35 +++++++++++++++++++ packages/cli/src/commands/db/revert.ts | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/db/__tests__/revert.test.ts b/packages/cli/src/commands/db/__tests__/revert.test.ts index 463c9d617c..ce3911b2b6 100644 --- a/packages/cli/src/commands/db/__tests__/revert.test.ts +++ b/packages/cli/src/commands/db/__tests__/revert.test.ts @@ -170,3 +170,38 @@ test('revert the last migration if it has a down migration', async () => { expect(dataSource.undoLastMigration).toHaveBeenCalled(); expect(dataSource.destroy).toHaveBeenCalled(); }); + +test("don't use transaction if the last migration has transaction = false", async () => { + // + // ARRANGE + // + class TestMigration implements ReversibleMigration { + name = 'ReversibleMigration'; + + transaction = false as const; + + async up() {} + + async down() {} + } + + const migrationsInDb: Migration[] = [ + { id: 1, timestamp: Date.now(), name: 'ReversibleMigration' }, + ]; + const dataSource = mock({ migrations: [new TestMigration()] }); + + const migrationExecutor = mock(); + migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb); + + // + // ACT + // + await main(logger, dataSource, migrationExecutor); + + // + // ASSERT + // + expect(dataSource.undoLastMigration).toHaveBeenCalledWith({ + transaction: 'none', + }); +}); diff --git a/packages/cli/src/commands/db/revert.ts b/packages/cli/src/commands/db/revert.ts index 7823506ee0..4510044405 100644 --- a/packages/cli/src/commands/db/revert.ts +++ b/packages/cli/src/commands/db/revert.ts @@ -55,7 +55,9 @@ export async function main( return; } - await connection.undoLastMigration(); + await connection.undoLastMigration({ + transaction: lastMigrationInstance.transaction === false ? 'none' : 'each', + }); await connection.destroy(); }