Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions forward_engineering/services/alterScript/collectionAlterHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,27 @@ const isCollectionRenamed = ({ entity } = {}) => {
return Boolean(oldName && newName && oldName !== newName);
};

/**
* @param {{ entity: object }} params
* @returns {boolean}
*/
const isCollectionCreated = ({ entity } = {}) => getCompMod({ entity }).created === true;

/**
* @param {{ entity: object }} params
* @returns {boolean}
*/
const isCollectionDeleted = ({ entity } = {}) => getCompMod({ entity }).deleted === true;

/**
* @param {{ entity: object }} params
* @returns {AlterScriptDto[]}
*/
const getAddedCollectionDtos = ({ entity } = {}) => {
if (!isCollectionCreated({ entity })) {
return [];
}

const collectionName = getCollectionName({ entity, nameType: 'new' }) || getCollectionName({ entity });
return [getCreateCollectionDto({ entity, collectionName })].filter(Boolean);
};
Expand All @@ -110,6 +126,10 @@ const getAddedCollectionDtos = ({ entity } = {}) => {
* @returns {AlterScriptDto[]}
*/
const getDeletedCollectionDtos = ({ entity } = {}) => {
if (!isCollectionDeleted({ entity })) {
return [];
}

const collectionName = getCollectionName({ entity, nameType: 'old' }) || getCollectionName({ entity });
return [getDropCollectionDto({ entity, collectionName })].filter(Boolean);
};
Expand Down Expand Up @@ -153,4 +173,8 @@ module.exports = {
getCollectionContext,
getCollectionName,
getCompMod,
getEntityRole,
isCollectionCreated,
isCollectionDeleted,
isCollectionRenamed,
};
17 changes: 16 additions & 1 deletion forward_engineering/services/alterScript/deltaSchemaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,23 @@ const getDeltaItems = ({ schema = {}, nameProperty, modify } = {}) =>
[schema.properties?.[nameProperty]?.properties?.[modify]?.items]
.flat()
.filter(Boolean)
.map(item => Object.values(item.properties || {})[0])
.flatMap(item => Object.values(item.properties || {}))
.filter(Boolean);

/**
* @param {{ properties: object | object[] }} params
* @returns {object}
*/
const normalizeProperties = ({ properties } = {}) => {
if (!Array.isArray(properties)) {
return properties || {};
}

return Object.fromEntries(
properties.map(property => [property?.code || property?.name, property]).filter(([name]) => name),
);
};

/**
* @param {{ connectionInfo: object }} params
* @returns {boolean}
Expand All @@ -47,5 +61,6 @@ const shouldApplyDropStatements = ({ connectionInfo = {} } = {}) =>
module.exports = {
getDeltaSchema,
getDeltaItems,
normalizeProperties,
shouldApplyDropStatements,
};
100 changes: 52 additions & 48 deletions forward_engineering/services/alterScript/indexAlterHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ const { getIndexKeyIdToKeyNameMap, injectKeysNamesIntoIndexKeys } = require('../
const { getIndexScript, getDropIndexScript, getAlterIndexScript } = require('../statements/indexesStatements');
const { commentStatement } = require('./commentHelper');
const { AlterScriptDto } = require('./AlterScriptDto');
const { getDeltaItems } = require('./deltaSchemaHelper');
const { getCollectionContext, getCollectionName, getCompMod } = require('./collectionAlterHelper');
const { getDeltaItems, normalizeProperties } = require('./deltaSchemaHelper');
const {
getCollectionContext,
getCollectionName,
getCompMod,
getEntityRole,
isCollectionCreated,
isCollectionDeleted,
isCollectionRenamed,
} = require('./collectionAlterHelper');

const NON_STRUCTURAL_INDEX_FIELDS = ['indxComments', 'indxDescription', 'id', 'GUID'];

/**
* @param {{ entity: object }} params
* @returns {object}
*/
const getEntityRole = ({ entity = {} } = {}) => entity.role || entity;

/**
* @param {{ entity: object }} params
* @returns {object}
*/
const getEntityProperties = ({ entity = {} } = {}) => {
const role = getEntityRole({ entity });

return {
...entity.properties,
...role.properties,
...normalizeProperties({ properties: entity.properties }),
...normalizeProperties({ properties: role.properties }),
};
};

Expand Down Expand Up @@ -333,23 +336,11 @@ const getIndexChangeDtos = ({ entity, collectionName, oldIndexes = [], newIndexe
}, []);
};

/**
* @param {{ entity: object }} params
* @returns {boolean}
*/
const isCollectionRenamed = ({ entity } = {}) => {
const compMod = getCompMod({ entity });
const oldName = compMod.code?.old || compMod.collectionName?.old;
const newName = compMod.code?.new || compMod.collectionName?.new;

return Boolean(oldName && newName && oldName !== newName);
};

/**
* @param {{ entity: object }} params
* @returns {AlterScriptDto[]}
*/
const getAddedEntityIndexDtos = ({ entity } = {}) => {
const getCreatedCollectionIndexDtos = ({ entity } = {}) => {
const role = getEntityRole({ entity });
const collectionName = getCollectionName({ entity, nameType: 'new' }) || getCollectionName({ entity });

Expand All @@ -365,7 +356,7 @@ const getAddedEntityIndexDtos = ({ entity } = {}) => {
* @param {{ entity: object }} params
* @returns {AlterScriptDto[]}
*/
const getDeletedEntityIndexDtos = ({ entity } = {}) => {
const getDeletedCollectionIndexDtos = ({ entity } = {}) => {
const role = getEntityRole({ entity });
const collectionName = getCollectionName({ entity, nameType: 'old' }) || getCollectionName({ entity });

Expand All @@ -381,39 +372,54 @@ const getDeletedEntityIndexDtos = ({ entity } = {}) => {
* @param {{ entity: object }} params
* @returns {AlterScriptDto[]}
*/
const getModifiedEntityIndexDtos = ({ entity } = {}) => {
const getRenamedCollectionIndexDtos = ({ entity } = {}) => {
const role = getEntityRole({ entity });
const compMod = getCompMod({ entity });
const oldCollectionName = getCollectionName({ entity, nameType: 'old' }) || getCollectionName({ entity });
const newCollectionName = getCollectionName({ entity, nameType: 'new' }) || getCollectionName({ entity });

if (isCollectionRenamed({ entity })) {
const oldIndexes = compMod.indexes?.old || role.indexes || [];
const newIndexes = compMod.indexes?.new || role.indexes || [];
return [
...getDropIndexDtos({
entity,
collectionName: oldCollectionName,
indexes: compMod.indexes?.old || role.indexes || [],
nameType: 'old',
}),
...getCreateIndexDtos({
entity,
collectionName: newCollectionName,
indexes: compMod.indexes?.new || role.indexes || [],
nameType: 'new',
}),
];
};

return [
...getDropIndexDtos({
entity,
collectionName: oldCollectionName,
indexes: oldIndexes,
nameType: 'old',
}),
...getCreateIndexDtos({
entity,
collectionName: newCollectionName,
indexes: newIndexes,
nameType: 'new',
}),
];
/**
* @param {{ entity: object }} params
* @returns {AlterScriptDto[]}
*/
const getEntityIndexDtos = ({ entity } = {}) => {
if (isCollectionCreated({ entity })) {
return getCreatedCollectionIndexDtos({ entity });
}

if (isCollectionDeleted({ entity })) {
return getDeletedCollectionIndexDtos({ entity });
}

if (isCollectionRenamed({ entity })) {
return getRenamedCollectionIndexDtos({ entity });
}

const compMod = getCompMod({ entity });

if (!compMod.indexes) {
return [];
}

return getIndexChangeDtos({
entity,
collectionName: newCollectionName,
collectionName: getCollectionName({ entity, nameType: 'new' }) || getCollectionName({ entity }),
oldIndexes: compMod.indexes.old || [],
newIndexes: compMod.indexes.new || [],
});
Expand All @@ -428,11 +434,9 @@ const getIndexAlterScriptDtos = ({ schema } = {}) => {
const modifiedEntities = getDeltaItems({ schema, nameProperty: 'entities', modify: 'modified' });
const deletedEntities = getDeltaItems({ schema, nameProperty: 'entities', modify: 'deleted' });

return [
...deletedEntities.flatMap(entity => getDeletedEntityIndexDtos({ entity })),
...modifiedEntities.flatMap(entity => getModifiedEntityIndexDtos({ entity })),
...addedEntities.flatMap(entity => getAddedEntityIndexDtos({ entity })),
].filter(Boolean);
return [...deletedEntities, ...modifiedEntities, ...addedEntities]
.flatMap(entity => getEntityIndexDtos({ entity }))
.filter(Boolean);
};

module.exports = {
Expand Down
9 changes: 8 additions & 1 deletion forward_engineering/services/statements/indexesStatements.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@
return `DROP INDEX ${wrapWithBackticks(indxName)} IF EXISTS ON ${keySpaceRefStatement}${usingGsiClause};`;
};

/**
* @param {{ num_replica?: number }} params
* @returns {boolean}
*/
const isReplicaCountSet = ({ num_replica } = {}) =>
num_replica !== undefined && num_replica !== null && num_replica !== '';

Check warning on line 259 in forward_engineering/services/statements/indexesStatements.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this "!==" check; it will always be true. Did you mean to use "!="?

See more on https://sonarcloud.io/project/issues?id=hackolade_CouchbaseV7Plus&issues=AaAeHpJbiiBgOZBA1TQR&open=AaAeHpJbiiBgOZBA1TQR&pullRequest=69

Check warning on line 259 in forward_engineering/services/statements/indexesStatements.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this "!==" check; it will always be true. Did you mean to use "!="?

See more on https://sonarcloud.io/project/issues?id=hackolade_CouchbaseV7Plus&issues=AaAeHpJbiiBgOZBA1TQS&open=AaAeHpJbiiBgOZBA1TQS&pullRequest=69

/**
*
* @param {{
Expand All @@ -274,7 +281,7 @@
return `{"action":"move","nodes":[${nodeStatement}]}`;
}

if (action === 'replica_count' && !isEmpty(num_replica)) {
if (action === 'replica_count' && isReplicaCountSet({ num_replica })) {
return `{"action":"replica_count","num_replica":${num_replica}}`;
}

Expand Down
Loading