diff --git a/forward_engineering/api.js b/forward_engineering/api.js index e6fd1a0..818fd12 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -1,8 +1,7 @@ const reApi = require('../reverse_engineering/api'); const { createLogger } = require('../reverse_engineering/helpers/loggerHelper'); const applyToInstanceHelper = require('./applyToInstanceHelper'); -const { commentDropStatements } = require('./helpers/commentDropStatements'); -const { DROP_STATEMENTS } = require('./helpers/constants'); +const { commentDropStatements, doesScriptContainDropStatements} = require('./helpers/commentDropStatements'); module.exports = { generateScript(data, logger, callback, app) { @@ -105,7 +104,7 @@ module.exports = { const cb = (error, script = '') => callback( error, - DROP_STATEMENTS.some(statement => script.includes(statement)), + doesScriptContainDropStatements(script), ); if (data.level === 'container') { diff --git a/forward_engineering/helpers/commentDropStatements.js b/forward_engineering/helpers/commentDropStatements.js index 2c0c109..e37e97d 100644 --- a/forward_engineering/helpers/commentDropStatements.js +++ b/forward_engineering/helpers/commentDropStatements.js @@ -1,17 +1,50 @@ const { DROP_STATEMENTS } = require('./constants'); +const isDropNotNullStatementRegex = /ALTER TABLE IF EXISTS .+ ALTER COLUMN .+ DROP NOT NULL;/; + +const isDropConstraintStatementRegex = /ALTER TABLE IF EXISTS .+ DROP CONSTRAINT IF EXISTS .+;/; + +const dropCommentRegex = /COMMENT ON (TABLE|SCHEMA|VIEW|COLUMN) .+ IS NULL;/; + +/** + * @param scriptLine {string} + * @return {boolean} + * */ +const shouldStatementBeCommentedOut = (scriptLine) => { + const doesContainDropStatements = DROP_STATEMENTS.some(statement => scriptLine.includes(statement)); + if (doesContainDropStatements) { + return true; + } + + return [ + isDropNotNullStatementRegex, + isDropConstraintStatementRegex, + dropCommentRegex, + ].some(regex => regex.test(scriptLine)); +} + +/** + * @param script {string} + * @return {boolean} + * */ +const doesScriptContainDropStatements = (script) => { + return script.split('\n') + .some(shouldStatementBeCommentedOut); +} + + const commentDropStatements = (script = '') => script .split('\n') .map(line => { - if (DROP_STATEMENTS.some(statement => line.includes(statement))) { + if (shouldStatementBeCommentedOut(line)) { return `-- ${line}`; - } else { - return line; } + return line; }) .join('\n'); module.exports = { commentDropStatements, + doesScriptContainDropStatements, };