From da2cece7ea69d19ad47c255083b2c4cd140f38e6 Mon Sep 17 00:00:00 2001 From: David Monichi Date: Sat, 14 Dec 2024 18:10:00 +0100 Subject: [PATCH] [#464] implement extended soft-delete functionality --- .../CompasSclDataPostgreSQLRepository.java | 33 +++++++++++++--- ...flag.sql => V1_5__add_is_deleted_flag.sql} | 0 .../repository/CompasSclDataRepository.java | 4 +- service/pom.xml | 4 ++ .../data/service/CompasSclDataService.java | 8 ++-- .../service/FeatureFlagsConfiguration.java | 15 ++++++++ .../service/CompasSclDataServiceTest.java | 38 ++++++++++++++++--- 7 files changed, 86 insertions(+), 16 deletions(-) rename repository-postgresql/src/main/resources/org/lfenergy/compas/scl/data/repository/postgresql/db/migration/{v1_5__add_is_deleted_flag.sql => V1_5__add_is_deleted_flag.sql} (100%) create mode 100644 service/src/main/java/org/lfenergy/compas/scl/data/service/FeatureFlagsConfiguration.java diff --git a/repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java b/repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java index 110ad5af..535b9847 100644 --- a/repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java +++ b/repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java @@ -308,13 +308,22 @@ insert into scl_label(scl_id, major_version, minor_version, patch_version, label @Override @Transactional(REQUIRED) - public void delete(SclFileType type, UUID id) { - var sql = """ + public void delete(SclFileType type, UUID id, boolean softDelete) { + String sql; + if (softDelete) { + sql = """ UPDATE scl_file - SET scl_file.is_deleted = false + SET scl_file.is_deleted = true + where scl_file.id = ? + and scl_file.type = ? + """; + } else { + sql = """ + delete from scl_file where scl_file.id = ? and scl_file.type = ? """; + } try (var connection = dataSource.getConnection(); var stmt = connection.prepareStatement(sql)) { @@ -328,16 +337,28 @@ public void delete(SclFileType type, UUID id) { @Override @Transactional(REQUIRED) - public void delete(SclFileType type, UUID id, Version version) { - var sql = """ + public void delete(SclFileType type, UUID id, Version version, boolean softDelete) { + String sql; + if (softDelete) { + sql = """ UPDATE scl_file - SET scl_file.is_deleted = false + SET scl_file.is_deleted = true where scl_file.id = ? and scl_file.type = ? and scl_file.major_version = ? and scl_file.minor_version = ? and scl_file.patch_version = ? """; + } else { + sql = """ + delete from scl_file + where scl_file.id = ? + and scl_file.type = ? + and scl_file.major_version = ? + and scl_file.minor_version = ? + and scl_file.patch_version = ? + """; + } try (var connection = dataSource.getConnection(); var stmt = connection.prepareStatement(sql)) { diff --git a/repository-postgresql/src/main/resources/org/lfenergy/compas/scl/data/repository/postgresql/db/migration/v1_5__add_is_deleted_flag.sql b/repository-postgresql/src/main/resources/org/lfenergy/compas/scl/data/repository/postgresql/db/migration/V1_5__add_is_deleted_flag.sql similarity index 100% rename from repository-postgresql/src/main/resources/org/lfenergy/compas/scl/data/repository/postgresql/db/migration/v1_5__add_is_deleted_flag.sql rename to repository-postgresql/src/main/resources/org/lfenergy/compas/scl/data/repository/postgresql/db/migration/V1_5__add_is_deleted_flag.sql diff --git a/repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java b/repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java index 4535fdda..fdf1798a 100644 --- a/repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java +++ b/repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java @@ -91,7 +91,7 @@ public interface CompasSclDataRepository { * @param type The type of SCL where to find the SCL File * @param id The ID of the SCL File to delete. */ - void delete(SclFileType type, UUID id); + void delete(SclFileType type, UUID id, boolean softDelete); /** * Delete passed versions for a specific SCL File using its ID. @@ -100,5 +100,5 @@ public interface CompasSclDataRepository { * @param id The ID of the SCL File to delete. * @param version The version of that SCL File to delete. */ - void delete(SclFileType type, UUID id, Version version); + void delete(SclFileType type, UUID id, Version version, boolean softDelete); } diff --git a/service/pom.xml b/service/pom.xml index a9d11023..43b2f67b 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -49,6 +49,10 @@ SPDX-License-Identifier: Apache-2.0 log4j-core provided + + org.eclipse.microprofile.config + microprofile-config-api + diff --git a/service/src/main/java/org/lfenergy/compas/scl/data/service/CompasSclDataService.java b/service/src/main/java/org/lfenergy/compas/scl/data/service/CompasSclDataService.java index dbbccd31..e780bdfe 100644 --- a/service/src/main/java/org/lfenergy/compas/scl/data/service/CompasSclDataService.java +++ b/service/src/main/java/org/lfenergy/compas/scl/data/service/CompasSclDataService.java @@ -43,13 +43,15 @@ public class CompasSclDataService { private final CompasSclDataRepository repository; private final ElementConverter converter; private final SclElementProcessor sclElementProcessor; + private final FeatureFlagsConfiguration featureFlagsConfiguration; @Inject public CompasSclDataService(CompasSclDataRepository repository, ElementConverter converter, - SclElementProcessor sclElementProcessor) { + SclElementProcessor sclElementProcessor, FeatureFlagsConfiguration featureFlagsConfiguration) { this.repository = repository; this.converter = converter; this.sclElementProcessor = sclElementProcessor; + this.featureFlagsConfiguration = featureFlagsConfiguration; } /** @@ -215,7 +217,7 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str */ @Transactional(REQUIRED) public void delete(SclFileType type, UUID id) { - repository.delete(type, id); + repository.delete(type, id, featureFlagsConfiguration.isSoftDeleteEnabled()); } /** @@ -227,7 +229,7 @@ public void delete(SclFileType type, UUID id) { */ @Transactional(REQUIRED) public void delete(SclFileType type, UUID id, Version version) { - repository.delete(type, id, version); + repository.delete(type, id, version, featureFlagsConfiguration.isSoftDeleteEnabled()); } /** diff --git a/service/src/main/java/org/lfenergy/compas/scl/data/service/FeatureFlagsConfiguration.java b/service/src/main/java/org/lfenergy/compas/scl/data/service/FeatureFlagsConfiguration.java new file mode 100644 index 00000000..444da518 --- /dev/null +++ b/service/src/main/java/org/lfenergy/compas/scl/data/service/FeatureFlagsConfiguration.java @@ -0,0 +1,15 @@ +package org.lfenergy.compas.scl.data.service; + +import jakarta.enterprise.context.ApplicationScoped; +import org.eclipse.microprofile.config.inject.ConfigProperty; + +@ApplicationScoped +public class FeatureFlagsConfiguration { + + @ConfigProperty(name = "compas.scl-data-service.features.soft-delete-enabled", defaultValue = "false") + boolean softDeleteEnabled; + + public boolean isSoftDeleteEnabled() { + return softDeleteEnabled; + } +} diff --git a/service/src/test/java/org/lfenergy/compas/scl/data/service/CompasSclDataServiceTest.java b/service/src/test/java/org/lfenergy/compas/scl/data/service/CompasSclDataServiceTest.java index 142674b8..30deaccb 100644 --- a/service/src/test/java/org/lfenergy/compas/scl/data/service/CompasSclDataServiceTest.java +++ b/service/src/test/java/org/lfenergy/compas/scl/data/service/CompasSclDataServiceTest.java @@ -48,10 +48,11 @@ class CompasSclDataServiceTest { private final ElementConverter converter = new ElementConverter(); private final SclElementProcessor processor = new SclElementProcessor(); + private final FeatureFlagsConfiguration featureFlagsConfiguration = new FeatureFlagsConfiguration(); @BeforeEach void beforeEach() { - compasSclDataService = new CompasSclDataService(compasSclDataRepository, converter, processor); + compasSclDataService = new CompasSclDataService(compasSclDataRepository, converter, processor, featureFlagsConfiguration); } @Test @@ -304,11 +305,12 @@ void update_WhenCalledWithXMLStringWithoutSCL_ThenCompasExceptionThrown() { void delete_WhenCalledWithoutVersion_ThenRepositoryIsCalled() { var uuid = UUID.randomUUID(); - doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid); + doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.FALSE); + featureFlagsConfiguration.softDeleteEnabled = Boolean.FALSE; compasSclDataService.delete(SCL_TYPE, uuid); - verify(compasSclDataRepository).delete(SCL_TYPE, uuid); + verify(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.FALSE); } @Test @@ -316,11 +318,37 @@ void delete_WhenCalledWithVersion_ThenRepositoryIsCalled() { var uuid = UUID.randomUUID(); var version = new Version(1, 0, 0); - doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version); + doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.FALSE); + featureFlagsConfiguration.softDeleteEnabled = Boolean.FALSE; compasSclDataService.delete(SCL_TYPE, uuid, version); - verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version); + verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.FALSE); + } + + @Test + void deleteWithSoftDelete_WhenCalledWithoutVersion_ThenRepositoryIsCalled() { + var uuid = UUID.randomUUID(); + + doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.TRUE); + + featureFlagsConfiguration.softDeleteEnabled = Boolean.TRUE; + compasSclDataService.delete(SCL_TYPE, uuid); + + verify(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.TRUE); + } + + @Test + void deleteWithSoftDelete_WhenCalledWithVersion_ThenRepositoryIsCalled() { + var uuid = UUID.randomUUID(); + var version = new Version(1, 0, 0); + + doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.TRUE); + + featureFlagsConfiguration.softDeleteEnabled = Boolean.TRUE; + compasSclDataService.delete(SCL_TYPE, uuid, version); + + verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.TRUE); } @Test