Skip to content

Commit

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

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

This reverts commit 2f88656.

* HCK-8008: remove trailing comma before last deactivated column
  • Loading branch information
serhii-filonenko authored Sep 19, 2024
1 parent 628c118 commit a0632c6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

const provider = (baseProvider, options, app) => {
const _ = app.require('lodash');
Expand Down Expand Up @@ -94,11 +95,11 @@ const provider = (baseProvider, options, app) => {
key => key.statement,
);
const keyConstraintsString = generateConstraintsString(dividedKeysConstraints, isActivated);

const columnStatements = joinActivatedAndDeactivatedStatements({ statements: columns, indent: '\n\t' });
const tableStatement = assignTemplates(templates.createTable, {
name: tableName,
external: persistence === 'external' ? ' EXTERNAL' : '',
column_definitions: columns.join(',\n\t'),
column_definitions: columnStatements,
checkConstraints: checkConstraints.length ? ',\n\t' + checkConstraints.join(',\n\t') : '',
foreignKeyConstraints: '',
options: getTableOptions(options),
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 a0632c6

Please sign in to comment.