diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index b39d1061..6cd85bfa 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -18,6 +18,7 @@ const { prepareCollectionStageCopyOptions, } = require('./helpers/alterScriptHelpers/common'); const { escapeString } = require('./utils/escapeString'); +const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements'); const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1; const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1; @@ -317,9 +318,8 @@ module.exports = (baseProvider, options, app) => { : ''; const copyOptions = tab(getCopyOptions(tableData.copyOptions), ' '); const atOrBefore = tab(getAtOrBefore(tableData.cloneParams), ' '); - const columnDefinitions = tableData.columns - .map(column => commentIfDeactivated(column.statement, column)) - .join(',\n\t\t'); + const columns = tableData.columns.map(column => commentIfDeactivated(column.statement, column)); + const columnDefinitions = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t\t' }); const tagsStatement = getTagStatement({ tags: tableData.tableTags, isCaseSensitive: tableData.isCaseSensitive, diff --git a/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js new file mode 100644 index 00000000..37d5863f --- /dev/null +++ b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js @@ -0,0 +1,53 @@ +/** + * @param {{ + * index: number; + * numberOfStatements: number; + * lastIndexOfActivatedStatement: number; + * delimiter: string; + * }} + * @return {string} + * */ +const getDelimiter = ({ index, numberOfStatements, lastIndexOfActivatedStatement, delimiter }) => { + const isLastStatement = index === numberOfStatements - 1; + const isLastActivatedStatement = index === lastIndexOfActivatedStatement; + + if (isLastStatement) { + return ''; + } + + if (isLastActivatedStatement) { + return ' //' + delimiter; + } + + return delimiter; +}; + +/** + * @param {{ + * statements?: string[]; + * delimiter?: string; + * indent?: string; + * }} + * @return {string} + * */ +const joinActivatedAndDeactivatedStatements = ({ statements = [], delimiter = ',', indent = '\n' }) => { + const lastIndexOfActivatedStatement = statements.findLastIndex(statement => !statement.startsWith('//')); + const numberOfStatements = statements.length; + + return statements + .map((statement, index) => { + const currentDelimiter = getDelimiter({ + index, + numberOfStatements, + lastIndexOfActivatedStatement, + delimiter, + }); + + return statement + currentDelimiter; + }) + .join(indent); +}; + +module.exports = { + joinActivatedAndDeactivatedStatements, +};