Skip to content

Commit

Permalink
feat: throw error on duplicate migration id (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashass authored Mar 2, 2023
1 parent 2f65c93 commit 9b4023e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default async ({
migrations: Migration[];
migrationStore: MigrationStore;
}): Promise<void> => {
if (migrations.length !== new Set(migrations.map((migration) => migration.id)).size) {
throw new Error('duplicate migration id');
}

const appliedMigrations = await migrationStore.getAppliedMigrations();
const migrationsToApply = migrations.filter(
(migration) =>
Expand Down
17 changes: 17 additions & 0 deletions test/up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ describe('up', () => {
expect(migration1.up).toHaveBeenCalledTimes(1);
expect(migration1.up).toHaveBeenCalledWith(context);
});

it('should throw an error when duplicate migration IDs are passed', async () => {
expect.assertions(1);

// given
const migrations = [migration1, migration1];
MongoMigrationStoreMock.mockImplementationOnce(() => ({
init: vi.fn(),
getAppliedMigrations: vi.fn().mockReturnValueOnce([]),
insertMigration: vi.fn(),
}));
const migrationStore = new MongoMigrationStore();

// when
// then
await expect(() => up({ migrations, migrationStore })).rejects.toThrowError('duplicate migration id');
});
});

0 comments on commit 9b4023e

Please sign in to comment.