Skip to content

Commit 37c5d40

Browse files
HCK-14683: resolve types (#176)
* fix: hydrate column JSON schema to resolve types * chore: untangled dependencies, applied sonar remarks --------- Co-authored-by: Alik <alik.rahmonov@gmail.com>
1 parent 4e6180c commit 37c5d40

30 files changed

+2451
-2478
lines changed

forward_engineering/dbtProvider.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class DbtProvider {
5252
* @returns {boolean}
5353
*/
5454
hasType(type) {
55-
return Object.keys(types).map(toLower).includes(toLower(type));
55+
return Object.keys(types)
56+
.map(element => toLower(element))
57+
.includes(toLower(type));
5658
}
5759

5860
/**

forward_engineering/ddlProvider.js

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,62 @@
1+
const _ = require('lodash');
12
const defaultTypes = require('./configs/defaultTypes');
23
const types = require('./configs/types');
34
const templates = require('./configs/templates');
45
const { commentIfDeactivated } = require('./helpers/commentIfDeactivated');
56
const { joinActivatedAndDeactivatedStatements } = require('./utils/joinActivatedAndDeactivatedStatements');
6-
7-
module.exports = (baseProvider, options, app) => {
8-
const _ = app.require('lodash');
9-
const { assignTemplates } = app.require('@hackolade/ddl-fe-utils');
10-
const { checkAllKeysDeactivated, divideIntoActivatedAndDeactivated, getEntityName } =
11-
app.require('@hackolade/ddl-fe-utils').general;
12-
const { wrapInBrackets } = require('./utils/general')(_);
13-
14-
const { decorateType, getIdentity, getEncryptedWith, getColumnsComments, canHaveIdentity } =
15-
require('./helpers/columnDefinitionHelper')(app);
16-
const {
17-
createIndex,
18-
hydrateIndex,
19-
getMemoryOptimizedIndexes,
20-
createMemoryOptimizedIndex,
21-
hydrateTableIndex,
22-
createTableIndex,
23-
} = require('./helpers/indexHelper')(app);
24-
const {
25-
getTableName,
26-
getTableOptions,
27-
hasType,
28-
foreignKeysToString,
29-
checkIndexActivated,
30-
getDefaultValue,
31-
getTempTableTime,
32-
foreignActiveKeysToString,
33-
additionalPropertiesForForeignKey,
34-
getFKConstraintName,
35-
} = require('./helpers/general')(app);
36-
const keyHelper = require('./helpers/keyHelper')(app);
37-
const { getTerminator } = require('./helpers/optionsHelper');
38-
const { createPKConstraint, createUKConstraint, createDefaultConstraint, generateConstraintsString } =
39-
require('./helpers/constraintsHelper')(app);
40-
const { wrapIfNotExistSchema, wrapIfNotExistDatabase, wrapIfNotExistTable, wrapIfNotExistView } =
41-
require('./helpers/ifNotExistStatementHelper')(app);
42-
const { getPartitionedTables, getCreateViewData } = require('./helpers/viewHelper')(app);
43-
const { getFullTableName, escapeSpecialCharacters, wrapInBracketsIfNecessary } = require('./utils/general')(_);
44-
7+
const { assignTemplates } = require('./utils/assignTemplates');
8+
const {
9+
wrapInBrackets,
10+
escapeSpecialCharacters,
11+
wrapInBracketsIfNecessary,
12+
checkAllKeysDeactivated,
13+
divideIntoActivatedAndDeactivated,
14+
getEntityName,
15+
} = require('./utils/general');
16+
const {
17+
decorateType,
18+
getIdentity,
19+
getEncryptedWith,
20+
getColumnsComments,
21+
canHaveIdentity,
22+
} = require('./helpers/columnDefinitionHelper');
23+
const {
24+
createIndex,
25+
hydrateIndex,
26+
getMemoryOptimizedIndexes,
27+
createMemoryOptimizedIndex,
28+
hydrateTableIndex,
29+
createTableIndex,
30+
} = require('./helpers/indexHelper');
31+
const {
32+
getTableName,
33+
getTableOptions,
34+
hasType,
35+
foreignKeysToString,
36+
checkIndexActivated,
37+
getDefaultValue,
38+
getTempTableTime,
39+
foreignActiveKeysToString,
40+
additionalPropertiesForForeignKey,
41+
getFKConstraintName,
42+
} = require('./helpers/general');
43+
const keyHelper = require('./helpers/keyHelper');
44+
const { getTerminator } = require('./helpers/optionsHelper');
45+
const {
46+
createPKConstraint,
47+
createUKConstraint,
48+
createDefaultConstraint,
49+
generateConstraintsString,
50+
} = require('./helpers/constraintsHelper');
51+
const {
52+
wrapIfNotExistSchema,
53+
wrapIfNotExistDatabase,
54+
wrapIfNotExistTable,
55+
wrapIfNotExistView,
56+
} = require('./helpers/ifNotExistStatementHelper');
57+
const { getPartitionedTables, getCreateViewData } = require('./helpers/viewHelper');
58+
59+
const ddlProvider = (baseProvider, options, app) => {
4560
const terminator = getTerminator(options);
4661

4762
return {
@@ -206,9 +221,9 @@ module.exports = (baseProvider, options, app) => {
206221
? ` MASKED WITH (FUNCTION='${columnDefinition.maskedWithFunction}')`
207222
: '';
208223
const identityContainer = columnDefinition.identity && { identity: getIdentity(columnDefinition.identity) };
209-
const encryptedWith = !_.isEmpty(columnDefinition.encryption)
210-
? getEncryptedWith(columnDefinition.encryption[0])
211-
: '';
224+
const encryptedWith = _.isEmpty(columnDefinition.encryption)
225+
? ''
226+
: getEncryptedWith(columnDefinition.encryption[0]);
212227
const unique = columnDefinition.unique
213228
? ' ' + createUKConstraint(templates, terminator, true)(columnDefinition.uniqueKeyOptions).statement
214229
: '';
@@ -250,7 +265,7 @@ module.exports = (baseProvider, options, app) => {
250265
createIndex(tableName, index, dbData, isParentActivated = true) {
251266
const isActivated = checkIndexActivated(index);
252267
if (!isParentActivated) {
253-
return createTableIndex(terminator, tableName, index, isActivated && isParentActivated);
268+
return createTableIndex(terminator, tableName, index, isActivated);
254269
}
255270
return createTableIndex(terminator, tableName, index, isActivated && isParentActivated);
256271
},
@@ -446,6 +461,15 @@ module.exports = (baseProvider, options, app) => {
446461
return hasType(type);
447462
},
448463

464+
hydrateJsonSchemaColumn(jsonSchema, definitionJsonSchema) {
465+
if (!jsonSchema.$ref || _.isEmpty(definitionJsonSchema)) {
466+
return jsonSchema;
467+
}
468+
469+
jsonSchema = _.omit(jsonSchema, '$ref');
470+
return { ...definitionJsonSchema, ...jsonSchema };
471+
},
472+
449473
hydrateColumn({ columnDefinition, jsonSchema, schemaData, parentJsonSchema }) {
450474
let encryption = [];
451475

@@ -476,7 +500,8 @@ module.exports = (baseProvider, options, app) => {
476500
const isTempTableEndTimeColumnHidden =
477501
_.get(parentJsonSchema, 'periodForSystemTime[0].startTime[0].type', '') === 'hidden';
478502

479-
return Object.assign({}, columnDefinition, {
503+
return {
504+
...columnDefinition,
480505
defaultConstraint: {
481506
name: jsonSchema.defaultConstraintName,
482507
value: columnDefinition.default,
@@ -513,7 +538,7 @@ module.exports = (baseProvider, options, app) => {
513538
increment: Number(_.get(jsonSchema, 'identity.identityIncrement', 0)),
514539
},
515540
}),
516-
});
541+
};
517542
},
518543

519544
hydrateIndex(indexData, tableData, schemaData) {
@@ -557,7 +582,8 @@ module.exports = (baseProvider, options, app) => {
557582
idToNameHashTable[_.get(jsonSchema, 'periodForSystemTime[0].startTime[0].keyId', '')];
558583
const temporalTableTimeEndColumnName =
559584
idToNameHashTable[_.get(jsonSchema, 'periodForSystemTime[0].endTime[0].keyId', '')];
560-
return Object.assign({}, tableData, {
585+
return {
586+
...tableData,
561587
foreignKeyConstraints: tableData.foreignKeyConstraints || [],
562588
keyConstraints: keyHelper.getTableKeyConstraints({ jsonSchema }),
563589
ifNotExist: jsonSchema.ifNotExist,
@@ -587,7 +613,7 @@ module.exports = (baseProvider, options, app) => {
587613
memoryOptimizedIndexes: isMemoryOptimized
588614
? getMemoryOptimizedIndexes(entityData, tableData.schemaData)
589615
: [],
590-
});
616+
};
591617
},
592618

593619
hydrateViewColumn(data) {
@@ -1071,3 +1097,5 @@ module.exports = (baseProvider, options, app) => {
10711097
},
10721098
};
10731099
};
1100+
1101+
module.exports = ddlProvider;

0 commit comments

Comments
 (0)