Skip to content

Commit

Permalink
HCK-8003: add comma commenting for last activated column statement
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-filonenko committed Sep 18, 2024
1 parent 1a35818 commit 64983ba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
8 changes: 6 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');

module.exports = (baseProvider, options, app) => {
const _ = app.require('lodash');
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -215,6 +217,8 @@ module.exports = (baseProvider, options, app) => {
temporalTableTime,
...identityContainer,
});

return commentIfDeactivated(statement, { isActivated: columnDefinition.isActivated });
},

createIndex(tableName, index, dbData, isParentActivated = true) {
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 64983ba

Please sign in to comment.