Skip to content
9 changes: 4 additions & 5 deletions forward_engineering/generateContainerScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { buildScript } = require('./helpers/buildScript');
const { parseEntities } = require('./helpers/parseEntities');
const { getWorkloadManagementStatements } = require('./helpers/getWorkloadManagementStatements');
const { getIsPkOrFkConstraintAvailable, getIsConstraintAvailable } = require('./helpers/constraintHelper');
const { setMinify } = require('./helpers/generalHelper');

const sortEntitiesByForeignKeyDependencies = ({ entities, relationships }) => {
const entitySet = new Set(entities);
Expand Down Expand Up @@ -64,11 +65,12 @@ const generateContainerScript = (data, logger, callback, app) => {
const areColumnConstraintsAvailable = getIsConstraintAvailable(data);
const isPkOrFkConstraintAvailable = getIsPkOrFkConstraintAvailable(data);
const needMinify = _.get(data, 'options.additionalOptions', []).find(option => option.id === 'minify')?.value;
setMinify(needMinify);

if (data.isUpdateScript) {
const deltaModelSchema = _.first(Object.values(jsonSchema)) || {};
const definitions = [modelDefinitions, internalDefinitions, externalDefinitions];
const scripts = getAlterScript(deltaModelSchema, definitions, data, app, needMinify);
const scripts = getAlterScript(deltaModelSchema, definitions, data, app);
callback(null, scripts);
return;
}
Expand Down Expand Up @@ -117,10 +119,7 @@ const generateContainerScript = (data, logger, callback, app) => {
]);
}, []);

callback(
null,
buildScript(needMinify)(...workloadManagementStatements, databaseStatement, ...entities, ...viewsScripts),
);
callback(null, buildScript(...workloadManagementStatements, databaseStatement, ...entities, ...viewsScripts));
} catch (e) {
logger.log('error', { message: e.message, stack: e.stack }, 'Hive Forward-Engineering Error');

Expand Down
6 changes: 4 additions & 2 deletions forward_engineering/generateScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { getTableStatement } = require('./helpers/tableHelper');
const { getIndexes } = require('./helpers/indexHelper');
const { buildScript } = require('./helpers/buildScript');
const { getIsPkOrFkConstraintAvailable, getIsConstraintAvailable } = require('./helpers/constraintHelper');
const { setMinify } = require('./helpers/generalHelper');

const generateScript = (data, logger, callback, app) => {
try {
Expand All @@ -17,17 +18,18 @@ const generateScript = (data, logger, callback, app) => {
const areColumnConstraintsAvailable = getIsConstraintAvailable(data);
const isPkOrFkConstraintAvailable = getIsPkOrFkConstraintAvailable(data);
const needMinify = _.get(data, 'options.additionalOptions', []).find(option => option.id === 'minify')?.value;
setMinify(needMinify);

if (data.isUpdateScript) {
const definitions = [modelDefinitions, internalDefinitions, externalDefinitions];
const scripts = getAlterScript(jsonSchema, definitions, data, app, needMinify);
const scripts = getAlterScript(jsonSchema, definitions, data, app);
callback(null, scripts);
return;
}

callback(
null,
buildScript(needMinify)(
buildScript(
getDatabaseStatement(containerData),
getTableStatement(
containerData,
Expand Down
4 changes: 3 additions & 1 deletion forward_engineering/generateViewScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const _ = require('lodash');
const { getDatabaseStatement } = require('./helpers/databaseHelper');
const { getViewScript } = require('./helpers/viewHelper');
const { buildScript } = require('./helpers/buildScript');
const { setMinify } = require('./helpers/generalHelper');

const generateViewScript = (data, logger, callback, app) => {
try {
const viewSchema = JSON.parse(data.jsonSchema || '{}');
const needMinify = _.get(data, 'options.additionalOptions', []).find(option => option.id === 'minify')?.value;
setMinify(needMinify);

const databaseStatement = getDatabaseStatement(data.containerData);

Expand All @@ -18,7 +20,7 @@ const generateViewScript = (data, logger, callback, app) => {
isKeyspaceActivated: true,
});

callback(null, buildScript(needMinify)(databaseStatement, script));
callback(null, buildScript(databaseStatement, script));
} catch (error) {
logger.log('error', { message: error.message, stack: error.stack }, 'Hive Forward-Engineering Error');

Expand Down
4 changes: 2 additions & 2 deletions forward_engineering/helpers/alterScriptFromDeltaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const getInlineRelationships = ({ schema, options }) => {
return addedRelationships;
};

const getAlterScript = (schema, definitions, data, app, needMinify) => {
const getAlterScript = (schema, definitions, data, app) => {
const provider = require('./alterScriptHelpers/provider')(app);

const inlineDeltaRelationships = getInlineRelationships({ schema, options: data.options });
Expand Down Expand Up @@ -236,7 +236,7 @@ const getAlterScript = (schema, definitions, data, app, needMinify) => {
.filter(Boolean)
.map(script => script.trim());
scripts = getCommentedDropScript(scripts, data);
return buildScript(needMinify)(...scripts);
return buildScript(...scripts);
};

const getCommentedDropScript = (scripts, data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const getAddCollectionsScripts =

return statement;
})
.join('\n');
.join(',\n');

const collectionScript = getTableStatement(
...hydratedCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ module.exports = {
'ALTER TABLE ${tableName} ADD CONSTRAINT ${constraintName} CHECK (${expression}) ${enable}${noValidate}${rely};',

addNotNullConstraint:
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} NOT NULL ${enable}${noValidate}${rely};',
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} NOT NULL${enable}${noValidate}${rely};',

addColumnCheckConstraint:
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} CHECK (${expression}) ${enable}${noValidate}${rely};',
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} CHECK (${expression})${enable}${noValidate}${rely};',

addDefaultValueConstraint:
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} DEFAULT ${defaultValue} ${enable}${noValidate}${rely};',
'ALTER TABLE ${tableName} CHANGE ${columnName} ${columnName} ${type} CONSTRAINT ${constraintName} DEFAULT ${defaultValue}${enable}${noValidate}${rely};',

addFkConstraint:
'ALTER TABLE ${childTableName} ADD CONSTRAINT ${constraintName} FOREIGN KEY (${childColumns}) REFERENCES ${parentTableName}(${parentColumns})${disableNoValidate};',
Expand Down
23 changes: 2 additions & 21 deletions forward_engineering/helpers/buildScript.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
const sqlFormatter = require('sql-formatter');

const tryFormat = statement => {
try {
// Fails for complex types https://github.com/sql-formatter-org/sql-formatter/issues/735
return sqlFormatter.format(statement, { language: 'spark', tabWidth: 4, linesBetweenQueries: 2 });
} catch {
return statement;
}
const buildScript = (...statements) => {
return statements.filter(Boolean).join('\n\n');
};

const buildScript =
needMinify =>
(...statements) => {
if (needMinify) {
return statements.filter(Boolean).join('\n\n');
}

const script = statements.filter(Boolean).map(tryFormat).join('\n\n');

return script + '\n';
};

module.exports = {
buildScript,
};
19 changes: 14 additions & 5 deletions forward_engineering/helpers/columnHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
prepareName,
commentDeactivatedStatements,
encodeStringLiteral,
indentString,
shouldMinify,
} = require('./generalHelper');
const { getConstraintOpts } = require('./constraintHelper');
const { getDefaultConstraintName } = require('./alterScriptHelpers/generalHelper');
Expand Down Expand Up @@ -53,15 +55,22 @@ const getStructChildProperties = (getTypeByProperty, definitions) => property =>
};

const getStruct = (getTypeByProperty, definitions) => property => {
const getStructStatement = propertiesString => `struct<${propertiesString}>`;
const minify = shouldMinify();

const getStructStatement = propertiesString =>
minify ? `struct<${propertiesString}>` : `struct<\n${propertiesString}\n>`;

const { activatedProps, deactivatedProps } = getStructChildProperties(getTypeByProperty, definitions)(property);

const activePropsString = minify ? activatedProps.join(', ') : indentString(activatedProps.join(',\n'));
const deactivatedPropsString = minify ? deactivatedProps.join(', ') : indentString(deactivatedProps.join(',\n'));

if (deactivatedProps.length === 0) {
return getStructStatement(activatedProps.join(', '));
return getStructStatement(activePropsString);
} else if (activatedProps.length === 0) {
return getStructStatement(`/* ${activatedProps.join(', ')} */`);
return getStructStatement(`/* ${activePropsString} */`);
}
return getStructStatement(`${activatedProps.join(', ')} /*, ${deactivatedProps.join(', ')}*/`);
return getStructStatement(`${activePropsString} /*, ${deactivatedPropsString}*/`);
};

const getChildBySubtype = (parentType, subtype) => {
Expand Down Expand Up @@ -402,7 +411,7 @@ const getColumnConstraintsStatement = ({ collection, column, isAlterScript }) =>
postfix,
});
const columnName = skipName ? '' : ` (${column.name})`;
return `CONSTRAINT ${constraintName} ${statement}${columnName} ${noValidate}`;
return `CONSTRAINT ${constraintName} ${statement}${columnName} ${noValidate}`.trim();
};

const statements = [];
Expand Down
2 changes: 1 addition & 1 deletion forward_engineering/helpers/constraintHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getConstraintOpts = ({ noValidateSpecification, enableSpecification, rely
return '';
}

return ` ${enableSpecification}${getPartConstraintOpts(noValidateSpecification)}${getPartConstraintOpts(rely)}`;
return `${enableSpecification}${getPartConstraintOpts(noValidateSpecification)}${getPartConstraintOpts(rely)}`;
};

const getUniqueKeyStatement = (jsonSchema, isParentItemActivated) => {
Expand Down
4 changes: 2 additions & 2 deletions forward_engineering/helpers/foreignKeyHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const getForeignKeyConstraint = ({
disableNoValidate,
}) => {
const constraintNameStatement = constraintName ? `CONSTRAINT ${prepareName(constraintName)} ` : '';
const statement = `,${constraintNameStatement}FOREIGN KEY (${childColumns}) REFERENCES ${parentTableName}(${parentColumns}) ${disableNoValidate ? 'DISABLE NOVALIDATE' : ''}`;
const statement = `${constraintNameStatement}FOREIGN KEY (${childColumns}) REFERENCES ${parentTableName}(${parentColumns}) ${disableNoValidate ? 'DISABLE NOVALIDATE' : ''}`;
return statement;
};

Expand All @@ -138,7 +138,7 @@ const getForeignKeyStatementsByHashItem = hashItem => {

return commentDeactivatedStatements(statement, isActivated);
})
.join('\n');
.join(',\n');
};

const getPreparedForeignColumns = (columnsPaths, idToNameHashTable) => {
Expand Down
14 changes: 14 additions & 0 deletions forward_engineering/helpers/generalHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ const stripParentheses = str => {
return result.trim();
};

const minifyState = {
enabled: false,
};

const setMinify = enabled => {
minifyState.enabled = enabled;
};

const shouldMinify = () => {
return minifyState.enabled;
};

module.exports = {
buildStatement,
getName,
Expand All @@ -162,4 +174,6 @@ module.exports = {
encodeStringLiteral,
isDeactivatedStatement,
stripParentheses,
setMinify,
shouldMinify,
};
2 changes: 1 addition & 1 deletion forward_engineering/helpers/tableHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const getCreateStatement = ({
.map(item => item + ' ')
.join('');
const fullTableName = dbName ? `${dbName}.${tableName}` : tableName;
const hasConstraint = primaryKeyStatement || uniqueKeyStatement || checkStatement;
const hasConstraint = primaryKeyStatement || uniqueKeyStatement || checkStatement || foreignKeyStatement;

return buildStatement(
`CREATE${tempExtStatement}TABLE ${ifNotExist ? 'IF NOT EXISTS ' : ''}${fullTableName} (`,
Expand Down
47 changes: 26 additions & 21 deletions forward_engineering/helpers/viewHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('lodash');
const { prepareName, commentDeactivatedStatements, encodeStringLiteral } = require('./generalHelper');
const { prepareName, commentDeactivatedStatements, encodeStringLiteral, indentString } = require('./generalHelper');

const itemIsDeactivated = item => item.startsWith('-- ');

Expand Down Expand Up @@ -62,7 +62,7 @@ const getFromStatement = (collectionRefsDefinitionsMap, columns) => {
return '';
}

return 'FROM ' + sourceCollections.join(' INNER JOIN ');
return 'FROM\n' + indentString(sourceCollections.join('\nINNER JOIN '));
};

const retrievePropertyFromConfig = (config, tab, propertyName, defaultValue = '') =>
Expand All @@ -73,7 +73,7 @@ const retrieveContainerName = containerConfig =>

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

Expand All @@ -86,22 +86,32 @@ module.exports = {
const comment = view.description;
const fromStatement = getFromStatement(collectionRefsDefinitionsMap, columns);
const name = bucketName ? `${bucketName}.${viewName}` : `${viewName}`;
const createStatement = `CREATE ${orReplace && !ifNotExists ? 'OR REPLACE ' : ''}${isMaterialized ? 'MATERIALIZED ' : ''}VIEW ${ifNotExist ? 'IF NOT EXISTS ' : ''}${name}`;

script.push(createStatement);
const createStatement = [
'CREATE',
orReplace && !ifNotExists ? 'OR REPLACE' : '',
isMaterialized ? 'MATERIALIZED' : '',
'VIEW',
ifNotExist ? 'IF NOT EXISTS' : '',
name,
comment ? `COMMENT '${encodeStringLiteral(comment)}'` : '',
'AS',
]
.filter(Boolean)
.join(' ');

statements.push(createStatement);

if (schema.selectStatement) {
let statement = schema.selectStatement;
if (!_.trim(statement).toLowerCase().startsWith('as')) {
statement = 'AS ' + statement;
if (_.trim(statement).toLowerCase().startsWith('as')) {
statement = statement
.trim()
.replace(/\bAS\b/i, '')
.trim();
}

return (
createStatement +
(comment ? " COMMENT '" + encodeStringLiteral(comment) + "' " : ' ') +
statement +
';\n\n'
);
return `${createStatement}\n${statement};`;
}

if (_.isEmpty(columns)) {
Expand All @@ -115,14 +125,9 @@ module.exports = {
return;
}

if (comment) {
script.push(`COMMENT '${encodeStringLiteral(comment)}'`);
}

const joinedColumns = joinLastDeactivatedItem(columnsNames).join(',\n');
script.push(`AS SELECT ${joinedColumns}`);
script.push(fromStatement);
const joinedColumns = indentString(joinLastDeactivatedItem(columnsNames).join(',\n'));
statements.push('SELECT', joinedColumns, fromStatement);

return commentDeactivatedStatements(script.join('\n ') + ';', view.isActivated);
return commentDeactivatedStatements(statements.join('\n') + ';', view.isActivated);
},
};
Loading