diff options
4 files changed, 40 insertions, 30 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepository.java index 13320bf763..bad68f7e58 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepository.java @@ -26,7 +26,6 @@ import java.util.Collection; * This interface is used in delete fragment entity by id with child using native sql queries. */ public interface FragmentNativeRepository { - void deleteFragmentEntity(long fragmentEntityId); /** * Delete fragment entities for each supplied xpath. diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepositoryImpl.java index 5c5458a039..04b7080def 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentNativeRepositoryImpl.java @@ -31,30 +31,13 @@ import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class FragmentNativeRepositoryImpl implements FragmentNativeRepository { - private static final String DROP_FRAGMENT_CONSTRAINT - = "ALTER TABLE fragment DROP CONSTRAINT fragment_parent_id_fkey;"; - private static final String ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE - = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) " - + "REFERENCES fragment (id) ON DELETE CASCADE;"; - private static final String ADD_ORIGINAL_FRAGMENT_CONSTRAINT - = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) " - + "REFERENCES fragment (id) ON DELETE NO ACTION;"; - @PersistenceContext private final EntityManager entityManager; @Override - public void deleteFragmentEntity(final long fragmentEntityId) { - entityManager.createNativeQuery( - addFragmentConstraintWithDeleteCascade("DELETE FROM fragment WHERE id = ?")) - .setParameter(1, fragmentEntityId) - .executeUpdate(); - } - - @Override public void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) { - final String queryString = addFragmentConstraintWithDeleteCascade( - "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath IN (:parameterPlaceholders))"); + final String queryString = + "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath IN (:parameterPlaceholders))"; executeUpdateWithAnchorIdAndCollection(queryString, anchorId, xpaths); } @@ -62,8 +45,8 @@ public class FragmentNativeRepositoryImpl implements FragmentNativeRepository { public void deleteListsByAnchorIdAndXpaths(final int anchorId, final Collection<String> listXpaths) { final Collection<String> listXpathPatterns = listXpaths.stream().map(listXpath -> listXpath + "[%").collect(Collectors.toSet()); - final String queryString = addFragmentConstraintWithDeleteCascade( - "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath LIKE ANY (array[:parameterPlaceholders]))"); + final String queryString = + "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath LIKE ANY (array[:parameterPlaceholders]))"; executeUpdateWithAnchorIdAndCollection(queryString, anchorId, listXpathPatterns); } @@ -86,12 +69,4 @@ public class FragmentNativeRepositoryImpl implements FragmentNativeRepository { } } - private static String addFragmentConstraintWithDeleteCascade(final String queryString) { - return DROP_FRAGMENT_CONSTRAINT - + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE - + queryString + ";" - + DROP_FRAGMENT_CONSTRAINT - + ADD_ORIGINAL_FRAGMENT_CONSTRAINT; - } - } diff --git a/cps-ri/src/main/resources/changelog/changelog-master.yaml b/cps-ri/src/main/resources/changelog/changelog-master.yaml index 43a54caf64..bf172d8eae 100644 --- a/cps-ri/src/main/resources/changelog/changelog-master.yaml +++ b/cps-ri/src/main/resources/changelog/changelog-master.yaml @@ -50,3 +50,5 @@ databaseChangeLog: file: changelog/db/changes/16-insert-cm-handle-state.yaml - include: file: changelog/db/changes/17-add-index-to-schema-set-yang-resources.yaml + - include: + file: changelog/db/changes/18-cascade-delete-fragment-children.yaml diff --git a/cps-ri/src/main/resources/changelog/db/changes/18-cascade-delete-fragment-children.yaml b/cps-ri/src/main/resources/changelog/db/changes/18-cascade-delete-fragment-children.yaml new file mode 100644 index 0000000000..62a9e2f636 --- /dev/null +++ b/cps-ri/src/main/resources/changelog/db/changes/18-cascade-delete-fragment-children.yaml @@ -0,0 +1,34 @@ +databaseChangeLog: + - changeSet: + author: cps + id: 18 + changes: + - dropForeignKeyConstraint: + baseTableName: fragment + constraintName: fragment_parent_id_fkey + - addForeignKeyConstraint: + baseColumnNames: parent_id + baseTableName: fragment + constraintName: fragment_parent_id_fkey + deferrable: false + initiallyDeferred: false + onDelete: CASCADE + onUpdate: NO ACTION + referencedColumnNames: id + referencedTableName: fragment + validate: true + rollback: + - dropForeignKeyConstraint: + baseTableName: fragment + constraintName: fragment_parent_id_fkey + - addForeignKeyConstraint: + baseColumnNames: parent_id + baseTableName: fragment + constraintName: fragment_parent_id_fkey + deferrable: false + initiallyDeferred: false + onDelete: NO ACTION + onUpdate: NO ACTION + referencedColumnNames: id + referencedTableName: fragment + validate: true |