dbgate-api 5.5.7-alpha.16 → 5.5.7-alpha.25
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/package.json +5 -5
- package/src/controllers/config.js +4 -2
- package/src/controllers/connections.js +5 -0
- package/src/controllers/databaseConnections.js +38 -8
- package/src/controllers/runners.js +26 -14
- package/src/controllers/serverConnections.js +2 -1
- package/src/currentVersion.js +2 -2
- package/src/shell/autoIndexForeignKeysTransform.js +1 -1
- package/src/shell/deployDb.js +38 -12
- package/src/shell/dropAllDbObjects.js +42 -0
- package/src/shell/generateDeploySql.js +16 -0
- package/src/shell/index.js +4 -0
- package/src/utility/exportDbModelSql.js +80 -0
- package/src/utility/importDbModel.js +2 -22
- package/src/utility/loadModelFolder.js +27 -0
- package/src/utility/loadModelTransform.js +36 -0
- package/src/utility/platformInfo.js +10 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbgate-api",
|
|
3
3
|
"main": "src/index.js",
|
|
4
|
-
"version": "5.5.7-alpha.
|
|
4
|
+
"version": "5.5.7-alpha.25",
|
|
5
5
|
"homepage": "https://dbgate.org/",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"compare-versions": "^3.6.0",
|
|
28
28
|
"cors": "^2.8.5",
|
|
29
29
|
"cross-env": "^6.0.3",
|
|
30
|
-
"dbgate-datalib": "^5.5.7-alpha.
|
|
30
|
+
"dbgate-datalib": "^5.5.7-alpha.25",
|
|
31
31
|
"dbgate-query-splitter": "^4.11.2",
|
|
32
|
-
"dbgate-sqltree": "^5.5.7-alpha.
|
|
33
|
-
"dbgate-tools": "^5.5.7-alpha.
|
|
32
|
+
"dbgate-sqltree": "^5.5.7-alpha.25",
|
|
33
|
+
"dbgate-tools": "^5.5.7-alpha.25",
|
|
34
34
|
"debug": "^4.3.4",
|
|
35
35
|
"diff": "^5.0.0",
|
|
36
36
|
"diff2html": "^3.4.13",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"@types/fs-extra": "^9.0.11",
|
|
80
80
|
"@types/lodash": "^4.14.149",
|
|
81
|
-
"dbgate-types": "^5.5.7-alpha.
|
|
81
|
+
"dbgate-types": "^5.5.7-alpha.25",
|
|
82
82
|
"env-cmd": "^10.1.0",
|
|
83
83
|
"node-loader": "^1.0.2",
|
|
84
84
|
"nodemon": "^2.0.2",
|
|
@@ -69,7 +69,7 @@ module.exports = {
|
|
|
69
69
|
!adminConfig?.adminPasswordState
|
|
70
70
|
);
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
const configResult = {
|
|
73
73
|
runAsPortal: !!connections.portalConnections,
|
|
74
74
|
singleDbConnection: connections.singleDbConnection,
|
|
75
75
|
singleConnection: singleConnection,
|
|
@@ -95,13 +95,15 @@ module.exports = {
|
|
|
95
95
|
!process.env.BASIC_AUTH
|
|
96
96
|
),
|
|
97
97
|
isAdminPasswordMissing,
|
|
98
|
-
isInvalidToken: req
|
|
98
|
+
isInvalidToken: req?.isInvalidToken,
|
|
99
99
|
adminPasswordState: adminConfig?.adminPasswordState,
|
|
100
100
|
storageDatabase: process.env.STORAGE_DATABASE,
|
|
101
101
|
logsFilePath: getLogsFilePath(),
|
|
102
102
|
connectionsFilePath: path.join(datadir(), 'connections.jsonl'),
|
|
103
103
|
...currentVersion,
|
|
104
104
|
};
|
|
105
|
+
|
|
106
|
+
return configResult;
|
|
105
107
|
},
|
|
106
108
|
|
|
107
109
|
logout_meta: {
|
|
@@ -368,6 +368,11 @@ module.exports = {
|
|
|
368
368
|
|
|
369
369
|
get_meta: true,
|
|
370
370
|
async get({ conid }, req) {
|
|
371
|
+
if (conid == '__model') {
|
|
372
|
+
return {
|
|
373
|
+
_id: '__model',
|
|
374
|
+
};
|
|
375
|
+
}
|
|
371
376
|
testConnectionPermission(conid, req);
|
|
372
377
|
return this.getCore({ conid, mask: true });
|
|
373
378
|
},
|
|
@@ -13,6 +13,7 @@ const {
|
|
|
13
13
|
modelCompareDbDiffOptions,
|
|
14
14
|
getLogger,
|
|
15
15
|
extractErrorLogData,
|
|
16
|
+
filterStructureBySchema,
|
|
16
17
|
} = require('dbgate-tools');
|
|
17
18
|
const { html, parse } = require('diff2html');
|
|
18
19
|
const { handleProcessCommunication } = require('../utility/processComm');
|
|
@@ -31,6 +32,8 @@ const { testConnectionPermission } = require('../utility/hasPermission');
|
|
|
31
32
|
const { MissingCredentialsError } = require('../utility/exceptions');
|
|
32
33
|
const pipeForkLogs = require('../utility/pipeForkLogs');
|
|
33
34
|
const crypto = require('crypto');
|
|
35
|
+
const loadModelTransform = require('../utility/loadModelTransform');
|
|
36
|
+
const exportDbModelSql = require('../utility/exportDbModelSql');
|
|
34
37
|
|
|
35
38
|
const logger = getLogger('databaseConnections');
|
|
36
39
|
|
|
@@ -349,6 +352,11 @@ module.exports = {
|
|
|
349
352
|
|
|
350
353
|
syncModel_meta: true,
|
|
351
354
|
async syncModel({ conid, database, isFullRefresh }, req) {
|
|
355
|
+
if (conid == '__model') {
|
|
356
|
+
socket.emitChanged('database-structure-changed', { conid, database });
|
|
357
|
+
return { status: 'ok' };
|
|
358
|
+
}
|
|
359
|
+
|
|
352
360
|
testConnectionPermission(conid, req);
|
|
353
361
|
const conn = await this.ensureOpened(conid, database);
|
|
354
362
|
conn.subprocess.send({ msgtype: 'syncModel', isFullRefresh });
|
|
@@ -392,11 +400,12 @@ module.exports = {
|
|
|
392
400
|
},
|
|
393
401
|
|
|
394
402
|
structure_meta: true,
|
|
395
|
-
async structure({ conid, database }, req) {
|
|
403
|
+
async structure({ conid, database, modelTransFile = null }, req) {
|
|
396
404
|
testConnectionPermission(conid, req);
|
|
397
405
|
if (conid == '__model') {
|
|
398
406
|
const model = await importDbModel(database);
|
|
399
|
-
|
|
407
|
+
const trans = await loadModelTransform(modelTransFile);
|
|
408
|
+
return trans ? trans(model) : model;
|
|
400
409
|
}
|
|
401
410
|
|
|
402
411
|
const opened = await this.ensureOpened(conid, database);
|
|
@@ -432,14 +441,35 @@ module.exports = {
|
|
|
432
441
|
},
|
|
433
442
|
|
|
434
443
|
exportModel_meta: true,
|
|
435
|
-
async exportModel({ conid, database }, req) {
|
|
444
|
+
async exportModel({ conid, database, outputFolder, schema }, req) {
|
|
445
|
+
testConnectionPermission(conid, req);
|
|
446
|
+
|
|
447
|
+
const realFolder = outputFolder.startsWith('archive:')
|
|
448
|
+
? resolveArchiveFolder(outputFolder.substring('archive:'.length))
|
|
449
|
+
: outputFolder;
|
|
450
|
+
|
|
451
|
+
const model = await this.structure({ conid, database });
|
|
452
|
+
const filteredModel = schema ? filterStructureBySchema(model, schema) : model;
|
|
453
|
+
await exportDbModel(extendDatabaseInfo(filteredModel), realFolder);
|
|
454
|
+
|
|
455
|
+
if (outputFolder.startsWith('archive:')) {
|
|
456
|
+
socket.emitChanged(`archive-files-changed`, { folder: outputFolder.substring('archive:'.length) });
|
|
457
|
+
}
|
|
458
|
+
return { status: 'ok' };
|
|
459
|
+
},
|
|
460
|
+
|
|
461
|
+
exportModelSql_meta: true,
|
|
462
|
+
async exportModelSql({ conid, database, outputFolder, outputFile, schema }, req) {
|
|
436
463
|
testConnectionPermission(conid, req);
|
|
437
|
-
|
|
438
|
-
await
|
|
464
|
+
|
|
465
|
+
const connection = await connections.getCore({ conid });
|
|
466
|
+
const driver = requireEngineDriver(connection);
|
|
467
|
+
|
|
439
468
|
const model = await this.structure({ conid, database });
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
469
|
+
const filteredModel = schema ? filterStructureBySchema(model, schema) : model;
|
|
470
|
+
await exportDbModelSql(extendDatabaseInfo(filteredModel), driver, outputFolder, outputFile);
|
|
471
|
+
|
|
472
|
+
return { status: 'ok' };
|
|
443
473
|
},
|
|
444
474
|
|
|
445
475
|
generateDeploySql_meta: true,
|
|
@@ -12,6 +12,7 @@ const {
|
|
|
12
12
|
jsonScriptToJavascript,
|
|
13
13
|
getLogger,
|
|
14
14
|
safeJsonParse,
|
|
15
|
+
pinoLogRecordToMessageRecord,
|
|
15
16
|
} = require('dbgate-tools');
|
|
16
17
|
const { handleProcessCommunication } = require('../utility/processComm');
|
|
17
18
|
const processArgs = require('../utility/processArgs');
|
|
@@ -68,18 +69,20 @@ module.exports = {
|
|
|
68
69
|
|
|
69
70
|
dispatchMessage(runid, message) {
|
|
70
71
|
if (message) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
if (_.isPlainObject(message)) logger.log(message);
|
|
73
|
+
else logger.info(message);
|
|
74
|
+
|
|
75
|
+
const toEmit = _.isPlainObject(message)
|
|
76
|
+
? {
|
|
77
|
+
time: new Date(),
|
|
78
|
+
...message,
|
|
79
|
+
}
|
|
80
|
+
: {
|
|
81
|
+
message,
|
|
82
|
+
time: new Date(),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
if (toEmit.level >= 50) {
|
|
83
86
|
toEmit.severity = 'error';
|
|
84
87
|
}
|
|
85
88
|
|
|
@@ -131,7 +134,16 @@ module.exports = {
|
|
|
131
134
|
}
|
|
132
135
|
);
|
|
133
136
|
const pipeDispatcher = severity => data => {
|
|
134
|
-
|
|
137
|
+
const json = safeJsonParse(data, null);
|
|
138
|
+
|
|
139
|
+
if (json) {
|
|
140
|
+
return this.dispatchMessage(runid, pinoLogRecordToMessageRecord(json));
|
|
141
|
+
} else {
|
|
142
|
+
return this.dispatchMessage(runid, {
|
|
143
|
+
message: json == null ? data.toString().trim() : null,
|
|
144
|
+
severity,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
135
147
|
};
|
|
136
148
|
|
|
137
149
|
byline(subprocess.stdout).on('data', pipeDispatcher('info'));
|
|
@@ -165,7 +177,7 @@ module.exports = {
|
|
|
165
177
|
|
|
166
178
|
start_meta: true,
|
|
167
179
|
async start({ script }) {
|
|
168
|
-
const runid = crypto.randomUUID()
|
|
180
|
+
const runid = crypto.randomUUID();
|
|
169
181
|
|
|
170
182
|
if (script.type == 'json') {
|
|
171
183
|
const js = jsonScriptToJavascript(script);
|
|
@@ -134,6 +134,7 @@ module.exports = {
|
|
|
134
134
|
listDatabases_meta: true,
|
|
135
135
|
async listDatabases({ conid }, req) {
|
|
136
136
|
if (!conid) return [];
|
|
137
|
+
if (conid == '__model') return [];
|
|
137
138
|
testConnectionPermission(conid, req);
|
|
138
139
|
const opened = await this.ensureOpened(conid);
|
|
139
140
|
return opened.databases;
|
|
@@ -172,7 +173,7 @@ module.exports = {
|
|
|
172
173
|
}
|
|
173
174
|
})
|
|
174
175
|
);
|
|
175
|
-
socket.setStreamIdFilter(strmid, { conid: conidArray });
|
|
176
|
+
socket.setStreamIdFilter(strmid, { conid: [...(conidArray ?? []), '__model'] });
|
|
176
177
|
return { status: 'ok' };
|
|
177
178
|
},
|
|
178
179
|
|
package/src/currentVersion.js
CHANGED
|
@@ -8,7 +8,7 @@ const autoIndexForeignKeysTransform = () => database => {
|
|
|
8
8
|
...(table.indexes || []),
|
|
9
9
|
...table.foreignKeys.map(fk => ({
|
|
10
10
|
constraintName: `IX_${fk.constraintName}`,
|
|
11
|
-
columns: fk.columns,
|
|
11
|
+
columns: fk.columns.map(x => ({ columnName: x.columnName })),
|
|
12
12
|
})),
|
|
13
13
|
],
|
|
14
14
|
};
|
package/src/shell/deployDb.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
const generateDeploySql = require('./generateDeploySql');
|
|
2
2
|
const executeQuery = require('./executeQuery');
|
|
3
|
+
const { ScriptDrivedDeployer } = require('dbgate-datalib');
|
|
4
|
+
const connectUtility = require('../utility/connectUtility');
|
|
5
|
+
const requireEngineDriver = require('../utility/requireEngineDriver');
|
|
6
|
+
const loadModelFolder = require('../utility/loadModelFolder');
|
|
3
7
|
|
|
4
8
|
async function deployDb({
|
|
5
9
|
connection,
|
|
@@ -10,19 +14,41 @@ async function deployDb({
|
|
|
10
14
|
loadedDbModel,
|
|
11
15
|
modelTransforms,
|
|
12
16
|
dbdiffOptionsExtra,
|
|
17
|
+
ignoreNameRegex = '',
|
|
18
|
+
targetSchema = null,
|
|
13
19
|
}) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
if (!driver) driver = requireEngineDriver(connection);
|
|
21
|
+
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read'));
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const scriptDeployer = new ScriptDrivedDeployer(
|
|
25
|
+
dbhan,
|
|
26
|
+
driver,
|
|
27
|
+
loadedDbModel ?? (await loadModelFolder(modelFolder))
|
|
28
|
+
);
|
|
29
|
+
await scriptDeployer.runPre();
|
|
30
|
+
|
|
31
|
+
const { sql } = await generateDeploySql({
|
|
32
|
+
connection,
|
|
33
|
+
systemConnection: dbhan,
|
|
34
|
+
driver,
|
|
35
|
+
analysedStructure,
|
|
36
|
+
modelFolder,
|
|
37
|
+
loadedDbModel,
|
|
38
|
+
modelTransforms,
|
|
39
|
+
dbdiffOptionsExtra,
|
|
40
|
+
ignoreNameRegex,
|
|
41
|
+
targetSchema,
|
|
42
|
+
});
|
|
43
|
+
// console.log('RUNNING DEPLOY SCRIPT:', sql);
|
|
44
|
+
await executeQuery({ connection, systemConnection: dbhan, driver, sql, logScriptItems: true });
|
|
45
|
+
|
|
46
|
+
await scriptDeployer.runPost();
|
|
47
|
+
} finally {
|
|
48
|
+
if (!systemConnection) {
|
|
49
|
+
await driver.close(dbhan);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
26
52
|
}
|
|
27
53
|
|
|
28
54
|
module.exports = deployDb;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const executeQuery = require('./executeQuery');
|
|
2
|
+
const requireEngineDriver = require('../utility/requireEngineDriver');
|
|
3
|
+
const connectUtility = require('../utility/connectUtility');
|
|
4
|
+
const { getLogger, extendDatabaseInfo } = require('dbgate-tools');
|
|
5
|
+
|
|
6
|
+
const logger = getLogger('dropAllDbObjects');
|
|
7
|
+
|
|
8
|
+
async function dropAllDbObjects({ connection, systemConnection, driver, analysedStructure }) {
|
|
9
|
+
if (!driver) driver = requireEngineDriver(connection);
|
|
10
|
+
|
|
11
|
+
const dbhan = systemConnection || (await connectUtility(driver, connection, 'write'));
|
|
12
|
+
|
|
13
|
+
logger.info(`Connected.`);
|
|
14
|
+
|
|
15
|
+
if (!analysedStructure) {
|
|
16
|
+
analysedStructure = await driver.analyseFull(dbhan);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
analysedStructure = extendDatabaseInfo(analysedStructure);
|
|
20
|
+
|
|
21
|
+
const dmp = driver.createDumper();
|
|
22
|
+
|
|
23
|
+
for (const table of analysedStructure.tables) {
|
|
24
|
+
for (const fk of table.foreignKeys) {
|
|
25
|
+
dmp.dropForeignKey(fk);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const table of analysedStructure.tables) {
|
|
29
|
+
dmp.dropTable(table);
|
|
30
|
+
}
|
|
31
|
+
for (const field of Object.keys(analysedStructure)) {
|
|
32
|
+
if (dmp.getSqlObjectSqlName(field)) {
|
|
33
|
+
for (const obj of analysedStructure[field]) {
|
|
34
|
+
dmp.dropSqlObject(obj);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
await executeQuery({ connection, systemConnection, driver, sql: dmp.s, logScriptItems: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = dropAllDbObjects;
|
|
@@ -6,6 +6,10 @@ const {
|
|
|
6
6
|
extendDatabaseInfo,
|
|
7
7
|
modelCompareDbDiffOptions,
|
|
8
8
|
enrichWithPreloadedRows,
|
|
9
|
+
skipNamesInStructureByRegex,
|
|
10
|
+
replaceSchemaInStructure,
|
|
11
|
+
filterStructureBySchema,
|
|
12
|
+
skipDbGateInternalObjects,
|
|
9
13
|
} = require('dbgate-tools');
|
|
10
14
|
const importDbModel = require('../utility/importDbModel');
|
|
11
15
|
const requireEngineDriver = require('../utility/requireEngineDriver');
|
|
@@ -20,6 +24,8 @@ async function generateDeploySql({
|
|
|
20
24
|
loadedDbModel = undefined,
|
|
21
25
|
modelTransforms = undefined,
|
|
22
26
|
dbdiffOptionsExtra = {},
|
|
27
|
+
ignoreNameRegex = '',
|
|
28
|
+
targetSchema = null,
|
|
23
29
|
}) {
|
|
24
30
|
if (!driver) driver = requireEngineDriver(connection);
|
|
25
31
|
|
|
@@ -30,6 +36,11 @@ async function generateDeploySql({
|
|
|
30
36
|
analysedStructure = await driver.analyseFull(dbhan);
|
|
31
37
|
}
|
|
32
38
|
|
|
39
|
+
if (ignoreNameRegex) {
|
|
40
|
+
analysedStructure = skipNamesInStructureByRegex(analysedStructure, new RegExp(ignoreNameRegex, 'i'));
|
|
41
|
+
}
|
|
42
|
+
analysedStructure = skipDbGateInternalObjects(analysedStructure);
|
|
43
|
+
|
|
33
44
|
let deployedModelSource = loadedDbModel
|
|
34
45
|
? databaseInfoFromYamlModel(loadedDbModel)
|
|
35
46
|
: await importDbModel(modelFolder);
|
|
@@ -38,6 +49,11 @@ async function generateDeploySql({
|
|
|
38
49
|
deployedModelSource = transform(deployedModelSource);
|
|
39
50
|
}
|
|
40
51
|
|
|
52
|
+
if (targetSchema) {
|
|
53
|
+
deployedModelSource = replaceSchemaInStructure(deployedModelSource, targetSchema);
|
|
54
|
+
analysedStructure = filterStructureBySchema(analysedStructure, targetSchema);
|
|
55
|
+
}
|
|
56
|
+
|
|
41
57
|
const deployedModel = generateDbPairingId(extendDatabaseInfo(deployedModelSource));
|
|
42
58
|
const currentModel = generateDbPairingId(extendDatabaseInfo(analysedStructure));
|
|
43
59
|
const opts = {
|
package/src/shell/index.js
CHANGED
|
@@ -34,6 +34,8 @@ const dataTypeMapperTransform = require('./dataTypeMapperTransform');
|
|
|
34
34
|
const sqlTextReplacementTransform = require('./sqlTextReplacementTransform');
|
|
35
35
|
const autoIndexForeignKeysTransform = require('./autoIndexForeignKeysTransform');
|
|
36
36
|
const generateDeploySql = require('./generateDeploySql');
|
|
37
|
+
const dropAllDbObjects = require('./dropAllDbObjects');
|
|
38
|
+
const scriptDrivedDeploy = require('./scriptDrivedDeploy');
|
|
37
39
|
|
|
38
40
|
const dbgateApi = {
|
|
39
41
|
queryReader,
|
|
@@ -71,6 +73,8 @@ const dbgateApi = {
|
|
|
71
73
|
sqlTextReplacementTransform,
|
|
72
74
|
autoIndexForeignKeysTransform,
|
|
73
75
|
generateDeploySql,
|
|
76
|
+
dropAllDbObjects,
|
|
77
|
+
scriptDrivedDeploy,
|
|
74
78
|
};
|
|
75
79
|
|
|
76
80
|
requirePlugin.initializeDbgateApi(dbgateApi);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { getSchemasUsedByStructure } = require('dbgate-tools');
|
|
4
|
+
|
|
5
|
+
async function exportDbModelSql(dbModel, driver, outputDir, outputFile) {
|
|
6
|
+
const { tables, views, procedures, functions, triggers, matviews } = dbModel;
|
|
7
|
+
|
|
8
|
+
const usedSchemas = getSchemasUsedByStructure(dbModel);
|
|
9
|
+
const useSchemaDir = usedSchemas.length > 1;
|
|
10
|
+
|
|
11
|
+
const createdDirs = new Set();
|
|
12
|
+
async function ensureDir(dir) {
|
|
13
|
+
if (!createdDirs.has(dir)) {
|
|
14
|
+
await fs.mkdir(dir, { recursive: true });
|
|
15
|
+
createdDirs.add(dir);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function writeLists(writeList) {
|
|
20
|
+
await writeList(views, 'views');
|
|
21
|
+
await writeList(procedures, 'procedures');
|
|
22
|
+
await writeList(functions, 'functions');
|
|
23
|
+
await writeList(triggers, 'triggers');
|
|
24
|
+
await writeList(matviews, 'matviews');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (outputFile) {
|
|
28
|
+
const dmp = driver.createDumper();
|
|
29
|
+
for (const table of tables || []) {
|
|
30
|
+
dmp.createTable({
|
|
31
|
+
...table,
|
|
32
|
+
foreignKeys: [],
|
|
33
|
+
dependencies: [],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
for (const table of tables || []) {
|
|
37
|
+
for (const fk of table.foreignKeys || []) {
|
|
38
|
+
dmp.createForeignKey(fk);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
writeLists((list, folder) => {
|
|
42
|
+
for (const obj of list || []) {
|
|
43
|
+
dmp.createSqlObject(obj);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const script = dmp.s;
|
|
48
|
+
await fs.writeFile(outputFile, script);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (outputDir) {
|
|
52
|
+
for (const table of tables || []) {
|
|
53
|
+
const tablesDir = useSchemaDir
|
|
54
|
+
? path.join(outputDir, table.schemaName ?? 'default', 'tables')
|
|
55
|
+
: path.join(outputDir, 'tables');
|
|
56
|
+
await ensureDir(tablesDir);
|
|
57
|
+
const dmp = driver.createDumper();
|
|
58
|
+
dmp.createTable({
|
|
59
|
+
...table,
|
|
60
|
+
foreignKeys: [],
|
|
61
|
+
dependencies: [],
|
|
62
|
+
});
|
|
63
|
+
await fs.writeFile(path.join(tablesDir, `${table.pureName}.sql`), dmp.s);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await writeLists(async (list, folder) => {
|
|
67
|
+
for (const obj of list || []) {
|
|
68
|
+
const objdir = useSchemaDir
|
|
69
|
+
? path.join(outputDir, obj.schemaName ?? 'default', folder)
|
|
70
|
+
: path.join(outputDir, folder);
|
|
71
|
+
await ensureDir(objdir);
|
|
72
|
+
const dmp = driver.createDumper();
|
|
73
|
+
dmp.createSqlObject(obj);
|
|
74
|
+
await fs.writeFile(path.join(objdir, `${obj.pureName}.sql`), dmp.s);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = exportDbModelSql;
|
|
@@ -1,28 +1,8 @@
|
|
|
1
|
-
const fs = require('fs-extra');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const yaml = require('js-yaml');
|
|
4
1
|
const { databaseInfoFromYamlModel, DatabaseAnalyser } = require('dbgate-tools');
|
|
5
|
-
const
|
|
6
|
-
const { archivedir, resolveArchiveFolder } = require('./directories');
|
|
7
|
-
const loadFilesRecursive = require('./loadFilesRecursive');
|
|
2
|
+
const loadModelFolder = require('./loadModelFolder');
|
|
8
3
|
|
|
9
4
|
async function importDbModel(inputDir) {
|
|
10
|
-
const files =
|
|
11
|
-
|
|
12
|
-
const dir = inputDir.startsWith('archive:') ? resolveArchiveFolder(inputDir.substring('archive:'.length)) : inputDir;
|
|
13
|
-
|
|
14
|
-
for (const name of await loadFilesRecursive(dir)) {
|
|
15
|
-
if (name.endsWith('.table.yaml') || name.endsWith('.sql')) {
|
|
16
|
-
const text = await fs.readFile(path.join(dir, name), { encoding: 'utf-8' });
|
|
17
|
-
|
|
18
|
-
files.push({
|
|
19
|
-
name: path.parse(name).base,
|
|
20
|
-
text,
|
|
21
|
-
json: name.endsWith('.yaml') ? yaml.load(text) : null,
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
5
|
+
const files = await loadModelFolder(inputDir);
|
|
26
6
|
return databaseInfoFromYamlModel(files);
|
|
27
7
|
}
|
|
28
8
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const yaml = require('js-yaml');
|
|
4
|
+
const { resolveArchiveFolder } = require('./directories');
|
|
5
|
+
const loadFilesRecursive = require('./loadFilesRecursive');
|
|
6
|
+
|
|
7
|
+
async function loadModelFolder(inputDir) {
|
|
8
|
+
const files = [];
|
|
9
|
+
|
|
10
|
+
const dir = inputDir.startsWith('archive:') ? resolveArchiveFolder(inputDir.substring('archive:'.length)) : inputDir;
|
|
11
|
+
|
|
12
|
+
for (const name of await loadFilesRecursive(dir)) {
|
|
13
|
+
if (name.endsWith('.table.yaml') || name.endsWith('.sql')) {
|
|
14
|
+
const text = await fs.readFile(path.join(dir, name), { encoding: 'utf-8' });
|
|
15
|
+
|
|
16
|
+
files.push({
|
|
17
|
+
name: path.parse(name).base,
|
|
18
|
+
text,
|
|
19
|
+
json: name.endsWith('.yaml') ? yaml.load(text) : null,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return files;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = loadModelFolder;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const { filesdir } = require('./directories');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs-extra');
|
|
4
|
+
const _ = require('lodash');
|
|
5
|
+
const dbgateApi = require('../shell');
|
|
6
|
+
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
|
7
|
+
const logger = getLogger('loadModelTransform');
|
|
8
|
+
|
|
9
|
+
function modelTransformFromJson(json) {
|
|
10
|
+
if (!dbgateApi[json.transform]) return null;
|
|
11
|
+
const creator = dbgateApi[json.transform];
|
|
12
|
+
return creator(...json.arguments);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function loadModelTransform(file) {
|
|
16
|
+
if (!file) return null;
|
|
17
|
+
try {
|
|
18
|
+
const dir = filesdir();
|
|
19
|
+
const fullPath = path.join(dir, 'modtrans', file);
|
|
20
|
+
const text = await fs.readFile(fullPath, { encoding: 'utf-8' });
|
|
21
|
+
const json = JSON.parse(text);
|
|
22
|
+
if (_.isArray(json)) {
|
|
23
|
+
const array = _.compact(json.map(x => modelTransformFromJson(x)));
|
|
24
|
+
return array.length ? structure => array.reduce((acc, val) => val(acc), structure) : null;
|
|
25
|
+
}
|
|
26
|
+
if (_.isPlainObject(json)) {
|
|
27
|
+
return modelTransformFromJson(json);
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
logger.error(extractErrorLogData(err), `Error loading model transform ${file}`);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = loadModelTransform;
|
|
@@ -43,9 +43,17 @@ const platformInfo = {
|
|
|
43
43
|
platform,
|
|
44
44
|
runningInWebpack: !!process.env.WEBPACK_DEV_SERVER_URL,
|
|
45
45
|
allowShellConnection:
|
|
46
|
-
(!processArgs.listenApiChild && !isNpmDist) ||
|
|
46
|
+
(!processArgs.listenApiChild && !isNpmDist) ||
|
|
47
|
+
!!process.env.SHELL_CONNECTION ||
|
|
48
|
+
!!isElectron() ||
|
|
49
|
+
!!isDbModel ||
|
|
50
|
+
isDevMode,
|
|
47
51
|
allowShellScripting:
|
|
48
|
-
(!processArgs.listenApiChild && !isNpmDist) ||
|
|
52
|
+
(!processArgs.listenApiChild && !isNpmDist) ||
|
|
53
|
+
!!process.env.SHELL_SCRIPTING ||
|
|
54
|
+
!!isElectron() ||
|
|
55
|
+
!!isDbModel ||
|
|
56
|
+
isDevMode,
|
|
49
57
|
allowConnectionFromEnvVariables: !!isDbModel,
|
|
50
58
|
defaultKeyfile: path.join(os.homedir(), '.ssh/id_rsa'),
|
|
51
59
|
isAwsUbuntuLayout,
|