From 6f2d0db079a60d830e1cd43c66c5b45764e17ebd Mon Sep 17 00:00:00 2001 From: sonika-shah <58761340+sonika-shah@users.noreply.github.com> Date: Sun, 23 Mar 2025 14:50:21 +0530 Subject: [PATCH] Minor : Fix issue with soft delete and restore (#20336) * Minor : Fix issue with soft delete and restore * fix search * fix condition * fx search condition --------- Co-authored-by: Shailesh Parmar --- .../service/jdbi3/EntityRepository.java | 8 +++++--- .../service/search/SearchRepository.java | 19 +++++++++++-------- .../openmetadata/service/util/EntityUtil.java | 9 +++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index c4389a9ccd5..18f66146bca 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -62,6 +62,7 @@ import static org.openmetadata.service.util.EntityUtil.fieldUpdated; import static org.openmetadata.service.util.EntityUtil.getColumnField; import static org.openmetadata.service.util.EntityUtil.getEntityReferences; import static org.openmetadata.service.util.EntityUtil.getExtensionField; +import static org.openmetadata.service.util.EntityUtil.isNullOrEmptyChangeDescription; import static org.openmetadata.service.util.EntityUtil.mergedInheritedEntityRefs; import static org.openmetadata.service.util.EntityUtil.nextMajorVersion; import static org.openmetadata.service.util.EntityUtil.nextVersion; @@ -2625,7 +2626,7 @@ public abstract class EntityRepository { protected void createAndInsertChangeEvent( T original, T updated, ChangeDescription changeDescription, EventType eventType) { - if (changeDescription == null) { + if (isNullOrEmptyChangeDescription(changeDescription)) { return; } @@ -2990,7 +2991,7 @@ public abstract class EntityRepository { } private void updateChangeSummary() { - if (changeDescription == null) { + if (isNullOrEmptyChangeDescription(changeDescription)) { return; } // build new change summary @@ -3113,7 +3114,8 @@ public abstract class EntityRepository { // delete operation if (!Objects.equals(updated.getDeleted(), original.getDeleted()) && Boolean.TRUE.equals(updated.getDeleted()) - && changeDescription != null) { + && isNullOrEmptyChangeDescription(changeDescription) + && (Objects.equals(original.getVersion(), updated.getVersion()))) { throw new IllegalArgumentException( CatalogExceptionMessage.readOnlyAttribute(entityType, FIELD_DELETED)); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java index 79784316db4..6cdc050a5ed 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java @@ -47,6 +47,7 @@ import static org.openmetadata.service.search.SearchConstants.TEST_SUITES; import static org.openmetadata.service.search.SearchUtils.isConnectedVia; import static org.openmetadata.service.search.models.IndexMapping.INDEX_NAME_SEPARATOR; import static org.openmetadata.service.util.EntityUtil.compareEntityReferenceById; +import static org.openmetadata.service.util.EntityUtil.isNullOrEmptyChangeDescription; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; @@ -422,10 +423,7 @@ public class SearchRepository { ChangeDescription incrementalChangeDescription = entity.getIncrementalChangeDescription(); ChangeDescription changeDescription; - if (incrementalChangeDescription != null - && (!incrementalChangeDescription.getFieldsAdded().isEmpty() - || !incrementalChangeDescription.getFieldsUpdated().isEmpty() - || !incrementalChangeDescription.getFieldsDeleted().isEmpty())) { + if (!isNullOrEmptyChangeDescription(incrementalChangeDescription)) { changeDescription = incrementalChangeDescription; } else { changeDescription = entity.getChangeDescription(); @@ -948,10 +946,15 @@ public class SearchRepository { scriptTxt, List.of(new ImmutablePair<>("dashboards.id", docId))); } - default -> searchClient.softDeleteOrRestoreChildren( - indexMapping.getChildAliases(clusterAlias), - scriptTxt, - List.of(new ImmutablePair<>(entityType + ".id", docId))); + default -> { + List indexNames = indexMapping.getChildAliases(clusterAlias); + if (!indexNames.isEmpty()) { + searchClient.softDeleteOrRestoreChildren( + indexMapping.getChildAliases(clusterAlias), + scriptTxt, + List.of(new ImmutablePair<>(entityType + ".id", docId))); + } + } } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java index 99c4c1b7945..227112e2206 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java @@ -712,4 +712,13 @@ public final class EntityUtil { public static String encodeEntityFqn(String fqn) { return URLEncoder.encode(fqn.trim(), StandardCharsets.UTF_8).replace("+", "%20"); } + + public static boolean isNullOrEmptyChangeDescription(ChangeDescription changeDescription) { + if (changeDescription == null) { + return true; + } + return changeDescription.getFieldsAdded().isEmpty() + && changeDescription.getFieldsUpdated().isEmpty() + && changeDescription.getFieldsDeleted().isEmpty(); + } }