Skip to content

Commit

Permalink
feat: upgraded redshift-related packages to latest, eliminated redund…
Browse files Browse the repository at this point in the history
…ant setDependencies module, fixed sonar remarks (#48)
  • Loading branch information
chulanovskyi-bs authored Oct 28, 2024
1 parent 1c93381 commit 79a379a
Show file tree
Hide file tree
Showing 18 changed files with 944 additions and 1,008 deletions.
7 changes: 5 additions & 2 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const applyToInstanceHelper = require('./helpers/applyToInstanceHelper');
const { commentDropStatements } = require('./helpers/commentDropStatements');
const { DROP_STATEMENTS } = require('./helpers/constants');

// Delete once v7.8.3 is released
const getHiddenKeys = hiddenKeys => [...hiddenKeys, 'password'];

module.exports = {
generateScript(data, logger, callback, app) {
try {
Expand Down Expand Up @@ -52,7 +55,7 @@ module.exports = {

applyToInstance(connectionInfo, logger, cb, app) {
logger.clear();
logger.log('info', connectionInfo, 'connectionInfo', connectionInfo.hiddenKeys);
logger.log('info', connectionInfo, 'connectionInfo', getHiddenKeys(connectionInfo.hiddenKeys));

applyToInstanceHelper
.applyToInstance(connectionInfo, logger, app)
Expand Down Expand Up @@ -83,7 +86,7 @@ module.exports = {

logInfo(step, connectionInfo, logger) {
logger.clear();
logger.log('info', connectionInfo, 'connectionInfo', connectionInfo.hiddenKeys);
logger.log('info', connectionInfo, 'connectionInfo', getHiddenKeys(connectionInfo.hiddenKeys));
},

isDropInStatements(data, logger, callback, app) {
Expand Down
7 changes: 4 additions & 3 deletions forward_engineering/ddlProvider.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const _ = require('lodash');

const defaultTypes = require('./configs/defaultTypes');
const templates = require('./configs/templates');
const types = require('./configs/types');
const { commentIfDeactivated } = require('./helpers/commentDeactivatedHelper');
const { getTableAttributes, getTableConstraints, getTableLikeConstraint } = require('./helpers/tableHelper');

module.exports = (baseProvider, options, app) => {
const _ = app.require('lodash');
const { hasType } = app.require('@hackolade/ddl-fe-utils').general;
const assignTemplates = app.require('@hackolade/ddl-fe-utils').assignTemplates;
const {
Expand All @@ -30,8 +33,6 @@ module.exports = (baseProvider, options, app) => {
getColumnsDefinitions,
} = require('./helpers/columnDefinitionHelper')(app);
const { generateConstraint } = require('./helpers/constraintHelper')(app);
const commentIfDeactivated = require('./helpers/commentDeactivatedHelper')(app);
const { getTableAttributes, getTableConstraints, getTableLikeConstraint } = require('./helpers/tableHelper')(app);

return {
createDatabase({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const _ = require('lodash');
const { modifyGroupItems } = require('./common');

module.exports = app => {
const _ = app.require('lodash');
const ddlProvider = require('../../ddlProvider')(null, null, app);
const { getDbData, getDbName } = app.require('@hackolade/ddl-fe-utils').general;
const { hydrateUdf, hydrateProcedure, filterUdf, filterProcedure } = require('../general')(app);
const { modifyGroupItems } = require('./common')(app);

const getAddContainerScript = containerData => {
const constructedDbData = getDbData([containerData]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const _ = require('lodash');
const { checkFieldPropertiesChanged, getCompMod, setTableKeys, getKeys, setNewProperties } = require('./common');
const { createColumnDefinitionBySchema } = require('./createColumnDefinition');

module.exports = app => {
const _ = app.require('lodash');
const { getEntityName } = app.require('@hackolade/ddl-fe-utils').general;
const { createColumnDefinitionBySchema } = require('./createColumnDefinition')(_);
const {
generateIdToNameHashTable,
generateIdToActivatedHashTable,
general: { getEntityName },
} = app.require('@hackolade/ddl-fe-utils');

const { getCompositeName } = require('../general')(app);
const ddlProvider = require('../../ddlProvider')(null, null, app);
const { generateIdToNameHashTable, generateIdToActivatedHashTable } = app.require('@hackolade/ddl-fe-utils');
const { checkFieldPropertiesChanged, getCompMod, setTableKeys, getKeys, setNewProperties } =
require('./common')(app);

const getAddCollectionScript = collection => {
const dbName = collection.compMod.keyspaceName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');

module.exports = app => {
const _ = app.require('lodash');
const { mapProperties } = app.require('@hackolade/ddl-fe-utils');
const ddlProvider = require('../../ddlProvider')(null, null, app);
const { getCompositeName } = require('../general')(app);
Expand Down
240 changes: 119 additions & 121 deletions forward_engineering/helpers/alterScriptHelpers/common.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,135 @@
module.exports = app => {
const _ = app.require('lodash');
const _ = require('lodash');

const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => {
return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]);
};
const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => {
return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]);
};

const modifyGroupItems = ({ data, key, hydrate, drop, create, filter = Boolean }) => {
const compMod = getCompMod(data);
const parentName = data.code || data.name || data.collectionName;
const modifyGroupItems = ({ data, key, hydrate, drop, create, filter = Boolean }) => {
const compMod = getCompMod(data);
const parentName = data.code || data.name || data.collectionName;

const { removed, added, modified } = getModifiedGroupItems(compMod[key] || {}, hydrate, filter);
const { removed, added, modified } = getModifiedGroupItems(compMod[key] || {}, hydrate, filter);

const removedScripts = removed.map(item => drop(item, parentName));
const addedScripts = added.map(item => create(item, parentName));
const modifiedScripts = modified.map(item => create({ ...item, orReplace: true }, parentName));
const removedScripts = removed.map(item => drop(item, parentName));
const addedScripts = added.map(item => create(item, parentName));
const modifiedScripts = modified.map(item => create({ ...item, orReplace: true }, parentName));

return [].concat(modifiedScripts).concat(removedScripts).concat(addedScripts).filter(Boolean).join('\n\n');
};
return [].concat(modifiedScripts).concat(removedScripts).concat(addedScripts).filter(Boolean).join('\n\n');
};

const getModifiedGroupItems = ({ new: newItems = [], old: oldItems = [] }, hydrate, filter) => {
const oldHydrated = oldItems.map(hydrate).filter(filter);
const newHydrated = newItems.map(hydrate).filter(filter);

const { removed, added, modified } = oldHydrated.reduce(
(accumulator, oldItem) => {
const newItem = newHydrated.find(item => item.name === oldItem.name);
const itemsAreNotEqual = !isGroupItemsEqual(newItem, oldItem);

if (!newItem) {
return {
removed: [...accumulator.removed, oldItem],
modified: accumulator.modified,
added: accumulator.added,
};
}

if (itemsAreNotEqual) {
return {
removed: accumulator.removed,
modified: [...accumulator.modified, newItem],
added: accumulator.added,
};
}

return accumulator;
},
{
removed: [],
modified: [],
added: newHydrated.filter(newItem => !oldHydrated.some(item => item.name === newItem.name)),
},
);
const getModifiedGroupItems = ({ new: newItems = [], old: oldItems = [] }, hydrate, filter) => {
const oldHydrated = oldItems.map(hydrate).filter(filter);
const newHydrated = newItems.map(hydrate).filter(filter);

const { removed, added, modified } = oldHydrated.reduce(
(accumulator, oldItem) => {
const newItem = newHydrated.find(item => item.name === oldItem.name);
const itemsAreNotEqual = !isGroupItemsEqual(newItem, oldItem);

if (!newItem) {
return {
removed: [...accumulator.removed, oldItem],
modified: accumulator.modified,
added: accumulator.added,
};
}

return { removed, added, modified };
};
if (itemsAreNotEqual) {
return {
removed: accumulator.removed,
modified: [...accumulator.modified, newItem],
added: accumulator.added,
};
}

const isGroupItemsEqual = (leftItem, rightItem) => _.isEqual(leftItem, rightItem);

const getCompMod = containerData => containerData.role?.compMod ?? {};

const checkCompModEqual = ({ new: newItem, old: oldItem } = {}) => _.isEqual(newItem, oldItem);

const setTableKeys = (idToNameHashTable, idToActivatedHashTable, table) => {
return {
...table,
distKey: [
{
...(table.distKey?.[0] ?? {}),
compositeDistKey:
table.distKey?.[0]?.compositeDistKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
sortKey: [
{
...(table.sortKey?.[0] ?? {}),
compositeSortKey:
table.sortKey?.[0]?.compositeSortKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
primaryKey: [
{
...(table.primaryKey?.[0] ?? {}),
compositePrimaryKey:
table.primaryKey?.[0]?.compositePrimaryKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
uniqueKey: [
{
...(table.uniqueKey?.[0] ?? {}),
compositeUniqueKey:
table.uniqueKey?.[0]?.compositeUniqueKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
};
};
return accumulator;
},
{
removed: [],
modified: [],
added: newHydrated.filter(newItem => !oldHydrated.some(item => item.name === newItem.name)),
},
);

const getKeys = keys => _.map(keys, 'name');
return { removed, added, modified };
};

const setNewProperties = jsonSchema => {
const compMod = getCompMod(jsonSchema);
const isGroupItemsEqual = (leftItem, rightItem) => _.isEqual(leftItem, rightItem);

return _.toPairs(compMod).reduce((jsonSchema, [key, compMod]) => {
if (compMod.new) {
return { ...jsonSchema, [key]: compMod.new };
}
const getCompMod = containerData => containerData.role?.compMod ?? {};

return jsonSchema;
}, jsonSchema);
};
const checkCompModEqual = ({ new: newItem, old: oldItem } = {}) => _.isEqual(newItem, oldItem);

const setTableKeys = (idToNameHashTable, idToActivatedHashTable, table) => {
return {
checkFieldPropertiesChanged,
getCompMod,
modifyGroupItems,
checkCompModEqual,
setTableKeys,
getKeys,
setNewProperties,
...table,
distKey: [
{
...(table.distKey?.[0] ?? {}),
compositeDistKey:
table.distKey?.[0]?.compositeDistKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
sortKey: [
{
...(table.sortKey?.[0] ?? {}),
compositeSortKey:
table.sortKey?.[0]?.compositeSortKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
primaryKey: [
{
...(table.primaryKey?.[0] ?? {}),
compositePrimaryKey:
table.primaryKey?.[0]?.compositePrimaryKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
uniqueKey: [
{
...(table.uniqueKey?.[0] ?? {}),
compositeUniqueKey:
table.uniqueKey?.[0]?.compositeUniqueKey?.map(key => ({
...key,
name: idToNameHashTable[key.keyId],
isActivated: idToActivatedHashTable[key.keyId],
})) || [],
},
],
};
};

const getKeys = keys => _.map(keys, 'name');

const setNewProperties = jsonSchema => {
const compMod = getCompMod(jsonSchema);

return _.toPairs(compMod).reduce((jsonSchema, [key, compMod]) => {
if (compMod.new) {
return { ...jsonSchema, [key]: compMod.new };
}

return jsonSchema;
}, jsonSchema);
};

module.exports = {
checkFieldPropertiesChanged,
getCompMod,
modifyGroupItems,
checkCompModEqual,
setTableKeys,
getKeys,
setNewProperties,
};
Loading

0 comments on commit 79a379a

Please sign in to comment.