sureshanaparti commented on code in PR #5797:
URL: https://github.com/apache/cloudstack/pull/5797#discussion_r847071594


##########
engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41610to41700.java:
##########
@@ -83,4 +92,99 @@ public void updateSystemVmTemplates(Connection conn) {
             throw new CloudRuntimeException("Failed to find / register 
SystemVM template(s)");
         }
     }
+
+    private void updateConfigurationGroups(Connection conn) {
+        LOG.debug("Updating configuration groups");
+        try {
+            String stmt = "SELECT name FROM `cloud`.`configuration`";
+            PreparedStatement pstmt = conn.prepareStatement(stmt);
+            ResultSet rs = pstmt.executeQuery();
+
+            while (rs.next()) {
+                String configName = rs.getString(1);
+                if (StringUtils.isBlank(configName)) {
+                    continue;
+                }
+
+                // Get words from the dot notation in the configuration
+                String[] nameWords = configName.split("\\.");
+                if (nameWords.length <= 0) {
+                    continue;
+                }
+
+                for (int index = 0; index < nameWords.length; index++) {
+                    Pair<Long, Long> configGroupAndSubGroup = 
getConfigurationGroupAndSubGroup(conn, nameWords[index]);
+                    if (configGroupAndSubGroup.first() != 1 && 
configGroupAndSubGroup.second() != 1) {
+                        stmt = "UPDATE `cloud`.`configuration` SET group_id = 
?, subgroup_id = ? WHERE name = ?";
+                        pstmt = conn.prepareStatement(stmt);
+                        pstmt.setLong(1, configGroupAndSubGroup.first());
+                        pstmt.setLong(2, configGroupAndSubGroup.second());
+                        pstmt.setString(3, configName);
+                        pstmt.executeUpdate();
+                        break;
+                    }
+                }
+            }
+
+            rs.close();
+            pstmt.close();
+            LOG.debug("Successfully updated configuration groups.");
+        } catch (SQLException e) {
+            String errorMsg = "Failed to update configuration groups due to " 
+ e.getMessage();
+            LOG.error(errorMsg, e);
+            throw new CloudRuntimeException(errorMsg, e);
+        }
+    }
+
+    private Pair<Long, Long> getConfigurationGroupAndSubGroup(Connection conn, 
String name) {
+        Long subGroupId = 1L;
+        Long groupId = 1L;
+        try {
+            String stmt = "SELECT id, group_id FROM 
`cloud`.`configuration_subgroup` WHERE name = ?";
+            PreparedStatement pstmt = conn.prepareStatement(stmt);
+            pstmt.setString(1, name);
+            ResultSet rs = pstmt.executeQuery();
+            if (rs.next()) {
+                subGroupId = rs.getLong(1);
+                groupId = rs.getLong(2);
+            } else {
+                // Try with keywords in the configuration subgroup
+                stmt = "SELECT id, group_id, keywords FROM 
`cloud`.`configuration_subgroup` WHERE keywords IS NOT NULL";
+                pstmt = conn.prepareStatement(stmt);
+                ResultSet rsConfigurationSubGroups = pstmt.executeQuery();
+                while (rsConfigurationSubGroups.next()) {
+                    Long keywordsSubGroupId = 
rsConfigurationSubGroups.getLong(1);
+                    Long keywordsGroupId = rsConfigurationSubGroups.getLong(2);
+                    String keywords = rsConfigurationSubGroups.getString(3);
+                    if(StringUtils.isBlank(keywords)) {
+                        continue;
+                    }
+
+                    String[] configKeywords = keywords.split(",");
+                    if (configKeywords.length <= 0) {
+                        continue;
+                    }
+
+                    List<String> keywordsList = Arrays.asList(configKeywords);
+                    for (String configKeyword : keywordsList) {
+                        if (StringUtils.isNotBlank(configKeyword)) {
+                            configKeyword = configKeyword.strip();
+                            if (configKeyword.equalsIgnoreCase(name)) {
+                                subGroupId = keywordsSubGroupId;
+                                groupId = keywordsGroupId;
+                                return new Pair<Long, Long>(groupId, 
subGroupId);
+                            }
+                        }
+                    }
+                }
+                rsConfigurationSubGroups.close();
+            }
+            rs.close();
+            pstmt.close();
+        } catch (SQLException e) {
+            LOG.error("Failed to get configuration subgroup due to " + 
e.getMessage(), e);

Review Comment:
   set to default group / subgroup if any issues. throwing any error might 
block upgrade.



-- 
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