@orion-js/migrations 3.6.6 → 3.6.8

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
  }
@@ -1,9 +1,10 @@
1
+ import { ExecutionContext } from '@orion-js/dogs';
1
2
  export interface MigrationServiceOptions {
2
3
  name: string;
3
4
  useMongoTransactions: false;
4
5
  }
5
6
  export declare function MigrationService(options: MigrationServiceOptions): ClassDecorator;
6
7
  export type MigrationExecutable = {
7
- runMigration(): Promise<void>;
8
+ runMigration(context: ExecutionContext): Promise<void>;
8
9
  } & MigrationServiceOptions;
9
10
  export declare function getMigrationsFromServices(services: any[]): MigrationExecutable[];
@@ -19,9 +19,9 @@ function getMigrationsFromServices(services) {
19
19
  const options = service.prototype.options;
20
20
  return {
21
21
  ...options,
22
- runMigration: async () => {
22
+ runMigration: async (context) => {
23
23
  const instance = (0, services_1.getInstance)(service);
24
- return await instance.runMigration();
24
+ return await instance.runMigration(context);
25
25
  }
26
26
  };
27
27
  });
@@ -10,9 +10,11 @@ const _1 = require(".");
10
10
  describe('Migration as IOC', () => {
11
11
  it('should create a migration service', async () => {
12
12
  let didRun = false;
13
+ let didExtend = false;
13
14
  let MoveUsersMigrationService = class MoveUsersMigrationService {
14
- async runMigration() {
15
+ async runMigration(context) {
15
16
  didRun = true;
17
+ context.extendLockTime(1000);
16
18
  }
17
19
  };
18
20
  MoveUsersMigrationService = __decorate([
@@ -25,9 +27,14 @@ describe('Migration as IOC', () => {
25
27
  let lastName = null;
26
28
  for (const { runMigration, name } of migrations) {
27
29
  lastName = name;
28
- await runMigration();
30
+ await runMigration({
31
+ extendLockTime: time => {
32
+ didExtend = true;
33
+ }
34
+ });
29
35
  }
30
36
  expect(lastName).toBe('moveUsers');
31
37
  expect(didRun).toBe(true);
38
+ expect(didExtend).toBe(true);
32
39
  });
33
40
  });
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.8",
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": "90170d94e2b04b940ac2d1272f77e139fa56ecd8"
44
44
  }