@fluidware-it/mysql2-client 0.3.0 → 0.4.0

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.
@@ -86,11 +86,8 @@ var UpgradeManager = /** @class */ (function () {
86
86
  case 5: return [3 /*break*/, 7];
87
87
  case 6:
88
88
  e_1 = _a.sent();
89
- // eslint-disable-next-line no-console
90
- console.error(e_1);
91
- this.logger.error('Failed to read current version, exiting. ', e_1.message);
92
- process.exit(92);
93
- return [3 /*break*/, 7];
89
+ this.logger.error("Failed to read current version: ".concat(e_1.message));
90
+ throw e_1;
94
91
  case 7: return [2 /*return*/];
95
92
  }
96
93
  });
@@ -115,9 +112,8 @@ var UpgradeManager = /** @class */ (function () {
115
112
  _a.sent();
116
113
  return [2 /*return*/, -1];
117
114
  case 4:
118
- this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e_2.code, e_2.message);
119
- process.exit(90);
120
- _a.label = 5;
115
+ this.logger.error("Unable to create ".concat(this.version_table, " table: [").concat(e_2.code, "] ").concat(e_2.message));
116
+ throw e_2;
121
117
  case 5: return [3 /*break*/, 6];
122
118
  case 6: return [2 /*return*/];
123
119
  }
@@ -160,8 +156,7 @@ var UpgradeManager = /** @class */ (function () {
160
156
  this.logger.error('\n');
161
157
  this.logger.error('\n');
162
158
  this.logger.error(e_3.stack);
163
- process.exit(96);
164
- return [3 /*break*/, 8];
159
+ throw e_3;
165
160
  case 6: return [4 /*yield*/, this.client.close()];
166
161
  case 7:
167
162
  _a.sent();
@@ -342,7 +337,7 @@ var UpgradeManager = /** @class */ (function () {
342
337
  case 6:
343
338
  if (currentVersion < 0) {
344
339
  this.logger.error('Db in initialization, exiting ');
345
- process.exit(91);
340
+ throw new Error('Db in initialization, do not proceed');
346
341
  }
347
342
  _a.label = 7;
348
343
  case 7: return [2 /*return*/, this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade)];
@@ -1 +1 @@
1
- {"version":3,"file":"upgradeManager.js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AoE;;;;;4BAEpE,qBAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EAAA;;wBAAxB,SAAwB,CAAC;wBACJ,qBAAM,IAAI,CAAC,kBAAkB,CAAC,aAAa,CAAC,EAAA;;wBAA7D,cAAc,GAAG,SAA4C;wBACjE,IAAI,cAAc,KAAK,IAAI,EAAE;4BAC3B,sBAAO,IAAI,EAAC;yBACb;wBACD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE,cAAc,EAAE,aAAa,CAAC,CAAC;6BAE5F,CAAA,cAAc,KAAK,CAAC,GAAG,CAAA,EAAvB,wBAAuB;wBACR,qBAAM,IAAI,CAAC,QAAQ,EAAE,EAAA;;wBAAtC,cAAc,GAAG,SAAqB,CAAC;6BACnC,CAAA,cAAc,GAAG,CAAC,CAAA,EAAlB,wBAAkB;wBACpB,qBAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,EAAA;;wBAAhE,SAAgE,CAAC;wBACjE,sBAAO;;;wBAEJ,IAAI,cAAc,GAAG,CAAC,EAAE;4BAC7B,IAAI,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 = ?`;\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
+ {"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,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,EAAA;;wBAAjD,GAAG,GAAG,SAA2C;6BACnD,CAAA,GAAG,KAAK,KAAK,CAAA,EAAb,wBAAa;wBACf,sBA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l\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 this.logger.error(`Failed to read current version: ${e.message}`);\n throw e;\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 ${this.version_table} table: [${e.code}] ${e.message}`);\n throw e;\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 throw e;\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 = ?`;\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 throw new Error('Db in initialization, do not proceed');\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
@@ -39,10 +39,8 @@ export class UpgradeManager {
39
39
  }
40
40
  }
41
41
  catch (e) {
42
- // eslint-disable-next-line no-console
43
- console.error(e);
44
- this.logger.error('Failed to read current version, exiting. ', e.message);
45
- process.exit(92);
42
+ this.logger.error(`Failed to read current version: ${e.message}`);
43
+ throw e;
46
44
  }
47
45
  }
48
46
  async createDb() {
@@ -56,8 +54,8 @@ export class UpgradeManager {
56
54
  return -1;
57
55
  }
58
56
  else {
59
- this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);
60
- process.exit(90);
57
+ this.logger.error(`Unable to create ${this.version_table} table: [${e.code}] ${e.message}`);
58
+ throw e;
61
59
  }
62
60
  }
63
61
  }
@@ -88,7 +86,7 @@ export class UpgradeManager {
88
86
  this.logger.error('\n');
89
87
  this.logger.error('\n');
90
88
  this.logger.error(e.stack);
91
- process.exit(96);
89
+ throw e;
92
90
  }
93
91
  finally {
94
92
  await this.client.close();
@@ -176,7 +174,7 @@ export class UpgradeManager {
176
174
  }
177
175
  else if (currentVersion < 0) {
178
176
  this.logger.error('Db in initialization, exiting ');
179
- process.exit(91);
177
+ throw new Error('Db in initialization, do not proceed');
180
178
  }
181
179
  return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
182
180
  }
@@ -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;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,gBAAgB,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,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,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b;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 = ?`;\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}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jE,OAAO;aACR;SACF;aAAM,IAAI,cAAc,GAAG,CAAC,EAAE;YAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;YACpD,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACzD;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 this.logger.error(`Failed to read current version: ${e.message}`);\n throw e;\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 ${this.version_table} table: [${e.code}] ${e.message}`);\n throw e;\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 throw e;\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 = ?`;\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 throw new Error('Db in initialization, do not proceed');\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
@@ -42,10 +42,8 @@ class UpgradeManager {
42
42
  }
43
43
  }
44
44
  catch (e) {
45
- // eslint-disable-next-line no-console
46
- console.error(e);
47
- this.logger.error('Failed to read current version, exiting. ', e.message);
48
- process.exit(92);
45
+ this.logger.error(`Failed to read current version: ${e.message}`);
46
+ throw e;
49
47
  }
50
48
  }
51
49
  async createDb() {
@@ -59,8 +57,8 @@ class UpgradeManager {
59
57
  return -1;
60
58
  }
61
59
  else {
62
- this.logger.error('Unable to create %s table: [%s] %s', this.version_table, e.code, e.message);
63
- process.exit(90);
60
+ this.logger.error(`Unable to create ${this.version_table} table: [${e.code}] ${e.message}`);
61
+ throw e;
64
62
  }
65
63
  }
66
64
  }
@@ -91,7 +89,7 @@ class UpgradeManager {
91
89
  this.logger.error('\n');
92
90
  this.logger.error('\n');
93
91
  this.logger.error(e.stack);
94
- process.exit(96);
92
+ throw e;
95
93
  }
96
94
  finally {
97
95
  await this.client.close();
@@ -179,7 +177,7 @@ class UpgradeManager {
179
177
  }
180
178
  else if (currentVersion < 0) {
181
179
  this.logger.error('Db in initialization, exiting ');
182
- process.exit(91);
180
+ throw new Error('Db in initialization, do not proceed');
183
181
  }
184
182
  return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);
185
183
  }
@@ -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;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,IAA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AmD,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;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 = ?`;\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
+ {"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,CAAC,KAAK,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,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,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACzD;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;IACrF,CAAC;CACF;AAjLD,wCAiLC","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 this.logger.error(`Failed to read current version: ${e.message}`);\n throw e;\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 ${this.version_table} table: [${e.code}] ${e.message}`);\n throw e;\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 throw e;\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 = ?`;\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 throw new Error('Db in initialization, do not proceed');\n }\n return this._checkDb(currentVersion, targetVersion, onSchemaInit, onSchemaUpgrade);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidware-it/mysql2-client",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "main": "build/src/index.js",
5
5
  "module": "build/esm/index.js",
6
6
  "esnext": "build/esnext/index.js",
@@ -16,7 +16,8 @@
16
16
  "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
17
17
  "lint": "eslint src",
18
18
  "lint:fix": "eslint --fix src",
19
- "test": "jest"
19
+ "test": "jest",
20
+ "preversion": "npm run lint && npm run test"
20
21
  },
21
22
  "engines": {
22
23
  "node": ">=18.4"