Skip to content

Commit 43ef0fc

Browse files
author
unknown
committed
Added commenting out of harmful DDL that drops comments and constraints
1 parent 16da9a5 commit 43ef0fc

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

forward_engineering/api.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const reApi = require('../reverse_engineering/api');
22
const { createLogger } = require('../reverse_engineering/helpers/loggerHelper');
33
const applyToInstanceHelper = require('./applyToInstanceHelper');
4-
const { commentDropStatements } = require('./helpers/commentDropStatements');
5-
const { DROP_STATEMENTS } = require('./helpers/constants');
4+
const { commentDropStatements, doesScriptContainDropStatements} = require('./helpers/commentDropStatements');
65

76
module.exports = {
87
generateScript(data, logger, callback, app) {
@@ -105,7 +104,7 @@ module.exports = {
105104
const cb = (error, script = '') =>
106105
callback(
107106
error,
108-
DROP_STATEMENTS.some(statement => script.includes(statement)),
107+
doesScriptContainDropStatements(script),
109108
);
110109

111110
if (data.level === 'container') {
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
const { DROP_STATEMENTS } = require('./constants');
22

3+
const isDropNotNullStatementRegex = /ALTER TABLE IF EXISTS .+ ALTER COLUMN .+ DROP NOT NULL;/;
4+
5+
const isDropConstraintStatementRegex = /ALTER TABLE IF EXISTS .+ DROP CONSTRAINT IF EXISTS .+;/;
6+
7+
const dropCommentRegex = /COMMENT ON (TABLE|SCHEMA|VIEW|COLUMN) .+ IS NULL;/;
8+
9+
/**
10+
* @param scriptLine {string}
11+
* @return {boolean}
12+
* */
13+
const shouldStatementBeCommentedOut = (scriptLine) => {
14+
const doesContainDropStatements = DROP_STATEMENTS.some(statement => scriptLine.includes(statement));
15+
if (doesContainDropStatements) {
16+
return true;
17+
}
18+
19+
return [
20+
isDropNotNullStatementRegex,
21+
isDropConstraintStatementRegex,
22+
dropCommentRegex,
23+
].some(regex => regex.test(scriptLine));
24+
}
25+
26+
/**
27+
* @param script {string}
28+
* @return {boolean}
29+
* */
30+
const doesScriptContainDropStatements = (script) => {
31+
return script.split('\n')
32+
.some(shouldStatementBeCommentedOut);
33+
}
34+
35+
336
const commentDropStatements = (script = '') =>
437
script
538
.split('\n')
639
.map(line => {
7-
if (DROP_STATEMENTS.some(statement => line.includes(statement))) {
40+
if (shouldStatementBeCommentedOut(line)) {
841
return `-- ${line}`;
9-
} else {
10-
return line;
1142
}
43+
return line;
1244
})
1345
.join('\n');
1446

1547
module.exports = {
1648
commentDropStatements,
49+
doesScriptContainDropStatements,
1750
};

0 commit comments

Comments
 (0)