Skip to content

Commit 9b4023e

Browse files
authored
feat: throw error on duplicate migration id (#46)
1 parent 2f65c93 commit 9b4023e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/up.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default async ({
1010
migrations: Migration[];
1111
migrationStore: MigrationStore;
1212
}): Promise<void> => {
13+
if (migrations.length !== new Set(migrations.map((migration) => migration.id)).size) {
14+
throw new Error('duplicate migration id');
15+
}
16+
1317
const appliedMigrations = await migrationStore.getAppliedMigrations();
1418
const migrationsToApply = migrations.filter(
1519
(migration) =>

test/up.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,21 @@ describe('up', () => {
8282
expect(migration1.up).toHaveBeenCalledTimes(1);
8383
expect(migration1.up).toHaveBeenCalledWith(context);
8484
});
85+
86+
it('should throw an error when duplicate migration IDs are passed', async () => {
87+
expect.assertions(1);
88+
89+
// given
90+
const migrations = [migration1, migration1];
91+
MongoMigrationStoreMock.mockImplementationOnce(() => ({
92+
init: vi.fn(),
93+
getAppliedMigrations: vi.fn().mockReturnValueOnce([]),
94+
insertMigration: vi.fn(),
95+
}));
96+
const migrationStore = new MongoMigrationStore();
97+
98+
// when
99+
// then
100+
await expect(() => up({ migrations, migrationStore })).rejects.toThrowError('duplicate migration id');
101+
});
85102
});

0 commit comments

Comments
 (0)