-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from ArzamastsevVladyslav/feature/HCK-3445-com…
…ment-out-drop-statements Added commenting out of harmful DDL that drops comments and constraints
- Loading branch information
Showing
2 changed files
with
38 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |