@orion-js/migrations 3.6.6 → 3.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,9 @@
1
1
  import { MigrationExecutable } from './service';
2
+ import { ExecutionContext } from '@orion-js/dogs';
2
3
  export declare class MigrationsService {
3
4
  private migrationsRepo;
4
5
  getNextMigration(migrationsList: MigrationExecutable[]): Promise<MigrationExecutable>;
5
- runMigrations(migrationsList: MigrationExecutable[]): Promise<void>;
6
- runMigration(func: () => Promise<void>): Promise<void>;
7
- runAsTransaction(func: () => Promise<void>): Promise<void>;
6
+ runMigrations(migrationsList: MigrationExecutable[], context: ExecutionContext): Promise<void>;
7
+ runMigration(func: (context: ExecutionContext) => Promise<void>, context: ExecutionContext): Promise<void>;
8
+ runAsTransaction(func: (context: ExecutionContext) => Promise<void>, context: ExecutionContext): Promise<void>;
8
9
  }
@@ -22,36 +22,36 @@ let MigrationsService = exports.MigrationsService = class MigrationsService {
22
22
  return migrationExecutable;
23
23
  }
24
24
  }
25
- async runMigrations(migrationsList) {
25
+ async runMigrations(migrationsList, context) {
26
26
  const next = await this.getNextMigration(migrationsList);
27
27
  if (!next)
28
28
  return;
29
29
  logger_1.logger.info('[orionjs/migrations] Running migration...', { name: next.name });
30
30
  if (next.useMongoTransactions) {
31
- await this.runAsTransaction(next.runMigration);
31
+ await this.runAsTransaction(next.runMigration, context);
32
32
  }
33
33
  else {
34
- await this.runMigration(next.runMigration);
34
+ await this.runMigration(next.runMigration, context);
35
35
  }
36
36
  logger_1.logger.info('[orionjs/migrations] Migration executed correctly', { name: next.name });
37
37
  await this.migrationsRepo.saveCompletedMigration(next.name);
38
- await this.runMigrations(migrationsList);
38
+ await this.runMigrations(migrationsList, context);
39
39
  }
40
- async runMigration(func) {
40
+ async runMigration(func, context) {
41
41
  try {
42
- await func();
42
+ await func(context);
43
43
  }
44
44
  catch (error) {
45
45
  logger_1.logger.error('[orionjs/migrations] Error running migration', error);
46
46
  throw error;
47
47
  }
48
48
  }
49
- async runAsTransaction(func) {
49
+ async runAsTransaction(func, context) {
50
50
  const { client } = this.migrationsRepo.collection.client;
51
51
  const session = client.startSession();
52
52
  await session.withTransaction(async () => {
53
53
  try {
54
- await func();
54
+ await func(context);
55
55
  }
56
56
  catch (error) {
57
57
  logger_1.logger.error('[orionjs/migrations] Error running migration, will abort transaction', error);
package/lib/index.test.js CHANGED
@@ -28,11 +28,12 @@ describe('Migrations end to end', () => {
28
28
  ], MoveUsersMigrationService);
29
29
  const migrationExecutables = (0, loadMigrations_1.loadMigrations)([MoveUsersMigrationService], { omitJob: true });
30
30
  const instance = (0, services_1.getInstance)(MigrationsService_1.MigrationsService);
31
- await instance.runMigrations(migrationExecutables);
32
- await instance.runMigrations(migrationExecutables);
33
- await instance.runMigrations(migrationExecutables);
34
- await instance.runMigrations(migrationExecutables);
35
- await instance.runMigrations(migrationExecutables);
31
+ const context = {};
32
+ await instance.runMigrations(migrationExecutables, context);
33
+ await instance.runMigrations(migrationExecutables, context);
34
+ await instance.runMigrations(migrationExecutables, context);
35
+ await instance.runMigrations(migrationExecutables, context);
36
+ await instance.runMigrations(migrationExecutables, context);
36
37
  expect(executions).toBe(1);
37
38
  });
38
39
  it('should not set a migration completed if it fails', async () => {
@@ -50,8 +51,9 @@ describe('Migrations end to end', () => {
50
51
  ], MoveUsersMigrationService);
51
52
  const migrationExecutables = (0, loadMigrations_1.loadMigrations)([MoveUsersMigrationService], { omitJob: true });
52
53
  const instance = (0, services_1.getInstance)(MigrationsService_1.MigrationsService);
54
+ const context = {};
53
55
  try {
54
- await instance.runMigrations(migrationExecutables);
56
+ await instance.runMigrations(migrationExecutables, context);
55
57
  }
56
58
  catch { }
57
59
  const migrationsRepo = (0, services_1.getInstance)(Repo_1.MigrationsRepo);
@@ -18,9 +18,9 @@ function loadMigrations(migrationServices, options) {
18
18
  orionjsRunMigrations: (0, dogs_1.defineJob)({
19
19
  type: 'recurrent',
20
20
  runEvery: 30 * 1000,
21
- async resolve() {
21
+ async resolve(params, context) {
22
22
  const instance = (0, services_1.getInstance)(MigrationsService_1.MigrationsService);
23
- await instance.runMigrations(migrations);
23
+ await instance.runMigrations(migrations, context);
24
24
  }
25
25
  })
26
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/migrations",
3
- "version": "3.6.6",
3
+ "version": "3.6.7",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -40,5 +40,5 @@
40
40
  "publishConfig": {
41
41
  "access": "public"
42
42
  },
43
- "gitHead": "909ed69b06dd958b2f30f5b5446afce6a34bef51"
43
+ "gitHead": "62cb1e35543bf4a4a5275659d009ddfc1d73d9fa"
44
44
  }