dbgate-tools 5.4.5-alpha.5 → 5.5.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.
@@ -1,7 +1,7 @@
1
- import { DatabaseInfo, DatabaseModification, EngineDriver, SqlDialect } from 'dbgate-types';
1
+ import { DatabaseHandle, DatabaseInfo, DatabaseModification, EngineDriver, SqlDialect } from 'dbgate-types';
2
2
  import { type Logger } from 'pinomin';
3
3
  export declare class DatabaseAnalyser {
4
- pool: any;
4
+ dbhan: DatabaseHandle;
5
5
  driver: EngineDriver;
6
6
  structure: DatabaseInfo;
7
7
  modifications: DatabaseModification[];
@@ -9,7 +9,7 @@ export declare class DatabaseAnalyser {
9
9
  singleObjectId: string;
10
10
  dialect: SqlDialect;
11
11
  logger: Logger;
12
- constructor(pool: any, driver: EngineDriver, version: any);
12
+ constructor(dbhan: DatabaseHandle, driver: EngineDriver, version: any);
13
13
  _runAnalysis(): Promise<DatabaseInfo>;
14
14
  _getFastSnapshot(): Promise<DatabaseInfo>;
15
15
  _computeSingleObjectId(): Promise<void>;
@@ -19,6 +19,7 @@ export declare class DatabaseAnalyser {
19
19
  incrementalAnalysis(structure: any): Promise<DatabaseInfo>;
20
20
  mergeAnalyseResult(newlyAnalysed: any): any;
21
21
  getRequestedObjectPureNames(objectTypeField: any, allPureNames: any): any;
22
+ getDefaultSchemaNameCondition(): string;
22
23
  createQuery(template: any, typeFields: any, replacements?: {}): any;
23
24
  processQueryReplacements(query: any, replacements: any): any;
24
25
  createQueryCore(template: any, typeFields: any): any;
@@ -18,6 +18,7 @@ const groupBy_1 = __importDefault(require("lodash/groupBy"));
18
18
  const pick_1 = __importDefault(require("lodash/pick"));
19
19
  const compact_1 = __importDefault(require("lodash/compact"));
20
20
  const getLogger_1 = require("./getLogger");
21
+ const schemaInfoTools_1 = require("./schemaInfoTools");
21
22
  const logger = (0, getLogger_1.getLogger)('dbAnalyser');
22
23
  const STRUCTURE_FIELDS = ['tables', 'collections', 'views', 'matviews', 'functions', 'procedures', 'triggers'];
23
24
  const fp_pick = arg => array => (0, pick_1.default)(array, arg);
@@ -37,8 +38,8 @@ function areDifferentRowCounts(db1, db2) {
37
38
  return false;
38
39
  }
39
40
  class DatabaseAnalyser {
40
- constructor(pool, driver, version) {
41
- this.pool = pool;
41
+ constructor(dbhan, driver, version) {
42
+ this.dbhan = dbhan;
42
43
  this.driver = driver;
43
44
  this.singleObjectId = null;
44
45
  this.dialect = ((driver === null || driver === void 0 ? void 0 : driver.dialectByVersion) && (driver === null || driver === void 0 ? void 0 : driver.dialectByVersion(version))) || (driver === null || driver === void 0 ? void 0 : driver.dialect);
@@ -171,8 +172,16 @@ class DatabaseAnalyser {
171
172
  // containsObjectIdCondition(typeFields) {
172
173
  // return this.createQueryCore('=OBJECT_ID_CONDITION', typeFields) != ' is not null';
173
174
  // }
175
+ getDefaultSchemaNameCondition() {
176
+ return 'is not null';
177
+ }
174
178
  createQuery(template, typeFields, replacements = {}) {
175
- return this.createQueryCore(this.processQueryReplacements(template, replacements), typeFields);
179
+ let query = this.createQueryCore(this.processQueryReplacements(template, replacements), typeFields);
180
+ const dbname = this.dbhan.database;
181
+ const schemaCondition = (0, schemaInfoTools_1.isCompositeDbName)(dbname)
182
+ ? `= '${(0, schemaInfoTools_1.splitCompositeDbName)(dbname).schema}' `
183
+ : ` ${this.getDefaultSchemaNameCondition()} `;
184
+ return query === null || query === void 0 ? void 0 : query.replace(/=SCHEMA_NAME_CONDITION/g, schemaCondition);
176
185
  }
177
186
  processQueryReplacements(query, replacements) {
178
187
  for (const repl in replacements) {
@@ -232,8 +241,8 @@ class DatabaseAnalyser {
232
241
  ];
233
242
  }
234
243
  feedback(obj) {
235
- if (this.pool.feedback) {
236
- this.pool.feedback(obj);
244
+ if (this.dbhan.feedback) {
245
+ this.dbhan.feedback(obj);
237
246
  }
238
247
  if (obj && obj.analysingMessage) {
239
248
  logger.debug(obj.analysingMessage);
@@ -303,7 +312,7 @@ class DatabaseAnalyser {
303
312
  };
304
313
  }
305
314
  try {
306
- const res = yield this.driver.query(this.pool, sql);
315
+ const res = yield this.driver.query(this.dbhan, sql);
307
316
  this.logger.debug({ rows: res.rows.length, template }, `Loaded analyser query`);
308
317
  return res;
309
318
  }
@@ -324,7 +333,6 @@ class DatabaseAnalyser {
324
333
  functions: [],
325
334
  procedures: [],
326
335
  triggers: [],
327
- schemas: [],
328
336
  };
329
337
  }
330
338
  static byTableFilter(table) {
@@ -20,6 +20,8 @@ export declare class SqlDumper implements AlterProcessor {
20
20
  autoIncrement(): void;
21
21
  createDatabase(name: string): void;
22
22
  dropDatabase(name: string): void;
23
+ createSchema(name: string): void;
24
+ dropSchema(name: string): void;
23
25
  specialColumnOptions(column: any): void;
24
26
  selectScopeIdentity(table: TableInfo): void;
25
27
  columnType(dataType: string): void;
package/lib/SqlDumper.js CHANGED
@@ -200,6 +200,12 @@ class SqlDumper {
200
200
  dropDatabase(name) {
201
201
  this.putCmd('^drop ^database %i', name);
202
202
  }
203
+ createSchema(name) {
204
+ this.putCmd('^create ^schema %i', name);
205
+ }
206
+ dropSchema(name) {
207
+ this.putCmd('^drop ^schema %i', name);
208
+ }
203
209
  specialColumnOptions(column) { }
204
210
  selectScopeIdentity(table) { }
205
211
  columnType(dataType) {
package/lib/alterPlan.js CHANGED
@@ -171,32 +171,37 @@ class AlterPlan {
171
171
  ];
172
172
  return res;
173
173
  }
174
- if (op.operationType == 'changeColumn') {
175
- const constraints = this._getDependendColumnConstraints(op.oldObject, this.dialect.changeColumnDependencies);
176
- if (constraints.length > 0 && this.opts.noDropConstraint) {
177
- return [];
178
- }
179
- const res = [
180
- ...constraints.map(oldObject => {
181
- const opRes = {
182
- operationType: 'dropConstraint',
183
- oldObject,
184
- };
185
- return opRes;
186
- }),
187
- op,
188
- ...lodash_1.default.reverse([...constraints]).map(newObject => {
189
- const opRes = {
190
- operationType: 'createConstraint',
191
- newObject,
192
- };
193
- return opRes;
194
- }),
195
- ];
196
- if (constraints.length > 0) {
197
- this.recreates.constraints += 1;
174
+ for (const [testedOperationType, testedDependencies, testedObject] of [
175
+ ['changeColumn', this.dialect.changeColumnDependencies, op.oldObject],
176
+ ['renameColumn', this.dialect.renameColumnDependencies, op.object],
177
+ ]) {
178
+ if (op.operationType == testedOperationType) {
179
+ const constraints = this._getDependendColumnConstraints(testedObject, testedDependencies);
180
+ if (constraints.length > 0 && this.opts.noDropConstraint) {
181
+ return [];
182
+ }
183
+ const res = [
184
+ ...constraints.map(oldObject => {
185
+ const opRes = {
186
+ operationType: 'dropConstraint',
187
+ oldObject,
188
+ };
189
+ return opRes;
190
+ }),
191
+ op,
192
+ ...lodash_1.default.reverse([...constraints]).map(newObject => {
193
+ const opRes = {
194
+ operationType: 'createConstraint',
195
+ newObject,
196
+ };
197
+ return opRes;
198
+ }),
199
+ ];
200
+ if (constraints.length > 0) {
201
+ this.recreates.constraints += 1;
202
+ }
203
+ return res;
198
204
  }
199
- return res;
200
205
  }
201
206
  if (op.operationType == 'dropTable') {
202
207
  return [
@@ -238,7 +243,8 @@ class AlterPlan {
238
243
  this._testTableRecreate(op, 'dropColumn', this.dialect.dropColumn, 'oldObject') ||
239
244
  this._testTableRecreate(op, 'createConstraint', obj => this._canCreateConstraint(obj), 'newObject') ||
240
245
  this._testTableRecreate(op, 'dropConstraint', obj => this._canDropConstraint(obj), 'oldObject') ||
241
- this._testTableRecreate(op, 'changeColumn', this.dialect.changeColumn, 'newObject') || [op]);
246
+ this._testTableRecreate(op, 'changeColumn', this.dialect.changeColumn, 'newObject') ||
247
+ this._testTableRecreate(op, 'renameColumn', true, 'object') || [op]);
242
248
  });
243
249
  return lodash_1.default.flatten(lists);
244
250
  }
@@ -317,7 +323,7 @@ class AlterPlan {
317
323
  }
318
324
  else {
319
325
  // @ts-ignore
320
- const oldObject = op.oldObject;
326
+ const oldObject = op.oldObject || op.object;
321
327
  if (oldObject) {
322
328
  const recreated = recreates[`${oldObject.schemaName}||${oldObject.pureName}`];
323
329
  if (recreated) {
@@ -1,2 +1,2 @@
1
1
  import { EngineDriver, WriteTableOptions } from 'dbgate-types';
2
- export declare function createBulkInsertStreamBase(driver: EngineDriver, stream: any, pool: any, name: any, options: WriteTableOptions): any;
2
+ export declare function createBulkInsertStreamBase(driver: EngineDriver, stream: any, dbhan: any, name: any, options: WriteTableOptions): any;
@@ -17,7 +17,7 @@ const intersection_1 = __importDefault(require("lodash/intersection"));
17
17
  const getLogger_1 = require("./getLogger");
18
18
  const tableTransforms_1 = require("./tableTransforms");
19
19
  const logger = (0, getLogger_1.getLogger)('bulkStreamBase');
20
- function createBulkInsertStreamBase(driver, stream, pool, name, options) {
20
+ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
21
21
  const fullNameQuoted = name.schemaName
22
22
  ? `${driver.dialect.quoteIdentifier(name.schemaName)}.${driver.dialect.quoteIdentifier(name.pureName)}`
23
23
  : driver.dialect.quoteIdentifier(name.pureName);
@@ -38,21 +38,22 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
38
38
  }
39
39
  });
40
40
  writable.checkStructure = () => __awaiter(this, void 0, void 0, function* () {
41
- let structure = yield driver.analyseSingleTable(pool, name);
41
+ let structure = yield driver.analyseSingleTable(dbhan, name);
42
42
  // console.log('ANALYSING', name, structure);
43
43
  if (structure && options.dropIfExists) {
44
44
  logger.info(`Dropping table ${fullNameQuoted}`);
45
- yield driver.script(pool, `DROP TABLE ${fullNameQuoted}`);
45
+ yield driver.script(dbhan, `DROP TABLE ${fullNameQuoted}`);
46
46
  }
47
47
  if (options.createIfNotExists && (!structure || options.dropIfExists)) {
48
48
  const dmp = driver.createDumper();
49
- dmp.createTable((0, tableTransforms_1.prepareTableForImport)(Object.assign(Object.assign({}, writable.structure), name)));
49
+ const createdTableInfo = driver.adaptTableInfo((0, tableTransforms_1.prepareTableForImport)(Object.assign(Object.assign({}, writable.structure), name)));
50
+ dmp.createTable(createdTableInfo);
50
51
  logger.info({ sql: dmp.s }, `Creating table ${fullNameQuoted}`);
51
- yield driver.script(pool, dmp.s);
52
- structure = yield driver.analyseSingleTable(pool, name);
52
+ yield driver.script(dbhan, dmp.s);
53
+ structure = yield driver.analyseSingleTable(dbhan, name);
53
54
  }
54
55
  if (options.truncate) {
55
- yield driver.script(pool, `TRUNCATE TABLE ${fullNameQuoted}`);
56
+ yield driver.script(dbhan, `TRUNCATE TABLE ${fullNameQuoted}`);
56
57
  }
57
58
  writable.columnNames = (0, intersection_1.default)(structure.columns.map(x => x.columnName), writable.structure.columns.map(x => x.columnName));
58
59
  });
@@ -76,7 +77,7 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
76
77
  dmp.putRaw(';');
77
78
  // require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
78
79
  // console.log(dmp.s);
79
- yield driver.query(pool, dmp.s, { discardResult: true });
80
+ yield driver.query(dbhan, dmp.s, { discardResult: true });
80
81
  }
81
82
  else {
82
83
  for (const row of rows) {
@@ -87,13 +88,13 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
87
88
  dmp.putRaw('(');
88
89
  dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
89
90
  dmp.putRaw(')');
90
- yield driver.query(pool, dmp.s, { discardResult: true });
91
+ yield driver.query(dbhan, dmp.s, { discardResult: true });
91
92
  }
92
93
  }
93
94
  if (options.commitAfterInsert) {
94
95
  const dmp = driver.createDumper();
95
96
  dmp.commitTransaction();
96
- yield driver.query(pool, dmp.s, { discardResult: true });
97
+ yield driver.query(dbhan, dmp.s, { discardResult: true });
97
98
  }
98
99
  });
99
100
  writable.sendIfFull = () => __awaiter(this, void 0, void 0, function* () {
@@ -27,7 +27,7 @@ export declare const driverBase: {
27
27
  analyseIncremental(pool: any, structure: any, version: any): Promise<any>;
28
28
  createDumper(options?: any): SqlDumper;
29
29
  script(pool: any, sql: any, options: RunScriptOptions): Promise<void>;
30
- operation(pool: any, operation: any, options: RunScriptOptions): Promise<never>;
30
+ operation(pool: any, operation: any, options: RunScriptOptions): Promise<void>;
31
31
  getNewObjectTemplates(): {
32
32
  label: string;
33
33
  sql: string;
@@ -47,4 +47,6 @@ export declare const driverBase: {
47
47
  parseHexAsBuffer: boolean;
48
48
  };
49
49
  createSaveChangeSetScript(changeSet: any, dbinfo: any, defaultCreator: any): any;
50
+ adaptTableInfo(table: any): any;
51
+ listSchemas(pool: any): Promise<any>;
50
52
  };
package/lib/driverBase.js CHANGED
@@ -89,7 +89,7 @@ exports.driverBase = {
89
89
  }
90
90
  for (const sqlItem of (0, dbgate_query_splitter_1.splitQuery)(sql, this.getQuerySplitterOptions('script'))) {
91
91
  try {
92
- yield this.query(pool, sqlItem, { discardResult: true });
92
+ yield this.query(pool, sqlItem, Object.assign({ discardResult: true }, options === null || options === void 0 ? void 0 : options.queryOptions));
93
93
  }
94
94
  catch (err) {
95
95
  if ((options === null || options === void 0 ? void 0 : options.useTransaction) && this.supportsTransactions) {
@@ -105,7 +105,17 @@ exports.driverBase = {
105
105
  },
106
106
  operation(pool, operation, options) {
107
107
  return __awaiter(this, void 0, void 0, function* () {
108
- throw new Error('Operation not defined in target driver');
108
+ const { type } = operation;
109
+ switch (type) {
110
+ case 'createSchema':
111
+ yield runCommandOnDriver(pool, this, dmp => dmp.createSchema(operation.schemaName));
112
+ break;
113
+ case 'dropSchema':
114
+ yield runCommandOnDriver(pool, this, dmp => dmp.dropSchema(operation.schemaName));
115
+ break;
116
+ default:
117
+ throw new Error(`Operation type ${type} not supported`);
118
+ }
109
119
  });
110
120
  },
111
121
  getNewObjectTemplates() {
@@ -194,4 +204,12 @@ exports.driverBase = {
194
204
  createSaveChangeSetScript(changeSet, dbinfo, defaultCreator) {
195
205
  return defaultCreator(changeSet, dbinfo);
196
206
  },
207
+ adaptTableInfo(table) {
208
+ return table;
209
+ },
210
+ listSchemas(pool) {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ return null;
213
+ });
214
+ },
197
215
  };
package/lib/index.d.ts CHANGED
@@ -23,3 +23,4 @@ export * from './getLogger';
23
23
  export * from './getConnectionLabel';
24
24
  export * from './detectSqlFilterBehaviour';
25
25
  export * from './filterBehaviours';
26
+ export * from './schemaInfoTools';
package/lib/index.js CHANGED
@@ -39,3 +39,4 @@ __exportStar(require("./getLogger"), exports);
39
39
  __exportStar(require("./getConnectionLabel"), exports);
40
40
  __exportStar(require("./detectSqlFilterBehaviour"), exports);
41
41
  __exportStar(require("./filterBehaviours"), exports);
42
+ __exportStar(require("./schemaInfoTools"), exports);
@@ -0,0 +1,9 @@
1
+ import { SchemaInfo, SqlDialect } from 'dbgate-types';
2
+ export declare function findDefaultSchema(schemaList: SchemaInfo[], dialect: SqlDialect, schemaInStorage?: string): string;
3
+ export declare function isCompositeDbName(name: string): boolean;
4
+ export declare function splitCompositeDbName(name: string): {
5
+ database: string;
6
+ schema: string;
7
+ };
8
+ export declare function extractDbNameFromComposite(name: string): string;
9
+ export declare function extractSchemaNameFromComposite(name: string): string;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractSchemaNameFromComposite = exports.extractDbNameFromComposite = exports.splitCompositeDbName = exports.isCompositeDbName = exports.findDefaultSchema = void 0;
4
+ function findDefaultSchema(schemaList, dialect, schemaInStorage = null) {
5
+ var _a;
6
+ if (!schemaList) {
7
+ return null;
8
+ }
9
+ if (schemaInStorage && schemaList.find(x => x.schemaName == schemaInStorage)) {
10
+ return schemaInStorage;
11
+ }
12
+ const dynamicDefaultSchema = schemaList.find(x => x.isDefault);
13
+ if (dynamicDefaultSchema) {
14
+ return dynamicDefaultSchema.schemaName;
15
+ }
16
+ if ((dialect === null || dialect === void 0 ? void 0 : dialect.defaultSchemaName) && schemaList.find(x => x.schemaName == dialect.defaultSchemaName)) {
17
+ return dialect.defaultSchemaName;
18
+ }
19
+ return (_a = schemaList[0]) === null || _a === void 0 ? void 0 : _a.schemaName;
20
+ }
21
+ exports.findDefaultSchema = findDefaultSchema;
22
+ function isCompositeDbName(name) {
23
+ return name === null || name === void 0 ? void 0 : name.includes('::');
24
+ }
25
+ exports.isCompositeDbName = isCompositeDbName;
26
+ function splitCompositeDbName(name) {
27
+ if (!isCompositeDbName(name))
28
+ return null;
29
+ const [database, schema] = name.split('::');
30
+ return { database, schema };
31
+ }
32
+ exports.splitCompositeDbName = splitCompositeDbName;
33
+ function extractDbNameFromComposite(name) {
34
+ return isCompositeDbName(name) ? splitCompositeDbName(name).database : name;
35
+ }
36
+ exports.extractDbNameFromComposite = extractDbNameFromComposite;
37
+ function extractSchemaNameFromComposite(name) {
38
+ var _a;
39
+ return (_a = splitCompositeDbName(name)) === null || _a === void 0 ? void 0 : _a.schema;
40
+ }
41
+ exports.extractSchemaNameFromComposite = extractSchemaNameFromComposite;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "5.4.5-alpha.5",
2
+ "version": "5.5.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": "^5.4.5-alpha.5",
28
+ "dbgate-types": "^5.5.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.10.3",
35
- "dbgate-sqltree": "^5.4.5-alpha.5",
35
+ "dbgate-sqltree": "^5.5.1",
36
36
  "debug": "^4.3.4",
37
37
  "json-stable-stringify": "^1.0.1",
38
38
  "lodash": "^4.17.21",