Skip to content

Commit

Permalink
fix: Fix transaction handling for 'revert' command (#11145)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi authored and ivov committed Oct 8, 2024
1 parent 73f15dd commit d1fe22b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/cli/src/commands/db/__tests__/revert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataSource>({ migrations: [new TestMigration()] });

const migrationExecutor = mock<MigrationExecutor>();
migrationExecutor.getExecutedMigrations.mockResolvedValue(migrationsInDb);

//
// ACT
//
await main(logger, dataSource, migrationExecutor);

//
// ASSERT
//
expect(dataSource.undoLastMigration).toHaveBeenCalledWith({
transaction: 'none',
});
});
4 changes: 3 additions & 1 deletion packages/cli/src/commands/db/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export async function main(
return;
}

await connection.undoLastMigration();
await connection.undoLastMigration({
transaction: lastMigrationInstance.transaction === false ? 'none' : 'each',
});
await connection.destroy();
}

Expand Down

0 comments on commit d1fe22b

Please sign in to comment.