dbgate-tools 6.2.0 → 6.2.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.
@@ -39,6 +39,7 @@ export declare class SqlDumper implements AlterProcessor {
39
39
  transform(type: TransformType, dumpExpr: any): void;
40
40
  allowIdentityInsert(table: NamedObjectInfo, allow: boolean): void;
41
41
  enableConstraints(table: NamedObjectInfo, enabled: boolean): void;
42
+ enableAllForeignKeys(enabled: boolean): void;
42
43
  comment(value: string): void;
43
44
  createView(obj: ViewInfo): void;
44
45
  dropView(obj: ViewInfo, { testIfExists }: {
package/lib/SqlDumper.js CHANGED
@@ -224,7 +224,7 @@ class SqlDumper {
224
224
  }
225
225
  }
226
226
  columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
227
- var _a, _b, _c, _d;
227
+ var _a, _b, _c, _d, _e, _f, _g, _h;
228
228
  if (column.computedExpression) {
229
229
  this.put('^as %s', column.computedExpression);
230
230
  if (column.isPersisted)
@@ -237,11 +237,21 @@ class SqlDumper {
237
237
  }
238
238
  this.putRaw(' ');
239
239
  this.specialColumnOptions(column);
240
- if (includeNullable && !((_b = this.dialect) === null || _b === void 0 ? void 0 : _b.specificNullabilityImplementation)) {
241
- this.put(column.notNull ? '^not ^null' : '^null');
240
+ if ((_b = this.dialect) === null || _b === void 0 ? void 0 : _b.defaultValueBeforeNullability) {
241
+ if (includeDefault && ((_d = (_c = column.defaultValue) === null || _c === void 0 ? void 0 : _c.toString()) === null || _d === void 0 ? void 0 : _d.trim())) {
242
+ this.columnDefault(column);
243
+ }
244
+ if (includeNullable && !((_e = this.dialect) === null || _e === void 0 ? void 0 : _e.specificNullabilityImplementation)) {
245
+ this.put(column.notNull ? '^not ^null' : '^null');
246
+ }
242
247
  }
243
- if (includeDefault && ((_d = (_c = column.defaultValue) === null || _c === void 0 ? void 0 : _c.toString()) === null || _d === void 0 ? void 0 : _d.trim())) {
244
- this.columnDefault(column);
248
+ else {
249
+ if (includeNullable && !((_f = this.dialect) === null || _f === void 0 ? void 0 : _f.specificNullabilityImplementation)) {
250
+ this.put(column.notNull ? '^not ^null' : '^null');
251
+ }
252
+ if (includeDefault && ((_h = (_g = column.defaultValue) === null || _g === void 0 ? void 0 : _g.toString()) === null || _h === void 0 ? void 0 : _h.trim())) {
253
+ this.columnDefault(column);
254
+ }
245
255
  }
246
256
  }
247
257
  columnDefault(column) {
@@ -310,8 +320,9 @@ class SqlDumper {
310
320
  }
311
321
  }
312
322
  createForeignKeyFore(fk) {
313
- if (fk.constraintName != null)
323
+ if (fk.constraintName != null && !this.dialect.anonymousForeignKey) {
314
324
  this.put('^constraint %i ', fk.constraintName);
325
+ }
315
326
  this.put('^foreign ^key (%,i) ^references %f (%,i)', fk.columns.map(x => x.columnName), { schemaName: fk.refSchemaName, pureName: fk.refTableName }, fk.columns.map(x => x.refColumnName));
316
327
  if (fk.deleteAction)
317
328
  this.put(' ^on ^delete %k', fk.deleteAction);
@@ -323,6 +334,7 @@ class SqlDumper {
323
334
  }
324
335
  allowIdentityInsert(table, allow) { }
325
336
  enableConstraints(table, enabled) { }
337
+ enableAllForeignKeys(enabled) { }
326
338
  comment(value) {
327
339
  if (!value)
328
340
  return;
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.createBulkInsertStreamBase = void 0;
16
16
  const intersection_1 = __importDefault(require("lodash/intersection"));
17
+ const fromPairs_1 = __importDefault(require("lodash/fromPairs"));
17
18
  const getLogger_1 = require("./getLogger");
18
19
  const tableTransforms_1 = require("./tableTransforms");
19
20
  const logger = (0, getLogger_1.getLogger)('bulkStreamBase');
@@ -28,6 +29,7 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
28
29
  writable.buffer = [];
29
30
  writable.structure = null;
30
31
  writable.columnNames = null;
32
+ writable.columnDataTypes = null;
31
33
  writable.requireFixedStructure = driver.databaseEngineTypes.includes('sql');
32
34
  writable.addRow = (row) => __awaiter(this, void 0, void 0, function* () {
33
35
  if (writable.structure) {
@@ -39,7 +41,8 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
39
41
  }
40
42
  });
41
43
  writable.checkStructure = () => __awaiter(this, void 0, void 0, function* () {
42
- let structure = yield driver.analyseSingleTable(dbhan, name);
44
+ var _a;
45
+ let structure = (_a = options.targetTableStructure) !== null && _a !== void 0 ? _a : (yield driver.analyseSingleTable(dbhan, name));
43
46
  if (structure) {
44
47
  writable.structure = structure;
45
48
  }
@@ -54,11 +57,22 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
54
57
  logger.info({ sql: dmp.s }, `Creating table ${fullNameQuoted}`);
55
58
  yield driver.script(dbhan, dmp.s);
56
59
  structure = yield driver.analyseSingleTable(dbhan, name);
60
+ writable.structure = structure;
61
+ }
62
+ if (!writable.structure) {
63
+ throw new Error(`Error importing table - ${fullNameQuoted} not found`);
57
64
  }
58
65
  if (options.truncate) {
59
66
  yield driver.script(dbhan, `TRUNCATE TABLE ${fullNameQuoted}`);
60
67
  }
61
68
  writable.columnNames = (0, intersection_1.default)(structure.columns.map(x => x.columnName), writable.structure.columns.map(x => x.columnName));
69
+ writable.columnDataTypes = (0, fromPairs_1.default)(writable.columnNames.map(colName => {
70
+ var _a;
71
+ return [
72
+ colName,
73
+ (_a = writable.structure.columns.find(x => x.columnName == colName)) === null || _a === void 0 ? void 0 : _a.dataType,
74
+ ];
75
+ }));
62
76
  });
63
77
  writable.send = () => __awaiter(this, void 0, void 0, function* () {
64
78
  const rows = writable.buffer;
@@ -73,7 +87,7 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
73
87
  if (wasRow)
74
88
  dmp.putRaw(',\n');
75
89
  dmp.putRaw('(');
76
- dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
90
+ dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
77
91
  dmp.putRaw(')');
78
92
  wasRow = true;
79
93
  }
@@ -89,8 +103,9 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
89
103
  dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
90
104
  dmp.putRaw(')\n VALUES\n');
91
105
  dmp.putRaw('(');
92
- dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
106
+ dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
93
107
  dmp.putRaw(')');
108
+ // console.log(dmp.s);
94
109
  yield driver.query(dbhan, dmp.s, { discardResult: true });
95
110
  }
96
111
  }
@@ -49,6 +49,7 @@ export declare const driverBase: {
49
49
  parseHexAsBuffer: boolean;
50
50
  };
51
51
  createSaveChangeSetScript(changeSet: any, dbinfo: any, defaultCreator: any): any;
52
+ adaptDataType(dataType: string): string;
52
53
  adaptTableInfo(table: any): any;
53
54
  listSchemas(pool: any): Promise<any>;
54
55
  writeQueryFromStream(dbhan: any, sql: any): Promise<any>;
package/lib/driverBase.js CHANGED
@@ -246,8 +246,12 @@ exports.driverBase = {
246
246
  createSaveChangeSetScript(changeSet, dbinfo, defaultCreator) {
247
247
  return defaultCreator(changeSet, dbinfo);
248
248
  },
249
+ adaptDataType(dataType) {
250
+ return dataType;
251
+ },
249
252
  adaptTableInfo(table) {
250
- return table;
253
+ var _a;
254
+ return Object.assign(Object.assign({}, table), { columns: (_a = table.columns) === null || _a === void 0 ? void 0 : _a.map(col => (Object.assign(Object.assign({}, col), { dataType: this.adaptDataType(col.dataType) }))) });
251
255
  },
252
256
  listSchemas(pool) {
253
257
  return __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "6.2.0",
2
+ "version": "6.2.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": "^6.2.0",
28
+ "dbgate-types": "^6.2.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
  "dbgate-query-splitter": "^4.11.3",
35
- "dbgate-sqltree": "^6.2.0",
35
+ "dbgate-sqltree": "^6.2.1",
36
36
  "debug": "^4.3.4",
37
37
  "json-stable-stringify": "^1.0.1",
38
38
  "lodash": "^4.17.21",