Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down
39 changes: 36 additions & 3 deletions forward_engineering/helpers/commentDropStatements.js
Original file line number Diff line number Diff line change
@@ -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,
};