nvazquez commented on code in PR #12780:
URL: https://github.com/apache/cloudstack/pull/12780#discussion_r2917389337


##########
engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java:
##########
@@ -27,4 +35,46 @@ public String[] getUpgradableVersionRange() {
     public String getUpgradedVersion() {
         return "4.22.1.0";
     }
+
+    @Override
+    public void performDataMigration(Connection conn) {
+        removeDuplicateDummyTemplates(conn);
+    }
+
+    private void removeDuplicateDummyTemplates(Connection conn) {
+        List<Long> templateIds = new ArrayList<>();
+        try (PreparedStatement selectStmt = 
conn.prepareStatement(String.format("SELECT id FROM cloud.vm_template WHERE 
name='%s' ORDER BY id ASC", 
UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME))) {
+            ResultSet rs = selectStmt.executeQuery();
+            while (rs.next()) {
+                templateIds.add(rs.getLong(1));
+            }
+
+            if (templateIds.size() <= 1) {
+                return;
+            }
+
+            Long firstTemplateId = templateIds.get(0);
+
+            String updateTemplateSql = "UPDATE cloud.vm_instance SET 
vm_template_id = ? WHERE vm_template_id = ?";
+            String deleteTemplateSql = "DELETE FROM cloud.vm_template WHERE id 
= ?";
+
+            try (PreparedStatement updateTemplateStmt = 
conn.prepareStatement(updateTemplateSql);
+                 PreparedStatement deleteTemplateStmt = 
conn.prepareStatement(deleteTemplateSql)) {
+                for (int i = 1; i < templateIds.size(); i++) {
+                    Long duplicateTemplateId = templateIds.get(i);
+
+                    // Update VM references
+                    updateTemplateStmt.setLong(1, firstTemplateId);
+                    updateTemplateStmt.setLong(2, duplicateTemplateId);
+                    updateTemplateStmt.executeUpdate();
+
+                    // Delete duplicate dummy template
+                    deleteTemplateStmt.setLong(1, duplicateTemplateId);
+                    deleteTemplateStmt.executeUpdate();
+                }
+            }
+        } catch (Exception e) {
+            logger.warn("Failed to clean duplicate 
kvm-default-vm-import-dummy-template entries", e);

Review Comment:
   ```suggestion
               logger.warn("Failed to clean duplicate " + 
UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME + " entries", e);
   ```



##########
engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java:
##########
@@ -27,4 +35,46 @@ public String[] getUpgradableVersionRange() {
     public String getUpgradedVersion() {
         return "4.22.1.0";
     }
+
+    @Override
+    public void performDataMigration(Connection conn) {
+        removeDuplicateDummyTemplates(conn);
+    }
+
+    private void removeDuplicateDummyTemplates(Connection conn) {

Review Comment:
   ```suggestion
       private void removeDuplicateKVMImportTemplates(Connection conn) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to