Skip to content

Commit

Permalink
HCK-8007: add comma commenting for last activated column statement (#149
Browse files Browse the repository at this point in the history
)
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent c2912bf commit ca0d3fc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
6 changes: 3 additions & 3 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -319,9 +320,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,
Expand Down
53 changes: 53 additions & 0 deletions forward_engineering/utils/joinActivatedAndDeactivatedStatements.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit ca0d3fc

Please sign in to comment.