diff --git a/forward_engineering/ddlProvider/ddlProvider.js b/forward_engineering/ddlProvider/ddlProvider.js index 9390bc2..1b7b778 100644 --- a/forward_engineering/ddlProvider/ddlProvider.js +++ b/forward_engineering/ddlProvider/ddlProvider.js @@ -31,6 +31,33 @@ const { getTableType } = require('./ddlHelpers/table/getTableType.js'); const { getName } = require('./ddlHelpers/jsonSchema/jsonSchemaHelper.js'); const { hydrateAuxiliaryTableData } = require('./ddlHelpers/table/hydrateAuxiliaryTableData.js'); +/** + * @param {{ columns: object[] }} + * @returns {string} + */ +const getViewColumnsAsString = ({ columns }) => { + const statements = columns.map(({ statement, isActivated }) => + commentIfDeactivated(statement, { isActivated, isPartOfLine: false }), + ); + const lastNonCommentIndex = statements.findLastIndex(statement => !statement.startsWith('--')); + + if (lastNonCommentIndex === -1) { + return statements.join('\n'); + } + + return statements + .map((st, index) => { + const isNotLast = index !== statements.length - 1; + + if (lastNonCommentIndex === index && isNotLast) { + return `${st} -- ,`; + } + + return `${st}${isNotLast ? ',' : ''}`; + }) + .join('\n\t\t'); +}; + module.exports = (baseProvider, options, app) => { return { getDefaultType(type) { @@ -418,7 +445,7 @@ module.exports = (baseProvider, options, app) => { const orReplace = viewData.orReplace ? ' OR REPLACE' : ''; const { columns, tables } = getViewData({ keys: viewData.keys }); - const columnsAsString = columns.map(column => column.statement).join(',\n\t\t'); + const columnsAsString = getViewColumnsAsString({ columns }); const commentStatement = getTableCommentStatement({ tableName: viewName, description: viewData.description,