Skip to content

Commit fc4b8db

Browse files
committed
feat: pass optional context to migration functions
1 parent dc2aa3b commit fc4b8db

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/Migration.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// A declaration of a context passed to each migration.
2+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
3+
export interface MigrationContext {}
4+
15
export interface Migration {
26
id: string;
3-
up(): Promise<void> | void;
4-
down(): Promise<void> | void;
7+
up(context?: MigrationContext): Promise<void> | void;
8+
down(context?: MigrationContext): Promise<void> | void;
59
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Migration } from './Migration';
1+
import { Migration, MigrationContext } from './Migration';
22
import { MongoMigrationStore } from './MongoMigrationStore';
33
import up from './up';
44

5-
export { Migration, MongoMigrationStore, up };
5+
export { Migration, MigrationContext, MongoMigrationStore, up };

src/up.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Migration } from './Migration';
1+
import { Migration, MigrationContext } from './Migration';
22
import { MigrationStore } from './MigrationStore';
33

44
export default async ({
5+
context,
56
migrations,
67
migrationStore,
78
}: {
9+
context?: MigrationContext;
810
migrations: Migration[];
911
migrationStore: MigrationStore;
1012
}): Promise<void> => {
@@ -15,7 +17,7 @@ export default async ({
1517
);
1618

1719
for await (const migration of migrationsToApply) {
18-
await migration.up();
20+
await migration.up(context);
1921
await migrationStore.insertMigration(migration);
2022
}
2123
};

test/up.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { MongoMigrationStore } from '~/MongoMigrationStore';
22
import up from '~/up';
33
import { migration1, migration2, migration3 } from '$/__helpers__/TestMigrations';
44

5+
declare module '~/Migration' {
6+
interface MigrationContext {
7+
test: string;
8+
}
9+
}
10+
511
jest.mock('~/MongoMigrationStore');
612

713
const MongoMigrationStoreMock = MongoMigrationStore as jest.MockedClass<typeof MongoMigrationStore>;
@@ -53,4 +59,25 @@ describe('up', () => {
5359
expect(migration2.up).not.toHaveBeenCalled();
5460
expect(migration3.up).toHaveBeenCalledTimes(1);
5561
});
62+
63+
it('should pass a given context to the migrations up() function', async () => {
64+
expect.assertions(2);
65+
66+
// given
67+
const migrations = [migration1];
68+
MongoMigrationStoreMock.mockImplementationOnce(() => ({
69+
init: jest.fn(),
70+
getAppliedMigrations: jest.fn().mockReturnValueOnce([]),
71+
insertMigration: jest.fn(),
72+
}));
73+
const migrationStore = new MongoMigrationStore();
74+
const context = { test: '123' };
75+
76+
// when
77+
await up({ migrations, migrationStore, context });
78+
79+
// then
80+
expect(migration1.up).toHaveBeenCalledTimes(1);
81+
expect(migration1.up).toHaveBeenCalledWith(context);
82+
});
5683
});

0 commit comments

Comments
 (0)