]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19125 fixed an issue where one of 10.0 migration did not work on Oracle Databases
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Mon, 8 May 2023 07:44:26 +0000 (09:44 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 8 May 2023 20:03:47 +0000 (20:03 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v100/RemoveOrphanRulesFromQualityProfiles.java

index 38283a3761c476ca2181e7dd35fbd65fe46f3889..60733dcc5c9561c089f601a1d150a3c3f7f48a9b 100644 (file)
@@ -24,8 +24,9 @@ import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.Select;
-import org.sonar.server.platform.db.migration.step.Upsert;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 
 public class RemoveOrphanRulesFromQualityProfiles extends DataChange {
 
@@ -40,36 +41,38 @@ public class RemoveOrphanRulesFromQualityProfiles extends DataChange {
 
   @Override
   protected void execute(Context context) throws SQLException {
-    Select select = context.prepareSelect("select ar.uuid, ar.profile_uuid, ar.rule_uuid from rules_profiles rp " +
+    final String SELECT_QUERY = "select ar.uuid, ar.profile_uuid, ar.rule_uuid from rules_profiles rp " +
       "inner join active_rules ar on ar.profile_uuid = rp.uuid " +
       "inner join rules r on r.uuid = ar.rule_uuid " +
-      "where rp.language != r.language");
-    select.scroll(row -> process(context, row));
-  }
+      "where rp.language != r.language";
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select(SELECT_QUERY);
+
+    final String UPDATE_QUERY = """
+      INSERT INTO qprofile_changes
+      (kee, rules_profile_uuid, change_type, created_at, user_uuid, change_data)
+      VALUES(?, ?, ?, ?, ?, ?)
+        """;
+    massUpdate.update(UPDATE_QUERY);
+    massUpdate.update("delete from active_rules where uuid = ?");
 
-  private void process(Context context, Select.Row row) throws SQLException {
-    Upsert insertQualityProfileChange = insertQualityProfileChange(context, row);
-    Upsert deleteActiveRule = deleteActiveRule(context, row);
-    insertQualityProfileChange.commit();
-    deleteActiveRule.commit();
+    massUpdate.execute((row, update, index) -> {
+      if (index == 0) {
+        prepareUpdateForQProfileChanges(row, update);
+      }
+      if (index == 1) {
+        update.setString(1, row.getString(1));
+      }
+      return true;
+    });
   }
 
-  private Upsert insertQualityProfileChange(Context context, Select.Row row) throws SQLException {
-    return context.prepareUpsert("INSERT INTO qprofile_changes " +
-        "(kee, rules_profile_uuid, change_type, created_at, user_uuid, change_data) " +
-        "VALUES(?, ?, ?, ?, ?, ?);")
-      .setString(1, uuidFactory.create())
-      .setString(2, row.getString(2))
+  private void prepareUpdateForQProfileChanges(Select.Row selectedRow, SqlStatement update) throws SQLException {
+    update.setString(1, uuidFactory.create())
+      .setString(2, selectedRow.getString(2))
       .setString(3, "DEACTIVATED")
       .setLong(4, system2.now())
       .setString(5, null)
-      .setString(6, "ruleUuid=" + row.getString(3))
-      .execute();
-  }
-
-  private static Upsert deleteActiveRule(Context context, Select.Row row) throws SQLException {
-    return context.prepareUpsert("delete from active_rules where uuid = ?")
-      .setString(1, row.getString(1))
-      .execute();
+      .setString(6, "ruleUuid=" + selectedRow.getString(3));
   }
 }