@itrocks/mysql-maintainer 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.
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Contextual = void 0;
4
- const mariadb_1 = require("mariadb");
5
4
  const mysql_maintainer_1 = require("./mysql-maintainer");
6
5
  const mysql_maintainer_2 = require("./mysql-maintainer");
7
6
  const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_NO_SUCH_TABLE'];
@@ -21,8 +20,7 @@ class Contextual {
21
20
  return await this.superQuery(sql, values);
22
21
  }
23
22
  catch (error) {
24
- if (!(error instanceof mariadb_1.SqlError)
25
- || !error.code
23
+ if (!(error && (typeof error === 'object') && error.code)
26
24
  || !MANAGED_ERROR_CODES.includes(error.code)) {
27
25
  if (mysql_maintainer_1.DEBUG)
28
26
  console.log('MAINTAINER: throw', error);
@@ -5,7 +5,7 @@ import { QueryOptions } from 'mariadb';
5
5
  import { SqlError } from 'mariadb';
6
6
  import { Context } from './contextual-connection';
7
7
  export { Mysql } from './mysql';
8
- export declare const DEBUG = false;
8
+ export declare const DEBUG = true;
9
9
  export declare class MysqlMaintainer {
10
10
  connection: Connection;
11
11
  constructor(connection: Connection);
@@ -11,7 +11,7 @@ const schema_to_mysql_1 = require("@itrocks/schema-to-mysql");
11
11
  const store_1 = require("@itrocks/store");
12
12
  var mysql_2 = require("./mysql");
13
13
  Object.defineProperty(exports, "Mysql", { enumerable: true, get: function () { return mysql_2.Mysql; } });
14
- exports.DEBUG = false;
14
+ exports.DEBUG = true;
15
15
  const DELETION = false;
16
16
  class MysqlMaintainer {
17
17
  connection;
@@ -57,7 +57,7 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
57
57
  switch (error.code) {
58
58
  case 'ER_BAD_FIELD_ERROR':
59
59
  case 'ER_CANNOT_ADD_FOREIGN':
60
- return await this.updateContextTables(context);
60
+ return this.updateContextTables(context);
61
61
  case 'ER_NO_SUCH_TABLE':
62
62
  const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
63
63
  return this.updateContextTables(context, tableName);
@@ -65,6 +65,7 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
65
65
  return false;
66
66
  }
67
67
  async updateContextTables(context, tableName) {
68
+ let result = false;
68
69
  const contexts = Array.isArray(context) ? context : [context];
69
70
  for (const context of contexts) {
70
71
  const type = (0, class_type_1.typeOf)(context);
@@ -73,12 +74,12 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
73
74
  throw 'No table name for type';
74
75
  }
75
76
  const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
76
- await (exists.length ? this.updateTable(type) : this.createTable(type));
77
+ result ||= await (exists.length ? this.updateTable(type) : this.createTable(type));
77
78
  }
78
79
  if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
79
- await this.createImplicitTable(contexts[0], contexts[1]);
80
+ result ||= await this.createImplicitTable(contexts[0], contexts[1]);
80
81
  }
81
- return true;
82
+ return result;
82
83
  }
83
84
  async updateTable(type) {
84
85
  const tableName = (0, store_1.storeOf)(type);
@@ -1,4 +1,3 @@
1
- import { SqlError } from 'mariadb';
2
1
  import { DEBUG } from './mysql-maintainer';
3
2
  import { MysqlMaintainer } from './mysql-maintainer';
4
3
  const MANAGED_ERROR_CODES = ['ER_BAD_FIELD_ERROR', 'ER_CANNOT_ADD_FOREIGN', 'ER_NO_SUCH_TABLE'];
@@ -18,8 +17,7 @@ export class Contextual {
18
17
  return await this.superQuery(sql, values);
19
18
  }
20
19
  catch (error) {
21
- if (!(error instanceof SqlError)
22
- || !error.code
20
+ if (!(error && (typeof error === 'object') && error.code)
23
21
  || !MANAGED_ERROR_CODES.includes(error.code)) {
24
22
  if (DEBUG)
25
23
  console.log('MAINTAINER: throw', error);
@@ -5,7 +5,7 @@ import { QueryOptions } from 'mariadb';
5
5
  import { SqlError } from 'mariadb';
6
6
  import { Context } from './contextual-connection';
7
7
  export { Mysql } from './mysql';
8
- export declare const DEBUG = false;
8
+ export declare const DEBUG = true;
9
9
  export declare class MysqlMaintainer {
10
10
  connection: Connection;
11
11
  constructor(connection: Connection);
@@ -7,7 +7,7 @@ import { SchemaDiffMysql } from '@itrocks/schema-diff-mysql';
7
7
  import { SchemaToMysql } from '@itrocks/schema-to-mysql';
8
8
  import { storeOf } from '@itrocks/store';
9
9
  export { Mysql } from './mysql';
10
- export const DEBUG = false;
10
+ export const DEBUG = true;
11
11
  const DELETION = false;
12
12
  export class MysqlMaintainer {
13
13
  connection;
@@ -53,7 +53,7 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
53
53
  switch (error.code) {
54
54
  case 'ER_BAD_FIELD_ERROR':
55
55
  case 'ER_CANNOT_ADD_FOREIGN':
56
- return await this.updateContextTables(context);
56
+ return this.updateContextTables(context);
57
57
  case 'ER_NO_SUCH_TABLE':
58
58
  const tableName = /Table '.*?\.(.*?)'/.exec(error.message)?.[1];
59
59
  return this.updateContextTables(context, tableName);
@@ -61,6 +61,7 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
61
61
  return false;
62
62
  }
63
63
  async updateContextTables(context, tableName) {
64
+ let result = false;
64
65
  const contexts = Array.isArray(context) ? context : [context];
65
66
  for (const context of contexts) {
66
67
  const type = typeOf(context);
@@ -69,12 +70,12 @@ CONSTRAINT \`${joinTable}.${table2}_id\` FOREIGN KEY (${table2}_id) REFERENCES \
69
70
  throw 'No table name for type';
70
71
  }
71
72
  const exists = (await this.connection.query('SHOW TABLES LIKE ?', tableName));
72
- await (exists.length ? this.updateTable(type) : this.createTable(type));
73
+ result ||= await (exists.length ? this.updateTable(type) : this.createTable(type));
73
74
  }
74
75
  if ((contexts.length === 2) && (this.implicitTableName(contexts[0], contexts[1]) === tableName)) {
75
- await this.createImplicitTable(contexts[0], contexts[1]);
76
+ result ||= await this.createImplicitTable(contexts[0], contexts[1]);
76
77
  }
77
- return true;
78
+ return result;
78
79
  }
79
80
  async updateTable(type) {
80
81
  const tableName = storeOf(type);
package/package.json CHANGED
@@ -57,5 +57,5 @@
57
57
  "build:esm": "tsc -p tsconfig.esm.json"
58
58
  },
59
59
  "types": "./esm/mysql-maintainer.d.ts",
60
- "version": "0.2.0"
60
+ "version": "0.2.2"
61
61
  }