Skip to content

Commit

Permalink
Merge pull request #1656 from Accenture/feature/1651-deploy-matchname…
Browse files Browse the repository at this point in the history
…-for-dataextensions

#1651: allow using --matchName with deploy dataExtension
  • Loading branch information
JoernBerkefeld authored Sep 2, 2024
2 parents a9e2803 + ac622a7 commit 61c97ab
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
68 changes: 66 additions & 2 deletions lib/metadataTypes/DataExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class DataExtension extends MetadataType {
DataExtension.oldFields[metadataMap[metadataKey][this.definition.keyField]] =
await DataExtensionField.prepareDeployColumnsOnUpdate(
metadataMap[metadataKey].Fields,
metadataKey
Util.matchedByName?.[this.definition.type]?.[metadataKey] || metadataKey
);

if (
Expand Down Expand Up @@ -893,7 +893,7 @@ class DataExtension extends MetadataType {
if (metadata[field?.DataExtension?.CustomerKey]) {
metadata[field.DataExtension.CustomerKey].Fields.push(field);
} else {
Util.logger.warn(` - Issue retrieving data extension fields. key='${key}'`);
// field was retrieved for which we do not have the right dataExtension. This might be due to us having to resort to not using a DE filter to avoid the "String or binary data would be truncated." error
}
}

Expand Down Expand Up @@ -1632,6 +1632,70 @@ class DataExtension extends MetadataType {
return super.getFilesToCommit(keyArr);
}
}
/**
* helper for {@link MetadataType.createOrUpdate}
*
* @param {MetadataTypeItem} metadataItem to be deployed item
* @returns {MetadataTypeItem} cached item or undefined
*/
static getCacheMatchedByName(metadataItem) {
let cacheMatchedByName;

if (Util.OPTIONS.matchName) {
// make sure to run the search ONLY if OPTIONS.matchName is true and definition.allowMatchingByName signals support
const typeCache = cache.getCache()?.[this.definition.type];
const potentials = [];
for (const key in typeCache) {
const cachedItem = typeCache[key];
if (
cachedItem[this.definition.nameField] ===
metadataItem[this.definition.nameField]
) {
potentials.push(cachedItem);
}
}
if (potentials.length > 1) {
throw new Error(
`found multiple name matches in cache for ${this.definition.type} ${metadataItem[this.definition.keyField]} / ${metadataItem[this.definition.nameField]}. Check their keys for more details: ${potentials.map((p) => p[this.definition.keyField]).join(', ')}`
);
} else if (potentials.length === 1) {
// only one item found, confirm that it's in the same folder
const deployFolderPath = cache.searchForField(
'folder',
metadataItem[this.definition.folderIdField],
'ID',
'Path'
);
if (
potentials[0][this.definition.folderIdField] ===
metadataItem[this.definition.folderIdField]
) {
cacheMatchedByName = potentials[0];

Util.logger.info(
Util.getGrayMsg(
` - found ${this.definition.type} ${metadataItem[this.definition.keyField]} in cache by name "${metadataItem[this.definition.nameField]}" and folder "${deployFolderPath}": ${cacheMatchedByName[this.definition.keyField]}`
)
);
} else {
const cacheFolderPath = cache.searchForField(
'folder',
potentials[0][this.definition.folderIdField],
'ID',
'Path'
);
throw new Error(
`found ${this.definition.type} ${metadataItem[this.definition.keyField]} in cache by name "${metadataItem[this.definition.nameField]}" but in different folders: "${deployFolderPath}" vs "${cacheFolderPath}": ${potentials[0][this.definition.keyField]}`
);
}
} else {
Util.logger.debug(
` - no name-match found for ${this.definition.type} ${metadataItem[this.definition.keyField]}. Creating new ${this.definition.type} instead.`
);
}
}
return cacheMatchedByName;
}
}

// Assign definition to static attributes
Expand Down
5 changes: 5 additions & 0 deletions lib/metadataTypes/MetadataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,11 @@ class MetadataType {
} else if (cacheMatchedByKey || cacheMatchedByName) {
// normal way of processing update files
const cachedVersion = cacheMatchedByKey || cacheMatchedByName;
if (!cacheMatchedByKey && cacheMatchedByName) {
Util.matchedByName[this.definition.type] ||= {};
Util.matchedByName[this.definition.type][metadataKey] =
cacheMatchedByName[this.definition.keyField];
}
if (!this.hasChanged(cachedVersion, metadataMap[metadataKey])) {
hasError = true;
}
Expand Down
1 change: 1 addition & 0 deletions lib/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const Util = {
packageJsonMcdev: readJsonSync(path.join(__dirname, '../../package.json')),
OPTIONS: {},
changedKeysMap: {},
matchedByName: {},

/**
* helper that allows filtering an object by its keys
Expand Down

0 comments on commit 61c97ab

Please sign in to comment.