Skip to content

Commit

Permalink
HCK-8001: remove trailing comma before last deactivated column (#26)
Browse files Browse the repository at this point in the history
* HCK-8001: remove trailing comma before last deactivated column

* Revert "HCK-8001: remove trailing comma before last deactivated column"

This reverts commit 219a67c.

* HCK-8001: remove trailing comma before last deactivated column

* HCK-8001: add comma commenting for last activated column statement
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent d5d65e6 commit 76867db
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = (baseProvider, options, app) => {
getViewData,
} = require('../utils/general')(_);
const assignTemplates = require('../utils/assignTemplates');
const { joinActivatedAndDeactivatedStatements } = require('../utils/joinActivatedAndDeactivatedStatements');

const {
generateConstraintsString,
Expand Down Expand Up @@ -190,12 +191,13 @@ module.exports = (baseProvider, options, app) => {
partitionOf && !keyConstraintsValue && !checkConstraintsValue && !foreignKeyConstraintsString;
const openParenthesis = isEmptyPartitionBody ? '' : '(';
const closeParenthesis = isEmptyPartitionBody ? '' : ')';
const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' });

const tableStatement = assignTemplates(template, {
temporary: getTableTemporaryValue(temporary, unlogged),
ifNotExist: ifNotExistStr,
name: tableName,
columnDefinitions: !partitionOf ? '\t' + _.join(columns, ',\n\t') : '',
columnDefinitions: !partitionOf ? '\t' + columnStatements : '',
keyConstraints: keyConstraintsValue,
checkConstraints: checkConstraintsValue,
foreignKeyConstraints: foreignKeyConstraintsString,
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 76867db

Please sign in to comment.