taon 18.0.25 → 18.0.26
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/README.md +112 -51
- package/browser/esm2022/lib/endpoint-context.mjs +4 -1
- package/browser/esm2022/lib/index.mjs +2 -2
- package/browser/esm2022/lib/models.mjs +1 -1
- package/browser/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
- package/browser/fesm2022/taon.mjs +3 -0
- package/browser/fesm2022/taon.mjs.map +1 -1
- package/browser/lib/models.d.ts +5 -0
- package/browser/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
- package/cli.js +11 -17
- package/cli.js.map +1 -1
- package/client/esm2022/lib/endpoint-context.mjs +4 -1
- package/client/esm2022/lib/index.mjs +2 -2
- package/client/esm2022/lib/models.mjs +1 -1
- package/client/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
- package/client/fesm2022/taon.mjs +3 -0
- package/client/fesm2022/taon.mjs.map +1 -1
- package/client/lib/models.d.ts +5 -0
- package/client/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
- package/index.js +15 -2
- package/index.js.map +1 -1
- package/lib/base-classes/base-abstract-entity.js +30 -28
- package/lib/base-classes/base-abstract-entity.js.map +1 -1
- package/lib/base-classes/base-class.js +9 -17
- package/lib/base-classes/base-class.js.map +1 -1
- package/lib/base-classes/base-context.js +4 -4
- package/lib/base-classes/base-context.js.map +1 -1
- package/lib/base-classes/base-controller.js +15 -15
- package/lib/base-classes/base-controller.js.map +1 -1
- package/lib/base-classes/base-crud-controller.js +211 -329
- package/lib/base-classes/base-crud-controller.js.map +1 -1
- package/lib/base-classes/base-entity.js +15 -15
- package/lib/base-classes/base-entity.js.map +1 -1
- package/lib/base-classes/base-injector.js +99 -124
- package/lib/base-classes/base-injector.js.map +1 -1
- package/lib/base-classes/base-migration.js +15 -31
- package/lib/base-classes/base-migration.js.map +1 -1
- package/lib/base-classes/base-provider.js +3 -9
- package/lib/base-classes/base-provider.js.map +1 -1
- package/lib/base-classes/base-repository.js +310 -444
- package/lib/base-classes/base-repository.js.map +1 -1
- package/lib/base-classes/base-subscriber-for-entity.js +73 -73
- package/lib/base-classes/base-subscriber-for-entity.js.map +1 -1
- package/lib/base-classes/base.js +10 -10
- package/lib/base-classes/base.js.map +1 -1
- package/lib/build-info._auto-generated_.js.map +1 -1
- package/lib/constants.js.map +1 -1
- package/lib/context-db-migrations.js +325 -581
- package/lib/context-db-migrations.js.map +1 -1
- package/lib/create-context.js +80 -109
- package/lib/create-context.js.map +1 -1
- package/lib/decorators/classes/controller-decorator.js +15 -13
- package/lib/decorators/classes/controller-decorator.js.map +1 -1
- package/lib/decorators/classes/entity-decorator.js +23 -20
- package/lib/decorators/classes/entity-decorator.js.map +1 -1
- package/lib/decorators/classes/migration-decorator.js +7 -13
- package/lib/decorators/classes/migration-decorator.js.map +1 -1
- package/lib/decorators/classes/provider-decorator.js +7 -13
- package/lib/decorators/classes/provider-decorator.js.map +1 -1
- package/lib/decorators/classes/repository-decorator.js +7 -13
- package/lib/decorators/classes/repository-decorator.js.map +1 -1
- package/lib/decorators/classes/subscriber-decorator.js +8 -13
- package/lib/decorators/classes/subscriber-decorator.js.map +1 -1
- package/lib/decorators/http/http-decorators.js +4 -4
- package/lib/decorators/http/http-decorators.js.map +1 -1
- package/lib/decorators/http/http-methods-decorators.js +19 -25
- package/lib/decorators/http/http-methods-decorators.js.map +1 -1
- package/lib/decorators/http/http-params-decorators.js +7 -9
- package/lib/decorators/http/http-params-decorators.js.map +1 -1
- package/lib/dependency-injection/di-container.js +13 -18
- package/lib/dependency-injection/di-container.js.map +1 -1
- package/lib/endpoint-context.js +1196 -1543
- package/lib/endpoint-context.js.map +1 -1
- package/lib/entity-process.js +102 -112
- package/lib/entity-process.js.map +1 -1
- package/lib/env.js +2 -2
- package/lib/env.js.map +1 -1
- package/lib/formly/formly-group-wrapper-component.js +45 -23
- package/lib/formly/formly-group-wrapper-component.js.map +1 -1
- package/lib/formly/formly-repeat-component.js +74 -27
- package/lib/formly/formly-repeat-component.js.map +1 -1
- package/lib/formly/formly.models.js.map +1 -1
- package/lib/formly/fromly.js +57 -61
- package/lib/formly/fromly.js.map +1 -1
- package/lib/formly/type-from-entity.js +11 -16
- package/lib/formly/type-from-entity.js.map +1 -1
- package/lib/get-response-value.js +22 -33
- package/lib/get-response-value.js.map +1 -1
- package/lib/helpers/class-helpers.js +62 -73
- package/lib/helpers/class-helpers.js.map +1 -1
- package/lib/helpers/taon-helpers.js +33 -31
- package/lib/helpers/taon-helpers.js.map +1 -1
- package/lib/index._auto-generated_.d.ts +1 -0
- package/lib/index._auto-generated_.js.map +1 -1
- package/lib/index.d.ts +1 -25
- package/lib/index.js +44 -41
- package/lib/index.js.map +1 -1
- package/lib/inject.js +15 -15
- package/lib/inject.js.map +1 -1
- package/lib/models.d.ts +5 -0
- package/lib/models.js +124 -100
- package/lib/models.js.map +1 -1
- package/lib/orm.js +13 -27
- package/lib/orm.js.map +1 -1
- package/lib/realtime/realtime-client.js +70 -65
- package/lib/realtime/realtime-client.js.map +1 -1
- package/lib/realtime/realtime-core.js +48 -29
- package/lib/realtime/realtime-core.js.map +1 -1
- package/lib/realtime/realtime-server.js +81 -80
- package/lib/realtime/realtime-server.js.map +1 -1
- package/lib/realtime/realtime-strategy/index.js +18 -5
- package/lib/realtime/realtime-strategy/index.js.map +1 -1
- package/lib/realtime/realtime-strategy/realtime-strategy-ipc.js +132 -172
- package/lib/realtime/realtime-strategy/realtime-strategy-ipc.js.map +1 -1
- package/lib/realtime/realtime-strategy/realtime-strategy-mock.js +158 -241
- package/lib/realtime/realtime-strategy/realtime-strategy-mock.js.map +1 -1
- package/lib/realtime/realtime-strategy/realtime-strategy-socket-io.js +18 -29
- package/lib/realtime/realtime-strategy/realtime-strategy-socket-io.js.map +1 -1
- package/lib/realtime/realtime-strategy/realtime-strategy.js +9 -13
- package/lib/realtime/realtime-strategy/realtime-strategy.js.map +1 -1
- package/lib/realtime/realtime-subs-manager.js +29 -30
- package/lib/realtime/realtime-subs-manager.js.map +1 -1
- package/lib/realtime/realtime.models.js.map +1 -1
- package/lib/storage.js +1 -1
- package/lib/storage.js.map +1 -1
- package/lib/symbols.js +62 -66
- package/lib/symbols.js.map +1 -1
- package/lib/ui/directives/index.js +2 -2
- package/lib/ui/directives/index.js.map +1 -1
- package/lib/ui/directives/view-mode.js.map +1 -1
- package/lib/ui/index.js +2 -2
- package/lib/ui/index.js.map +1 -1
- package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.d.ts +2 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.js +5 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/index.js.map +1 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/taon-admin-edit-mode.component.d.ts +29 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-admin-edit-mode/taon-admin-edit-mode.module.d.ts +2 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.d.ts +1 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.js +5 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/index.js.map +1 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-db-admin/taon-db-admin.component.d.ts +8 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.d.ts +2 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.js +5 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/index.js.map +1 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/taon-file-general-opt.component.d.ts +13 -0
- package/lib/ui/taon-admin-mode-configuration/components/taon-file-general-opt/taon-file-general-opt.module.d.ts +2 -0
- package/lib/ui/taon-admin-mode-configuration/index.js +2 -2
- package/lib/ui/taon-admin-mode-configuration/index.js.map +1 -1
- package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.d.ts +5 -0
- package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.js +3 -0
- package/lib/ui/taon-admin-mode-configuration/models/taon-admin-mode-tabs.js.map +1 -0
- package/lib/ui/taon-admin-mode-configuration/taon-admin-control.service.d.ts +14 -0
- package/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
- package/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.module.d.ts +2 -0
- package/lib/ui/taon-admin-mode-configuration/taon-admin.models.js.map +1 -1
- package/lib/ui/taon-github-fork-me-corner/index.js +2 -2
- package/lib/ui/taon-github-fork-me-corner/index.js.map +1 -1
- package/lib/ui/taon-github-fork-me-ribbon/index.js +2 -2
- package/lib/ui/taon-github-fork-me-ribbon/index.js.map +1 -1
- package/lib/ui/taon-notifications/index.js +15 -2
- package/lib/ui/taon-notifications/index.js.map +1 -1
- package/lib/ui/taon-notifications/taon-notifications.models.js +2 -2
- package/lib/ui/taon-notifications/taon-notifications.models.js.map +1 -1
- package/lib/ui/taon-progress-bar/index.js +2 -2
- package/lib/ui/taon-progress-bar/index.js.map +1 -1
- package/lib/ui/taon-progress-bar/taon-progress-bar.component.d.ts +2 -1
- package/lib/ui/taon-session-passcode/index.js +2 -2
- package/lib/ui/taon-session-passcode/index.js.map +1 -1
- package/lib/ui/taon-table/index.js +2 -2
- package/lib/ui/taon-table/index.js.map +1 -1
- package/lib/ui/taon.models.js.map +1 -1
- package/lib/validators.js +48 -16
- package/lib/validators.js.map +1 -1
- package/migrations/index.js +15 -2
- package/migrations/index.js.map +1 -1
- package/migrations/migrations_index._auto-generated_.d.ts +1 -0
- package/migrations/migrations_index._auto-generated_.js.map +1 -1
- package/old-app .d.ts +1 -0
- package/old-app .js +67 -78
- package/old-app .js.map +1 -1
- package/package.json +17 -64
- package/playground.d.ts +1 -0
- package/playground.js.map +1 -1
- package/src.d.ts +1 -1
- package/taon.jsonc +39 -42
- package/tmp-environment.json +8 -401
- package/websql/esm2022/lib/endpoint-context.mjs +4 -1
- package/websql/esm2022/lib/index.mjs +2 -2
- package/websql/esm2022/lib/models.mjs +1 -1
- package/websql/esm2022/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.mjs +11 -11
- package/websql/fesm2022/taon.mjs +3 -0
- package/websql/fesm2022/taon.mjs.map +1 -1
- package/websql/lib/models.d.ts +5 -0
- package/websql/lib/ui/taon-admin-mode-configuration/taon-admin-mode-configuration.component.d.ts +4 -4
- package/browser/package.json +0 -25
- package/websql/package.json +0 -25
|
@@ -1,605 +1,349 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ContextDbMigrations = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
4
|
//#region imports
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const tnp_core_1 = require("tnp-core");
|
|
6
|
+
const models_1 = require("./models");
|
|
7
|
+
const class_helpers_1 = require("./helpers/class-helpers");
|
|
8
|
+
const taon_typeorm_1 = require("taon-typeorm");
|
|
9
|
+
class ContextDbMigrations {
|
|
10
|
+
ctx;
|
|
11
|
+
//#region fields
|
|
12
|
+
//#region fields / migration table name
|
|
13
|
+
DEFAULT_MIGRATION_TABLE_NAME = 'TAON_MIGRATION_META';
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region fields / migration statuses
|
|
16
|
+
MIGRATION_STATUS_COMPLETED = 'completed';
|
|
17
|
+
MIGRATION_STATUS_PENDING = 'pending';
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region fields / migration table schema
|
|
20
|
+
table = new taon_typeorm_1.Table({
|
|
21
|
+
name: this.DEFAULT_MIGRATION_TABLE_NAME,
|
|
22
|
+
columns: [
|
|
23
|
+
{
|
|
24
|
+
name: 'id',
|
|
25
|
+
type: 'integer',
|
|
26
|
+
isPrimary: true, // Mark it as the primary key
|
|
27
|
+
isGenerated: true, // Enable auto-generation
|
|
28
|
+
generationStrategy: 'increment', // Use auto-increment strategy
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'name',
|
|
32
|
+
type: 'varchar',
|
|
33
|
+
length: '255',
|
|
34
|
+
isUnique: true, // Ensure the name is unique
|
|
35
|
+
isNullable: false, // Ensure this field is required
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
// context is a part of name
|
|
39
|
+
name: 'context',
|
|
40
|
+
type: 'varchar',
|
|
41
|
+
length: '255',
|
|
42
|
+
isNullable: false, // Optional context for migrations (e.g., tenant or module name)
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'applied_at',
|
|
46
|
+
type: 'timestamp',
|
|
47
|
+
default: 'CURRENT_TIMESTAMP', // Automatically set the timestamp
|
|
48
|
+
isNullable: true,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: 'status',
|
|
52
|
+
type: 'varchar',
|
|
53
|
+
length: '50',
|
|
54
|
+
default: `'${this.MIGRATION_STATUS_COMPLETED}'`,
|
|
55
|
+
isNullable: false,
|
|
56
|
+
},
|
|
57
|
+
// { // TODO not needed for now
|
|
58
|
+
// name: 'checksum',
|
|
59
|
+
// type: 'varchar',
|
|
60
|
+
// length: '64',
|
|
61
|
+
// isNullable: true, // Optional field to store a hash/checksum of migration file
|
|
62
|
+
// },
|
|
63
|
+
],
|
|
64
|
+
});
|
|
11
65
|
//#endregion
|
|
12
66
|
//#endregion
|
|
13
67
|
//#region constructor
|
|
14
|
-
|
|
68
|
+
constructor(ctx) {
|
|
15
69
|
this.ctx = ctx;
|
|
16
|
-
//#region fields
|
|
17
|
-
//#region fields / migration table name
|
|
18
|
-
this.DEFAULT_MIGRATION_TABLE_NAME = 'TAON_MIGRATION_META';
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region fields / migration statuses
|
|
21
|
-
this.MIGRATION_STATUS_COMPLETED = 'completed';
|
|
22
|
-
this.MIGRATION_STATUS_PENDING = 'pending';
|
|
23
|
-
//#endregion
|
|
24
|
-
//#region fields / migration table schema
|
|
25
|
-
this.table = new taon_typeorm_1.Table({
|
|
26
|
-
name: this.DEFAULT_MIGRATION_TABLE_NAME,
|
|
27
|
-
columns: [
|
|
28
|
-
{
|
|
29
|
-
name: 'id',
|
|
30
|
-
type: 'integer',
|
|
31
|
-
isPrimary: true, // Mark it as the primary key
|
|
32
|
-
isGenerated: true, // Enable auto-generation
|
|
33
|
-
generationStrategy: 'increment', // Use auto-increment strategy
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
name: 'name',
|
|
37
|
-
type: 'varchar',
|
|
38
|
-
length: '255',
|
|
39
|
-
isUnique: true, // Ensure the name is unique
|
|
40
|
-
isNullable: false, // Ensure this field is required
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
// context is a part of name
|
|
44
|
-
name: 'context',
|
|
45
|
-
type: 'varchar',
|
|
46
|
-
length: '255',
|
|
47
|
-
isNullable: false, // Optional context for migrations (e.g., tenant or module name)
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: 'applied_at',
|
|
51
|
-
type: 'timestamp',
|
|
52
|
-
default: 'CURRENT_TIMESTAMP', // Automatically set the timestamp
|
|
53
|
-
isNullable: true,
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
name: 'status',
|
|
57
|
-
type: 'varchar',
|
|
58
|
-
length: '50',
|
|
59
|
-
default: "'".concat(this.MIGRATION_STATUS_COMPLETED, "'"),
|
|
60
|
-
isNullable: false,
|
|
61
|
-
},
|
|
62
|
-
// { // TODO not needed for now
|
|
63
|
-
// name: 'checksum',
|
|
64
|
-
// type: 'varchar',
|
|
65
|
-
// length: '64',
|
|
66
|
-
// isNullable: true, // Optional field to store a hash/checksum of migration file
|
|
67
|
-
// },
|
|
68
|
-
],
|
|
69
|
-
});
|
|
70
70
|
}
|
|
71
71
|
//#endregion
|
|
72
72
|
//#region methods & getters / make sure migration table exists
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return [4 /*yield*/, queryRunner.createIndex(this.DEFAULT_MIGRATION_TABLE_NAME, new taon_typeorm_1.TableIndex({
|
|
109
|
-
name: 'IDX_NAME',
|
|
110
|
-
columnNames: ['name'],
|
|
111
|
-
}))];
|
|
112
|
-
case 8:
|
|
113
|
-
_a.sent();
|
|
114
|
-
return [4 /*yield*/, queryRunner.commitTransaction()];
|
|
115
|
-
case 9:
|
|
116
|
-
_a.sent();
|
|
117
|
-
return [3 /*break*/, 14];
|
|
118
|
-
case 10:
|
|
119
|
-
error_1 = _a.sent();
|
|
120
|
-
this.ctx.logMigrations &&
|
|
121
|
-
console.error("Transaction failed [ensureMigrationTableExists]" + ", rolling back:", error_1);
|
|
122
|
-
return [4 /*yield*/, queryRunner.rollbackTransaction()];
|
|
123
|
-
case 11:
|
|
124
|
-
_a.sent();
|
|
125
|
-
return [3 /*break*/, 14];
|
|
126
|
-
case 12: return [4 /*yield*/, queryRunner.release()];
|
|
127
|
-
case 13:
|
|
128
|
-
_a.sent();
|
|
129
|
-
return [7 /*endfinally*/];
|
|
130
|
-
case 14: return [2 /*return*/];
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
};
|
|
73
|
+
async ensureMigrationTableExists() {
|
|
74
|
+
//#region @websqlFunc
|
|
75
|
+
if (this.ctx.remoteHost) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const queryRunner = this.ctx.connection.createQueryRunner();
|
|
79
|
+
await queryRunner.connect();
|
|
80
|
+
await queryRunner.startTransaction();
|
|
81
|
+
// Check if the table already exists
|
|
82
|
+
const hasTable = await queryRunner.hasTable(this.DEFAULT_MIGRATION_TABLE_NAME);
|
|
83
|
+
if (hasTable) {
|
|
84
|
+
this.ctx.logMigrations &&
|
|
85
|
+
console.log(`Table ${this.DEFAULT_MIGRATION_TABLE_NAME} already exists.`);
|
|
86
|
+
await queryRunner.commitTransaction();
|
|
87
|
+
await queryRunner.release();
|
|
88
|
+
return; // Exit early if the table exists
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await queryRunner.createTable(this.table);
|
|
92
|
+
await queryRunner.createIndex(this.DEFAULT_MIGRATION_TABLE_NAME, new taon_typeorm_1.TableIndex({
|
|
93
|
+
name: 'IDX_NAME',
|
|
94
|
+
columnNames: ['name'],
|
|
95
|
+
}));
|
|
96
|
+
await queryRunner.commitTransaction();
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
this.ctx.logMigrations &&
|
|
100
|
+
console.error(`Transaction failed [ensureMigrationTableExists]` + `, rolling back:`, error);
|
|
101
|
+
await queryRunner.rollbackTransaction();
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
await queryRunner.release();
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
}
|
|
135
108
|
//#endregion
|
|
136
109
|
//#region methods & getters / revert migration to timestamp
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
.filter(function (f) { return !!f; })
|
|
185
|
-
.map(function (f) { return f; })
|
|
186
|
-
.filter(function (migrationInstance) { return migrationInstance.isReadyToRun(); });
|
|
187
|
-
queryRunner = this.ctx.connection.createQueryRunner();
|
|
188
|
-
return [4 /*yield*/, queryRunner.connect()];
|
|
189
|
-
case 1:
|
|
190
|
-
_b.sent();
|
|
191
|
-
_b.label = 2;
|
|
192
|
-
case 2:
|
|
193
|
-
_b.trys.push([2, 15, 17, 19]);
|
|
194
|
-
return [4 /*yield*/, queryRunner.startTransaction()];
|
|
195
|
-
case 3:
|
|
196
|
-
_b.sent();
|
|
197
|
-
return [4 /*yield*/, queryRunner.query("SELECT name FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, "\n WHERE status = $1 AND context = $2"), [this.MIGRATION_STATUS_COMPLETED, this.ctx.contextName])];
|
|
198
|
-
case 4:
|
|
199
|
-
appliedMigrationsForContext = _b.sent();
|
|
200
|
-
appliedMigrationsForContextNames = appliedMigrationsForContext.map(function (m) { return m.name; });
|
|
201
|
-
_b.label = 5;
|
|
202
|
-
case 5:
|
|
203
|
-
_b.trys.push([5, 11, 12, 13]);
|
|
204
|
-
migrationClassesInstancesToRevert_1 = tslib_1.__values(migrationClassesInstancesToRevert), migrationClassesInstancesToRevert_1_1 = migrationClassesInstancesToRevert_1.next();
|
|
205
|
-
_b.label = 6;
|
|
206
|
-
case 6:
|
|
207
|
-
if (!!migrationClassesInstancesToRevert_1_1.done) return [3 /*break*/, 10];
|
|
208
|
-
migrationClassInstance = migrationClassesInstancesToRevert_1_1.value;
|
|
209
|
-
migrationName = class_helpers_1.ClassHelpers.getName(migrationClassInstance);
|
|
210
|
-
if (!appliedMigrationsForContextNames.includes(migrationName)) {
|
|
211
|
-
this.ctx.logMigrations &&
|
|
212
|
-
console.warn("Skipping migration not marked as applied: ".concat(migrationName));
|
|
213
|
-
return [3 /*break*/, 9];
|
|
214
|
-
}
|
|
215
|
-
this.ctx.logMigrations &&
|
|
216
|
-
console.log("Reverting migration: ".concat(migrationName, " , context: ").concat(this.ctx.contextName));
|
|
217
|
-
return [4 /*yield*/, migrationClassInstance.down(queryRunner)];
|
|
218
|
-
case 7:
|
|
219
|
-
_b.sent();
|
|
220
|
-
// Remove the reverted migration from the tracking table
|
|
221
|
-
return [4 /*yield*/, queryRunner.query("DELETE FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " WHERE name = $1"), [migrationName])];
|
|
222
|
-
case 8:
|
|
223
|
-
// Remove the reverted migration from the tracking table
|
|
224
|
-
_b.sent();
|
|
225
|
-
_b.label = 9;
|
|
226
|
-
case 9:
|
|
227
|
-
migrationClassesInstancesToRevert_1_1 = migrationClassesInstancesToRevert_1.next();
|
|
228
|
-
return [3 /*break*/, 6];
|
|
229
|
-
case 10: return [3 /*break*/, 13];
|
|
230
|
-
case 11:
|
|
231
|
-
e_1_1 = _b.sent();
|
|
232
|
-
e_1 = { error: e_1_1 };
|
|
233
|
-
return [3 /*break*/, 13];
|
|
234
|
-
case 12:
|
|
235
|
-
try {
|
|
236
|
-
if (migrationClassesInstancesToRevert_1_1 && !migrationClassesInstancesToRevert_1_1.done && (_a = migrationClassesInstancesToRevert_1.return)) _a.call(migrationClassesInstancesToRevert_1);
|
|
237
|
-
}
|
|
238
|
-
finally { if (e_1) throw e_1.error; }
|
|
239
|
-
return [7 /*endfinally*/];
|
|
240
|
-
case 13: return [4 /*yield*/, queryRunner.commitTransaction()];
|
|
241
|
-
case 14:
|
|
242
|
-
_b.sent();
|
|
243
|
-
this.ctx.logMigrations &&
|
|
244
|
-
console.log("Migrations successfully reverted " +
|
|
245
|
-
"to the specified timestamp ".concat(timestamp, " ."));
|
|
246
|
-
return [3 /*break*/, 19];
|
|
247
|
-
case 15:
|
|
248
|
-
error_2 = _b.sent();
|
|
249
|
-
this.ctx.logMigrations &&
|
|
250
|
-
console.error('Transaction failed, rolling back:', error_2);
|
|
251
|
-
return [4 /*yield*/, queryRunner.rollbackTransaction()];
|
|
252
|
-
case 16:
|
|
253
|
-
_b.sent();
|
|
254
|
-
return [3 /*break*/, 19];
|
|
255
|
-
case 17: return [4 /*yield*/, queryRunner.release()];
|
|
256
|
-
case 18:
|
|
257
|
-
_b.sent();
|
|
258
|
-
return [7 /*endfinally*/];
|
|
259
|
-
case 19: return [2 /*return*/];
|
|
110
|
+
async logSelectALl(name, queryRunner) {
|
|
111
|
+
console.log(name, (await queryRunner.query(`SELECT * FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE context = $1`, [this.ctx.contextName])).map(m => m.name));
|
|
112
|
+
}
|
|
113
|
+
async revertMigrationToTimestamp(timestamp) {
|
|
114
|
+
//#region @websqlFunc
|
|
115
|
+
if (this.ctx.remoteHost) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (!tnp_core_1.UtilsMigrations.isValidTimestamp(timestamp)) {
|
|
119
|
+
tnp_core_1.Helpers.throw(`Invalid timestamp provided for migration revert: ${timestamp}`);
|
|
120
|
+
}
|
|
121
|
+
// Get all migration class functions and reverse the order
|
|
122
|
+
const migrationsClassFns = this.ctx
|
|
123
|
+
.getClassFunByArr(models_1.Models.ClassType.MIGRATION)
|
|
124
|
+
.reverse();
|
|
125
|
+
// Filter migrations that need to be reverted
|
|
126
|
+
const migrationClassesInstancesToRevert = migrationsClassFns
|
|
127
|
+
.map(classFn => {
|
|
128
|
+
const timestampFromClassName = Number(tnp_core_1.UtilsMigrations.getTimestampFromClassName(class_helpers_1.ClassHelpers.getName(classFn)));
|
|
129
|
+
if (timestampFromClassName <= timestamp) {
|
|
130
|
+
// this.ctx.logMigrations &&
|
|
131
|
+
// console.log(
|
|
132
|
+
// `Stopping migration filter at: ${ClassHelpers.getName(classFn)} ` +
|
|
133
|
+
// `with timestamp ${timestampFromClassName}`,
|
|
134
|
+
// );
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return this.ctx.getInstanceBy(classFn);
|
|
138
|
+
})
|
|
139
|
+
.filter(f => !!f)
|
|
140
|
+
.map(f => f)
|
|
141
|
+
.filter(migrationInstance => migrationInstance.isReadyToRun());
|
|
142
|
+
const queryRunner = this.ctx.connection.createQueryRunner();
|
|
143
|
+
await queryRunner.connect();
|
|
144
|
+
try {
|
|
145
|
+
await queryRunner.startTransaction();
|
|
146
|
+
// Fetch applied migrations from the database
|
|
147
|
+
const appliedMigrationsForContext = await queryRunner.query(`SELECT name FROM ${this.DEFAULT_MIGRATION_TABLE_NAME}
|
|
148
|
+
WHERE status = $1 AND context = $2`, [this.MIGRATION_STATUS_COMPLETED, this.ctx.contextName]);
|
|
149
|
+
const appliedMigrationsForContextNames = appliedMigrationsForContext.map(m => m.name);
|
|
150
|
+
// console.log({ appliedMigrationsForContextNames });
|
|
151
|
+
for (const migrationClassInstance of migrationClassesInstancesToRevert) {
|
|
152
|
+
const migrationName = class_helpers_1.ClassHelpers.getName(migrationClassInstance);
|
|
153
|
+
if (!appliedMigrationsForContextNames.includes(migrationName)) {
|
|
154
|
+
this.ctx.logMigrations &&
|
|
155
|
+
console.warn(`Skipping migration not marked as applied: ${migrationName}`);
|
|
156
|
+
continue;
|
|
260
157
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
158
|
+
this.ctx.logMigrations &&
|
|
159
|
+
console.log(`Reverting migration: ${migrationName} , context: ${this.ctx.contextName}`);
|
|
160
|
+
await migrationClassInstance.down(queryRunner);
|
|
161
|
+
// Remove the reverted migration from the tracking table
|
|
162
|
+
await queryRunner.query(`DELETE FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE name = $1`, [migrationName]);
|
|
163
|
+
}
|
|
164
|
+
await queryRunner.commitTransaction();
|
|
165
|
+
this.ctx.logMigrations &&
|
|
166
|
+
console.log(`Migrations successfully reverted ` +
|
|
167
|
+
`to the specified timestamp ${timestamp} .`);
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
this.ctx.logMigrations &&
|
|
171
|
+
console.error('Transaction failed, rolling back:', error);
|
|
172
|
+
await queryRunner.rollbackTransaction();
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
await queryRunner.release();
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
}
|
|
264
179
|
//#endregion
|
|
265
180
|
//#region methods & getters / clear migration table
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
_a.sent();
|
|
289
|
-
return [4 /*yield*/, queryRunner.commitTransaction()];
|
|
290
|
-
case 5:
|
|
291
|
-
_a.sent();
|
|
292
|
-
return [3 /*break*/, 10];
|
|
293
|
-
case 6:
|
|
294
|
-
error_3 = _a.sent();
|
|
295
|
-
this.ctx.logMigrations &&
|
|
296
|
-
console.error('Transaction failed, rolling back:', error_3);
|
|
297
|
-
return [4 /*yield*/, queryRunner.rollbackTransaction()];
|
|
298
|
-
case 7:
|
|
299
|
-
_a.sent();
|
|
300
|
-
return [3 /*break*/, 10];
|
|
301
|
-
case 8: return [4 /*yield*/, queryRunner.release()];
|
|
302
|
-
case 9:
|
|
303
|
-
_a.sent();
|
|
304
|
-
return [7 /*endfinally*/];
|
|
305
|
-
case 10: return [2 /*return*/];
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
};
|
|
181
|
+
async clearMigrationTable() {
|
|
182
|
+
//#region @websqlFunc
|
|
183
|
+
if (this.ctx.remoteHost) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const queryRunner = this.ctx.connection.createQueryRunner();
|
|
187
|
+
await queryRunner.connect();
|
|
188
|
+
await queryRunner.startTransaction();
|
|
189
|
+
try {
|
|
190
|
+
await queryRunner.clearTable(this.DEFAULT_MIGRATION_TABLE_NAME);
|
|
191
|
+
await queryRunner.commitTransaction();
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
this.ctx.logMigrations &&
|
|
195
|
+
console.error('Transaction failed, rolling back:', error);
|
|
196
|
+
await queryRunner.rollbackTransaction();
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
await queryRunner.release();
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
}
|
|
310
203
|
//#endregion
|
|
311
204
|
//#region methods & getters / mark all migrations as applied
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
_b.trys.push([2, 15, 17, 19]);
|
|
336
|
-
return [4 /*yield*/, queryRunner.startTransaction()];
|
|
337
|
-
case 3:
|
|
338
|
-
_b.sent();
|
|
339
|
-
return [4 /*yield*/, queryRunner.query("SELECT name FROM ".concat(this.DEFAULT_MIGRATION_TABLE_NAME))];
|
|
340
|
-
case 4:
|
|
341
|
-
allMigrationsInDb = _b.sent();
|
|
342
|
-
allMigrationInDBNames = allMigrationsInDb.map(function (m) { return m.name; });
|
|
343
|
-
_b.label = 5;
|
|
344
|
-
case 5:
|
|
345
|
-
_b.trys.push([5, 10, 11, 12]);
|
|
346
|
-
migrationClassesInstances_1 = tslib_1.__values(migrationClassesInstances), migrationClassesInstances_1_1 = migrationClassesInstances_1.next();
|
|
347
|
-
_b.label = 6;
|
|
348
|
-
case 6:
|
|
349
|
-
if (!!migrationClassesInstances_1_1.done) return [3 /*break*/, 9];
|
|
350
|
-
instance = migrationClassesInstances_1_1.value;
|
|
351
|
-
migrationName = class_helpers_1.ClassHelpers.getName(instance);
|
|
352
|
-
if (allMigrationInDBNames.includes(migrationName)) {
|
|
353
|
-
this.ctx.logMigrations &&
|
|
354
|
-
console.log("Skipping already applied migration: ".concat(migrationName));
|
|
355
|
-
return [3 /*break*/, 8];
|
|
356
|
-
}
|
|
357
|
-
this.ctx.logMigrations &&
|
|
358
|
-
console.log("Marking migration as applied: ".concat(migrationName));
|
|
359
|
-
// Insert migration as 'complete' without running
|
|
360
|
-
return [4 /*yield*/, queryRunner.query("INSERT INTO ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, " (name, status, context, applied_at) ") +
|
|
361
|
-
"VALUES ($1, $2, $3, CURRENT_TIMESTAMP)", [
|
|
362
|
-
migrationName,
|
|
363
|
-
this.MIGRATION_STATUS_COMPLETED,
|
|
364
|
-
instance.ctx.contextName,
|
|
365
|
-
])];
|
|
366
|
-
case 7:
|
|
367
|
-
// Insert migration as 'complete' without running
|
|
368
|
-
_b.sent();
|
|
369
|
-
_b.label = 8;
|
|
370
|
-
case 8:
|
|
371
|
-
migrationClassesInstances_1_1 = migrationClassesInstances_1.next();
|
|
372
|
-
return [3 /*break*/, 6];
|
|
373
|
-
case 9: return [3 /*break*/, 12];
|
|
374
|
-
case 10:
|
|
375
|
-
e_2_1 = _b.sent();
|
|
376
|
-
e_2 = { error: e_2_1 };
|
|
377
|
-
return [3 /*break*/, 12];
|
|
378
|
-
case 11:
|
|
379
|
-
try {
|
|
380
|
-
if (migrationClassesInstances_1_1 && !migrationClassesInstances_1_1.done && (_a = migrationClassesInstances_1.return)) _a.call(migrationClassesInstances_1);
|
|
381
|
-
}
|
|
382
|
-
finally { if (e_2) throw e_2.error; }
|
|
383
|
-
return [7 /*endfinally*/];
|
|
384
|
-
case 12:
|
|
385
|
-
// update all pending migrations to completed
|
|
386
|
-
return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this.DEFAULT_MIGRATION_TABLE_NAME, "\n SET status = $1, applied_at = CURRENT_TIMESTAMP\n WHERE status = $2"), [this.MIGRATION_STATUS_COMPLETED, this.MIGRATION_STATUS_PENDING])];
|
|
387
|
-
case 13:
|
|
388
|
-
// update all pending migrations to completed
|
|
389
|
-
_b.sent();
|
|
390
|
-
return [4 /*yield*/, queryRunner.commitTransaction()];
|
|
391
|
-
case 14:
|
|
392
|
-
_b.sent();
|
|
393
|
-
this.ctx.logMigrations &&
|
|
394
|
-
console.log('All migrations marked as applied.');
|
|
395
|
-
return [3 /*break*/, 19];
|
|
396
|
-
case 15:
|
|
397
|
-
error_4 = _b.sent();
|
|
398
|
-
this.ctx.logMigrations &&
|
|
399
|
-
console.error('Failed to mark all migrations as applied, rolling back:', error_4);
|
|
400
|
-
return [4 /*yield*/, queryRunner.rollbackTransaction()];
|
|
401
|
-
case 16:
|
|
402
|
-
_b.sent();
|
|
403
|
-
return [3 /*break*/, 19];
|
|
404
|
-
case 17: return [4 /*yield*/, queryRunner.release()];
|
|
405
|
-
case 18:
|
|
406
|
-
_b.sent();
|
|
407
|
-
return [7 /*endfinally*/];
|
|
408
|
-
case 19: return [2 /*return*/];
|
|
205
|
+
async markAllMigrationsAsApplied() {
|
|
206
|
+
//#region @websqlFunc
|
|
207
|
+
if (this.ctx.remoteHost) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
|
|
211
|
+
const migrationClassesInstances = migrationsClassFns
|
|
212
|
+
.map(classFn => this.ctx.getInstanceBy(classFn))
|
|
213
|
+
.map(f => f)
|
|
214
|
+
.filter(migrationInstance => migrationInstance.isReadyToRun());
|
|
215
|
+
const queryRunner = this.ctx.connection.createQueryRunner();
|
|
216
|
+
await queryRunner.connect();
|
|
217
|
+
try {
|
|
218
|
+
await queryRunner.startTransaction();
|
|
219
|
+
// Fetch already applied migrations from the database
|
|
220
|
+
const allMigrationsInDb = await queryRunner.query(`SELECT name FROM ${this.DEFAULT_MIGRATION_TABLE_NAME}`);
|
|
221
|
+
const allMigrationInDBNames = allMigrationsInDb.map(m => m.name);
|
|
222
|
+
for (const instance of migrationClassesInstances) {
|
|
223
|
+
const migrationName = class_helpers_1.ClassHelpers.getName(instance);
|
|
224
|
+
if (allMigrationInDBNames.includes(migrationName)) {
|
|
225
|
+
this.ctx.logMigrations &&
|
|
226
|
+
console.log(`Skipping already applied migration: ${migrationName}`);
|
|
227
|
+
continue;
|
|
409
228
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
229
|
+
this.ctx.logMigrations &&
|
|
230
|
+
console.log(`Marking migration as applied: ${migrationName}`);
|
|
231
|
+
// Insert migration as 'complete' without running
|
|
232
|
+
await queryRunner.query(`INSERT INTO ${this.DEFAULT_MIGRATION_TABLE_NAME} (name, status, context, applied_at) ` +
|
|
233
|
+
`VALUES ($1, $2, $3, CURRENT_TIMESTAMP)`, [
|
|
234
|
+
migrationName,
|
|
235
|
+
this.MIGRATION_STATUS_COMPLETED,
|
|
236
|
+
instance.ctx.contextName,
|
|
237
|
+
]);
|
|
238
|
+
}
|
|
239
|
+
// update all pending migrations to completed
|
|
240
|
+
await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME}
|
|
241
|
+
SET status = $1, applied_at = CURRENT_TIMESTAMP
|
|
242
|
+
WHERE status = $2`, [this.MIGRATION_STATUS_COMPLETED, this.MIGRATION_STATUS_PENDING]);
|
|
243
|
+
await queryRunner.commitTransaction();
|
|
244
|
+
this.ctx.logMigrations &&
|
|
245
|
+
console.log('All migrations marked as applied.');
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
this.ctx.logMigrations &&
|
|
249
|
+
console.error('Failed to mark all migrations as applied, rolling back:', error);
|
|
250
|
+
await queryRunner.rollbackTransaction();
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
await queryRunner.release();
|
|
254
|
+
}
|
|
255
|
+
//#endregion
|
|
256
|
+
}
|
|
413
257
|
//#endregion
|
|
414
258
|
//#region methods & getters / run all migrations
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
case 0:
|
|
452
|
-
migrationInstance = migrationClassesInstances.find(function (instance) {
|
|
453
|
-
return class_helpers_1.ClassHelpers.getName(instance) === pendingContextMigration.name;
|
|
454
|
-
});
|
|
455
|
-
if (!migrationInstance) {
|
|
456
|
-
this_1.ctx.logMigrations &&
|
|
457
|
-
console.warn("Pending migration ".concat(pendingContextMigration.name, " not found in loaded migrations."));
|
|
458
|
-
return [2 /*return*/, "continue"];
|
|
459
|
-
}
|
|
460
|
-
this_1.ctx.logMigrations &&
|
|
461
|
-
console.log("Completing pending migration: ".concat(pendingContextMigration.name));
|
|
462
|
-
return [4 /*yield*/, migrationInstance.up(queryRunner)];
|
|
463
|
-
case 1:
|
|
464
|
-
_d.sent();
|
|
465
|
-
// Update migration status to 'complete'
|
|
466
|
-
return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this_1.DEFAULT_MIGRATION_TABLE_NAME, "\n SET status = $1, applied_at = CURRENT_TIMESTAMP\n WHERE name = $2"), [this_1.MIGRATION_STATUS_COMPLETED, pendingContextMigration.name])];
|
|
467
|
-
case 2:
|
|
468
|
-
// Update migration status to 'complete'
|
|
469
|
-
_d.sent();
|
|
470
|
-
return [2 /*return*/];
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
};
|
|
474
|
-
this_1 = this;
|
|
475
|
-
_c.label = 5;
|
|
476
|
-
case 5:
|
|
477
|
-
_c.trys.push([5, 10, 11, 12]);
|
|
478
|
-
pendingMigrationsForContext_1 = tslib_1.__values(pendingMigrationsForContext), pendingMigrationsForContext_1_1 = pendingMigrationsForContext_1.next();
|
|
479
|
-
_c.label = 6;
|
|
480
|
-
case 6:
|
|
481
|
-
if (!!pendingMigrationsForContext_1_1.done) return [3 /*break*/, 9];
|
|
482
|
-
pendingContextMigration = pendingMigrationsForContext_1_1.value;
|
|
483
|
-
return [5 /*yield**/, _loop_1(pendingContextMigration)];
|
|
484
|
-
case 7:
|
|
485
|
-
_c.sent();
|
|
486
|
-
_c.label = 8;
|
|
487
|
-
case 8:
|
|
488
|
-
pendingMigrationsForContext_1_1 = pendingMigrationsForContext_1.next();
|
|
489
|
-
return [3 /*break*/, 6];
|
|
490
|
-
case 9: return [3 /*break*/, 12];
|
|
491
|
-
case 10:
|
|
492
|
-
e_3_1 = _c.sent();
|
|
493
|
-
e_3 = { error: e_3_1 };
|
|
494
|
-
return [3 /*break*/, 12];
|
|
495
|
-
case 11:
|
|
496
|
-
try {
|
|
497
|
-
if (pendingMigrationsForContext_1_1 && !pendingMigrationsForContext_1_1.done && (_a = pendingMigrationsForContext_1.return)) _a.call(pendingMigrationsForContext_1);
|
|
498
|
-
}
|
|
499
|
-
finally { if (e_3) throw e_3.error; }
|
|
500
|
-
return [7 /*endfinally*/];
|
|
501
|
-
case 12:
|
|
502
|
-
_loop_2 = function (instance) {
|
|
503
|
-
var migrationName, error_6;
|
|
504
|
-
return tslib_1.__generator(this, function (_e) {
|
|
505
|
-
switch (_e.label) {
|
|
506
|
-
case 0:
|
|
507
|
-
migrationName = class_helpers_1.ClassHelpers.getName(instance);
|
|
508
|
-
if (appliedMigrationsForContext.some(function (m) { return m.name === migrationName; })) {
|
|
509
|
-
this_2.ctx.logMigrations &&
|
|
510
|
-
console.log("Skipping already applied migration: ".concat(migrationName));
|
|
511
|
-
return [2 /*return*/, "continue"];
|
|
512
|
-
}
|
|
513
|
-
this_2.ctx.logMigrations &&
|
|
514
|
-
console.log("Applying new migration: ".concat(migrationName));
|
|
515
|
-
// Insert migration as 'pending' before execution
|
|
516
|
-
return [4 /*yield*/, queryRunner.query("INSERT INTO ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " (name, status, context, applied_at) ") +
|
|
517
|
-
"VALUES ($1, $2, $3, NULL)", [migrationName, this_2.MIGRATION_STATUS_PENDING, this_2.ctx.contextName])];
|
|
518
|
-
case 1:
|
|
519
|
-
// Insert migration as 'pending' before execution
|
|
520
|
-
_e.sent();
|
|
521
|
-
_e.label = 2;
|
|
522
|
-
case 2:
|
|
523
|
-
_e.trys.push([2, 5, , 7]);
|
|
524
|
-
// Apply migration
|
|
525
|
-
return [4 /*yield*/, instance.up(queryRunner)];
|
|
526
|
-
case 3:
|
|
527
|
-
// Apply migration
|
|
528
|
-
_e.sent();
|
|
529
|
-
// Update migration to 'complete' after successful execution
|
|
530
|
-
return [4 /*yield*/, queryRunner.query("UPDATE ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " ") +
|
|
531
|
-
"SET status = '".concat(this_2.MIGRATION_STATUS_COMPLETED, "', applied_at = CURRENT_TIMESTAMP ") +
|
|
532
|
-
"WHERE name = $1", [migrationName])];
|
|
533
|
-
case 4:
|
|
534
|
-
// Update migration to 'complete' after successful execution
|
|
535
|
-
_e.sent();
|
|
536
|
-
return [3 /*break*/, 7];
|
|
537
|
-
case 5:
|
|
538
|
-
error_6 = _e.sent();
|
|
539
|
-
this_2.ctx.logMigrations &&
|
|
540
|
-
console.error("Failed to apply migration: ".concat(migrationName), error_6);
|
|
541
|
-
// Rollback pending migration entry
|
|
542
|
-
return [4 /*yield*/, queryRunner.query("DELETE FROM ".concat(this_2.DEFAULT_MIGRATION_TABLE_NAME, " WHERE name = $1"), [migrationName])];
|
|
543
|
-
case 6:
|
|
544
|
-
// Rollback pending migration entry
|
|
545
|
-
_e.sent();
|
|
546
|
-
throw error_6; // Rethrow to ensure the transaction is rolled back
|
|
547
|
-
case 7: return [2 /*return*/];
|
|
548
|
-
}
|
|
549
|
-
});
|
|
550
|
-
};
|
|
551
|
-
this_2 = this;
|
|
552
|
-
_c.label = 13;
|
|
553
|
-
case 13:
|
|
554
|
-
_c.trys.push([13, 18, 19, 20]);
|
|
555
|
-
migrationClassesInstances_2 = tslib_1.__values(migrationClassesInstances), migrationClassesInstances_2_1 = migrationClassesInstances_2.next();
|
|
556
|
-
_c.label = 14;
|
|
557
|
-
case 14:
|
|
558
|
-
if (!!migrationClassesInstances_2_1.done) return [3 /*break*/, 17];
|
|
559
|
-
instance = migrationClassesInstances_2_1.value;
|
|
560
|
-
return [5 /*yield**/, _loop_2(instance)];
|
|
561
|
-
case 15:
|
|
562
|
-
_c.sent();
|
|
563
|
-
_c.label = 16;
|
|
564
|
-
case 16:
|
|
565
|
-
migrationClassesInstances_2_1 = migrationClassesInstances_2.next();
|
|
566
|
-
return [3 /*break*/, 14];
|
|
567
|
-
case 17: return [3 /*break*/, 20];
|
|
568
|
-
case 18:
|
|
569
|
-
e_4_1 = _c.sent();
|
|
570
|
-
e_4 = { error: e_4_1 };
|
|
571
|
-
return [3 /*break*/, 20];
|
|
572
|
-
case 19:
|
|
573
|
-
try {
|
|
574
|
-
if (migrationClassesInstances_2_1 && !migrationClassesInstances_2_1.done && (_b = migrationClassesInstances_2.return)) _b.call(migrationClassesInstances_2);
|
|
575
|
-
}
|
|
576
|
-
finally { if (e_4) throw e_4.error; }
|
|
577
|
-
return [7 /*endfinally*/];
|
|
578
|
-
case 20:
|
|
579
|
-
//#endregion
|
|
580
|
-
return [4 /*yield*/, queryRunner.commitTransaction()];
|
|
581
|
-
case 21:
|
|
582
|
-
//#endregion
|
|
583
|
-
_c.sent();
|
|
584
|
-
return [3 /*break*/, 26];
|
|
585
|
-
case 22:
|
|
586
|
-
error_5 = _c.sent();
|
|
587
|
-
this.ctx.logMigrations &&
|
|
588
|
-
console.error('Transaction failed, rolling back:', error_5);
|
|
589
|
-
return [4 /*yield*/, queryRunner.rollbackTransaction()];
|
|
590
|
-
case 23:
|
|
591
|
-
_c.sent();
|
|
592
|
-
return [3 /*break*/, 26];
|
|
593
|
-
case 24: return [4 /*yield*/, queryRunner.release()];
|
|
594
|
-
case 25:
|
|
595
|
-
_c.sent();
|
|
596
|
-
return [7 /*endfinally*/];
|
|
597
|
-
case 26: return [2 /*return*/];
|
|
259
|
+
async runAllNotCompletedMigrations() {
|
|
260
|
+
//#region @websqlFunc
|
|
261
|
+
if (this.ctx.remoteHost) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const migrationsClassFns = this.ctx.getClassFunByArr(models_1.Models.ClassType.MIGRATION);
|
|
265
|
+
// console.log({
|
|
266
|
+
// migrationClassesALl: migrationsClassFns.map(f => ClassHelpers.getName(f)),
|
|
267
|
+
// });
|
|
268
|
+
const migrationClassesInstances = migrationsClassFns
|
|
269
|
+
.map(classFn => this.ctx.getInstanceBy(classFn))
|
|
270
|
+
.map(f => f)
|
|
271
|
+
.filter(migrationInstance => migrationInstance.isReadyToRun());
|
|
272
|
+
// console.log({
|
|
273
|
+
// migrationClassesInstances: migrationsClassFns.map(f =>
|
|
274
|
+
// ClassHelpers.getName(f),
|
|
275
|
+
// ),
|
|
276
|
+
// });
|
|
277
|
+
const queryRunner = this.ctx.connection.createQueryRunner();
|
|
278
|
+
await queryRunner.connect();
|
|
279
|
+
try {
|
|
280
|
+
await queryRunner.startTransaction();
|
|
281
|
+
// Check if the migrations table exists
|
|
282
|
+
// TODO: Implement check for migrations table existence here
|
|
283
|
+
// Fetch applied migrations from the database
|
|
284
|
+
const appliedMigrationsForContext = await queryRunner.query(`SELECT name, status FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} ` +
|
|
285
|
+
`WHERE context = $1`, [this.ctx.contextName]);
|
|
286
|
+
//#region check and update pending migrations
|
|
287
|
+
const pendingMigrationsForContext = appliedMigrationsForContext.filter(m => m.status === this.MIGRATION_STATUS_PENDING);
|
|
288
|
+
// Run pending migrations first
|
|
289
|
+
for (const pendingContextMigration of pendingMigrationsForContext) {
|
|
290
|
+
const migrationInstance = migrationClassesInstances.find(instance => class_helpers_1.ClassHelpers.getName(instance) === pendingContextMigration.name);
|
|
291
|
+
if (!migrationInstance) {
|
|
292
|
+
this.ctx.logMigrations &&
|
|
293
|
+
console.warn(`Pending migration ${pendingContextMigration.name} not found in loaded migrations.`);
|
|
294
|
+
continue;
|
|
598
295
|
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
}
|
|
296
|
+
this.ctx.logMigrations &&
|
|
297
|
+
console.log(`Completing pending migration: ${pendingContextMigration.name}`);
|
|
298
|
+
await migrationInstance.up(queryRunner);
|
|
299
|
+
// Update migration status to 'complete'
|
|
300
|
+
await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME}
|
|
301
|
+
SET status = $1, applied_at = CURRENT_TIMESTAMP
|
|
302
|
+
WHERE name = $2`, [this.MIGRATION_STATUS_COMPLETED, pendingContextMigration.name]);
|
|
303
|
+
}
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region run new migrations
|
|
306
|
+
for (const instance of migrationClassesInstances) {
|
|
307
|
+
const migrationName = class_helpers_1.ClassHelpers.getName(instance);
|
|
308
|
+
if (appliedMigrationsForContext.some(m => m.name === migrationName)) {
|
|
309
|
+
this.ctx.logMigrations &&
|
|
310
|
+
console.log(`Skipping already applied migration: ${migrationName}`);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
this.ctx.logMigrations &&
|
|
314
|
+
console.log(`Applying new migration: ${migrationName}`);
|
|
315
|
+
// Insert migration as 'pending' before execution
|
|
316
|
+
await queryRunner.query(`INSERT INTO ${this.DEFAULT_MIGRATION_TABLE_NAME} (name, status, context, applied_at) ` +
|
|
317
|
+
`VALUES ($1, $2, $3, NULL)`, [migrationName, this.MIGRATION_STATUS_PENDING, this.ctx.contextName]);
|
|
318
|
+
try {
|
|
319
|
+
// Apply migration
|
|
320
|
+
await instance.up(queryRunner);
|
|
321
|
+
// Update migration to 'complete' after successful execution
|
|
322
|
+
await queryRunner.query(`UPDATE ${this.DEFAULT_MIGRATION_TABLE_NAME} ` +
|
|
323
|
+
`SET status = '${this.MIGRATION_STATUS_COMPLETED}', applied_at = CURRENT_TIMESTAMP ` +
|
|
324
|
+
`WHERE name = $1`, [migrationName]);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
this.ctx.logMigrations &&
|
|
328
|
+
console.error(`Failed to apply migration: ${migrationName}`, error);
|
|
329
|
+
// Rollback pending migration entry
|
|
330
|
+
await queryRunner.query(`DELETE FROM ${this.DEFAULT_MIGRATION_TABLE_NAME} WHERE name = $1`, [migrationName]);
|
|
331
|
+
throw error; // Rethrow to ensure the transaction is rolled back
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
//#endregion
|
|
335
|
+
await queryRunner.commitTransaction();
|
|
336
|
+
}
|
|
337
|
+
catch (error) {
|
|
338
|
+
this.ctx.logMigrations &&
|
|
339
|
+
console.error('Transaction failed, rolling back:', error);
|
|
340
|
+
await queryRunner.rollbackTransaction();
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
await queryRunner.release();
|
|
344
|
+
}
|
|
345
|
+
//#endregion
|
|
346
|
+
}
|
|
347
|
+
}
|
|
604
348
|
exports.ContextDbMigrations = ContextDbMigrations;
|
|
605
349
|
//# sourceMappingURL=context-db-migrations.js.map
|