@tstdl/base 0.89.6 → 0.89.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.
- package/migration/migrator.d.ts +1 -7
- package/migration/migrator.js +17 -30
- package/package.json +1 -1
package/migration/migrator.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { LockProvider } from '../lock/index.js';
|
|
2
|
-
import { Logger } from '../logger/index.js';
|
|
3
|
-
import { MigrationStateRepository } from './migration-state-repository.js';
|
|
4
1
|
export type MigrationDefinition<T = void> = {
|
|
5
2
|
name: string;
|
|
6
3
|
migrations: Migration<T>[];
|
|
@@ -21,9 +18,6 @@ export type MigrationResult<T> = {
|
|
|
21
18
|
restartRequested: boolean;
|
|
22
19
|
};
|
|
23
20
|
export declare class Migrator {
|
|
24
|
-
private
|
|
25
|
-
private readonly lockProvider;
|
|
26
|
-
private readonly logger;
|
|
27
|
-
constructor(migrationStateRepository: MigrationStateRepository, lockProvider: LockProvider, logger: Logger);
|
|
21
|
+
#private;
|
|
28
22
|
migrate<T>({ name, migrations }: MigrationDefinition<T>): Promise<MigrationResult<T>[]>;
|
|
29
23
|
}
|
package/migration/migrator.js
CHANGED
|
@@ -4,14 +4,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
-
};
|
|
13
7
|
import { CancellationToken } from '../cancellation/index.js';
|
|
14
|
-
import {
|
|
8
|
+
import { Injector, Singleton, inject, runInInjectionContext } from '../injector/index.js';
|
|
15
9
|
import { LockProvider } from '../lock/index.js';
|
|
16
10
|
import { Logger } from '../logger/index.js';
|
|
17
11
|
import { toArray } from '../utils/array/array.js';
|
|
@@ -21,20 +15,16 @@ import { Timer } from '../utils/timer.js';
|
|
|
21
15
|
import { isDefined } from '../utils/type-guards.js';
|
|
22
16
|
import { MigrationStateRepository } from './migration-state-repository.js';
|
|
23
17
|
let Migrator = class Migrator {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.migrationStateRepository = migrationStateRepository;
|
|
29
|
-
this.lockProvider = lockProvider;
|
|
30
|
-
this.logger = logger;
|
|
31
|
-
}
|
|
18
|
+
#injector = inject(Injector);
|
|
19
|
+
#migrationStateRepository = inject(MigrationStateRepository);
|
|
20
|
+
#lockProvider = inject(LockProvider, 'migrator:');
|
|
21
|
+
#logger = inject(Logger, 'Migrator');
|
|
32
22
|
// eslint-disable-next-line max-statements, max-lines-per-function
|
|
33
23
|
async migrate({ name, migrations }) {
|
|
34
24
|
if (migrations.length == 0) {
|
|
35
|
-
throw new Error('
|
|
25
|
+
throw new Error('No migrations provided.');
|
|
36
26
|
}
|
|
37
|
-
const lock = this
|
|
27
|
+
const lock = this.#lockProvider.get(`${name}`);
|
|
38
28
|
// eslint-disable-next-line max-statements, max-lines-per-function
|
|
39
29
|
const { result } = await lock.use(30000, true, async () => {
|
|
40
30
|
const results = [];
|
|
@@ -43,7 +33,7 @@ let Migrator = class Migrator {
|
|
|
43
33
|
const control = {
|
|
44
34
|
restart: () => restartToken.set()
|
|
45
35
|
};
|
|
46
|
-
const currentState = await this
|
|
36
|
+
const currentState = await this.#migrationStateRepository.tryLoadByFilter({ name });
|
|
47
37
|
const currentRevision = currentState?.revision ?? 'init';
|
|
48
38
|
const latestRevision = migrations.sort(compareByValueSelectionDescending((migration) => migration.to))[0].to;
|
|
49
39
|
if (currentRevision == latestRevision) {
|
|
@@ -51,29 +41,29 @@ let Migrator = class Migrator {
|
|
|
51
41
|
}
|
|
52
42
|
const suitableMigrations = migrations.filter((migration) => toArray(migration.from).includes(currentRevision));
|
|
53
43
|
if (suitableMigrations.length == 0) {
|
|
54
|
-
throw new Error(`
|
|
44
|
+
throw new Error(`No suitable migration path from current revision ${currentRevision} to latest revision ${latestRevision} found.`);
|
|
55
45
|
}
|
|
56
46
|
const migration = suitableMigrations.sort(compareByValueSelectionDescending((m) => m.to))[0];
|
|
57
|
-
this
|
|
47
|
+
this.#logger.warn(`Starting migration for "${name}" from revision ${currentRevision} to ${migration.to}.`);
|
|
58
48
|
let migratorResult;
|
|
59
|
-
const time = await Timer.measureAsync(async () => (migratorResult = await migration.migrator(control)));
|
|
49
|
+
const time = await Timer.measureAsync(async () => (migratorResult = await runInInjectionContext(this.#injector, async () => migration.migrator(control))));
|
|
60
50
|
results.push({ from: currentRevision, to: migration.to, time, result: migratorResult, restartRequested: restartToken.isSet });
|
|
61
51
|
if (restartToken.isSet) {
|
|
62
|
-
this
|
|
63
|
-
this
|
|
52
|
+
this.#logger.warn(`Finished migration in ${round(time / 1000, 2)} seconds.`);
|
|
53
|
+
this.#logger.warn('Migration-restart requested.');
|
|
64
54
|
continue;
|
|
65
55
|
}
|
|
66
56
|
if (isDefined(currentState)) {
|
|
67
|
-
await this
|
|
57
|
+
await this.#migrationStateRepository.patchByFilter({ name }, { revision: migration.to });
|
|
68
58
|
}
|
|
69
59
|
else {
|
|
70
60
|
const newState = {
|
|
71
61
|
name,
|
|
72
62
|
revision: migration.to
|
|
73
63
|
};
|
|
74
|
-
await this
|
|
64
|
+
await this.#migrationStateRepository.insert(newState);
|
|
75
65
|
}
|
|
76
|
-
this
|
|
66
|
+
this.#logger.warn(`Finished migration in ${round(time / 1000, 2)} seconds.`);
|
|
77
67
|
}
|
|
78
68
|
return results;
|
|
79
69
|
});
|
|
@@ -81,9 +71,6 @@ let Migrator = class Migrator {
|
|
|
81
71
|
}
|
|
82
72
|
};
|
|
83
73
|
Migrator = __decorate([
|
|
84
|
-
Singleton()
|
|
85
|
-
__param(1, ResolveArg('migrator:')),
|
|
86
|
-
__param(2, ResolveArg('Migrator')),
|
|
87
|
-
__metadata("design:paramtypes", [MigrationStateRepository, LockProvider, Logger])
|
|
74
|
+
Singleton()
|
|
88
75
|
], Migrator);
|
|
89
76
|
export { Migrator };
|