@fluidware-it/mysql2-client 0.2.0 → 0.2.2
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/build/esm/upgradeManager.d.ts +6 -7
- package/build/esm/upgradeManager.js +36 -36
- package/build/esm/upgradeManager.js.map +1 -1
- package/build/esnext/upgradeManager.d.ts +6 -7
- package/build/esnext/upgradeManager.js +20 -20
- package/build/esnext/upgradeManager.js.map +1 -1
- package/build/src/upgradeManager.d.ts +6 -7
- package/build/src/upgradeManager.js +20 -20
- package/build/src/upgradeManager.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import { DbClient } from './dbClient';
|
|
2
|
-
import { Logger } from 'pino';
|
|
3
2
|
export type UpgradeManagerConfig = {
|
|
4
3
|
version_table_suffix?: string;
|
|
5
4
|
};
|
|
6
5
|
export declare class UpgradeManager {
|
|
7
|
-
version_table
|
|
8
|
-
client
|
|
9
|
-
logger
|
|
10
|
-
_default_version_table
|
|
6
|
+
private version_table;
|
|
7
|
+
private client;
|
|
8
|
+
private logger;
|
|
9
|
+
private _default_version_table;
|
|
11
10
|
constructor(opts?: UpgradeManagerConfig);
|
|
12
11
|
private loadCurrentVersion;
|
|
13
12
|
private createDb;
|
|
14
|
-
_checkDb
|
|
15
|
-
checkDb(targetVersion: number, onSchemaInit: () => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
13
|
+
private _checkDb;
|
|
16
14
|
private getCurrentVersion;
|
|
17
15
|
private createVersionTable;
|
|
18
16
|
private updateVersion;
|
|
19
17
|
private initDb;
|
|
20
18
|
private upgradeDb;
|
|
19
|
+
checkDb(targetVersion: number, onSchemaInit: (dbClient: DbClient) => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
21
20
|
}
|
|
22
21
|
//# sourceMappingURL=upgradeManager.d.ts.map
|
|
@@ -171,42 +171,6 @@ var UpgradeManager = /** @class */ (function () {
|
|
|
171
171
|
});
|
|
172
172
|
});
|
|
173
173
|
};
|
|
174
|
-
UpgradeManager.prototype.checkDb = function (targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
176
|
-
var currentVersion;
|
|
177
|
-
return __generator(this, function (_a) {
|
|
178
|
-
switch (_a.label) {
|
|
179
|
-
case 0: return [4 /*yield*/, this.client.open()];
|
|
180
|
-
case 1:
|
|
181
|
-
_a.sent();
|
|
182
|
-
return [4 /*yield*/, this.loadCurrentVersion(targetVersion)];
|
|
183
|
-
case 2:
|
|
184
|
-
currentVersion = _a.sent();
|
|
185
|
-
if (currentVersion === true) {
|
|
186
|
-
return [2 /*return*/, true];
|
|
187
|
-
}
|
|
188
|
-
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
189
|
-
if (!(currentVersion === -999)) return [3 /*break*/, 6];
|
|
190
|
-
return [4 /*yield*/, this.createDb()];
|
|
191
|
-
case 3:
|
|
192
|
-
currentVersion = _a.sent();
|
|
193
|
-
if (!(currentVersion < 0)) return [3 /*break*/, 5];
|
|
194
|
-
return [4 /*yield*/, this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade)];
|
|
195
|
-
case 4:
|
|
196
|
-
_a.sent();
|
|
197
|
-
return [2 /*return*/];
|
|
198
|
-
case 5: return [3 /*break*/, 7];
|
|
199
|
-
case 6:
|
|
200
|
-
if (currentVersion < 0) {
|
|
201
|
-
this.logger.error('Db in initialization, exiting ');
|
|
202
|
-
process.exit(91);
|
|
203
|
-
}
|
|
204
|
-
_a.label = 7;
|
|
205
|
-
case 7: return [2 /*return*/, this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade)];
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
};
|
|
210
174
|
UpgradeManager.prototype.getCurrentVersion = function (targetVersion) {
|
|
211
175
|
return __awaiter(this, void 0, void 0, function () {
|
|
212
176
|
var sqlCheck, row, e_4;
|
|
@@ -350,6 +314,42 @@ var UpgradeManager = /** @class */ (function () {
|
|
|
350
314
|
});
|
|
351
315
|
});
|
|
352
316
|
};
|
|
317
|
+
UpgradeManager.prototype.checkDb = function (targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
318
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
319
|
+
var currentVersion;
|
|
320
|
+
return __generator(this, function (_a) {
|
|
321
|
+
switch (_a.label) {
|
|
322
|
+
case 0: return [4 /*yield*/, this.client.open()];
|
|
323
|
+
case 1:
|
|
324
|
+
_a.sent();
|
|
325
|
+
return [4 /*yield*/, this.loadCurrentVersion(targetVersion)];
|
|
326
|
+
case 2:
|
|
327
|
+
currentVersion = _a.sent();
|
|
328
|
+
if (currentVersion === true) {
|
|
329
|
+
return [2 /*return*/, true];
|
|
330
|
+
}
|
|
331
|
+
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
332
|
+
if (!(currentVersion === -999)) return [3 /*break*/, 6];
|
|
333
|
+
return [4 /*yield*/, this.createDb()];
|
|
334
|
+
case 3:
|
|
335
|
+
currentVersion = _a.sent();
|
|
336
|
+
if (!(currentVersion < 0)) return [3 /*break*/, 5];
|
|
337
|
+
return [4 /*yield*/, this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade)];
|
|
338
|
+
case 4:
|
|
339
|
+
_a.sent();
|
|
340
|
+
return [2 /*return*/];
|
|
341
|
+
case 5: return [3 /*break*/, 7];
|
|
342
|
+
case 6:
|
|
343
|
+
if (currentVersion < 0) {
|
|
344
|
+
this.logger.error('Db in initialization, exiting ');
|
|
345
|
+
process.exit(91);
|
|
346
|
+
}
|
|
347
|
+
_a.label = 7;
|
|
348
|
+
case 7: return [2 /*return*/, this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade)];
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
};
|
|
353
353
|
return UpgradeManager;
|
|
354
354
|
}());
|
|
355
355
|
export { UpgradeManager };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAOtC;IAOE,wBAAY,IAA2B;QAFvC,2BAAsB,GAAG,UAAU,CAAC;QAGlC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,sBAAsB,GAAG,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,oBAAoB,KAAI,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;IAC/B,CAAC;IAEa,2CAAkB,GAAhC,UAAiC,aAAqB;;;;;;;wBAEtC,qBAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,EAAA;;wBAAjD,GAAG,GAAG,SAA2C;6BACnD,CAAA,GAAG,KAAK,KAAK,CAAA,EAAb,wBAAa;wBACf,sBAAO,CAAC,GAAG,EAAC;;6BACH,CAAA,GAAG,KAAK,IAAI,CAAA,EAAZ,wBAAY;wBACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,aAAa,CAAC,CAAC;wBAC7D,qBAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAA;;wBAAzB,SAAyB,CAAC;wBAC1B,sBAAO,IAAI,EA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,CAAC,CAAC;wBAC7G,MAAM,GAAC,CAAC;4BAEV,sBAAO,CAAC,EAAC;;;;KACV;IAEa,sCAAa,GAA3B,UAA4B,aAAqB;;;;;;wBACzC,GAAG,GAAG,iBAAU,IAAI,CAAC,aAAa,oBAAiB,CAAC;wBAC1D,qBAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,CAAC,EAAA;;wBAA9C,SAA8C,CAAC;wBAC/C,qBAAM,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAA;;wBAA1B,SAA0B,CAAC;;;;;KAC5B;IAEa,+BAAM,GAApB,UAAqB,aAAqB,EAAE,MAA6C;;;;;;wBACjF,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;;;;wBAE5B,qBAAM,MAAM,CAAC,IAAI,EAAE,EAAA;;wBAAnB,SAAmB,CAAC;wBACpB,qBAAM,MAAM,CAAC,MAAM,CAAC,EAAA;;wBAApB,SAAoB,CAAC;wBACrB,qBAAM,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,EAAA;;wBAAvC,SAAuC,CAAC;;;6BAEpC,MAAM,EAAN,wBAAM;wBACR,qBAAM,MAAM,CAAC,KAAK,EAAE,EAAA;;wBAApB,SAAoB,CAAC;;;;;;;KAG1B;IAEa,kCAAS,GAAvB,UACE,WAAmB,EACnB,aAAqB,EACrB,SAA8D;;;;4BAE9D,qBAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,EAAA;;wBAAzC,SAAyC,CAAC;wBAC1C,qBAAM,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,EAAA;;wBAAvC,SAAuC,CAAC;;;;;KACzC;IACH,qBAAC;AAAD,CAAC,AApLD,IAoLC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n version_table: string;\n client: DbClient;\n logger: Logger;\n\n _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: () => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAOtC;IAME,wBAAY,IAA2B;QAF/B,2BAAsB,GAAG,UAAU,CAAC;QAG1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,sBAAsB,GAAG,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,oBAAoB,KAAI,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;IAC/B,CAAC;IAEa,2CAAkB,GAAhC,UAAiC,aAAqB;;;;;;;wBAEtC,qBAAM,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,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;4BACpD,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;yBAClB;;4BACD,sBAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,EAAC;;;;KACpF;IACH,qBAAC;AAAD,CAAC,AAnLD,IAmLC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n private version_table: string;\n private client: DbClient;\n private logger: Logger;\n private _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n private async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
|
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import { DbClient } from './dbClient';
|
|
2
|
-
import { Logger } from 'pino';
|
|
3
2
|
export type UpgradeManagerConfig = {
|
|
4
3
|
version_table_suffix?: string;
|
|
5
4
|
};
|
|
6
5
|
export declare class UpgradeManager {
|
|
7
|
-
version_table
|
|
8
|
-
client
|
|
9
|
-
logger
|
|
10
|
-
_default_version_table
|
|
6
|
+
private version_table;
|
|
7
|
+
private client;
|
|
8
|
+
private logger;
|
|
9
|
+
private _default_version_table;
|
|
11
10
|
constructor(opts?: UpgradeManagerConfig);
|
|
12
11
|
private loadCurrentVersion;
|
|
13
12
|
private createDb;
|
|
14
|
-
_checkDb
|
|
15
|
-
checkDb(targetVersion: number, onSchemaInit: () => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
13
|
+
private _checkDb;
|
|
16
14
|
private getCurrentVersion;
|
|
17
15
|
private createVersionTable;
|
|
18
16
|
private updateVersion;
|
|
19
17
|
private initDb;
|
|
20
18
|
private upgradeDb;
|
|
19
|
+
checkDb(targetVersion: number, onSchemaInit: (dbClient: DbClient) => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
21
20
|
}
|
|
22
21
|
//# sourceMappingURL=upgradeManager.d.ts.map
|
|
@@ -95,26 +95,6 @@ export class UpgradeManager {
|
|
|
95
95
|
}
|
|
96
96
|
return true;
|
|
97
97
|
}
|
|
98
|
-
async checkDb(targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
99
|
-
await this.client.open();
|
|
100
|
-
let currentVersion = await this.loadCurrentVersion(targetVersion);
|
|
101
|
-
if (currentVersion === true) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
105
|
-
if (currentVersion === -999) {
|
|
106
|
-
currentVersion = await this.createDb();
|
|
107
|
-
if (currentVersion < 0) {
|
|
108
|
-
await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
else if (currentVersion < 0) {
|
|
113
|
-
this.logger.error('Db in initialization, exiting ');
|
|
114
|
-
process.exit(91);
|
|
115
|
-
}
|
|
116
|
-
return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
117
|
-
}
|
|
118
98
|
async getCurrentVersion(targetVersion) {
|
|
119
99
|
const sqlCheck = `select value from ${this.version_table} for update`;
|
|
120
100
|
await this.client.startTransaction();
|
|
@@ -180,5 +160,25 @@ export class UpgradeManager {
|
|
|
180
160
|
await onUpgrade(this.client, fromVersion);
|
|
181
161
|
await this.updateVersion(targetVersion);
|
|
182
162
|
}
|
|
163
|
+
async checkDb(targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
164
|
+
await this.client.open();
|
|
165
|
+
let currentVersion = await this.loadCurrentVersion(targetVersion);
|
|
166
|
+
if (currentVersion === true) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
170
|
+
if (currentVersion === -999) {
|
|
171
|
+
currentVersion = await this.createDb();
|
|
172
|
+
if (currentVersion < 0) {
|
|
173
|
+
await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (currentVersion < 0) {
|
|
178
|
+
this.logger.error('Db in initialization, exiting ');
|
|
179
|
+
process.exit(91);
|
|
180
|
+
}
|
|
181
|
+
return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
182
|
+
}
|
|
183
183
|
}
|
|
184
184
|
//# sourceMappingURL=upgradeManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAOtC,MAAM,OAAO,cAAc;IAOzB,YAAY,IAA2B;QAFvC,2BAAsB,GAAG,UAAU,CAAC;QAGlC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,s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,aAAa,CAAC,CAAC;IAC1C,CAAC;CACF","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n version_table: string;\n client: DbClient;\n logger: Logger;\n\n _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: () => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAOtC,MAAM,OAAO,cAAc;IAMzB,YAAY,IAA2B;QAF/B,2BAAsB,GAAG,UAAU,CAAC;QAG1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,sBAAsB,GAAG,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,oBAAoB,KAAI,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,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,MAAM,CAAC,KAAK,EAAE,CAAC;aACtB;SACF;IACH,CAAC;IAEO,KAAK,CAAC,SAAS,CACrB,WAAmB,EACnB,aAAqB,EACrB,SAA8D;QAE9D,MAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAC1C,MAAM,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC;IAC1C,CAAC;IAED,KAAK,CAAC,OAAO,CACX,aAAqB,EACrB,YAAmD,EACnD,eAAoE;QAEpE,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACzB,IAAI,cAAc,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC;QAClE,IAAI,cAAc,KAAK,IAAI,EAAE;YAC3B,OAAO,IAAI,CAAC;SACb;QACD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE,cAAc,EAAE,aAAa,CAAC,CAAC;QAEhG,IAAI,cAAc,KAAK,CAAC,GAAG,EAAE;YAC3B,cAAc,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,IAAI,cAAc,GAAG,CAAC,EAAE;gBACtB,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;gBACjE,OAAO;aACR;SACF;aAAM,IAAI,cAAc,GAAG,CAAC,EAAE;YAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;YACpD,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SAClB;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;IACrF,CAAC;CACF","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n private version_table: string;\n private client: DbClient;\n private logger: Logger;\n private _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n private async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
|
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import { DbClient } from './dbClient';
|
|
2
|
-
import { Logger } from 'pino';
|
|
3
2
|
export type UpgradeManagerConfig = {
|
|
4
3
|
version_table_suffix?: string;
|
|
5
4
|
};
|
|
6
5
|
export declare class UpgradeManager {
|
|
7
|
-
version_table
|
|
8
|
-
client
|
|
9
|
-
logger
|
|
10
|
-
_default_version_table
|
|
6
|
+
private version_table;
|
|
7
|
+
private client;
|
|
8
|
+
private logger;
|
|
9
|
+
private _default_version_table;
|
|
11
10
|
constructor(opts?: UpgradeManagerConfig);
|
|
12
11
|
private loadCurrentVersion;
|
|
13
12
|
private createDb;
|
|
14
|
-
_checkDb
|
|
15
|
-
checkDb(targetVersion: number, onSchemaInit: () => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
13
|
+
private _checkDb;
|
|
16
14
|
private getCurrentVersion;
|
|
17
15
|
private createVersionTable;
|
|
18
16
|
private updateVersion;
|
|
19
17
|
private initDb;
|
|
20
18
|
private upgradeDb;
|
|
19
|
+
checkDb(targetVersion: number, onSchemaInit: (dbClient: DbClient) => Promise<void>, onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>): Promise<boolean | undefined>;
|
|
21
20
|
}
|
|
22
21
|
//# sourceMappingURL=upgradeManager.d.ts.map
|
|
@@ -98,26 +98,6 @@ class UpgradeManager {
|
|
|
98
98
|
}
|
|
99
99
|
return true;
|
|
100
100
|
}
|
|
101
|
-
async checkDb(targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
102
|
-
await this.client.open();
|
|
103
|
-
let currentVersion = await this.loadCurrentVersion(targetVersion);
|
|
104
|
-
if (currentVersion === true) {
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
108
|
-
if (currentVersion === -999) {
|
|
109
|
-
currentVersion = await this.createDb();
|
|
110
|
-
if (currentVersion < 0) {
|
|
111
|
-
await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else if (currentVersion < 0) {
|
|
116
|
-
this.logger.error('Db in initialization, exiting ');
|
|
117
|
-
process.exit(91);
|
|
118
|
-
}
|
|
119
|
-
return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
120
|
-
}
|
|
121
101
|
async getCurrentVersion(targetVersion) {
|
|
122
102
|
const sqlCheck = `select value from ${this.version_table} for update`;
|
|
123
103
|
await this.client.startTransaction();
|
|
@@ -183,6 +163,26 @@ class UpgradeManager {
|
|
|
183
163
|
await onUpgrade(this.client, fromVersion);
|
|
184
164
|
await this.updateVersion(targetVersion);
|
|
185
165
|
}
|
|
166
|
+
async checkDb(targetVersion, onSchemaInit, onSchemaUpgrade) {
|
|
167
|
+
await this.client.open();
|
|
168
|
+
let currentVersion = await this.loadCurrentVersion(targetVersion);
|
|
169
|
+
if (currentVersion === true) {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);
|
|
173
|
+
if (currentVersion === -999) {
|
|
174
|
+
currentVersion = await this.createDb();
|
|
175
|
+
if (currentVersion < 0) {
|
|
176
|
+
await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else if (currentVersion < 0) {
|
|
181
|
+
this.logger.error('Db in initialization, exiting ');
|
|
182
|
+
process.exit(91);
|
|
183
|
+
}
|
|
184
|
+
return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
|
|
185
|
+
}
|
|
186
186
|
}
|
|
187
187
|
exports.UpgradeManager = UpgradeManager;
|
|
188
188
|
//# sourceMappingURL=upgradeManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,mDAAkD;AAClD,uDAAoD;AACpD,yCAAsC;AAOtC,MAAa,cAAc;IAOzB,YAAY,IAA2B;QAFvC,2BAAsB,GAAG,UAAU,CAAC;QAGlC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,sBAAsB,GAAG,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,oBAAoB,KAAI,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,IAAA,qBAAS,GAAE,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,GAAG,IAAI,mBAAQ,EAAE,CAAC;IAC/B,CAAC;IAEO,KAAK,CAAC,kBAAkB,CAAC,aAAqB;QACpD,IAAI;YACF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC;YACxD,IAAI,GAAG,KAAK,KAAK,EAAE;gBACjB,OAAO,CAAC,GAAG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AAC;IAEO,KAAK,CAAC,aAAa,CAAC,aAAqB;QAC/C,MAAM,GAAG,GAAG,UAAU,IAAI,CAAC,aAAa,iBAAiB,CAAC;QAC1D,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC;QAC/C,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC7B,CAAC;IAEO,KAAK,CAAC,MAAM,CAAC,aAAqB,EAAE,MAA6C;QACvF,MAAM,MAAM,GAAG,IAAI,mBAAQ,EAAE,CAAC;QAC9B,IAAI;YACF,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YACpB,MAAM,MAAM,CAAC,MAAM,CAAC,CAAC;YACrB,MAAM,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC;SACzC;gBAAS;YACR,IAAI,MAAM,EAAE;gBACV,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;aACtB;SACF;IACH,CAAC;IAEO,KAAK,CAAC,SAAS,CACrB,WAAmB,EACnB,aAAqB,EACrB,SAA8D;QAE9D,MAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAC1C,MAAM,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC;IAC1C,CAAC;CACF;AApLD,wCAoLC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n version_table: string;\n client: DbClient;\n logger: Logger;\n\n _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: () => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"upgradeManager.js","sourceRoot":"","sources":["../../src/upgradeManager.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,mDAAkD;AAClD,uDAAoD;AACpD,yCAAsC;AAOtC,MAAa,cAAc;IAMzB,YAAY,IAA2B;QAF/B,2BAAsB,GAAG,UAAU,CAAC;QAG1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,sBAAsB,GAAG,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,oBAAoB,KAAI,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,IAAA,qBAAS,GAAE,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,GAAG,IAAI,mBAAQ,EAAE,CAAC;IAC/B,CAAC;IAEO,KAAK,CAAC,kBAAkB,CAAC,aAAqB;QACpD,IAAI;YACF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC;YACxD,IAAI,GAAG,KAAK,KAAK,EAAE;gBACjB,OAAO,CAAC,GAAG,CAAC;aACb;iBAAM,IAAI,GAAG,KAAK,IAAI,EAAE;gBACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,aAAa,CAAC,CAAC;gBAC7D,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;gBAC1B,OAAO,IAAI,CAAC;aACb;iBAAM,IAAI,GAAG,EAAE;gBACd,OAAO,GAAG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MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE,cAAc,EAAE,aAAa,CAAC,CAAC;QAEhG,IAAI,cAAc,KAAK,CAAC,GAAG,EAAE;YAC3B,cAAc,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,IAAI,cAAc,GAAG,CAAC,EAAE;gBACtB,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;gBACjE,OAAO;aACR;SACF;aAAM,IAAI,cAAc,GAAG,CAAC,EAAE;YAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;YACpD,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SAClB;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;IACrF,CAAC;CACF;AAnLD,wCAmLC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { setTimeout } from 'node:timers/promises';\nimport { getLogger } from '@fluidware-it/saddlebag';\nimport { DbClient } from './dbClient';\nimport { Logger } from 'pino';\n\nexport type UpgradeManagerConfig = {\n version_table_suffix?: string;\n};\n\nexport class UpgradeManager {\n private version_table: string;\n private client: DbClient;\n private logger: Logger;\n private _default_version_table = '_version';\n\n constructor(opts?: UpgradeManagerConfig) {\n this.version_table = this._default_version_table + (opts?.version_table_suffix || '');\n this.logger = getLogger().child({ component: 'mysql-migrator' });\n this.client = new DbClient();\n }\n\n private async loadCurrentVersion(targetVersion: number) {\n try {\n const row = await this.getCurrentVersion(targetVersion);\n if (row === false) {\n return -999;\n } else if (row === true) {\n this.logger.info('Check db: same version %s', targetVersion);\n await this.client.close();\n return true;\n } else if (row) {\n return row.value;\n }\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(e);\n this.logger.error('Failed to read current version, exiting. ', e.message);\n process.exit(92);\n }\n }\n\n private async createDb() {\n try {\n return this.createVersionTable();\n } catch (e) {\n if (e.code === '23505') {\n await this.client.close();\n await setTimeout(2000);\n return -1;\n } else {\n this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);\n process.exit(90);\n }\n }\n }\n\n private async _checkDb(\n currentVersion: number,\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n try {\n if (currentVersion === 0) {\n await this.initDb(targetVersion, onSchemaInit);\n this.logger.info('Db created');\n return true;\n }\n if (targetVersion === currentVersion) {\n return true;\n }\n if (targetVersion > currentVersion) {\n await this.upgradeDb(currentVersion, targetVersion, onSchemaUpgrade);\n this.logger.info('Db updated to ', targetVersion);\n return true;\n }\n } catch (e) {\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('checkDb failed %s', e.message);\n this.logger.error('\\n');\n this.logger.error(' !!!!!!!! FATAL ERROR !!!!!!!!');\n this.logger.error('\\n');\n this.logger.error('\\n');\n this.logger.error(e.stack);\n process.exit(96);\n } finally {\n await this.client.close();\n }\n return true;\n }\n\n private async getCurrentVersion(targetVersion: number) {\n const sqlCheck = `select value from ${this.version_table} for update`;\n await this.client.startTransaction();\n let row;\n try {\n row = await this.client.get(sqlCheck);\n this.logger.debug('Versions: %s vs %s', row.value, targetVersion);\n if (row.value === targetVersion) {\n this.logger.debug('versions are equal, rollback');\n await this.client.rollback();\n return true;\n }\n return row;\n } catch (e) {\n await this.client.rollback();\n if (e.code === 'ER_NO_SUCH_TABLE') {\n return false;\n }\n this.logger.error('Ooops: %s', e.message, e.code);\n throw e;\n }\n }\n\n private async createVersionTable() {\n const sqlCreateTable = `create table ${this.version_table} (value INTEGER PRIMARY KEY)`;\n try {\n await this.client.run(sqlCreateTable);\n } catch (e) {\n await this.client.rollback();\n throw e;\n }\n try {\n const sqlInsertVersionZero = `insert into ${this.version_table} values (-1)`;\n await this.client.insert(sqlInsertVersionZero);\n await this.client.get(`select value from ${this.version_table} for update`);\n } catch (e) {\n this.logger.error('Unable to insert -1 version in %s table: [%s] %s', this.version_table, e.code, e.message);\n throw e;\n }\n return 0;\n }\n\n private async updateVersion(targetVersion: number) {\n const sql = `update ${this.version_table} set value = $1`;\n await this.client.update(sql, [targetVersion]);\n await this.client.commit();\n }\n\n private async initDb(targetVersion: number, onInit: (dbClient: DbClient) => Promise<void>) {\n const dbConn = new DbClient();\n try {\n await dbConn.open();\n await onInit(dbConn);\n await this.updateVersion(targetVersion);\n } finally {\n if (dbConn) {\n await dbConn.close();\n }\n }\n }\n\n private async upgradeDb(\n fromVersion: number,\n targetVersion: number,\n onUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await onUpgrade(this.client, fromVersion);\n await this.updateVersion(targetVersion);\n }\n\n async checkDb(\n targetVersion: number,\n onSchemaInit: (dbClient: DbClient) => Promise<void>,\n onSchemaUpgrade: (dbClient: DbClient, from: number) => Promise<void>\n ) {\n await this.client.open();\n let currentVersion = await this.loadCurrentVersion(targetVersion);\n if (currentVersion === true) {\n return true;\n }\n this.logger.info('Check db: currentVersion %s targetVersion %s', currentVersion, targetVersion);\n\n if (currentVersion === -999) {\n currentVersion = await this.createDb();\n if (currentVersion < 0) {\n await this.checkDb(targetVersion, onSchemaInit, onSchemaUpgrade);\n return;\n }\n } else if (currentVersion < 0) {\n this.logger.error('Db in initialization, exiting ');\n process.exit(91);\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
|