Skip to content

Commit

Permalink
Merge pull request #61 from ArzamastsevVladyslav/feature/HCK-3445-com…
Browse files Browse the repository at this point in the history
…ment-out-drop-statements

Added commenting out of harmful DDL that drops comments and constraints
  • Loading branch information
Vitalii4as authored Apr 26, 2023
2 parents e9054d5 + 43ef0fc commit 7ed8c87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
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,
};

0 comments on commit 7ed8c87

Please sign in to comment.