Skip to content

Commit

Permalink
Merge pull request #1206 from contentstack/fix/CS-42677
Browse files Browse the repository at this point in the history
fix: handled deleted entries condition in generated migration scripts
  • Loading branch information
aman19K authored Dec 8, 2023
2 parents eb542b7 + 42c96fb commit ecc3736
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 88 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-branches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-branches
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-branches/1.0.18 darwin-arm64 node-v20.8.0
@contentstack/cli-cm-branches/1.0.19 darwin-arm64 node-v20.8.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-branches",
"description": "Contentstack CLI plugin to do branches operations",
"version": "1.0.18",
"version": "1.0.19",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
18 changes: 10 additions & 8 deletions packages/contentstack-branches/src/utils/entry-create-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,17 @@ export function entryCreateScript(contentType) {
try {
compareFilteredProperties.length !== 0 &&
compareFilteredProperties.forEach(async (entryDetails) => {
entryDetails = updateAssetDetailsInEntries(entryDetails);
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(error => {
throw error;
});
if(createdEntry){
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(error => {
throw error;
});
if(createdEntry){
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
await updateEntry(createdEntry, entryDetails);
}
});
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,56 +494,70 @@ export function entryCreateUpdateScript(contentType) {
}
try {
if (contentType.options.singleton) {
compareBranchEntries.items.map(async (el) => {
if (contentType?.options?.singleton) {
compareBranchEntries?.items?.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove);
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
}
await updateEntry(entry, entryDetails);
} else {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
}
await updateEntry(entry, entryDetails);
} else {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
await updateEntry(createdEntry, entryDetails);
}
});
} else {
let compareMap = new Map(converter(compareBranchEntries.items));
let baseMap = new Map(converter(baseBranchEntries.items));
//NOTE: Filter distinct entries from the base and compare branches according to their titles.
//TODO: Need to discuss this approach and replace it with uid condition
let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys()));
arr.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(el), keysToRemove);
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (compareMap.get(el) && !baseMap.get(el)) {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
//NOTE: In the compare branch, entry must exist. Condition of deleted entry not handled
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (compareMap.get(el) && !baseMap.get(el)) {
let createdEntry = await stackSDKInstance
.contentType('${contentType}')
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
})
if(createdEntry){
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
} else if (compareMap.get(el) && baseMap.get(el)) {
let baseEntry = baseMap.get(el);
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
if (flag.references) {
await updateReferences(entryDetails, baseEntry, references);
}
await updateEntry(createdEntry, entryDetails);
} else if (compareMap.get(el) && baseMap.get(el)) {
let baseEntry = baseMap.get(el);
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
if (flag.references) {
await updateReferences(entryDetails, baseEntry, references);
await updateEntry(entry, entryDetails);
}
await updateEntry(entry, entryDetails);
}
});
}
Expand Down
86 changes: 50 additions & 36 deletions packages/contentstack-branches/src/utils/entry-update-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,56 +491,70 @@ export function entryUpdateScript(contentType) {
}
try {
if (contentType.options.singleton) {
compareBranchEntries.items.map(async (el) => {
if (contentType?.options?.singleton) {
compareBranchEntries?.items?.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove);
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (baseBranchEntries && baseBranchEntries.items.length) {
let baseEntryUid = baseBranchEntries.items[0].uid;
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
}
await updateEntry(entry, entryDetails);
} else {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, baseBranchEntries.items[0], references);
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
await updateEntry(entry, entryDetails);
} else {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
});
} else {
let compareMap = new Map(converter(compareBranchEntries.items));
let baseMap = new Map(converter(baseBranchEntries.items));
//NOTE: Filter distinct entries from the base and compare branches according to their titles.
//TODO: Need to discuss this approach and replace it with uid approach
let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys()));
arr.map(async (el) => {
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(el), keysToRemove);
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (compareMap.get(el) && !baseMap.get(el)) {
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
} else if (compareMap.get(el) && baseMap.get(el)) {
let baseEntry = baseMap.get(el);
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
//NOTE: In the compare branch, entry must exist. Condition of deleted entry not handled
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
if (compareMap.get(el) && !baseMap.get(el)) {
let createdEntry = await stackSDKInstance
.contentType('${contentType}')
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
})
if (flag.references) {
await updateReferences(entryDetails, baseEntry, references);
if(createdEntry){
if (flag.references) {
await updateReferences(entryDetails, createdEntry, references);
}
await updateEntry(createdEntry, entryDetails);
}
} else if (compareMap.get(el) && baseMap.get(el)) {
let baseEntry = baseMap.get(el);
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
if (flag.references) {
await updateReferences(entryDetails, baseEntry, references);
}
await updateEntry(entry, entryDetails);
}
await updateEntry(entry, entryDetails);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@contentstack/cli-audit": "~1.3.0",
"@contentstack/cli-auth": "~1.3.17",
"@contentstack/cli-cm-bootstrap": "~1.7.1",
"@contentstack/cli-cm-branches": "~1.0.18",
"@contentstack/cli-cm-branches": "~1.0.19",
"@contentstack/cli-cm-bulk-publish": "~1.3.15",
"@contentstack/cli-cm-clone": "~1.8.0",
"@contentstack/cli-cm-export": "~1.10.2",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ecc3736

Please sign in to comment.