Skip to content

Commit 9e60198

Browse files
committed
Fix issue of deletion of attachments from target entities
1 parent 8d924c1 commit 9e60198

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,47 +379,48 @@ public int deleteAttachmentsByObjectIds(
379379
return 0;
380380
}
381381

382-
// Get the target entity of the composition
382+
// Get the target entity of the composition (this is the SOURCE attachment entity)
383383
CdsAssociationType assocType = (CdsAssociationType) compositionElement.get().getType();
384-
String targetEntityName = assocType.getTarget().getQualifiedName();
384+
String sourceAttachmentEntityName = assocType.getTarget().getQualifiedName();
385385

386386
int deletedCount = 0;
387387

388388
// Try deleting from draft table first
389-
Optional<CdsEntity> attachmentDraftEntity = model.findEntity(targetEntityName + "_drafts");
389+
Optional<CdsEntity> attachmentDraftEntity =
390+
model.findEntity(sourceAttachmentEntityName + "_drafts");
390391
if (attachmentDraftEntity.isPresent()) {
391392
var deleteQuery =
392393
Delete.from(attachmentDraftEntity.get())
393394
.where(doc -> doc.get("objectId").in(objectIds.toArray()));
394395
Result result = persistenceService.run(deleteQuery);
395396
deletedCount += result.rowCount();
396397
logger.info(
397-
"Deleted {} attachment records from draft table '{}' for objectIds: {}",
398+
"Deleted {} attachment records from SOURCE draft table '{}' for objectIds: {}",
398399
result.rowCount(),
399-
targetEntityName + "_drafts",
400+
sourceAttachmentEntityName + "_drafts",
400401
objectIds);
401402
}
402403

403404
// Try deleting from non-draft table
404-
Optional<CdsEntity> attachmentEntity = model.findEntity(targetEntityName);
405+
Optional<CdsEntity> attachmentEntity = model.findEntity(sourceAttachmentEntityName);
405406
if (attachmentEntity.isPresent()) {
406407
var deleteQuery =
407408
Delete.from(attachmentEntity.get())
408409
.where(doc -> doc.get("objectId").in(objectIds.toArray()));
409410
Result result = persistenceService.run(deleteQuery);
410411
deletedCount += result.rowCount();
411412
logger.info(
412-
"Deleted {} attachment records from table '{}' for objectIds: {}",
413+
"Deleted {} attachment records from SOURCE table '{}' for objectIds: {}",
413414
result.rowCount(),
414-
targetEntityName,
415+
sourceAttachmentEntityName,
415416
objectIds);
416417
}
417418

418419
if (deletedCount == 0) {
419420
logger.warn(
420421
"No attachment metadata found in source entity '{}' for objectIds: {}. This may indicate"
421422
+ " the records were already cleaned up.",
422-
targetEntityName,
423+
sourceAttachmentEntityName,
423424
objectIds);
424425
}
425426

sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,23 @@ public void moveAttachments(AttachmentMoveEventContext context) throws IOExcepti
246246

247247
// After successful DB update, clean up source entity metadata
248248
// Only clean up successfully moved attachments (not failed ones)
249-
List<String> successfullyMovedObjectIds = new ArrayList<>(successfulMovesMap.keySet());
250-
if (!successfullyMovedObjectIds.isEmpty()) {
249+
// Use the OLD objectIds (before move) to identify source records
250+
List<String> oldObjectIds = new ArrayList<>(successfulMovesMap.keySet());
251+
if (!oldObjectIds.isEmpty()) {
251252
try {
252253
int deletedCount =
253-
dbQuery.deleteAttachmentsByObjectIds(
254-
persistenceService, successfullyMovedObjectIds, context);
254+
dbQuery.deleteAttachmentsByObjectIds(persistenceService, oldObjectIds, context);
255255
logger.info(
256256
"Cleaned up {} attachment metadata records from source entity for {} successfully"
257257
+ " moved attachments",
258258
deletedCount,
259-
successfullyMovedObjectIds.size());
259+
oldObjectIds.size());
260260
} catch (Exception cleanupException) {
261261
// Log cleanup failure but don't fail the entire move operation
262262
logger.warn(
263263
"Failed to clean up source entity metadata for {} attachments: {}. Attachments were"
264264
+ " successfully moved to target.",
265-
successfullyMovedObjectIds.size(),
265+
oldObjectIds.size(),
266266
cleanupException.getMessage());
267267
}
268268
}

0 commit comments

Comments
 (0)