dbgate-tools 4.6.2 → 4.7.1

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.
@@ -19,6 +19,21 @@ const pick_1 = __importDefault(require("lodash/pick"));
19
19
  const compact_1 = __importDefault(require("lodash/compact"));
20
20
  const STRUCTURE_FIELDS = ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers'];
21
21
  const fp_pick = arg => array => (0, pick_1.default)(array, arg);
22
+ function mergeTableRowCounts(info, rowCounts) {
23
+ return Object.assign(Object.assign({}, info), { tables: (info.tables || []).map(table => {
24
+ var _a, _b;
25
+ return (Object.assign(Object.assign({}, table), { tableRowCount: (_b = (_a = rowCounts.find(x => x.objectId == table.objectId)) === null || _a === void 0 ? void 0 : _a.tableRowCount) !== null && _b !== void 0 ? _b : table.tableRowCount }));
26
+ }) });
27
+ }
28
+ function areDifferentRowCounts(db1, db2) {
29
+ for (const t1 of db1.tables || []) {
30
+ const t2 = (db2.tables || []).find(x => x.objectId == t1.objectId);
31
+ if ((t1 === null || t1 === void 0 ? void 0 : t1.tableRowCount) !== (t2 === null || t2 === void 0 ? void 0 : t2.tableRowCount)) {
32
+ return true;
33
+ }
34
+ }
35
+ return false;
36
+ }
22
37
  class DatabaseAnalyser {
23
38
  constructor(pool, driver, version) {
24
39
  this.pool = pool;
@@ -78,14 +93,27 @@ class DatabaseAnalyser {
78
93
  incrementalAnalysis(structure) {
79
94
  return __awaiter(this, void 0, void 0, function* () {
80
95
  this.structure = structure;
81
- this.modifications = yield this.getModifications();
82
- if (this.modifications == null) {
96
+ const modifications = yield this.getModifications();
97
+ if (modifications == null) {
83
98
  // modifications not implemented, perform full analysis
84
99
  this.structure = null;
85
100
  return this.addEngineField(yield this._runAnalysis());
86
101
  }
87
- if (this.modifications.length == 0)
88
- return null;
102
+ const structureModifications = modifications.filter(x => x.action != 'setTableRowCounts');
103
+ const setTableRowCounts = modifications.find(x => x.action == 'setTableRowCounts');
104
+ let structureWithRowCounts = null;
105
+ if (setTableRowCounts) {
106
+ const newStructure = mergeTableRowCounts(structure, setTableRowCounts.rowCounts);
107
+ if (areDifferentRowCounts(structure, newStructure)) {
108
+ structureWithRowCounts = newStructure;
109
+ }
110
+ }
111
+ if (structureModifications.length == 0) {
112
+ return structureWithRowCounts ? this.addEngineField(structureWithRowCounts) : null;
113
+ }
114
+ this.modifications = structureModifications;
115
+ if (structureWithRowCounts)
116
+ this.structure = structureWithRowCounts;
89
117
  console.log('DB modifications detected:', this.modifications);
90
118
  return this.addEngineField(this.mergeAnalyseResult(yield this._runAnalysis()));
91
119
  });
@@ -228,6 +256,18 @@ class DatabaseAnalyser {
228
256
  res.push(action);
229
257
  }
230
258
  }
259
+ const rowCounts = (snapshot.tables || [])
260
+ .filter(x => x.tableRowCount != null)
261
+ .map(x => ({
262
+ objectId: x.objectId,
263
+ tableRowCount: x.tableRowCount,
264
+ }));
265
+ if (rowCounts.length > 0) {
266
+ res.push({
267
+ action: 'setTableRowCounts',
268
+ rowCounts,
269
+ });
270
+ }
231
271
  return [...(0, compact_1.default)(res), ...this.getDeletedObjects(snapshot)];
232
272
  });
233
273
  }
@@ -16,6 +16,7 @@ export declare class SqlDumper implements AlterProcessor {
16
16
  putFormattedList(c: any, collection: any): void;
17
17
  put(format: string, ...args: any[]): void;
18
18
  autoIncrement(): void;
19
+ specialColumnOptions(column: any): void;
19
20
  columnDefinition(column: ColumnInfo, { includeDefault, includeNullable, includeCollate }?: {
20
21
  includeDefault?: boolean;
21
22
  includeNullable?: boolean;
package/lib/SqlDumper.js CHANGED
@@ -170,6 +170,7 @@ class SqlDumper {
170
170
  autoIncrement() {
171
171
  this.put(' ^auto_increment');
172
172
  }
173
+ specialColumnOptions(column) { }
173
174
  columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
174
175
  var _a;
175
176
  if (column.computedExpression) {
@@ -183,9 +184,7 @@ class SqlDumper {
183
184
  this.autoIncrement();
184
185
  }
185
186
  this.putRaw(' ');
186
- if (column.isSparse) {
187
- this.put(' ^sparse ');
188
- }
187
+ this.specialColumnOptions(column);
189
188
  if (includeNullable) {
190
189
  this.put(column.notNull ? '^not ^null' : '^null');
191
190
  }
@@ -25,6 +25,7 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
25
25
  writable.buffer = [];
26
26
  writable.structure = null;
27
27
  writable.columnNames = null;
28
+ writable.requireFixedStructure = !driver.dialect.nosql;
28
29
  writable.addRow = (row) => __awaiter(this, void 0, void 0, function* () {
29
30
  if (writable.structure) {
30
31
  writable.buffer.push(row);
package/lib/diffTools.js CHANGED
@@ -134,6 +134,18 @@ function testEqualColumns(a, b, checkName, checkDefault, opts = {}) {
134
134
  // opts.DiffLogger.Trace('Column {0}, {1}: different is_sparse: {2}; {3}', a, b, a.IsSparse, b.IsSparse);
135
135
  return false;
136
136
  }
137
+ if ((a.isUnsigned || false) != (b.isUnsigned || false)) {
138
+ console.debug(`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different unsigned: ${a.isUnsigned}, ${b.isUnsigned}`);
139
+ return false;
140
+ }
141
+ if ((a.isZerofill || false) != (b.isZerofill || false)) {
142
+ console.debug(`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different zerofill: ${a.isZerofill}, ${b.isZerofill}`);
143
+ return false;
144
+ }
145
+ if ((a.columnComment || '') != (b.columnComment || '')) {
146
+ console.debug(`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different comment: ${a.columnComment}, ${b.columnComment}`);
147
+ return false;
148
+ }
137
149
  if (!testEqualTypes(a, b, opts)) {
138
150
  return false;
139
151
  }
@@ -9,6 +9,10 @@ export declare const driverBase: {
9
9
  stringEscapeChar: string;
10
10
  fallbackDataType: string;
11
11
  quoteIdentifier(s: any): any;
12
+ columnProperties: {
13
+ isSparse: boolean;
14
+ isPersisted: boolean;
15
+ };
12
16
  };
13
17
  analyseFull(pool: any, version: any): Promise<any>;
14
18
  analyseSingleObject(pool: any, name: any, typeField?: string): Promise<any>;
package/lib/driverBase.js CHANGED
@@ -21,6 +21,10 @@ const dialect = {
21
21
  quoteIdentifier(s) {
22
22
  return s;
23
23
  },
24
+ columnProperties: {
25
+ isSparse: false,
26
+ isPersisted: false,
27
+ },
24
28
  };
25
29
  exports.driverBase = {
26
30
  analyserClass: null,
@@ -2,3 +2,5 @@ export declare function arrayToHexString(byteArray: any): any;
2
2
  export declare function hexStringToArray(inputString: any): any[];
3
3
  export declare function parseCellValue(value: any): any;
4
4
  export declare function stringifyCellValue(value: any): any;
5
+ export declare function safeJsonParse(json: any, defaultValue?: any, logError?: boolean): any;
6
+ export declare function isJsonLikeLongString(value: any): RegExpMatchArray;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = void 0;
6
+ exports.isJsonLikeLongString = exports.safeJsonParse = exports.stringifyCellValue = exports.parseCellValue = exports.hexStringToArray = exports.arrayToHexString = void 0;
7
7
  const isString_1 = __importDefault(require("lodash/isString"));
8
8
  const isArray_1 = __importDefault(require("lodash/isArray"));
9
9
  const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
@@ -53,3 +53,19 @@ function stringifyCellValue(value) {
53
53
  return value;
54
54
  }
55
55
  exports.stringifyCellValue = stringifyCellValue;
56
+ function safeJsonParse(json, defaultValue, logError = false) {
57
+ try {
58
+ return JSON.parse(json);
59
+ }
60
+ catch (err) {
61
+ if (logError) {
62
+ console.error(`Error parsing JSON value "${json}"`, err);
63
+ }
64
+ return defaultValue;
65
+ }
66
+ }
67
+ exports.safeJsonParse = safeJsonParse;
68
+ function isJsonLikeLongString(value) {
69
+ return (0, isString_1.default)(value) && value.length > 100 && value.match(/^\s*\{.*\}\s*$|^\s*\[.*\]\s*$/);
70
+ }
71
+ exports.isJsonLikeLongString = isJsonLikeLongString;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.6.2",
2
+ "version": "4.7.1",
3
3
  "name": "dbgate-tools",
4
4
  "main": "lib/index.js",
5
5
  "typings": "lib/index.d.ts",
@@ -25,14 +25,14 @@
25
25
  ],
26
26
  "devDependencies": {
27
27
  "@types/node": "^13.7.0",
28
- "dbgate-types": "^4.6.2",
28
+ "dbgate-types": "^4.7.1",
29
29
  "jest": "^24.9.0",
30
30
  "ts-jest": "^25.2.1",
31
31
  "typescript": "^4.4.3"
32
32
  },
33
33
  "dependencies": {
34
34
  "lodash": "^4.17.21",
35
- "dbgate-query-splitter": "^4.6.2",
35
+ "dbgate-query-splitter": "^4.7.1",
36
36
  "uuid": "^3.4.0"
37
37
  }
38
38
  }