Skip to content

Commit

Permalink
HCK-8896: Code improvements (#242)
Browse files Browse the repository at this point in the history
* HCK-8896: Move view script creation to the provider

* HCK-8896: Refactor functions

* HCK-8896: fix script

* HCK-8896: fix script
  • Loading branch information
Nightlngale authored Dec 5, 2024
1 parent 504e092 commit e7b7711
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 83 deletions.
7 changes: 5 additions & 2 deletions forward_engineering/alterScript/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,11 @@ const getAlterViewsScriptDtos = (schema, provider, _, dbVersion) => {
.map(view => ({ ...view, ...(view.role || {}) }))
.filter(view => !view.compMod?.created && !view.compMod?.deleted)
.flatMap(getScript);

const addedViewScriptDtos = getViewScripts(getItems(schema, 'views', 'added'), 'created', getAddViewsScripts(_));
const addedViewScriptDtos = getViewScripts(
getItems(schema, 'views', 'added'),
'created',
getAddViewsScripts(provider, _),
);
const deletedViewScriptDtos = getViewScripts(
getItems(schema, 'views', 'deleted'),
'deleted',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { getViewScript } = require('../../helpers/viewHelper');
const { hydrateTableProperties } = require('../../helpers/tableHelper');
const {
getEntityData,
Expand Down Expand Up @@ -87,9 +86,10 @@ const hydrateAlterView = _ => view => {
/**
* @return {(view: Object) => AlterScriptDto}
* */
const getAddViewsScripts = _ => view => {
const getAddViewsScripts = (provider, _) => view => {
const hydratedView = hydrateView(_)(view);
const script = getViewScript({ _, ...hydratedView });
const script = provider.createView(hydratedView);

return {
isActivated: true,
scripts: [
Expand Down Expand Up @@ -137,7 +137,7 @@ const getModifyViewsScripts = (provider, _, dbVersion) => view => {
}
const dropViewScript = provider.dropView(viewName);
const hydratedView = hydrateView(_)(view);
const addViewScript = getViewScript({ _, ...hydratedView });
const addViewScript = provider.createView(hydratedView);
const dropViewScriptDto = AlterScriptDto.getInstance([dropViewScript], true, true);
const addViewScriptDto = AlterScriptDto.getInstance([addViewScript], true, false);

Expand Down
6 changes: 2 additions & 4 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { getDatabaseStatement, getUseCatalogStatement } = require('./helpers/databaseHelper');
const { getViewScript } = require('./helpers/viewHelper');
const { getCleanedUrl, buildScript, isSupportUnityCatalog, getDBVersionNumber } = require('./utils/general');
const fetchRequestHelper = require('../reverse_engineering/helpers/fetchRequestHelper');
const databricksHelper = require('../reverse_engineering/helpers/databricksHelper');
Expand Down Expand Up @@ -259,16 +258,15 @@ module.exports = {
* */
generateViewScript(data, logger, callback, app) {
try {
const _ = app.require('lodash');
const provider = require('./ddlProvider/ddlProvider')(app);
const viewSchema = JSON.parse(data.jsonSchema || '{}');
const dbVersion = data.modelData[0].dbVersion;
const isUnityCatalogSupports = isSupportUnityCatalog(dbVersion);

const useCatalogStatement = isUnityCatalogSupports ? getUseCatalogStatement(data.containerData) : '';
const databaseStatement = getDatabaseStatement(data.containerData, isUnityCatalogSupports, dbVersion);

const script = getViewScript({
_,
const script = provider.createView({
schema: viewSchema,
viewData: data.viewData,
containerData: data.containerData,
Expand Down
65 changes: 64 additions & 1 deletion forward_engineering/ddlProvider/ddlProvider.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
const templates = require('./ddlTemplates');
const { getFullEntityName } = require('../utils/general');
const {
getFullEntityName,
getName,
replaceSpaceWithUnderscore,
prepareName,
getContainerName,
getEntityData,
getEntityProperties,
wrapInBrackets,
} = require('../utils/general');
const { getViewTagsStatement } = require('../helpers/unityTagsHelper');
const { getTablePropertiesClause } = require('../helpers/tableHelper');
const viewHelper = require('../helpers/viewHelper');

module.exports = app => {
const { assignTemplates } = app.require('@hackolade/ddl-fe-utils');
const _ = app.require('lodash');

return {
dropView(name) {
return assignTemplates(templates.dropView, { name });
},

createView(data) {
const { schema, viewData, containerData, collectionRefsDefinitionsMap } = data;

const columns = schema.properties || {};
const view = _.first(viewData) || {};

if (!view.isActivated) {
return;
}

const bucketName = replaceSpaceWithUnderscore(prepareName(viewHelper.retrieveContainerName(containerData)));
const viewName = replaceSpaceWithUnderscore(prepareName(view.code || view.name));
const isGlobal = schema.viewGlobal && schema.viewTemporary;
const isTemporary = schema.viewTemporary;
const orReplace = schema.viewOrReplace;
const ifNotExists = view.viewIfNotExist;
const name = bucketName ? `${bucketName}.${viewName}` : `${viewName}`;
const tableProperties =
schema.tableProperties && Array.isArray(schema.tableProperties)
? viewHelper.filterRedundantProperties(schema.tableProperties, ['transient_lastDdlTime'])
: [];
const viewUnityTagsStatements =
schema.unityViewTags && getViewTagsStatement({ viewSchema: schema, viewName: name });

return assignTemplates(templates.createView, {
orReplace: orReplace && !ifNotExists ? ' OR REPLACE' : '',
global: isGlobal ? ' GLOBAL' : '',
temporary: isTemporary ? ' TEMPORARY' : '',
ifNotExists: ifNotExists ? ' IF NOT EXISTS' : '',
name,
columnList: view.columnList
? `${wrapInBrackets(view.columnList)}`
: viewHelper.getDefaultColumnList(columns),
schemaBinding: '',
comment: viewHelper.getCommentStatement(schema.description),
tablePropertyStatements: tableProperties.length
? `TBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`
: '',
query: schema.selectStatement
? `AS ${schema.selectStatement}`
: viewHelper.getTableSelectStatement({
_,
collectionRefsDefinitionsMap,
columns,
}),
viewUnityTagsStatements: viewUnityTagsStatements ? `${viewUnityTagsStatements};` : '',
});
},

dropTableIndex(name) {
return name ? assignTemplates(templates.dropTableIndex, { name }) : '';
},
Expand Down
3 changes: 2 additions & 1 deletion forward_engineering/ddlProvider/ddlTemplates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
createView:
'CREATE ${orReplace}${global}${temporary} VIEW${ifNotExists} ${name}${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}',
'CREATE${orReplace}${global}${temporary} VIEW${ifNotExists} ${name} ${columnList}${schemaBinding}${comment}${tablePropertyStatements}${query};${viewUnityTagsStatements}',

dropView: 'DROP VIEW IF EXISTS ${name};',

alterViewName: 'ALTER VIEW ${oldName} RENAME TO ${newName};',
Expand Down
8 changes: 4 additions & 4 deletions forward_engineering/helpers/feScriptBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ const buildEntityLevelFEScript =
* @param _ {any}
* @return {Array<ContainerLevelEntityDto>}
* */
const getContainerLevelViewScriptDtos = (data, _) => {
const getContainerLevelViewScriptDtos = (data, provider, _) => {
return data.views
.map(viewId => {
const viewSchema = JSON.parse(data.jsonSchema[viewId] || '{}');
const viewData = data.viewData[viewId];
const viewScript = getViewScript({
_,
const viewScript = provider.createView({
schema: viewSchema,
viewData: viewData,
containerData: data.containerData,
Expand Down Expand Up @@ -295,8 +294,9 @@ const buildContainerLevelFEScriptDto =
const arePkFkConstraintsAvailable = isSupportUnityCatalog(dbVersion);
const areNotNullConstraintsAvailable = isSupportNotNullConstraints(dbVersion);

const provider = require('../ddlProvider/ddlProvider')(app);
const useCatalogStatement = arePkFkConstraintsAvailable ? getUseCatalogStatement(containerData) : '';
const viewsScriptDtos = getContainerLevelViewScriptDtos(data, _);
const viewsScriptDtos = getContainerLevelViewScriptDtos(data, provider, _);
const databaseStatement = getDatabaseStatement(containerData, arePkFkConstraintsAvailable, dbVersion);
const entityScriptDtos = await getContainerLevelEntitiesScriptDtos(
app,
Expand Down
59 changes: 8 additions & 51 deletions forward_engineering/helpers/viewHelper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict';

const { prepareName, encodeStringLiteral, commentDeactivatedStatement } = require('../utils/general');
const { getTablePropertiesClause } = require('./tableHelper');
const { getViewTagsStatement } = require('./unityTagsHelper');
const ddlTemplates = require('../ddlProvider/ddlTemplates');
const assignTemplates = require('../utils/assignTemplates');

const getColumnNames = _ => (collectionRefsDefinitionsMap, columns) => {
return _.uniq(
Expand Down Expand Up @@ -121,57 +117,18 @@ function getTableSelectStatement({ _, collectionRefsDefinitionsMap, columns }) {
return '';
}

module.exports = {
getViewScript({ _, schema, viewData, containerData, collectionRefsDefinitionsMap }) {
const columns = schema.properties || {};
const view = _.first(viewData) || {};

if (!view.isActivated) {
return;
}

const bucketName = replaceSpaceWithUnderscore(prepareName(retrieveContainerName(containerData)));
const viewName = replaceSpaceWithUnderscore(prepareName(view.code || view.name));
const isGlobal = schema.viewGlobal && schema.viewTemporary;
const isTemporary = schema.viewTemporary;
const orReplace = schema.viewOrReplace;
const ifNotExists = view.viewIfNotExist;
const name = bucketName ? `${bucketName}.${viewName}` : `${viewName}`;
const tableProperties =
schema.tableProperties && Array.isArray(schema.tableProperties)
? filterRedundantProperties(schema.tableProperties, ['transient_lastDdlTime'])
: [];
const viewUnityTagsStatements =
schema.unityViewTags && getViewTagsStatement({ viewSchema: schema, viewName: name });

return assignTemplates(ddlTemplates.createView, {
orReplace: orReplace && !ifNotExists ? 'OR REPLACE ' : '',
global: isGlobal ? 'GLOBAL ' : '',
temporary: isTemporary ? 'TEMPORARY ' : '',
ifNotExists: ifNotExists ? ' IF NOT EXISTS' : '',
name,
columnList: view.columnList ? `\n(${view.columnList})` : getDefaultColumnList(columns),
schemaBinding: '',
comment: getCommentStatement(schema.description),
tablePropertyStatements: tableProperties.length
? `\nTBLPROPERTIES (${getTablePropertiesClause(_)(tableProperties)})`
: '',
query: schema.selectStatement
? `\nAS ${schema.selectStatement}`
: getTableSelectStatement({
_,
collectionRefsDefinitionsMap,
columns,
}),
viewUnityTagsStatements: viewUnityTagsStatements ? `\n${viewUnityTagsStatements};` : '',
});
},
};

const filterRedundantProperties = (tableProperties, propertiesList) => {
if (!Array.isArray(tableProperties)) {
return tableProperties;
}

return tableProperties.filter(prop => !propertiesList.includes(prop.propertyKey));
};

module.exports = {
getTableSelectStatement,
retrieveContainerName,
filterRedundantProperties,
getDefaultColumnList,
getCommentStatement,
};
15 changes: 0 additions & 15 deletions forward_engineering/utils/assignTemplates.js

This file was deleted.

2 changes: 1 addition & 1 deletion forward_engineering/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const wrapInTicks = (str = '') => {
};

const wrapInBrackets = (str = '') => {
return /^\(\S+\)$/.test(str) ? str : `(${str})`;
return /^\(.*\)$/.test(str) ? str : `(${str})`;
};

/**
Expand Down

0 comments on commit e7b7711

Please sign in to comment.