dbgate-tools 5.2.8 → 5.3.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.
- package/lib/DatabaseAnalyser.d.ts +5 -2
- package/lib/DatabaseAnalyser.js +14 -5
- package/lib/SqlDumper.d.ts +1 -0
- package/lib/SqlDumper.js +12 -9
- package/lib/createBulkInsertStreamBase.js +35 -16
- package/lib/getLogger.d.ts +2 -2
- package/lib/getLogger.js +20 -16
- package/package.json +5 -5
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DatabaseInfo, DatabaseModification, EngineDriver, SqlDialect } from 'dbgate-types';
|
|
2
|
+
import { type Logger } from 'pinomin';
|
|
2
3
|
export declare class DatabaseAnalyser {
|
|
3
4
|
pool: any;
|
|
4
5
|
driver: EngineDriver;
|
|
@@ -7,6 +8,7 @@ export declare class DatabaseAnalyser {
|
|
|
7
8
|
singleObjectFilter: any;
|
|
8
9
|
singleObjectId: string;
|
|
9
10
|
dialect: SqlDialect;
|
|
11
|
+
logger: Logger;
|
|
10
12
|
constructor(pool: any, driver: EngineDriver, version: any);
|
|
11
13
|
_runAnalysis(): Promise<DatabaseInfo>;
|
|
12
14
|
_getFastSnapshot(): Promise<DatabaseInfo>;
|
|
@@ -17,13 +19,14 @@ export declare class DatabaseAnalyser {
|
|
|
17
19
|
incrementalAnalysis(structure: any): Promise<DatabaseInfo>;
|
|
18
20
|
mergeAnalyseResult(newlyAnalysed: any): any;
|
|
19
21
|
getRequestedObjectPureNames(objectTypeField: any, allPureNames: any): any;
|
|
20
|
-
createQuery(template: any, typeFields: any): any;
|
|
22
|
+
createQuery(template: any, typeFields: any, replacements?: {}): any;
|
|
23
|
+
processQueryReplacements(query: any, replacements: any): any;
|
|
21
24
|
createQueryCore(template: any, typeFields: any): any;
|
|
22
25
|
getDeletedObjectsForField(snapshot: any, objectTypeField: any): any;
|
|
23
26
|
getDeletedObjects(snapshot: any): any[];
|
|
24
27
|
feedback(obj: any): void;
|
|
25
28
|
getModifications(): Promise<any[]>;
|
|
26
|
-
analyserQuery(template: any, typeFields: any): Promise<import("dbgate-types").QueryResult>;
|
|
29
|
+
analyserQuery(template: any, typeFields: any, replacements?: {}): Promise<import("dbgate-types").QueryResult>;
|
|
27
30
|
static createEmptyStructure(): DatabaseInfo;
|
|
28
31
|
static byTableFilter(table: any): (x: any) => boolean;
|
|
29
32
|
static extractPrimaryKeys(table: any, pkColumns: any): {
|
package/lib/DatabaseAnalyser.js
CHANGED
|
@@ -42,6 +42,7 @@ class DatabaseAnalyser {
|
|
|
42
42
|
this.driver = driver;
|
|
43
43
|
this.singleObjectId = null;
|
|
44
44
|
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);
|
|
45
|
+
this.logger = logger;
|
|
45
46
|
}
|
|
46
47
|
_runAnalysis() {
|
|
47
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -170,8 +171,14 @@ class DatabaseAnalyser {
|
|
|
170
171
|
// containsObjectIdCondition(typeFields) {
|
|
171
172
|
// return this.createQueryCore('=OBJECT_ID_CONDITION', typeFields) != ' is not null';
|
|
172
173
|
// }
|
|
173
|
-
createQuery(template, typeFields) {
|
|
174
|
-
return this.createQueryCore(template, typeFields);
|
|
174
|
+
createQuery(template, typeFields, replacements = {}) {
|
|
175
|
+
return this.createQueryCore(this.processQueryReplacements(template, replacements), typeFields);
|
|
176
|
+
}
|
|
177
|
+
processQueryReplacements(query, replacements) {
|
|
178
|
+
for (const repl in replacements) {
|
|
179
|
+
query = query.replaceAll(repl, replacements[repl]);
|
|
180
|
+
}
|
|
181
|
+
return query;
|
|
175
182
|
}
|
|
176
183
|
createQueryCore(template, typeFields) {
|
|
177
184
|
// let res = template;
|
|
@@ -287,16 +294,18 @@ class DatabaseAnalyser {
|
|
|
287
294
|
return [...(0, compact_1.default)(res), ...this.getDeletedObjects(snapshot)];
|
|
288
295
|
});
|
|
289
296
|
}
|
|
290
|
-
analyserQuery(template, typeFields) {
|
|
297
|
+
analyserQuery(template, typeFields, replacements = {}) {
|
|
291
298
|
return __awaiter(this, void 0, void 0, function* () {
|
|
292
|
-
const sql = this.createQuery(template, typeFields);
|
|
299
|
+
const sql = this.createQuery(template, typeFields, replacements);
|
|
293
300
|
if (!sql) {
|
|
294
301
|
return {
|
|
295
302
|
rows: [],
|
|
296
303
|
};
|
|
297
304
|
}
|
|
298
305
|
try {
|
|
299
|
-
|
|
306
|
+
const res = yield this.driver.query(this.pool, sql);
|
|
307
|
+
this.logger.debug({ rows: res.rows.length, template }, `Loaded analyser query`);
|
|
308
|
+
return res;
|
|
300
309
|
}
|
|
301
310
|
catch (err) {
|
|
302
311
|
logger.error({ err }, 'Error running analyser query');
|
package/lib/SqlDumper.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare class SqlDumper implements AlterProcessor {
|
|
|
22
22
|
dropDatabase(name: string): void;
|
|
23
23
|
specialColumnOptions(column: any): void;
|
|
24
24
|
selectScopeIdentity(table: TableInfo): void;
|
|
25
|
+
columnType(dataType: string): void;
|
|
25
26
|
columnDefinition(column: ColumnInfo, { includeDefault, includeNullable, includeCollate }?: {
|
|
26
27
|
includeDefault?: boolean;
|
|
27
28
|
includeNullable?: boolean;
|
package/lib/SqlDumper.js
CHANGED
|
@@ -184,15 +184,8 @@ class SqlDumper {
|
|
|
184
184
|
}
|
|
185
185
|
specialColumnOptions(column) { }
|
|
186
186
|
selectScopeIdentity(table) { }
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if (column.computedExpression) {
|
|
190
|
-
this.put('^as %s', column.computedExpression);
|
|
191
|
-
if (column.isPersisted)
|
|
192
|
-
this.put(' ^persisted');
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
const type = column.dataType || this.dialect.fallbackDataType;
|
|
187
|
+
columnType(dataType) {
|
|
188
|
+
const type = dataType || this.dialect.fallbackDataType;
|
|
196
189
|
const typeWithValues = type.match(/([^(]+)(\(.+[^)]\))/);
|
|
197
190
|
if (typeWithValues === null || typeWithValues === void 0 ? void 0 : typeWithValues.length) {
|
|
198
191
|
typeWithValues.shift();
|
|
@@ -202,6 +195,16 @@ class SqlDumper {
|
|
|
202
195
|
else {
|
|
203
196
|
this.putRaw(SqlDumper.convertKeywordCase(type));
|
|
204
197
|
}
|
|
198
|
+
}
|
|
199
|
+
columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
|
|
200
|
+
var _a;
|
|
201
|
+
if (column.computedExpression) {
|
|
202
|
+
this.put('^as %s', column.computedExpression);
|
|
203
|
+
if (column.isPersisted)
|
|
204
|
+
this.put(' ^persisted');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
this.columnType(column.dataType);
|
|
205
208
|
if (column.autoIncrement) {
|
|
206
209
|
this.autoIncrement();
|
|
207
210
|
}
|
|
@@ -59,23 +59,42 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
|
|
|
59
59
|
writable.send = () => __awaiter(this, void 0, void 0, function* () {
|
|
60
60
|
const rows = writable.buffer;
|
|
61
61
|
writable.buffer = [];
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
if (driver.dialect.allowMultipleValuesInsert) {
|
|
63
|
+
const dmp = driver.createDumper();
|
|
64
|
+
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
|
65
|
+
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
|
66
|
+
dmp.putRaw(')\n VALUES\n');
|
|
67
|
+
let wasRow = false;
|
|
68
|
+
for (const row of rows) {
|
|
69
|
+
if (wasRow)
|
|
70
|
+
dmp.putRaw(',\n');
|
|
71
|
+
dmp.putRaw('(');
|
|
72
|
+
dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
|
|
73
|
+
dmp.putRaw(')');
|
|
74
|
+
wasRow = true;
|
|
75
|
+
}
|
|
76
|
+
dmp.putRaw(';');
|
|
77
|
+
// require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
|
|
78
|
+
// console.log(dmp.s);
|
|
79
|
+
yield driver.query(pool, dmp.s, { discardResult: true });
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
for (const row of rows) {
|
|
83
|
+
const dmp = driver.createDumper();
|
|
84
|
+
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
|
85
|
+
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
|
86
|
+
dmp.putRaw(')\n VALUES\n');
|
|
87
|
+
dmp.putRaw('(');
|
|
88
|
+
dmp.putCollection(',', writable.columnNames, col => dmp.putValue(row[col]));
|
|
89
|
+
dmp.putRaw(')');
|
|
90
|
+
yield driver.query(pool, dmp.s, { discardResult: true });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (options.commitAfterInsert) {
|
|
94
|
+
const dmp = driver.createDumper();
|
|
95
|
+
dmp.commitTransaction();
|
|
96
|
+
yield driver.query(pool, dmp.s, { discardResult: true });
|
|
74
97
|
}
|
|
75
|
-
dmp.putRaw(';');
|
|
76
|
-
// require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
|
|
77
|
-
// console.log(dmp.s);
|
|
78
|
-
yield driver.query(pool, dmp.s, { discardResult: true });
|
|
79
98
|
});
|
|
80
99
|
writable.sendIfFull = () => __awaiter(this, void 0, void 0, function* () {
|
|
81
100
|
if (writable.buffer.length > 100) {
|
package/lib/getLogger.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from 'pinomin';
|
|
2
|
-
export declare function
|
|
1
|
+
import { Logger, type LogConfig } from 'pinomin';
|
|
2
|
+
export declare function setLogConfig(value: LogConfig): void;
|
|
3
3
|
export declare function setLoggerName(value: any): void;
|
|
4
4
|
export declare function getLogger(caller?: string): Logger;
|
package/lib/getLogger.js
CHANGED
|
@@ -4,31 +4,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
var _a;
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.getLogger = exports.setLoggerName = exports.
|
|
7
|
+
exports.getLogger = exports.setLoggerName = exports.setLogConfig = void 0;
|
|
8
8
|
const pinomin_1 = __importDefault(require("pinomin"));
|
|
9
|
-
let
|
|
9
|
+
let _logConfig;
|
|
10
10
|
let _name = null;
|
|
11
|
-
const
|
|
11
|
+
const defaultLogConfig = {
|
|
12
12
|
base: { pid: (_a = global === null || global === void 0 ? void 0 : global.process) === null || _a === void 0 ? void 0 : _a.pid },
|
|
13
13
|
targets: [{ type: 'console', level: 'info' }],
|
|
14
|
-
}
|
|
15
|
-
function
|
|
16
|
-
|
|
14
|
+
};
|
|
15
|
+
function setLogConfig(value) {
|
|
16
|
+
_logConfig = value;
|
|
17
17
|
}
|
|
18
|
-
exports.
|
|
18
|
+
exports.setLogConfig = setLogConfig;
|
|
19
19
|
function setLoggerName(value) {
|
|
20
20
|
_name = value;
|
|
21
21
|
}
|
|
22
22
|
exports.setLoggerName = setLoggerName;
|
|
23
23
|
function getLogger(caller) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
return (0, pinomin_1.default)({
|
|
25
|
+
getConfig: () => {
|
|
26
|
+
const config = _logConfig || defaultLogConfig;
|
|
27
|
+
if (caller) {
|
|
28
|
+
const props = { caller };
|
|
29
|
+
if (_name) {
|
|
30
|
+
props['name'] = _name;
|
|
31
|
+
}
|
|
32
|
+
return Object.assign(Object.assign({}, config), { base: Object.assign(Object.assign({}, config.base), props) });
|
|
33
|
+
}
|
|
34
|
+
return config;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
33
37
|
}
|
|
34
38
|
exports.getLogger = getLogger;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "5.
|
|
2
|
+
"version": "5.3.0",
|
|
3
3
|
"name": "dbgate-tools",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"typings": "lib/index.d.ts",
|
|
@@ -25,18 +25,18 @@
|
|
|
25
25
|
],
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^13.7.0",
|
|
28
|
-
"dbgate-types": "^5.
|
|
28
|
+
"dbgate-types": "^5.3.0",
|
|
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
|
-
"dbgate-query-splitter": "^4.
|
|
35
|
-
"dbgate-sqltree": "^5.
|
|
34
|
+
"dbgate-query-splitter": "^4.10.1",
|
|
35
|
+
"dbgate-sqltree": "^5.3.0",
|
|
36
36
|
"debug": "^4.3.4",
|
|
37
37
|
"json-stable-stringify": "^1.0.1",
|
|
38
38
|
"lodash": "^4.17.21",
|
|
39
|
-
"pinomin": "^1.0.
|
|
39
|
+
"pinomin": "^1.0.4",
|
|
40
40
|
"uuid": "^3.4.0"
|
|
41
41
|
}
|
|
42
42
|
}
|