Skip to content

Commit 9a968d0

Browse files
chulanovskyi-bspdesmaretsivan-m-devLinsted
authored
HCK-13135: annotations (#238)
* Annotations for tables, views and columns * Hck 14981: FE annotations table level (#232) * HCK-14981: add annotations to Table level * HCK-14981: update annotation name creation logic * HCK-14981: update annotation value creation logic --------- Co-authored-by: Linsted <ivan.malenko18@gmail.com> * chore: unified annotation keywords (#233) * chore: unified annotation keywords * chore: moved to a shared place * HCK-14987: add column level annotations (#234) * HCK-14987: add column level annotations * HCK-14987: fix unnecessary space * HCK-14982: view annotations (#235) * feat: view annotations * chore: duplicated import * feat: duality view annotations (#236) * HCK-14983: add index annotations (#237) * HCK-14983: add index annotations * HCK-14983: update index template --------- Co-authored-by: Pascal Desmarets <pascal.desmarets@hackolade.com> Co-authored-by: Ivan <ivan.malenko@binary-studio.com> Co-authored-by: Linsted <ivan.malenko18@gmail.com>
1 parent 8d338f8 commit 9a968d0

11 files changed

Lines changed: 3274 additions & 2570 deletions

File tree

forward_engineering/ddlProvider/ddlHelpers/dualityViewFeHelper/abstractDualityViewDdlCreator.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { getViewName } = require('../../../utils/general');
22
const { DualityViewPropertiesType } = require('../../../enums/DualityViewPropertiesType');
3+
const { getAnnotationsString } = require('../../../utils/getAnnotationsString');
34

45
/**
56
* @abstract
@@ -176,9 +177,10 @@ class AbstractDualityViewFeDdlCreator {
176177

177178
/**
178179
* @param createViewDto {CreateDualityViewDto}
180+
* @param prepareName {Function}
179181
* @return {string}
180182
* */
181-
getCreateJsonRelationalDualityViewHeadingDdl(createViewDto) {
183+
getCreateJsonRelationalDualityViewHeadingDdl(createViewDto, prepareName) {
182184
const { jsonSchema, view } = createViewDto;
183185
const template = this._ddlTemplates?.dualityView?.createJsonRelationalDualityViewHeading || '';
184186

@@ -187,12 +189,14 @@ class AbstractDualityViewFeDdlCreator {
187189
const editionableStatement = this._getEditionableStatement(jsonSchema);
188190
const viewName = getViewName(view);
189191
const ddlViewName = this._getNamePrefixedWithSchemaName(viewName, view.schemaName);
192+
const annotations = getAnnotationsString(prepareName)(view.viewAnnotations);
190193

191194
const params = {
192195
orReplaceStatement,
193196
forceStatement,
194197
editionableStatement,
195198
viewName: ddlViewName,
199+
annotations: annotations ? `\n\t${annotations}` : '',
196200
};
197201
return this._assignTemplates(template, params);
198202
}
@@ -210,7 +214,7 @@ class AbstractDualityViewFeDdlCreator {
210214
* @return {string}
211215
* */
212216
convertDualityViewToDdl(createViewDto) {
213-
const heading = this.getCreateJsonRelationalDualityViewHeadingDdl(createViewDto);
217+
const heading = this.getCreateJsonRelationalDualityViewHeadingDdl(createViewDto, this._prepareName);
214218
const body = this.getDualityViewBodyDdl(createViewDto);
215219
return heading + '\n' + body + '\n';
216220
}

forward_engineering/ddlProvider/ddlHelpers/tableHelper.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
const _ = require('lodash');
2+
const { getAnnotationsString } = require('../../utils/getAnnotationsString');
23

3-
module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivated, prepareName, assignTemplates }) => {
4-
const getTableType = ({
5-
duplicated,
6-
external,
7-
immutable,
8-
sharded,
9-
temporary,
10-
temporaryType,
11-
blockchain_table_clauses,
12-
}) => {
4+
module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivated, prepareName }) => {
5+
const getTableType = ({ duplicated, immutable, sharded, temporary, temporaryType, blockchain_table_clauses }) => {
136
const blockchain = !_.isEmpty(blockchain_table_clauses);
147
switch (true) {
158
case temporary:
@@ -36,6 +29,7 @@ module.exports = ({ getColumnsList, checkAllKeysDeactivated, commentIfDeactivate
3629
{ key: 'partitioning', getValue: getPartitioning },
3730
{ key: 'selectStatement', getValue: getBasicValue('AS') },
3831
{ key: 'tableProperties', getValue: value => _.trim(value) },
32+
{ key: 'tableAnnotations', getValue: getAnnotationsString(prepareName) },
3933
]
4034
.map(config => (tableData[config.key] ? wrap(config.getValue(tableData[config.key], tableData)) : ''))
4135
.filter(Boolean)

forward_engineering/ddlProvider/ddlProvider.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
getColumnsList,
2121
prepareNameForScriptFormat,
2222
} = require('../utils/general');
23+
const { getAnnotationsString } = require('../utils/getAnnotationsString');
2324
const { assignTemplates } = require('../utils/assignTemplates');
2425
const { decorateType } = require('./ddlHelpers/columnDefinitionHelpers/decorateType');
2526
const { getNotNullConstraints } = require('../alterScript/alterScriptHelpers/columnHelpers/nonNullConstraintHelper');
@@ -65,6 +66,7 @@ module.exports = (baseProvider, options, app) => {
6566
commentIfDeactivated,
6667
prepareName,
6768
assignTemplates,
69+
wrapComment,
6870
});
6971

7072
const { getUserDefinedType, isNotPlainType } = require('./ddlHelpers/udtHelper')({
@@ -206,6 +208,7 @@ module.exports = (baseProvider, options, app) => {
206208
subtype: jsonSchema.subtype,
207209
defaultOnNull: jsonSchema.defaultOnNull,
208210
generatedDefaultValue: jsonSchema.generatedDefaultValue,
211+
columnAnnotations: jsonSchema.columnAnnotations,
209212
};
210213
},
211214

@@ -220,6 +223,8 @@ module.exports = (baseProvider, options, app) => {
220223

221224
convertColumnDefinition(columnDefinition, template = templates.columnDefinition) {
222225
const type = replaceTypeByVersion(columnDefinition.type, columnDefinition.dbVersion);
226+
const annotations = getAnnotationsString(prepareName)(columnDefinition.columnAnnotations);
227+
const finalAnnotationsClause = annotations ? ' ' + annotations : '';
223228

224229
return commentIfDeactivated(
225230
assignTemplates(template, {
@@ -228,6 +233,7 @@ module.exports = (baseProvider, options, app) => {
228233
default: getColumnDefault(columnDefinition),
229234
encrypt: getColumnEncrypt(columnDefinition),
230235
constraints: getColumnConstraints(columnDefinition),
236+
annotations: finalAnnotationsClause,
231237
}),
232238
{
233239
isActivated: columnDefinition.isActivated,
@@ -375,6 +381,7 @@ module.exports = (baseProvider, options, app) => {
375381
'description',
376382
'ifNotExist',
377383
'tableProperties',
384+
'tableAnnotations',
378385
),
379386
synonyms:
380387
tableData?.schemaData?.synonyms?.filter(synonym => synonym.synonymEntityId === jsonSchema.GUID) ||
@@ -407,6 +414,7 @@ module.exports = (baseProvider, options, app) => {
407414
tableProperties,
408415
synonyms,
409416
notNullConstraints,
417+
tableAnnotations,
410418
},
411419
isActivated,
412420
) {
@@ -471,6 +479,7 @@ module.exports = (baseProvider, options, app) => {
471479
partitioning,
472480
selectStatement,
473481
tableProperties,
482+
tableAnnotations,
474483
}),
475484
});
476485
if (usingTryCatchWrapper) {
@@ -485,7 +494,7 @@ module.exports = (baseProvider, options, app) => {
485494
},
486495

487496
hydrateIndex(indexData, tableData, schemaData) {
488-
return { ...indexData, schemaName: schemaData.schemaName };
497+
return { ...indexData, schemaName: schemaData.schemaName, indexAnnotations: indexData.indexAnnotations };
489498
},
490499

491500
createIndex(tableName, index, dbData, isParentActivated = true) {
@@ -501,6 +510,9 @@ module.exports = (baseProvider, options, app) => {
501510
const dbVersion = options.dbVersion || '';
502511
const usingTryCatchWrapper = shouldUseTryCatchIfNotExistsWrapper(dbVersion);
503512

513+
const annotations = getAnnotationsString(prepareName)(index.indexAnnotations);
514+
const finalAnnotationsClause = annotations ? ' ' + annotations : '';
515+
504516
const shouldInsertIfNotExistsStatement = index.ifNotExist && !usingTryCatchWrapper;
505517

506518
let statement = assignTemplates(templates.createIndex, {
@@ -510,6 +522,7 @@ module.exports = (baseProvider, options, app) => {
510522
keys,
511523
options: indexOptions,
512524
tableName: getNamePrefixedWithSchemaName(tableName, index.schemaName),
525+
annotations: finalAnnotationsClause,
513526
});
514527

515528
if (index.ifNotExist && usingTryCatchWrapper) {
@@ -587,6 +600,7 @@ module.exports = (baseProvider, options, app) => {
587600
dbVersion: _.get(viewData, 'schemaData.dbVersion'),
588601
},
589602
whereClause: detailsTab.whereClause,
603+
viewAnnotations: detailsTab.viewAnnotations,
590604
};
591605
},
592606

@@ -650,6 +664,8 @@ module.exports = (baseProvider, options, app) => {
650664
const dbVersion = _.get(viewData, 'modelInfo.dbVersion', '');
651665
const usingTryCatchWrapper = shouldUseTryCatchIfNotExistsWrapper(dbVersion);
652666

667+
const annotations = getAnnotationsString(prepareName)(viewData.viewAnnotations);
668+
653669
let createViewDdl = assignTemplates(templates.createView, {
654670
name: viewName,
655671
ifNotExists: !usingTryCatchWrapper && viewData.ifNotExist ? ' IF NOT EXISTS' : '',
@@ -660,6 +676,7 @@ module.exports = (baseProvider, options, app) => {
660676
viewProperties: viewData.viewProperties ? ' \n' + tab(viewData.viewProperties) : '',
661677
sharing: viewData.sharing && !viewData.materialized ? ` SHARING=${viewData.sharing}` : '',
662678
selectStatement,
679+
annotations: annotations ? `\n\t${annotations}` : '',
663680
});
664681
if (usingTryCatchWrapper) {
665682
createViewDdl = wrapIfNotExists(createViewDdl, viewData.ifNotExist);

forward_engineering/ddlProvider/templates.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
createTableProps:
99
'${columnDefinitions}${keyConstraints}${checkConstraints}${foreignKeyConstraints}${notNullConstraints}',
1010

11-
columnDefinition: '${name}${type}${default}${encrypt}${constraints}',
11+
columnDefinition: '${name}${type}${default}${encrypt}${constraints}${annotations}',
1212

1313
createKeyConstraint: '${constraintName}${keyType}${columns}${options}',
1414

@@ -20,7 +20,7 @@ module.exports = {
2020
createForeignKey:
2121
'ALTER TABLE ${foreignTable} ADD CONSTRAINT ${name} FOREIGN KEY (${foreignKey}) REFERENCES ${primaryTable} (${primaryKey})${onDelete};',
2222

23-
createIndex: `CREATE$\{indexType} INDEX$\{ifNotExists}$\{name} ON $\{tableName}$\{keys}$\{options};\n`,
23+
createIndex: 'CREATE${indexType} INDEX${ifNotExists}${name} ON ${tableName}${keys}${options}${annotations};\n',
2424

2525
dropIndex: 'DROP INDEX ${name};',
2626

@@ -29,7 +29,7 @@ module.exports = {
2929
alterIndexRebuild: 'ALTER INDEX ${name} REBUILD ${options};',
3030

3131
createView:
32-
'CREATE${orReplace}${force}${viewType}${materialized} VIEW${ifNotExists} ${name} ${sharing}${viewProperties}\n\tAS ${selectStatement}',
32+
'CREATE${orReplace}${force}${viewType}${materialized} VIEW${ifNotExists} ${name} ${sharing}${viewProperties}${annotations}\n\tAS ${selectStatement}',
3333

3434
viewSelectStatement: 'SELECT ${keys}\n\tFROM ${tableName}',
3535

@@ -61,7 +61,7 @@ module.exports = {
6161

6262
dualityView: {
6363
createJsonRelationalDualityViewHeading:
64-
'CREATE${orReplaceStatement}${forceStatement}${editionableStatement} JSON RELATIONAL DUALITY VIEW ${viewName} AS',
64+
'CREATE${orReplaceStatement}${forceStatement}${editionableStatement} JSON RELATIONAL DUALITY VIEW ${viewName}${annotations} AS',
6565

6666
sql: {
6767
tableTagsStatement:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { wrapComment } = require('./general');
2+
3+
/**
4+
* @typedef {{
5+
* annotationName?: string
6+
* annotationValue?: string
7+
* }} Annotation
8+
*/
9+
10+
/**
11+
* Generates annotations string.
12+
* @param {function} prepareName
13+
* @returns {(annotations: Annotation[]) => string} - returns Annotations string (e.g: "\nANNOTATIONS (...)") or ''.
14+
*/
15+
const getAnnotationsString = prepareName => annotations => {
16+
if (!Array.isArray(annotations) || annotations.length === 0) {
17+
return '';
18+
}
19+
20+
const wrapValue = value => wrapComment(value);
21+
22+
const annotationsItems = annotations
23+
.filter(annotation => annotation?.annotationName?.trim())
24+
.map(annotation => {
25+
const { annotationName, annotationValue } = annotation;
26+
const name = prepareName(annotationName.trim());
27+
28+
let finalValue = '';
29+
30+
if (annotationValue !== undefined && annotationValue.trim() !== '') {
31+
finalValue = ' ' + wrapValue(annotationValue.trim());
32+
}
33+
34+
return `${name}${finalValue}`;
35+
});
36+
37+
if (annotationsItems.length > 0) {
38+
return `ANNOTATIONS (${annotationsItems.join(', ')})`;
39+
}
40+
41+
return '';
42+
};
43+
44+
module.exports = { getAnnotationsString };

lint-staged.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
* the agreement/contract under which the software has been supplied.
88
*/
99
module.exports = {
10-
'*.{js,jsx,ts,tsx,cjs,mjs}': ['prettier --write', 'npm run lint'],
11-
'*.{json,css,scss}': ['prettier --write'],
10+
'*.{js,jsx,ts,tsx,cjs,mjs}': ['prettier --write', 'npm run lint'],
11+
'*.{json,css,scss}': ['prettier --write'],
1212
};

0 commit comments

Comments
 (0)