From a0c895aa48714cedbad1e18b20f260477970453f Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Tue, 5 Mar 2024 14:52:14 +0100
Subject: [PATCH] Add migration script to transform data to not rely on status
 attribute

Migration script affects content most recent in line of uuid without latest.
Update verification of data migration accordingly.
Report about Naming is not updated accordingly as number of entries per status is important information.

Note that commit is second step in removing status as query parameter.
---
 docker-compose-integrationtest.yml            |  1 +
 .../controller/VerificationController.java    | 36 +++++++++-----
 ...V5__Data_transformation_status_deleted.sql | 48 +++++++++++++++++++
 3 files changed, 72 insertions(+), 13 deletions(-)
 create mode 100644 src/main/resources/db/migration/V5__Data_transformation_status_deleted.sql

diff --git a/docker-compose-integrationtest.yml b/docker-compose-integrationtest.yml
index 844c43a9..c0a14006 100644
--- a/docker-compose-integrationtest.yml
+++ b/docker-compose-integrationtest.yml
@@ -47,6 +47,7 @@ services:
       - ./src/main/resources/db/migration/V2__Commit_Msg_to_Device.sql:/docker-entrypoint-initdb.d/V2__Commit_Msg_to_Device.sql
       - ./src/main/resources/db/migration/V3__Notification_CC_List.sql:/docker-entrypoint-initdb.d/V3__Notification_CC_List.sql
       - ./src/main/resources/db/migration/V4__Schema_data_migration.sql:/docker-entrypoint-initdb.d/V4__Schema_data_migration.sql
+      - ./src/main/resources/db/migration/V5__Data_transformation_status_deleted.sql:/docker-entrypoint-initdb.d/V5__Data_transformation_status_deleted.sql
 
 volumes:
   naming-data:
diff --git a/src/main/java/org/openepics/names/rest/controller/VerificationController.java b/src/main/java/org/openepics/names/rest/controller/VerificationController.java
index b77ac693..9098baeb 100644
--- a/src/main/java/org/openepics/names/rest/controller/VerificationController.java
+++ b/src/main/java/org/openepics/names/rest/controller/VerificationController.java
@@ -385,8 +385,8 @@ public class VerificationController {
             namePartRevision = mapIdNamePartRevision.get(system.getId());
 
             check = namePartRevision != null
-                    && equals(system, namePartRevision, mapUuidMaxIdSystem)
-                    && system.getParentUuid().equals(namePartRevision.getParent().getUuid());
+                    && system.getParentUuid().equals(namePartRevision.getParent().getUuid())
+                    && equals(system, namePartRevision, mapUuidMaxIdSystem);
 
             // add to count
             if (check) {
@@ -404,8 +404,8 @@ public class VerificationController {
             namePartRevision = mapIdNamePartRevision.get(subsystem.getId());
 
             check = namePartRevision != null
-                    && equals(subsystem, namePartRevision, mapUuidMaxIdSubsystem)
-                    && subsystem.getParentUuid().equals(namePartRevision.getParent().getUuid());
+                    && subsystem.getParentUuid().equals(namePartRevision.getParent().getUuid())
+                    && equals(subsystem, namePartRevision, mapUuidMaxIdSubsystem);
 
             // add to count
             if (check) {
@@ -442,8 +442,8 @@ public class VerificationController {
             namePartRevision = mapIdNamePartRevision.get(deviceGroup.getId());
 
             check = namePartRevision != null
-                    && equals(deviceGroup, namePartRevision, mapUuidMaxIdDeviceGroup)
-                    && deviceGroup.getParentUuid().equals(namePartRevision.getParent().getUuid());
+                    && deviceGroup.getParentUuid().equals(namePartRevision.getParent().getUuid())
+                    && equals(deviceGroup, namePartRevision, mapUuidMaxIdDeviceGroup);
 
             // add to count
             if (check) {
@@ -461,8 +461,8 @@ public class VerificationController {
             namePartRevision = mapIdNamePartRevision.get(deviceType.getId());
 
             check = namePartRevision != null
-                    && equals(deviceType, namePartRevision, mapUuidMaxIdDeviceType)
-                    && deviceType.getParentUuid().equals(namePartRevision.getParent().getUuid());
+                    && deviceType.getParentUuid().equals(namePartRevision.getParent().getUuid())
+                    && equals(deviceType, namePartRevision, mapUuidMaxIdDeviceType);
 
             // add to count
             if (check) {
@@ -1051,6 +1051,9 @@ public class VerificationController {
         //     system.processed            = namepartrevision.processdate
         //     system.processed_by         = namepartrevision.processedby_id   (useraccount.id --> useraccount.username)
         //     system.processed_comment    = namepartrevision.processorcomment
+        //
+        //     check if deleted && (cancelled, rejected, pending) &&  no approved in line of uuid, then it might be ok from that perspective
+        //     consider - status, latest, deleted
 
         if (ValidateUtil.isAnyNull(structure, namePartRevision, mapUuidMaxId)) {
             return false;
@@ -1084,9 +1087,16 @@ public class VerificationController {
 
         // latest
         //     true if id = get max id for uuid
-        //     special rules for pending, not consider pending
-        check = check && structure.isLatest() == structure.getId().equals(mapUuidMaxId.get(structure.getUuid()));
-        check = check && structure.isDeleted() == namePartRevision.isDeleted();
+        //     special rules
+        //         for pending, not consider pending
+        //         for latest, deleted but not approved - ok because there are erroneous entries in database
+        //             handled with V5__Data_transformation_status_deleted.sql
+        check = check &&
+                (structure.isLatest() == structure.getId().equals(mapUuidMaxId.get(structure.getUuid()))
+                ||  structure.isLatest() && structure.isDeleted());
+        check = check &&
+                (structure.isDeleted() == namePartRevision.isDeleted()
+                ||  structure.isLatest() && structure.isDeleted());
 
         // date may be in different format for different objects, to be formatted before being compared
         check = check && ((structure.getRequested() == null && namePartRevision.getRequestDate() == null)
@@ -1197,10 +1207,10 @@ public class VerificationController {
         check = check && ((name.getRequested() == null && deviceRevision.getRequestDate() == null)
                             || StringUtils.equals(SDF.format(name.getRequested()), SDF.format(deviceRevision.getRequestDate())));
         check = check && StringUtils.equals(name.getRequestedBy(), deviceRevision.getRequestedBy().getUsername());
-        check = check && name.getRequestedComment() == null;
+        check = check && StringUtils.equals(name.getRequestedComment(), deviceRevision.getProcessorComment());
         check = check && name.getProcessed() == null;
         check = check && name.getProcessedBy() == null;
-        check = check && StringUtils.equals(name.getProcessedComment(), deviceRevision.getProcessorComment());
+        check = check && name.getProcessedComment() == null;
 
         return check;
     }
diff --git a/src/main/resources/db/migration/V5__Data_transformation_status_deleted.sql b/src/main/resources/db/migration/V5__Data_transformation_status_deleted.sql
new file mode 100644
index 00000000..5a172a12
--- /dev/null
+++ b/src/main/resources/db/migration/V5__Data_transformation_status_deleted.sql
@@ -0,0 +1,48 @@
+-- --------------------------------------------------------------------------------
+-- About
+--     transformation script
+--     postgresql 9.6.7
+-- Content
+--     data structures
+--         set most recent content (without latest) to deleted = true
+-- Note
+--     to have most recent in line of uuid without latest as deleted in order to be possible to query and find data when status attribute is removed as query parameter
+--         may affect entries with statuses PENDING, CANCELLED, REJECTED
+--         will not affect entries considered history
+--     strong (!) recommendation to make sure that no items with status pending exist prior to running this script (any such to be approved, cancelled, rejected)
+--     order of items is important
+-- --------------------------------------------------------------------------------
+
+-- --------------------------------------------------------------------------------
+-- structures
+--     systemgroup
+--     system
+--     subsystem
+--     discipline
+--     devicegroup
+--     devicetype
+-- --------------------------------------------------------------------------------
+
+update systemgroup s set latest = true, deleted = true where (
+                 not exists (select s2.id from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from systemgroup s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
+
+update system s set latest = true, deleted = true where (
+                 not exists (select s2.id from system s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from system s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
+
+update subsystem s set latest = true, deleted = true where (
+                 not exists (select s2.id from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from subsystem s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
+
+update discipline s set latest = true, deleted = true where (
+                 not exists (select s2.id from discipline s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from discipline s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
+
+update devicegroup s set latest = true, deleted = true where (
+                 not exists (select s2.id from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from devicegroup s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
+
+update devicetype s set latest = true, deleted = true where (
+                 not exists (select s2.id from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=true) and s.id = (select max(s2.id) from devicetype s2 where s2."uuid" = s."uuid" and s2.latest=false)
+);
-- 
GitLab