Skip to content

Commit

Permalink
FE: added support of <applyDropStatements> option
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii4as committed Mar 24, 2022
1 parent 40bb66c commit 585b272
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 27 deletions.
90 changes: 65 additions & 25 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -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' });
Expand All @@ -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);
})
Expand All @@ -58,5 +81,22 @@ module.exports = {
},
testConnection(connectionInfo, logger, callback, app) {
reApi.testConnection(connectionInfo, logger, callback, app);
}
};
},
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 });
}
},
};
13 changes: 11 additions & 2 deletions forward_engineering/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
17 changes: 17 additions & 0 deletions forward_engineering/helpers/commentDropStatements.js
Original file line number Diff line number Diff line change
@@ -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,
};
5 changes: 5 additions & 0 deletions forward_engineering/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const DROP_STATEMENTS = ['DROP USER', 'DROP TABLE', 'DROP COLUMN', 'DROP TYPE', 'DROP ATTRIBUTE', 'DROP VIEW'];

module.exports = {
DROP_STATEMENTS,
};

0 comments on commit 585b272

Please sign in to comment.