dbgate-tools 6.2.1 → 6.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/ScriptWriter.d.ts +2 -4
- package/lib/ScriptWriter.js +11 -21
- package/lib/createBulkInsertStreamBase.js +75 -57
- package/lib/getConnectionLabel.d.ts +1 -0
- package/lib/getConnectionLabel.js +9 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/rowProgressReporter.d.ts +10 -0
- package/lib/rowProgressReporter.js +45 -0
- package/package.json +3 -3
package/lib/ScriptWriter.d.ts
CHANGED
|
@@ -10,8 +10,7 @@ export declare class ScriptWriter {
|
|
|
10
10
|
assign(variableName: any, functionName: any, props: any): void;
|
|
11
11
|
assignValue(variableName: any, jsonValue: any): void;
|
|
12
12
|
requirePackage(packageName: any): void;
|
|
13
|
-
copyStream(sourceVar: any, targetVar: any, colmapVar?: any): void;
|
|
14
|
-
dumpDatabase(options: any): void;
|
|
13
|
+
copyStream(sourceVar: any, targetVar: any, colmapVar?: any, progressName?: string): void;
|
|
15
14
|
importDatabase(options: any): void;
|
|
16
15
|
dataDuplicator(options: any): void;
|
|
17
16
|
comment(s: any): void;
|
|
@@ -27,9 +26,8 @@ export declare class ScriptWriterJson {
|
|
|
27
26
|
endLine(): void;
|
|
28
27
|
assign(variableName: any, functionName: any, props: any): void;
|
|
29
28
|
assignValue(variableName: any, jsonValue: any): void;
|
|
30
|
-
copyStream(sourceVar: any, targetVar: any, colmapVar?: any): void;
|
|
29
|
+
copyStream(sourceVar: any, targetVar: any, colmapVar?: any, progressName?: string): void;
|
|
31
30
|
comment(text: any): void;
|
|
32
|
-
dumpDatabase(options: any): void;
|
|
33
31
|
importDatabase(options: any): void;
|
|
34
32
|
dataDuplicator(options: any): void;
|
|
35
33
|
getScript(schedule?: any): {
|
package/lib/ScriptWriter.js
CHANGED
|
@@ -37,16 +37,14 @@ class ScriptWriter {
|
|
|
37
37
|
requirePackage(packageName) {
|
|
38
38
|
this.packageNames.push(packageName);
|
|
39
39
|
}
|
|
40
|
-
copyStream(sourceVar, targetVar, colmapVar = null) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
dumpDatabase(options) {
|
|
49
|
-
this._put(`await dbgateApi.dumpDatabase(${JSON.stringify(options)});`);
|
|
40
|
+
copyStream(sourceVar, targetVar, colmapVar = null, progressName) {
|
|
41
|
+
let opts = '{';
|
|
42
|
+
if (colmapVar)
|
|
43
|
+
opts += `columns: ${colmapVar}, `;
|
|
44
|
+
if (progressName)
|
|
45
|
+
opts += `progressName: "${progressName}", `;
|
|
46
|
+
opts += '}';
|
|
47
|
+
this._put(`await dbgateApi.copyStream(${sourceVar}, ${targetVar}, ${opts});`);
|
|
50
48
|
}
|
|
51
49
|
importDatabase(options) {
|
|
52
50
|
this._put(`await dbgateApi.importDatabase(${JSON.stringify(options)});`);
|
|
@@ -103,12 +101,13 @@ class ScriptWriterJson {
|
|
|
103
101
|
jsonValue,
|
|
104
102
|
});
|
|
105
103
|
}
|
|
106
|
-
copyStream(sourceVar, targetVar, colmapVar = null) {
|
|
104
|
+
copyStream(sourceVar, targetVar, colmapVar = null, progressName) {
|
|
107
105
|
this.commands.push({
|
|
108
106
|
type: 'copyStream',
|
|
109
107
|
sourceVar,
|
|
110
108
|
targetVar,
|
|
111
109
|
colmapVar,
|
|
110
|
+
progressName,
|
|
112
111
|
});
|
|
113
112
|
}
|
|
114
113
|
comment(text) {
|
|
@@ -117,12 +116,6 @@ class ScriptWriterJson {
|
|
|
117
116
|
text,
|
|
118
117
|
});
|
|
119
118
|
}
|
|
120
|
-
dumpDatabase(options) {
|
|
121
|
-
this.commands.push({
|
|
122
|
-
type: 'dumpDatabase',
|
|
123
|
-
options,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
119
|
importDatabase(options) {
|
|
127
120
|
this.commands.push({
|
|
128
121
|
type: 'importDatabase',
|
|
@@ -163,7 +156,7 @@ function jsonScriptToJavascript(json) {
|
|
|
163
156
|
script.assignValue(cmd.variableName, cmd.jsonValue);
|
|
164
157
|
break;
|
|
165
158
|
case 'copyStream':
|
|
166
|
-
script.copyStream(cmd.sourceVar, cmd.targetVar, cmd.colmapVar);
|
|
159
|
+
script.copyStream(cmd.sourceVar, cmd.targetVar, cmd.colmapVar, cmd.progressName);
|
|
167
160
|
break;
|
|
168
161
|
case 'endLine':
|
|
169
162
|
script.endLine();
|
|
@@ -171,9 +164,6 @@ function jsonScriptToJavascript(json) {
|
|
|
171
164
|
case 'comment':
|
|
172
165
|
script.comment(cmd.text);
|
|
173
166
|
break;
|
|
174
|
-
case 'dumpDatabase':
|
|
175
|
-
script.dumpDatabase(cmd.options);
|
|
176
|
-
break;
|
|
177
167
|
case 'importDatabase':
|
|
178
168
|
script.importDatabase(cmd.options);
|
|
179
169
|
break;
|
|
@@ -17,6 +17,8 @@ const intersection_1 = __importDefault(require("lodash/intersection"));
|
|
|
17
17
|
const fromPairs_1 = __importDefault(require("lodash/fromPairs"));
|
|
18
18
|
const getLogger_1 = require("./getLogger");
|
|
19
19
|
const tableTransforms_1 = require("./tableTransforms");
|
|
20
|
+
const rowProgressReporter_1 = require("./rowProgressReporter");
|
|
21
|
+
const stringTools_1 = require("./stringTools");
|
|
20
22
|
const logger = (0, getLogger_1.getLogger)('bulkStreamBase');
|
|
21
23
|
function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
|
|
22
24
|
const fullNameQuoted = name.schemaName
|
|
@@ -31,6 +33,7 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
|
|
|
31
33
|
writable.columnNames = null;
|
|
32
34
|
writable.columnDataTypes = null;
|
|
33
35
|
writable.requireFixedStructure = driver.databaseEngineTypes.includes('sql');
|
|
36
|
+
writable.rowsReporter = new rowProgressReporter_1.RowProgressReporter(options.progressName);
|
|
34
37
|
writable.addRow = (row) => __awaiter(this, void 0, void 0, function* () {
|
|
35
38
|
if (writable.structure) {
|
|
36
39
|
writable.buffer.push(row);
|
|
@@ -42,77 +45,91 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
|
|
|
42
45
|
});
|
|
43
46
|
writable.checkStructure = () => __awaiter(this, void 0, void 0, function* () {
|
|
44
47
|
var _a;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
try {
|
|
49
|
+
let structure = (_a = options.targetTableStructure) !== null && _a !== void 0 ? _a : (yield driver.analyseSingleTable(dbhan, name));
|
|
50
|
+
if (structure) {
|
|
51
|
+
writable.structure = structure;
|
|
52
|
+
}
|
|
53
|
+
if (structure && options.dropIfExists) {
|
|
54
|
+
logger.info(`Dropping table ${fullNameQuoted}`);
|
|
55
|
+
yield driver.script(dbhan, `DROP TABLE ${fullNameQuoted}`);
|
|
56
|
+
}
|
|
57
|
+
if (options.createIfNotExists && (!structure || options.dropIfExists)) {
|
|
58
|
+
const dmp = driver.createDumper();
|
|
59
|
+
const createdTableInfo = driver.adaptTableInfo((0, tableTransforms_1.prepareTableForImport)(Object.assign(Object.assign({}, writable.structure), name)));
|
|
60
|
+
dmp.createTable(createdTableInfo);
|
|
61
|
+
logger.info({ sql: dmp.s }, `Creating table ${fullNameQuoted}`);
|
|
62
|
+
yield driver.script(dbhan, dmp.s);
|
|
63
|
+
structure = yield driver.analyseSingleTable(dbhan, name);
|
|
64
|
+
writable.structure = structure;
|
|
65
|
+
}
|
|
66
|
+
if (!writable.structure) {
|
|
67
|
+
throw new Error(`Error importing table - ${fullNameQuoted} not found`);
|
|
68
|
+
}
|
|
69
|
+
if (options.truncate) {
|
|
70
|
+
yield driver.script(dbhan, `TRUNCATE TABLE ${fullNameQuoted}`);
|
|
71
|
+
}
|
|
72
|
+
writable.columnNames = (0, intersection_1.default)(structure.columns.map(x => x.columnName), writable.structure.columns.map(x => x.columnName));
|
|
73
|
+
writable.columnDataTypes = (0, fromPairs_1.default)(writable.columnNames.map(colName => {
|
|
74
|
+
var _a;
|
|
75
|
+
return [
|
|
76
|
+
colName,
|
|
77
|
+
(_a = writable.structure.columns.find(x => x.columnName == colName)) === null || _a === void 0 ? void 0 : _a.dataType,
|
|
78
|
+
];
|
|
79
|
+
}));
|
|
64
80
|
}
|
|
65
|
-
|
|
66
|
-
|
|
81
|
+
catch (err) {
|
|
82
|
+
logger.error((0, stringTools_1.extractErrorLogData)(err), 'Error during preparing bulk insert table, stopped');
|
|
83
|
+
writable.destroy(err);
|
|
67
84
|
}
|
|
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
|
-
}));
|
|
76
85
|
});
|
|
77
86
|
writable.send = () => __awaiter(this, void 0, void 0, function* () {
|
|
78
87
|
const rows = writable.buffer;
|
|
79
88
|
writable.buffer = [];
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
|
83
|
-
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
|
84
|
-
dmp.putRaw(')\n VALUES\n');
|
|
85
|
-
let wasRow = false;
|
|
86
|
-
for (const row of rows) {
|
|
87
|
-
if (wasRow)
|
|
88
|
-
dmp.putRaw(',\n');
|
|
89
|
-
dmp.putRaw('(');
|
|
90
|
-
dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
|
|
91
|
-
dmp.putRaw(')');
|
|
92
|
-
wasRow = true;
|
|
93
|
-
}
|
|
94
|
-
dmp.putRaw(';');
|
|
95
|
-
// require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
|
|
96
|
-
// console.log(dmp.s);
|
|
97
|
-
yield driver.query(dbhan, dmp.s, { discardResult: true });
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
for (const row of rows) {
|
|
89
|
+
try {
|
|
90
|
+
if (driver.dialect.allowMultipleValuesInsert) {
|
|
101
91
|
const dmp = driver.createDumper();
|
|
102
92
|
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
|
103
93
|
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
|
104
94
|
dmp.putRaw(')\n VALUES\n');
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
95
|
+
let wasRow = false;
|
|
96
|
+
for (const row of rows) {
|
|
97
|
+
if (wasRow)
|
|
98
|
+
dmp.putRaw(',\n');
|
|
99
|
+
dmp.putRaw('(');
|
|
100
|
+
dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
|
|
101
|
+
dmp.putRaw(')');
|
|
102
|
+
wasRow = true;
|
|
103
|
+
}
|
|
104
|
+
dmp.putRaw(';');
|
|
105
|
+
// require('fs').writeFileSync('/home/jena/test.sql', dmp.s);
|
|
108
106
|
// console.log(dmp.s);
|
|
109
107
|
yield driver.query(dbhan, dmp.s, { discardResult: true });
|
|
108
|
+
writable.rowsReporter.add(rows.length);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
for (const row of rows) {
|
|
112
|
+
const dmp = driver.createDumper();
|
|
113
|
+
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
|
114
|
+
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
|
115
|
+
dmp.putRaw(')\n VALUES\n');
|
|
116
|
+
dmp.putRaw('(');
|
|
117
|
+
dmp.putCollection(',', writable.columnNames, col => { var _a; return dmp.putValue(row[col], (_a = writable.columnDataTypes) === null || _a === void 0 ? void 0 : _a[col]); });
|
|
118
|
+
dmp.putRaw(')');
|
|
119
|
+
// console.log(dmp.s);
|
|
120
|
+
yield driver.query(dbhan, dmp.s, { discardResult: true });
|
|
121
|
+
writable.rowsReporter.add(1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (options.commitAfterInsert) {
|
|
125
|
+
const dmp = driver.createDumper();
|
|
126
|
+
dmp.commitTransaction();
|
|
127
|
+
yield driver.query(dbhan, dmp.s, { discardResult: true });
|
|
110
128
|
}
|
|
111
129
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
yield driver.query(dbhan, dmp.s, { discardResult: true });
|
|
130
|
+
catch (err) {
|
|
131
|
+
logger.error((0, stringTools_1.extractErrorLogData)(err), 'Error during base bulk insert, insert stopped');
|
|
132
|
+
writable.destroy(err);
|
|
116
133
|
}
|
|
117
134
|
});
|
|
118
135
|
writable.sendIfFull = () => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -127,6 +144,7 @@ function createBulkInsertStreamBase(driver, stream, dbhan, name, options) {
|
|
|
127
144
|
});
|
|
128
145
|
writable._final = (callback) => __awaiter(this, void 0, void 0, function* () {
|
|
129
146
|
yield writable.send();
|
|
147
|
+
writable.rowsReporter.finish();
|
|
130
148
|
callback();
|
|
131
149
|
});
|
|
132
150
|
return writable;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getConnectionLabel = exports.getDatabaseFileLabel = void 0;
|
|
3
|
+
exports.getEngineLabel = exports.getConnectionLabel = exports.getDatabaseFileLabel = void 0;
|
|
4
4
|
function getDatabaseFileLabel(databaseFile) {
|
|
5
5
|
if (!databaseFile)
|
|
6
6
|
return databaseFile;
|
|
@@ -45,3 +45,11 @@ function getConnectionLabel(connection, { allowExplicitDatabase = true, showUnsa
|
|
|
45
45
|
return res;
|
|
46
46
|
}
|
|
47
47
|
exports.getConnectionLabel = getConnectionLabel;
|
|
48
|
+
function getEngineLabel(connection) {
|
|
49
|
+
const match = ((connection === null || connection === void 0 ? void 0 : connection.engine) || '').match(/^([^@]*)@/);
|
|
50
|
+
if (match) {
|
|
51
|
+
return match[1];
|
|
52
|
+
}
|
|
53
|
+
return connection === null || connection === void 0 ? void 0 : connection.engine;
|
|
54
|
+
}
|
|
55
|
+
exports.getEngineLabel = getEngineLabel;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -41,3 +41,4 @@ __exportStar(require("./detectSqlFilterBehaviour"), exports);
|
|
|
41
41
|
__exportStar(require("./filterBehaviours"), exports);
|
|
42
42
|
__exportStar(require("./schemaInfoTools"), exports);
|
|
43
43
|
__exportStar(require("./dbKeysLoader"), exports);
|
|
44
|
+
__exportStar(require("./rowProgressReporter"), exports);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RowProgressReporter = void 0;
|
|
4
|
+
class RowProgressReporter {
|
|
5
|
+
constructor(progressName, field = 'writtenRowCount') {
|
|
6
|
+
this.progressName = progressName;
|
|
7
|
+
this.field = field;
|
|
8
|
+
this.counter = 0;
|
|
9
|
+
this.timeoutHandle = null;
|
|
10
|
+
}
|
|
11
|
+
add(count) {
|
|
12
|
+
this.counter += count;
|
|
13
|
+
if (!this.progressName) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (this.timeoutHandle) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
this.timeoutHandle = setTimeout(() => {
|
|
20
|
+
this.timeoutHandle = null;
|
|
21
|
+
this.send();
|
|
22
|
+
}, 1000);
|
|
23
|
+
}
|
|
24
|
+
finish() {
|
|
25
|
+
if (!this.progressName) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (this.timeoutHandle) {
|
|
29
|
+
clearTimeout(this.timeoutHandle);
|
|
30
|
+
this.timeoutHandle = null;
|
|
31
|
+
}
|
|
32
|
+
this.send();
|
|
33
|
+
}
|
|
34
|
+
send() {
|
|
35
|
+
if (!this.progressName) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
process.send({
|
|
39
|
+
msgtype: 'progress',
|
|
40
|
+
progressName: this.progressName,
|
|
41
|
+
[this.field]: this.counter,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.RowProgressReporter = RowProgressReporter;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "6.
|
|
2
|
+
"version": "6.3.0",
|
|
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.
|
|
28
|
+
"dbgate-types": "^6.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
34
|
"dbgate-query-splitter": "^4.11.3",
|
|
35
|
-
"dbgate-sqltree": "^6.
|
|
35
|
+
"dbgate-sqltree": "^6.3.0",
|
|
36
36
|
"debug": "^4.3.4",
|
|
37
37
|
"json-stable-stringify": "^1.0.1",
|
|
38
38
|
"lodash": "^4.17.21",
|