Skip to content

Commit

Permalink
HCK-7296 (#123)
Browse files Browse the repository at this point in the history
* HCK-7296: Update config & clean up

* HCK-7296: remove redundant code

* HCK-7296: update comments logic

* HCK-7296: fix sonarqube warnings

* HCK-7296: fix comments

* HCK-7296: Update comments formatting
  • Loading branch information
Nightlngale authored Jul 30, 2024
1 parent 66b9fff commit 07a61ec
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 159 deletions.
5 changes: 3 additions & 2 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const _ = require('lodash');
const { commentDropStatements } = require('./helpers/commentDropStatements');
const { commentDropStatements } = require('./helpers/commentHelpers/commentDropStatements');
const { DROP_STATEMENTS } = require('./helpers/constants');

module.exports = {
Expand All @@ -18,7 +18,8 @@ module.exports = {
);
}

const script = getAlterScript({ collection, _, ddlProvider, app });
const scriptFormat = _.get(data, 'options.targetScriptOptions.keyword');
const script = getAlterScript({ scriptFormat, collection, ddlProvider, app });

const applyDropStatements = data.options?.additionalOptions?.some(
option => option.id === 'applyDropStatements' && option.value,
Expand Down
14 changes: 14 additions & 0 deletions forward_engineering/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"namePrefix": "Snowflake",
"resolvedDefinitions": true,
"combinedContainers": true,
"options": [
{
"name": "SnowSight",
"keyword": "snowSight",
"disableApplyScriptToInstance": false,
"supportsDeltaModel": true
},
{
"name": "Classic UI",
"keyword": "classicUI",
"disableApplyScriptToInstance": false,
"supportsDeltaModel": true
}
],
"level": {
"container": true,
"entity": true,
Expand Down
36 changes: 26 additions & 10 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require('lodash');
const defaultTypes = require('./configs/defaultTypes');
const types = require('./configs/types');
const templates = require('./configs/templates');
const { LANGUAGES } = require('./helpers/constants');
const { LANGUAGES, FORMATS } = require('./helpers/constants');
const {
prepareAlterSetUnsetData,
prepareContainerName,
Expand All @@ -17,13 +17,15 @@ const {
prepareCollectionFormatTypeOptions,
prepareCollectionStageCopyOptions,
} = require('./helpers/alterScriptHelpers/common');
const { escapeString } = require('./utils/escapeString');

const DEFAULT_SNOWFLAKE_SEQUENCE_START = 1;
const DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT = 1;

module.exports = (baseProvider, options, app) => {
const assignTemplates = app.require('@hackolade/ddl-fe-utils').assignTemplates;
const { tab, hasType, clean } = app.require('@hackolade/ddl-fe-utils').general;
const scriptFormat = options?.targetScriptOptions?.keyword || FORMATS.SNOWSIGHT;

const keyHelper = require('./helpers/keyHelper')(app);
const { getFileFormat, getCopyOptions, addOptions, getAtOrBefore, mergeKeys } =
Expand All @@ -48,7 +50,7 @@ module.exports = (baseProvider, options, app) => {
require('./helpers/columnDefinitionHelper')(app);

const { generateConstraint } = require('./helpers/constraintHelper')(app);
const { commentIfDeactivated } = require('./helpers/commentDeactivatedHelper');
const { commentIfDeactivated } = require('./helpers/commentHelpers/commentDeactivatedHelper');

const {
getAlterSchemaName,
Expand Down Expand Up @@ -92,8 +94,15 @@ module.exports = (baseProvider, options, app) => {
return !_.isEmpty(constraints) ? ',\n\t\t' + constraints.join(',\n\t\t') : '';
};

function insertNewlinesAtEdges(input) {
input = input.replace(/^(\$\$|')/, match => match + '\n');
input = input.replace(/(\$\$|')$/, match => '\n\t' + match);

return input;
}

const getOrReplaceStatement = isEnabled => (isEnabled ? ' OR REPLACE' : '');
const getBodyStatement = body => (body ? `\n\t$$\n${body}\n\t$$` : '');
const getBodyStatement = body => (body ? `\n\t${insertNewlinesAtEdges(escapeString(scriptFormat, body))}` : '');
const getCommentsStatement = text => (text ? `\n\tCOMMENT = '${text}'` : '');
const getNotNullStatement = isEnabled => (isEnabled ? '\n\tNOT NULL' : '');
const getIfNotExistStatement = ifNotExist => (ifNotExist ? ' IF NOT EXISTS' : '');
Expand All @@ -119,7 +128,7 @@ module.exports = (baseProvider, options, app) => {
const dataRetentionStatement =
!isNaN(dataRetention) && dataRetention ? `\n\tDATA_RETENTION_TIME_IN_DAYS=${dataRetention}` : '';
const managedAccessStatement = managedAccess ? '\n\tWITH MANAGED ACCESS' : '';
const commentStatement = comment ? `\n\tCOMMENT=$$${comment}$$` : '';
const commentStatement = comment ? `\n\tCOMMENT=${escapeString(scriptFormat, comment)}` : '';
const currentSchemaName = getName(isCaseSensitive, schemaName);
const currentDatabaseName = getName(isCaseSensitive, databaseName);
const fullName = getFullName(currentDatabaseName, currentSchemaName);
Expand Down Expand Up @@ -290,7 +299,7 @@ module.exports = (baseProvider, options, app) => {
: mergeKeys(tableData.partitioningKey)) +
')'
: '';
const comment = tableData.comment ? ` COMMENT=$$${tableData.comment}$$` : '';
const comment = tableData.comment ? ` COMMENT=${escapeString(scriptFormat, tableData.comment)}` : '';
const copyGrants = tableData.copyGrants ? ` COPY GRANTS` : '';
const dataRetentionTime = tableData.dataRetentionTime
? ` DATA_RETENTION_TIME_IN_DAYS=${tableData.dataRetentionTime}`
Expand Down Expand Up @@ -402,13 +411,20 @@ module.exports = (baseProvider, options, app) => {
type: decorateType(columnDefinition.type, columnDefinition),
collation: getCollation(columnDefinition.type, columnDefinition.collation),
default: !_.isUndefined(columnDefinition.default)
? ' DEFAULT ' + getDefault(columnDefinition.type, columnDefinition.default)
? ' DEFAULT ' +
getDefault({
scriptFormat,
type: columnDefinition.type,
defaultValue: columnDefinition.default,
})
: '',
autoincrement: getAutoIncrement(columnDefinition.type, 'AUTOINCREMENT', columnDefinition.autoincrement),
identity: getAutoIncrement(columnDefinition.type, 'IDENTITY', columnDefinition.identity),
not_nul: !columnDefinition.nullable ? ' NOT NULL' : '',
inline_constraint: getInlineConstraint(columnDefinition),
comment: columnDefinition.comment ? ` COMMENT $$${columnDefinition.comment}$$` : '',
comment: columnDefinition.comment
? ` COMMENT ${escapeString(scriptFormat, columnDefinition.comment)}`
: '',
tag: getTagStatement({
tags: columnDefinition.columnTags,
isCaseSensitive: columnDefinition.isCaseSensitive,
Expand Down Expand Up @@ -520,7 +536,7 @@ module.exports = (baseProvider, options, app) => {
name: getFullName(schemaName, viewData.name),
column_list: viewColumnsToString(columnList, isActivated),
copy_grants: viewData.copyGrants ? 'COPY GRANTS\n' : '',
comment: viewData.comment ? 'COMMENT=$$' + viewData.comment + '$$\n' : '',
comment: viewData.comment ? 'COMMENT=' + escapeString(scriptFormat, viewData.comment) + '\n' : '',
select_statement: selectStatement,
tag: tagStatement ? tagStatement + '\n' : '',
});
Expand Down Expand Up @@ -645,7 +661,7 @@ module.exports = (baseProvider, options, app) => {
start: sequence.sequenceStart || DEFAULT_SNOWFLAKE_SEQUENCE_START,
increment: sequence.sequenceIncrement || DEFAULT_SNOWFLAKE_SEQUENCE_INCREMENT,
comment: sequence.sequenceComments
? ` COMMENT=$$${sequence.sequenceComments}$$`
? ` COMMENT=${escapeString(scriptFormat, sequence.sequenceComments)}`
: '',
}),
)
Expand All @@ -661,7 +677,7 @@ module.exports = (baseProvider, options, app) => {
getFormatTypeOptions(fileFormat.fileFormat, fileFormat.formatTypeOptions),
),
comment: fileFormat.fileFormatComments
? ` COMMENT=$$${fileFormat.fileFormatComments}$$`
? ` COMMENT=${escapeString(scriptFormat, fileFormat.fileFormatComments)}`
: '',
}),
)
Expand Down
11 changes: 5 additions & 6 deletions forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const _ = require('lodash');
const {
getAddContainerScript,
getDeleteContainerScript,
Expand Down Expand Up @@ -34,7 +33,7 @@ const getAlterContainersScripts = (collection, ddlProvider, app) => {
return { addedContainerScripts, deletedContainerScripts, modifiedContainerScripts };
};

const getAlterCollectionsScripts = (collection, ddlProvider, app) => {
const getAlterCollectionsScripts = ({ collection, ddlProvider, app, scriptFormat }) => {
const getCollectionScripts = (items, compMode, getScript) =>
items.filter(item => item.compMod?.[compMode]).map(getScript);

Expand All @@ -43,7 +42,7 @@ const getAlterCollectionsScripts = (collection, ddlProvider, app) => {
const addedCollectionScripts = getCollectionScripts(
getItems(collection, 'entities', 'added', 'values'),
'created',
getAddCollectionScript(ddlProvider, app),
getAddCollectionScript({ ddlProvider, app, scriptFormat }),
);
const deletedCollectionScripts = getCollectionScripts(
getItems(collection, 'entities', 'deleted', 'values'),
Expand All @@ -59,7 +58,7 @@ const getAlterCollectionsScripts = (collection, ddlProvider, app) => {

const addedColumnScripts = getColumnScripts(
getItems(collection, 'entities', 'added', 'values'),
getAddColumnScript(ddlProvider, app),
getAddColumnScript({ ddlProvider, app, scriptFormat }),
);
const deletedColumnScripts = getColumnScripts(
getItems(collection, 'entities', 'deleted', 'values'),
Expand Down Expand Up @@ -131,9 +130,9 @@ const getAlterTagsScripts = ({ collection, ddlProvider, app }) => {
return { addedTagsScripts, deletedTagsScripts, modifiedTagsScripts };
};

const getAlterScript = ({ collection, ddlProvider, app }) => {
const getAlterScript = ({ scriptFormat, collection, ddlProvider, app }) => {
const script = {
...getAlterCollectionsScripts(collection, ddlProvider, app),
...getAlterCollectionsScripts({ collection, ddlProvider, app, scriptFormat }),
...getAlterContainersScripts(collection, ddlProvider, app),
...getAlterViewsScripts({ schema: collection, ddlProvider, app }),
...getAlterTagsScripts({ collection, ddlProvider, app }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const _ = require('lodash');
const { prepareContainerLevelData } = require('./common');

const getAddContainerScript = (ddlProvider, app) => container => {
const { getDbName } = require('../general')(app);
const containerData = { ...container.role, name: getDbName(container.role) };
const containerLevelData = prepareContainerLevelData(containerData);
const hydratedContainer = ddlProvider.hydrateSchema(containerData, containerLevelData);
const script = ddlProvider.createSchema(hydratedContainer);
return script;

return ddlProvider.createSchema(hydratedContainer);
};

const getDeleteContainerScript = ddlProvider => container => {
const { name } = ddlProvider.hydrateForDeleteSchema({ ...container, ...container.role });

return ddlProvider.dropSchema({ name });
};

Expand Down
119 changes: 63 additions & 56 deletions forward_engineering/helpers/alterScriptHelpers/alterEntityHelper.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
const _ = require('lodash');
const { checkFieldPropertiesChanged, getNames, getBaseAndContainerNames } = require('./common');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition');
const { commentIfDeactivated } = require('../commentDeactivatedHelper');

const getAddCollectionScript = (ddlProvider, app) => collection => {
const { getEntityName, getName } = require('../general')(app);
const { commentIfDeactivated } = require('../commentHelpers/commentDeactivatedHelper');

const getAddCollectionScript =
({ ddlProvider, app, scriptFormat }) =>
collection => {
const { getEntityName, getName } = require('../general')(app);

const { schemaName, databaseName } = getBaseAndContainerNames(collection, getName);
const jsonSchema = {
...collection,
...(_.omit(collection?.role, 'properties') || {}),
};
const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) =>
createColumnDefinitionBySchema({
name,
jsonSchema: column,
parentJsonSchema: jsonSchema,
ddlProvider,
scriptFormat,
}),
);

const { schemaName, databaseName } = getBaseAndContainerNames(collection, getName);
const jsonSchema = {
...collection,
...(_.omit(collection?.role, 'properties') || {}),
};
const columnDefinitions = _.toPairs(jsonSchema.properties).map(([name, column]) =>
createColumnDefinitionBySchema({
name,
jsonSchema: column,
parentJsonSchema: jsonSchema,
ddlProvider,
}),
);

const tableData = {
name: getEntityName(jsonSchema),
columns: columnDefinitions.map(ddlProvider.convertColumnDefinition),
foreignKeyConstraints: [],
schemaData: { schemaName, databaseName },
columnDefinitions,
const tableData = {
name: getEntityName(jsonSchema),
columns: columnDefinitions.map(ddlProvider.convertColumnDefinition),
foreignKeyConstraints: [],
schemaData: { schemaName, databaseName },
columnDefinitions,
};
const hydratedTable = ddlProvider.hydrateTable({
tableData,
entityData: [jsonSchema],
jsonSchema,
});

return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated);
};
const hydratedTable = ddlProvider.hydrateTable({
tableData,
entityData: [jsonSchema],
jsonSchema,
});

return ddlProvider.createTable(hydratedTable, jsonSchema.isActivated);
};

const getDeleteCollectionScript = app => collection => {
const { getEntityName, getFullName, getName } = require('../general')(app);
Expand All @@ -55,31 +58,35 @@ const getModifyCollectionScript = ddlProvider => collection => {
return ddlProvider.alterTable(data);
};

const getAddColumnScript = (ddlProvider, app) => collection => {
const { getEntityName, getFullName, getName } = require('../general')(app);

const collectionSchema = {
...collection,
...(_.omit(collection?.role, 'properties') || {}),
const getAddColumnScript =
({ ddlProvider, app, scriptFormat }) =>
collection => {
const { getEntityName, getFullName, getName } = require('../general')(app);

const collectionSchema = {
...collection,
...(_.omit(collection?.role, 'properties') || {}),
};
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));

return _.toPairs(collection.properties)
.filter(([_, jsonSchema]) => !jsonSchema.compMod)
.map(([name, jsonSchema]) =>
createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
scriptFormat,
}),
)
.map(ddlProvider.convertColumnDefinition)
.map(
column =>
`ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, column)};`,
);
};
const { schemaName, databaseName, tableName } = getNames(collectionSchema, getName, getEntityName);
const fullName = getFullName(databaseName, getFullName(schemaName, tableName));

return _.toPairs(collection.properties)
.filter(([_, jsonSchema]) => !jsonSchema.compMod)
.map(([name, jsonSchema]) =>
createColumnDefinitionBySchema({
name,
jsonSchema,
parentJsonSchema: collectionSchema,
ddlProvider,
}),
)
.map(ddlProvider.convertColumnDefinition)
.map(
column => `ALTER TABLE IF EXISTS ${fullName} ADD COLUMN ${commentIfDeactivated(column.statement, column)};`,
);
};

const getDeleteColumnScript = app => collection => {
const { getEntityName, getFullName, getName } = require('../general')(app);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash');
const { escapeString } = require('../../utils/escapeString');

module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) => {
const getSchemaFullName = (database, schemaName, isCaseSensitive) => {
Expand All @@ -10,13 +11,15 @@ module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) =>

const getAlterSchemaScript = ({ database, isCaseSensitive, newName } = {}) => {
const schemaFullName = getSchemaFullName(database, newName, isCaseSensitive);

return assignTemplates(templates.alterSchemaScript, { name: schemaFullName });
};

const getAlterEntityScript = (template, { database, isCaseSensitive, newName, schemaName } = {}) => {
const schemaFullName = getSchemaFullName(database, schemaName, isCaseSensitive);
const tableName = getName(isCaseSensitive, newName);
const tableFullName = getFullName(schemaFullName, tableName);

return assignTemplates(template, { name: tableFullName });
};

Expand Down Expand Up @@ -47,7 +50,10 @@ module.exports = ({ getName, getFullName, templates, assignTemplates, tab }) =>
}

const setPropertyData = Object.keys(setProperty).map((key, index) => {
const value = key === 'description' ? `$$${setProperty[key]}$$` : setProperty[key];
const propValue = setProperty[key];
const scriptFormat = _.get(data, 'options.targetScriptOptions.keyword');

const value = key === 'description' ? escapeString(scriptFormat, propValue) : propValue;
key = key === 'description' ? 'COMMENT' : key;
const statement = `${key} = ${value}`;

Expand Down
Loading

0 comments on commit 07a61ec

Please sign in to comment.