From 585b272205507c592006a4f804c3981c22fec85a Mon Sep 17 00:00:00 2001 From: Vitalii Yarmus Date: Thu, 24 Mar 2022 13:39:18 +0200 Subject: [PATCH] FE: added support of option --- forward_engineering/api.js | 90 +++++++++++++------ forward_engineering/config.json | 13 ++- .../helpers/commentDropStatements.js | 17 ++++ forward_engineering/helpers/constants.js | 5 ++ 4 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 forward_engineering/helpers/commentDropStatements.js create mode 100644 forward_engineering/helpers/constants.js diff --git a/forward_engineering/api.js b/forward_engineering/api.js index d0865fe..349d9e3 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -1,38 +1,60 @@ const reApi = require('../reverse_engineering/api'); const applyToInstanceHelper = require('./applyToInstanceHelper'); +const { commentDropStatements } = require('./helpers/commentDropStatements'); +const { DROP_STATEMENTS } = require('./helpers/constants'); module.exports = { generateScript(data, logger, callback, app) { - const { - getAlterContainersScripts, - getAlterCollectionsScripts, - getAlterViewScripts, - getAlterModelDefinitionsScripts, - } = require('./helpers/alterScriptFromDeltaHelper'); - - const collection = JSON.parse(data.jsonSchema); - if (!collection) { - throw new Error( - '"comparisonModelCollection" is not found. Alter script can be generated only from Delta model', + try { + const { + getAlterContainersScripts, + getAlterCollectionsScripts, + getAlterViewScripts, + getAlterModelDefinitionsScripts, + } = require('./helpers/alterScriptFromDeltaHelper'); + + const collection = JSON.parse(data.jsonSchema); + if (!collection) { + throw new Error( + '"comparisonModelCollection" is not found. Alter script can be generated only from Delta model', + ); + } + + const dbVersion = data.modelData[0]?.dbVersion; + const containersScripts = getAlterContainersScripts(collection); + const collectionsScripts = getAlterCollectionsScripts(collection, app, dbVersion); + const viewScripts = getAlterViewScripts(collection, app); + const modelDefinitionsScripts = getAlterModelDefinitionsScripts(collection, app); + const script = [ + ...containersScripts, + ...collectionsScripts, + ...viewScripts, + ...modelDefinitionsScripts, + ].join('\n\n'); + + const applyDropStatements = data.options?.additionalOptions?.some( + option => option.id === 'applyDropStatements' && option.value, ); - } - const dbVersion = data.modelData[0]?.dbVersion; - const containersScripts = getAlterContainersScripts(collection); - const collectionsScripts = getAlterCollectionsScripts(collection, app, dbVersion); - const viewScripts = getAlterViewScripts(collection, app); - const modelDefinitionsScripts = getAlterModelDefinitionsScripts(collection, app); + callback(null, applyDropStatements ? script : commentDropStatements(script)); + } catch (error) { + logger.log('error', { message: error.message, stack: error.stack }, 'Oracle Forward-Engineering Error'); - callback( - null, - [...containersScripts, ...collectionsScripts, ...viewScripts, ...modelDefinitionsScripts].join('\n\n'), - ); + callback({ message: error.message, stack: error.stack }); + } }, generateViewScript(data, logger, callback, app) { callback(new Error('Forward-Engineering of delta model on view level is not supported')); }, generateContainerScript(data, logger, callback, app) { - callback(new Error('Forward-Engineering of delta model on container level is not supported')); + try { + data.jsonSchema = data.collections[0]; + this.generateScript(data, logger, callback, app); + } catch (error) { + logger.log('error', { message: error.message, stack: error.stack }, 'Oracle Forward-Engineering Error'); + + callback({ message: error.message, stack: error.stack }); + } }, getDatabases(connectionInfo, logger, callback, app) { logger.progress({ message: 'Find all schemas' }); @@ -42,7 +64,8 @@ module.exports = { logger.clear(); logger.log('info', connectionInfo, 'connectionInfo', connectionInfo.hiddenKeys); - applyToInstanceHelper.applyToInstance(connectionInfo, logger, app) + applyToInstanceHelper + .applyToInstance(connectionInfo, logger, app) .then(result => { callback(null, result); }) @@ -58,5 +81,22 @@ module.exports = { }, testConnection(connectionInfo, logger, callback, app) { reApi.testConnection(connectionInfo, logger, callback, app); - } -}; \ No newline at end of file + }, + isDropInStatements(data, logger, callback, app) { + try { + const cb = (error, script = '') => + callback( + error, + DROP_STATEMENTS.some(statement => script.includes(statement)), + ); + + if (data.level === 'container') { + this.generateContainerScript(data, logger, cb, app); + } else { + this.generateScript(data, logger, cb, app); + } + } catch (error) { + callback({ message: error.message, stack: error.stack }); + } + }, +}; diff --git a/forward_engineering/config.json b/forward_engineering/config.json index e4df5d2..06c4e01 100644 --- a/forward_engineering/config.json +++ b/forward_engineering/config.json @@ -17,12 +17,21 @@ }, "compMode": { "entity": true, - "container": false + "container": true }, "namePrefix": "Oracle", "level": { "container": true, "entity": true, "view": true - } + }, + "additionalOptions": [ + { + "id": "applyDropStatements", + "value": false, + "forUpdate": true, + "name": "Apply Drop Statements", + "isDropInStatements": true + } + ] } \ No newline at end of file diff --git a/forward_engineering/helpers/commentDropStatements.js b/forward_engineering/helpers/commentDropStatements.js new file mode 100644 index 0000000..2c0c109 --- /dev/null +++ b/forward_engineering/helpers/commentDropStatements.js @@ -0,0 +1,17 @@ +const { DROP_STATEMENTS } = require('./constants'); + +const commentDropStatements = (script = '') => + script + .split('\n') + .map(line => { + if (DROP_STATEMENTS.some(statement => line.includes(statement))) { + return `-- ${line}`; + } else { + return line; + } + }) + .join('\n'); + +module.exports = { + commentDropStatements, +}; diff --git a/forward_engineering/helpers/constants.js b/forward_engineering/helpers/constants.js new file mode 100644 index 0000000..46117d6 --- /dev/null +++ b/forward_engineering/helpers/constants.js @@ -0,0 +1,5 @@ +const DROP_STATEMENTS = ['DROP USER', 'DROP TABLE', 'DROP COLUMN', 'DROP TYPE', 'DROP ATTRIBUTE', 'DROP VIEW']; + +module.exports = { + DROP_STATEMENTS, +};