File tree Expand file tree Collapse file tree 4 files changed +39
-6
lines changed Expand file tree Collapse file tree 4 files changed +39
-6
lines changed Original file line number Diff line number Diff line change
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
+
1
5
export interface Migration {
2
6
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 ;
5
9
}
Original file line number Diff line number Diff line change 1
- import { Migration } from './Migration' ;
1
+ import { Migration , MigrationContext } from './Migration' ;
2
2
import { MongoMigrationStore } from './MongoMigrationStore' ;
3
3
import up from './up' ;
4
4
5
- export { Migration , MongoMigrationStore , up } ;
5
+ export { Migration , MigrationContext , MongoMigrationStore , up } ;
Original file line number Diff line number Diff line change 1
- import { Migration } from './Migration' ;
1
+ import { Migration , MigrationContext } from './Migration' ;
2
2
import { MigrationStore } from './MigrationStore' ;
3
3
4
4
export default async ( {
5
+ context,
5
6
migrations,
6
7
migrationStore,
7
8
} : {
9
+ context ?: MigrationContext ;
8
10
migrations : Migration [ ] ;
9
11
migrationStore : MigrationStore ;
10
12
} ) : Promise < void > => {
@@ -15,7 +17,7 @@ export default async ({
15
17
) ;
16
18
17
19
for await ( const migration of migrationsToApply ) {
18
- await migration . up ( ) ;
20
+ await migration . up ( context ) ;
19
21
await migrationStore . insertMigration ( migration ) ;
20
22
}
21
23
} ;
Original file line number Diff line number Diff line change @@ -2,6 +2,12 @@ import { MongoMigrationStore } from '~/MongoMigrationStore';
2
2
import up from '~/up' ;
3
3
import { migration1 , migration2 , migration3 } from '$/__helpers__/TestMigrations' ;
4
4
5
+ declare module '~/Migration' {
6
+ interface MigrationContext {
7
+ test : string ;
8
+ }
9
+ }
10
+
5
11
jest . mock ( '~/MongoMigrationStore' ) ;
6
12
7
13
const MongoMigrationStoreMock = MongoMigrationStore as jest . MockedClass < typeof MongoMigrationStore > ;
@@ -53,4 +59,25 @@ describe('up', () => {
53
59
expect ( migration2 . up ) . not . toHaveBeenCalled ( ) ;
54
60
expect ( migration3 . up ) . toHaveBeenCalledTimes ( 1 ) ;
55
61
} ) ;
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
+ } ) ;
56
83
} ) ;
You can’t perform that action at this time.
0 commit comments