]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 Fix migration when no changelog
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Jun 2014 14:57:21 +0000 (16:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Jun 2014 15:08:04 +0000 (17:08 +0200)
sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ChangeLogMigration.java
sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ChangeLogMigrationTest.java
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ChangeLogMigrationTest/migrate_when_no_changelog.xml [new file with mode: 0644]

index 968069623241c434f69e281c35242f74f3b694c5..2d1576a34d5125beae7e666425d3eefb9bc3a69a 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.server.db.migrations.v44;
 
 import org.apache.commons.dbutils.DbUtils;
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.activity.Activity;
 import org.sonar.core.activity.db.ActivityDto;
@@ -49,6 +51,8 @@ import java.util.Date;
  */
 public class ChangeLogMigration implements DatabaseMigration {
 
+  private static final Logger LOGGER = LoggerFactory.getLogger(ChangeLogMigration.class);
+
   private static final String PARAM_VALUE = "param_value";
   private static final String PARAM_NAME = "param_name";
   private static final String RULE_NAME = "rule_name";
@@ -62,7 +66,6 @@ public class ChangeLogMigration implements DatabaseMigration {
   private DbSession session;
   private final DbClient db;
 
-
   private final String allActivation =
     "select" +
       "   rule_change.id," +
@@ -153,30 +156,33 @@ public class ChangeLogMigration implements DatabaseMigration {
       connection = db.database().getDataSource().getConnection();
       ResultSet result = connection.createStatement().executeQuery(sql);
 
-      //startCase
-      result.next();
-      int currentId = result.getInt("id");
-      Date currentTimeStamp = result.getTimestamp(CREATED_AT);
-      String currentAuthor = getAuthor(result);
-      ActiveRuleChange ruleChange = newActiveRuleChance(type, result);
-      processRuleChange(ruleChange, result);
-
-      while (result.next()) {
-        int id = result.getInt("id");
-        if (id != currentId) {
-          saveActiveRuleChange(ruleChange, currentAuthor, currentTimeStamp);
-          currentId = id;
-          currentTimeStamp = result.getTimestamp(CREATED_AT);
-          currentAuthor = getAuthor(result);
-          ruleChange = newActiveRuleChance(type, result);
-        }
+      // startCase
+      boolean hasNext = result.next();
+      if (hasNext) {
+        int currentId = result.getInt("id");
+        Date currentTimeStamp = result.getTimestamp(CREATED_AT);
+        String currentAuthor = getAuthor(result);
+        ActiveRuleChange ruleChange = newActiveRuleChance(type, result);
         processRuleChange(ruleChange, result);
+
+        while (result.next()) {
+          int id = result.getInt("id");
+          if (id != currentId) {
+            saveActiveRuleChange(ruleChange, currentAuthor, currentTimeStamp);
+            currentId = id;
+            currentTimeStamp = result.getTimestamp(CREATED_AT);
+            currentAuthor = getAuthor(result);
+            ruleChange = newActiveRuleChance(type, result);
+          }
+          processRuleChange(ruleChange, result);
+        }
+        // save the last
+        saveActiveRuleChange(ruleChange, currentAuthor, currentTimeStamp);
       }
-      //save the last
-      saveActiveRuleChange(ruleChange, currentAuthor, currentTimeStamp);
 
     } catch (Exception e) {
-      e.printStackTrace();
+      throw new IllegalStateException(e);
+
     } finally {
       DbUtils.closeQuietly(connection);
     }
@@ -195,7 +201,6 @@ public class ChangeLogMigration implements DatabaseMigration {
     }
   }
 
-
   private void saveActiveRuleChange(ActiveRuleChange ruleChange, String author, Date currentTimeStamp) {
     ActivityDto activity = ActivityDto.createFor(ruleChange);
     activity.setType(Activity.Type.ACTIVE_RULE);
@@ -209,7 +214,7 @@ public class ChangeLogMigration implements DatabaseMigration {
     try {
       ruleChange.setSeverity(SeverityUtil.getSeverityFromOrdinal(result.getInt(SEVERITY)));
     } catch (Exception e) {
-      //System.out.println("e.getMessage() = " + e.getMessage());
+      // System.out.println("e.getMessage() = " + e.getMessage());
     }
     try {
       String param_value = result.getString(PARAM_VALUE);
@@ -218,7 +223,7 @@ public class ChangeLogMigration implements DatabaseMigration {
         ruleChange.setParameter(param_name, param_value);
       }
     } catch (Exception e) {
-      //System.out.println("e.getMessage() = " + e.getMessage());
+      // System.out.println("e.getMessage() = " + e.getMessage());
     }
   }
 
index 0fcc7f2a5d0017e5610814dd197faff2ecf3caef..405ef8bcde16fa52e9e049c477c5bff8e62782b0 100644 (file)
@@ -71,4 +71,12 @@ public class ChangeLogMigrationTest {
 
     assertThat(dao.findAll(session)).hasSize(5);
   }
-}
\ No newline at end of file
+
+  @Test
+  public void migrate_when_no_changelog() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_when_no_changelog.xml");
+    migration.execute();
+
+    assertThat(dao.findAll(session)).isEmpty();
+  }
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ChangeLogMigrationTest/migrate_when_no_changelog.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ChangeLogMigrationTest/migrate_when_no_changelog.xml
new file mode 100644 (file)
index 0000000..9fb3885
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<dataset>
+
+  <rules_profiles id="10" name="DB_Testing" language="java"/>
+
+  <rules id="1" plugin_name="xoo" plugin_rule_key="S001"/>
+  <rules id="2" plugin_name="xoo" plugin_rule_key="S002"/>
+
+  <rules_parameters id="11" rule_id="1" param_type="STRING" name="param1"/>
+  <rules_parameters id="12" rule_id="1" param_type="STRING" name="param2"/>
+  <rules_parameters id="13" rule_id="1" param_type="STRING" name="param3"/>
+
+  <rules_parameters id="21" rule_id="2" param_type="STRING" name="PARAM1"/>
+  <rules_parameters id="22" rule_id="2" param_type="STRING" name="PARAM2"/>
+
+  <users id="1111" name="Administrator" login="admin"/>
+
+</dataset>