diff --git a/forward_engineering/ddlProvider.js b/forward_engineering/ddlProvider.js index d4d9d0a..cc1adf4 100644 --- a/forward_engineering/ddlProvider.js +++ b/forward_engineering/ddlProvider.js @@ -2,6 +2,7 @@ const defaultTypes = require('./configs/defaultTypes'); const types = require('./configs/types'); const templates = require('./configs/templates'); const { commentIfDeactivated } = require('./helpers/commentIfDeactivated'); +const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements'); module.exports = (baseProvider, options, app) => { const _ = app.require('lodash'); @@ -137,9 +138,10 @@ module.exports = (baseProvider, options, app) => { const dividedForeignKeys = divideIntoActivatedAndDeactivated(foreignKeyConstraints, key => key.statement); const foreignKeyConstraintsString = generateConstraintsString(dividedForeignKeys, isActivated); const tableAndColumnCommentsSeparator = tableComment ? '\n\n' : ''; + const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' }); const tableStatement = assignTemplates(templates.createTable, { name: tableName, - column_definitions: columns.join(',\n\t'), + column_definitions: columnStatements, temporalTableTime: temporalTableTimeStatement, checkConstraints: checkConstraints.length ? ',\n\t' + checkConstraints.join(',\n\t') : '', foreignKeyConstraints: foreignKeyConstraintsString, @@ -202,7 +204,7 @@ module.exports = (baseProvider, options, app) => { columnDefinition.isHidden, ); - return assignTemplates(templates.columnDefinition, { + const statement = assignTemplates(templates.columnDefinition, { name: columnDefinition.name, type: decorateType(type, columnDefinition), primary_key: primaryKey + unique, @@ -215,6 +217,8 @@ module.exports = (baseProvider, options, app) => { temporalTableTime, ...identityContainer, }); + + return commentIfDeactivated(statement, { isActivated: columnDefinition.isActivated }); }, createIndex(tableName, index, dbData, isParentActivated = true) { diff --git a/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js b/forward_engineering/utils/joinActivatedAndDeactivatedStatements.js new file mode 100644 index 0000000..440e120 --- /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, +};