Entity ID types do not match types in database definition 68/134568/2
authorToineSiebelink <toine.siebelink@est.tech>
Wed, 17 May 2023 15:40:44 +0000 (16:40 +0100)
committerToineSiebelink <toine.siebelink@est.tech>
Wed, 17 May 2023 15:40:44 +0000 (16:40 +0100)
As Liquibase is used, JPA does not manage database definition.
Within Liquibase changelog, only Anchor and Fragment have BIGINT (Long)
IDs. Other entities have INTEGER IDs.

- Change AnchorEntity/AnchorRepository ID type to Long
- Change YangResourceEntity/YangResourceRepository and
  ModuleReferenceRepository ID to Integer
- Modify datatypes of foreign key references in liquibase changelog to
  use correct types for dataspace_id, schema_set_id, yang_resource_id
- Update test liquibase changelog

Issue-ID: CPS-1676
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I26de6712b2e74d166fc5a48b1f8149de6a1e5199
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
18 files changed:
cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java
cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java
cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentExtract.java
cps-ri/src/main/java/org/onap/cps/spi/entities/YangResourceEntity.java
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
cps-ri/src/main/java/org/onap/cps/spi/repository/ModuleReferenceRepository.java
cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepository.java
cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java
cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java
cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java
cps-ri/src/main/java/org/onap/cps/spi/utils/SessionManager.java
cps-ri/src/main/resources/changelog/changelog-master.yaml
cps-ri/src/main/resources/changelog/db/changes/20-change-foreign-key-id-types-to-integer.yaml [new file with mode: 0644]
integration-test/src/test/resources/liquibase/test-changelog.yaml

index b59150f..403741a 100644 (file)
@@ -55,7 +55,7 @@ public class AnchorEntity implements Serializable {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
-    private Integer id;
+    private Long id;
 
     @NotNull
     @Column
index c1b1322..697eb8d 100644 (file)
@@ -58,7 +58,7 @@ public class FragmentEntityArranger {
      * @return a collection of FragmentEntities (trees) with descendants.
      */
     public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
-            final Map<Integer, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
+            final Map<Long, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
             final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
index 3aa19e6..50be3c7 100644 (file)
@@ -24,7 +24,7 @@ public interface FragmentExtract {
 
     Long getId();
 
-    Integer getAnchorId();
+    Long getAnchorId();
 
     String getXpath();
 
index 53ae820..71d97a3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
- *  Modifications Copyright (C) 2021-2022 Nordix Foundation
+ *  Modifications Copyright (C) 2021-2023 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public class YangResourceEntity implements Serializable {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
-    private Long id;
+    private Integer id;
 
     @NotNull
     @Column
index 890941b..fdb0148 100644 (file)
@@ -346,10 +346,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
             fragmentRepository.findExtractsWithDescendantsByIds(fragmentEntityIds, fetchDescendantsOption.getDepth());
 
         if (anchorEntity == ALL_ANCHORS) {
-            final Collection<Integer> anchorIds = fragmentExtracts.stream()
+            final Collection<Long> anchorIds = fragmentExtracts.stream()
                 .map(FragmentExtract::getAnchorId).collect(Collectors.toSet());
             final List<AnchorEntity> anchorEntities = anchorRepository.findAllById(anchorIds);
-            final Map<Integer, AnchorEntity> anchorEntityPerId = anchorEntities.stream()
+            final Map<Long, AnchorEntity> anchorEntityPerId = anchorEntities.stream()
                 .collect(Collectors.toMap(AnchorEntity::getId, Function.identity()));
             return FragmentEntityArranger.toFragmentEntityTreesAcrossAnchors(anchorEntityPerId, fragmentExtracts);
         }
index 5eda15a..e149a1f 100755 (executable)
@@ -172,7 +172,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final SchemaSetEntity schemaSetEntity =
                 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
-        final List<Long> allYangResourceIds =
+        final List<Integer> allYangResourceIds =
             yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences);
         yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), allYangResourceIds);
     }
index fe9ff9e..5bb5857 100755 (executable)
@@ -33,7 +33,7 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
 @Repository
-public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
+public interface AnchorRepository extends JpaRepository<AnchorEntity, Long> {
 
     Optional<AnchorEntity> findByDataspaceAndName(DataspaceEntity dataspaceEntity, String name);
 
index 2691be9..425a726 100755 (executable)
@@ -47,7 +47,7 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
             new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
     }\r
 \r
-    List<FragmentEntity> findByAnchorIdAndXpathIn(int anchorId, String[] xpaths);\r
+    List<FragmentEntity> findByAnchorIdAndXpathIn(long anchorId, String[] xpaths);\r
 \r
     default List<FragmentEntity> findByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
                                                         final Collection<String> xpaths) {\r
@@ -64,41 +64,41 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
         return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0]));\r
     }\r
 \r
-    boolean existsByAnchorId(int anchorId);\r
+    boolean existsByAnchorId(long anchorId);\r
 \r
     @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")\r
     List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);\r
 \r
     @Modifying\r
     @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true)\r
-    void deleteByAnchorIdIn(@Param("anchorIds") int[] anchorIds);\r
+    void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds);\r
 \r
     default void deleteByAnchorIn(final Collection<AnchorEntity> anchorEntities) {\r
-        deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToInt(id -> id).toArray());\r
+        deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray());\r
     }\r
 \r
     @Modifying\r
     @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
-    void deleteByAnchorIdAndXpaths(@Param("anchorId") int anchorId, @Param("xpaths") String[] xpaths);\r
+    void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths);\r
 \r
-    default void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {\r
+    default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection<String> xpaths) {\r
         deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0]));\r
     }\r
 \r
     @Modifying\r
     @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)",\r
         nativeQuery = true)\r
-    void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") int anchorId,\r
+    void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId,\r
                                          @Param("xpathPatterns") String[] xpathPatterns);\r
 \r
-    default void deleteListsByAnchorIdAndXpaths(int anchorId, Collection<String> xpaths) {\r
+    default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection<String> xpaths) {\r
         final String[] listXpathPatterns = xpaths.stream().map(xpath -> xpath + "[%").toArray(String[]::new);\r
         deleteByAnchorIdAndXpathLikeAny(anchorId, listXpathPatterns);\r
     }\r
 \r
     @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
         nativeQuery = true)\r
-    List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") int anchorId,\r
+    List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
                                                   @Param("xpaths") String[] xpaths);\r
 \r
     default List<String> findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
@@ -125,11 +125,11 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
         nativeQuery = true\r
     )\r
-    List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") int anchorId,\r
+    List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") long anchorId,\r
                                                       @Param("xpaths") String[] xpaths,\r
                                                       @Param("maxDepth") int maxDepth);\r
 \r
-    default List<FragmentExtract> findExtractsWithDescendants(final int anchorId, final Collection<String> xpaths,\r
+    default List<FragmentExtract> findExtractsWithDescendants(final long anchorId, final Collection<String> xpaths,\r
                                                               final int maxDepth) {\r
         return findExtractsWithDescendants(anchorId, xpaths.toArray(new String[0]), maxDepth);\r
     }\r
index ef701bc..15ffa37 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation.
+ *  Copyright (C) 2022-2023 Nordix Foundation.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -25,5 +25,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
 @Repository
-public interface ModuleReferenceRepository extends JpaRepository<YangResourceEntity, Long>, ModuleReferenceQuery {}
+public interface ModuleReferenceRepository extends JpaRepository<YangResourceEntity, Integer>, ModuleReferenceQuery {}
 
index 0361749..aacebd6 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation.
+ *  Copyright (C) 2021-2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,6 @@ import java.util.List;
 
 public interface SchemaSetYangResourceRepository {
 
-    void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceId);
+    void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Integer> yangResourceIds);
 
 }
index c87e15a..0f7f2c0 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2021-2022 Nordix Foundation.
+ *  Copyright (C) 2021-2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,15 +37,15 @@ public class SchemaSetYangResourceRepositoryImpl implements SchemaSetYangResourc
     private EntityManager entityManager;
 
     @Override
-    public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceIds) {
+    public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Integer> yangResourceIds) {
         final Session session = entityManager.unwrap(Session.class);
         session.doWork(connection -> {
             try (PreparedStatement preparedStatement = connection.prepareStatement(
                 "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES ( ?, ?)")) {
                 int sqlQueryCount = 1;
-                for (final long yangResourceId : yangResourceIds) {
+                for (final int yangResourceId : yangResourceIds) {
                     preparedStatement.setInt(1, schemaSetId);
-                    preparedStatement.setLong(2, yangResourceId);
+                    preparedStatement.setInt(2, yangResourceId);
                     preparedStatement.addBatch();
                     if (sqlQueryCount % MAX_INSERT_BATCH_SIZE == 0 || sqlQueryCount == yangResourceIds.size()) {
                         preparedStatement.executeBatch();
index 335c971..9ae32b3 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation.
+ *  Copyright (C) 2022-2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,6 +26,6 @@ import org.onap.cps.spi.model.ModuleReference;
 
 public interface YangResourceNativeRepository {
 
-    List<Long> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences);
+    List<Integer> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences);
 
 }
index 850b274..f09a1a0 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation.
+ *  Copyright (C) 2022-2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -42,14 +42,14 @@ public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepos
 
     @Override
     @Transactional
-    public List<Long> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) {
+    public List<Integer> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) {
         if (moduleReferences.isEmpty()) {
             return Collections.emptyList();
         }
         final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences))
             .unwrap(org.hibernate.query.NativeQuery.class)
-            .addScalar("id", StandardBasicTypes.LONG);
-        final List<Long> yangResourceIds = query.getResultList();
+            .addScalar("id", StandardBasicTypes.INTEGER);
+        final List<Integer> yangResourceIds = query.getResultList();
         if (yangResourceIds.size() != moduleReferences.size()) {
             log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(),
                     yangResourceIds.size());
index 7584ff6..df21ccc 100644 (file)
@@ -32,7 +32,7 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
 @Repository
-public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Long>,
+public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer>,
     YangResourceNativeRepository, SchemaSetYangResourceRepository {
 
     List<YangResourceEntity> findAllByChecksumIn(String[] checksums);
index 6f96cff..eb3c528 100644 (file)
@@ -163,7 +163,7 @@ public class SessionManager {
         final Session session = getSession(sessionId);
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
-        final int anchorId = anchorEntity.getId();
+        final long anchorId = anchorEntity.getId();
         log.debug("Attempting to lock anchor {} for session {}", anchorName, sessionId);
         session.get(AnchorEntity.class, anchorId, LockMode.PESSIMISTIC_WRITE);
         log.info("Anchor {} successfully locked", anchorName);
index 40e23e2..4e6986e 100644 (file)
@@ -54,3 +54,5 @@ databaseChangeLog:
       file: changelog/db/changes/18-cascade-delete-fragment-children.yaml
   - include:
       file: changelog/db/changes/19-delete-not-required-dataspace-id-from-fragment.yaml
+  - include:
+      file: changelog/db/changes/20-change-foreign-key-id-types-to-integer.yaml
diff --git a/cps-ri/src/main/resources/changelog/db/changes/20-change-foreign-key-id-types-to-integer.yaml b/cps-ri/src/main/resources/changelog/db/changes/20-change-foreign-key-id-types-to-integer.yaml
new file mode 100644 (file)
index 0000000..4c0cd9f
--- /dev/null
@@ -0,0 +1,48 @@
+# ============LICENSE_START=======================================================
+# Copyright (C) 2023 Nordix Foundation.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+databaseChangeLog:
+  - changeSet:
+      author: cps
+      id: 20
+      changes:
+        - modifyDataType:
+            columnName: dataspace_id
+            newDataType: INTEGER
+            tableName: schema_set
+        - modifyDataType:
+            columnName: schema_set_id
+            newDataType: INTEGER
+            tableName: schema_set_yang_resources
+        - modifyDataType:
+            columnName: yang_resource_id
+            newDataType: INTEGER
+            tableName: schema_set_yang_resources
+      rollback:
+        - modifyDataType:
+            columnName: dataspace_id
+            newDataType: BIGINT
+            tableName: schema_set
+        - modifyDataType:
+            columnName: schema_set_id
+            newDataType: BIGINT
+            tableName: schema_set_yang_resources
+        - modifyDataType:
+            columnName: yang_resource_id
+            newDataType: BIGINT
+            tableName: schema_set_yang_resources
index f9e2e2f..3f12331 100644 (file)
@@ -150,7 +150,7 @@ databaseChangeLog:
                   constraints:
                     nullable: false
                   name: dataspace_id
-                  type: BIGINT
+                  type: INTEGER
             tableName: schema_set
   - changeSet:
       id: 1-6
@@ -232,12 +232,12 @@ databaseChangeLog:
                   constraints:
                     nullable: false
                   name: schema_set_id
-                  type: BIGINT
+                  type: INTEGER
               - column:
                   constraints:
                     nullable: false
                   name: yang_resource_id
-                  type: BIGINT
+                  type: INTEGER
             tableName: schema_set_yang_resources
   - changeSet:
       id: 1-10