]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5810 Fix migration when project key does not exists
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Oct 2014 07:38:55 +0000 (08:38 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 31 Oct 2014 07:38:55 +0000 (08:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuid.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest.java
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/after.xml [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/before.xml [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute-result.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data-result.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data.xml [new file with mode: 0644]

index 6d79f726de16e78aed5267c88e10a926c1cea4df..c7e889bac7289e81b00fe2c2a9e4177693121bc3 100644 (file)
@@ -27,6 +27,8 @@ import org.sonar.server.db.migrations.MassUpdate;
 import org.sonar.server.db.migrations.Select;
 import org.sonar.server.db.migrations.SqlStatement;
 
+import javax.annotation.Nullable;
+
 import java.sql.SQLException;
 import java.util.Date;
 
@@ -51,14 +53,14 @@ public class ReplaceIssueFiltersProjectKeyByUuid extends BaseDataChange {
     final Date now = new Date(system.now());
 
     MassUpdate massUpdate = context.prepareMassUpdate();
-    massUpdate.select("SELECT f.id, f.data FROM issue_filters f");
+    massUpdate.select("SELECT f.id, f.data FROM issue_filters f WHERE f.data like '%componentRoots=%'");
     massUpdate.update("UPDATE issue_filters SET data=?, updated_at=? WHERE id=?");
     massUpdate.execute(new MassUpdate.Handler() {
       @Override
       public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
         Long id = row.getLong(1);
         String data = row.getString(2);
-        if (!data.contains(OLD_COMPONENT_ROOTS_FIELDS)) {
+        if (data == null) {
           return false;
         }
         update.setString(1, convertData(context, data));
@@ -76,11 +78,7 @@ public class ReplaceIssueFiltersProjectKeyByUuid extends BaseDataChange {
       String field = fields[i];
       if (field.contains(OLD_COMPONENT_ROOTS_FIELDS)) {
         String[] componentRootValues = field.split("=");
-        if (componentRootValues.length == 2) {
-          newFields.append(convertField(context, componentRootValues[1]));
-        } else {
-          newFields.append(NEW_COMPONENT_ROOTS_FIELDS + "=");
-        }
+        append(context, newFields, componentRootValues.length == 2 ? componentRootValues[1] : null);
       } else {
         newFields.append(field);
       }
@@ -91,9 +89,13 @@ public class ReplaceIssueFiltersProjectKeyByUuid extends BaseDataChange {
     return newFields.toString();
   }
 
-  private String convertField(Context context, String projectKey) throws SQLException {
-    String projectUuid = context.prepareSelect("SELECT p.uuid FROM projects p WHERE p.kee=?").setString(1, projectKey).get(Select.STRING_READER);
-    return NEW_COMPONENT_ROOTS_FIELDS + "=" + projectUuid;
+  private void append(Context context, StringBuilder newFields, @Nullable String projectKey) throws SQLException {
+    if (projectKey != null) {
+      String projectUuid = context.prepareSelect("SELECT p.uuid FROM projects p WHERE p.kee=?").setString(1, projectKey).get(Select.STRING_READER);
+      if (projectUuid != null) {
+        newFields.append(NEW_COMPONENT_ROOTS_FIELDS + "=" + projectUuid);
+      }
+    }
   }
 
 }
index 00d1a17cf85d4545d131e939637df6cfbc93af04..c75ff109b04a4310960985e713cd0725bde3f779 100644 (file)
@@ -47,11 +47,20 @@ public class ReplaceIssueFiltersProjectKeyByUuidTest {
 
   @Test
   public void execute() throws Exception {
-    db.prepareDbUnit(getClass(), "before.xml");
+    db.prepareDbUnit(getClass(), "execute.xml");
 
     migration.execute();
 
-    db.assertDbUnit(getClass(), "after.xml", "issue_filters");
+    db.assertDbUnit(getClass(), "execute-result.xml", "issue_filters");
   }
 
-}
\ No newline at end of file
+  @Test
+  public void not_migrate_already_migrated_data() throws Exception {
+    db.prepareDbUnit(getClass(), "not_migrate_already_migrated_data.xml");
+
+    migration.execute();
+
+    db.assertDbUnit(getClass(), "not_migrate_already_migrated_data-result.xml", "issue_filters");
+  }
+
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/after.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/after.xml
deleted file mode 100644 (file)
index ec83825..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<dataset>
-
-  <issue_filters
-      id="1"
-      name="Struts Issues"
-      user_login="stephane"
-      shared="[true]"
-      description="All issues of Struts"
-      data="componentRootUuids=ABCD"
-      created_at="2013-06-10"
-      updated_at="2014-10-29" />
-
-  <issue_filters
-      id="2"
-      name="Open issues"
-      user_login="michael"
-      shared="[false]"
-      description="All open issues"
-      data="statuses=OPEN"
-      created_at="2013-06-10"
-      updated_at="2013-06-10" />
-
-  <issue_filters
-      id="3"
-      name="Sonar Open issues"
-      user_login="michael"
-      shared="[true]"
-      description="All open issues on Sonar"
-      data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
-      created_at="2013-06-10"
-      updated_at="2014-10-29" />
-
-  <issue_filters
-      id="4"
-      name="Bad component roots fields"
-      user_login="michael"
-      shared="[true]"
-      description="All open issues on Sonar"
-      data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
-      created_at="2013-06-10"
-      updated_at="2014-10-29" />
-
-</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/before.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/before.xml
deleted file mode 100644 (file)
index 840bba3..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<dataset>
-
-  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
-            uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
-            description="the description" long_name="Apache Struts"
-            enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
-            created_at="2014-06-18" authorization_updated_at="2014-06-18" />
-
-  <issue_filters
-      id="1"
-      name="Struts Issues"
-      user_login="stephane"
-      shared="[true]"
-      description="All issues of Struts"
-      data="componentRoots=org.struts:struts"
-      created_at="2013-06-10"
-      updated_at="2013-06-10" />
-
-  <issue_filters
-      id="2"
-      name="Open issues"
-      user_login="michael"
-      shared="[false]"
-      description="All open issues"
-      data="statuses=OPEN"
-      created_at="2013-06-10"
-      updated_at="2013-06-10" />
-
-  <issue_filters
-      id="3"
-      name="Sonar Open issues"
-      user_login="michael"
-      shared="[true]"
-      description="All open issues on Sonar"
-      data="statuses=CLOSED|componentRoots=org.struts:struts|resolution=FIXED"
-      created_at="2013-06-10"
-      updated_at="2013-06-10" />
-
-  <issue_filters
-      id="4"
-      name="Bad component roots fields"
-      user_login="michael"
-      shared="[true]"
-      description="All open issues on Sonar"
-      data="statuses=CLOSED|componentRoots=|resolution=FIXED"
-      created_at="2013-06-10"
-      updated_at="2013-06-10" />
-
-</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute-result.xml
new file mode 100644 (file)
index 0000000..bd1e8a1
--- /dev/null
@@ -0,0 +1,63 @@
+<dataset>
+
+  <issue_filters
+      id="1"
+      name="Struts Issues"
+      user_login="stephane"
+      shared="[true]"
+      description="All issues of Struts"
+      data="componentRootUuids=ABCD"
+      created_at="2013-06-10"
+      updated_at="2014-10-29" />
+
+  <issue_filters
+      id="2"
+      name="Open issues"
+      user_login="michael"
+      shared="[false]"
+      description="All open issues"
+      data="statuses=OPEN"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="3"
+      name="Sonar Open issues"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2014-10-29" />
+
+  <issue_filters
+      id="4"
+      name="Bad component roots fields"
+      user_login="michael"
+      shared="[true]"
+      description="Bad component roots fields"
+      data="statuses=CLOSED||resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2014-10-29" />
+
+  <issue_filters
+      id="5"
+      name="Linked on not existing file"
+      user_login="michael"
+      shared="[true]"
+      description="Linked on not existing file"
+      data="statuses=CLOSED||resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2014-10-29" />
+
+  <issue_filters
+      id="6"
+      name="Empty data"
+      user_login="michael"
+      shared="[true]"
+      description="Empty data"
+      data="[null]"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/execute.xml
new file mode 100644 (file)
index 0000000..770f048
--- /dev/null
@@ -0,0 +1,69 @@
+<dataset>
+
+  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
+            description="the description" long_name="Apache Struts"
+            enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
+            created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+
+  <issue_filters
+      id="1"
+      name="Struts Issues"
+      user_login="stephane"
+      shared="[true]"
+      description="All issues of Struts"
+      data="componentRoots=org.struts:struts"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="2"
+      name="Open issues"
+      user_login="michael"
+      shared="[false]"
+      description="All open issues"
+      data="statuses=OPEN"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="3"
+      name="Sonar Open issues"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRoots=org.struts:struts|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="4"
+      name="Bad component roots fields"
+      user_login="michael"
+      shared="[true]"
+      description="Bad component roots fields"
+      data="statuses=CLOSED|componentRoots=|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="5"
+      name="Linked on not existing file"
+      user_login="michael"
+      shared="[true]"
+      description="Linked on not existing file"
+      data="statuses=CLOSED|componentRoots=unknown|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="6"
+      name="Empty data"
+      user_login="michael"
+      shared="[true]"
+      description="Empty data"
+      data="[null]"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data-result.xml
new file mode 100644 (file)
index 0000000..c0e063e
--- /dev/null
@@ -0,0 +1,43 @@
+<dataset>
+
+  <issue_filters
+      id="1"
+      name="Struts Issues"
+      user_login="stephane"
+      shared="[true]"
+      description="All issues of Struts"
+      data="componentRootUuids=ABCD"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="2"
+      name="Open issues"
+      user_login="michael"
+      shared="[false]"
+      description="All open issues"
+      data="statuses=OPEN"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="3"
+      name="Sonar Open issues"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="4"
+      name="Bad component roots fields"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+</dataset>
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/ReplaceIssueFiltersProjectKeyByUuidTest/not_migrate_already_migrated_data.xml
new file mode 100644 (file)
index 0000000..313bbb1
--- /dev/null
@@ -0,0 +1,49 @@
+<dataset>
+
+  <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+            uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
+            description="the description" long_name="Apache Struts"
+            enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
+            created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+
+  <issue_filters
+      id="1"
+      name="Struts Issues"
+      user_login="stephane"
+      shared="[true]"
+      description="All issues of Struts"
+      data="componentRootUuids=ABCD"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="2"
+      name="Open issues"
+      user_login="michael"
+      shared="[false]"
+      description="All open issues"
+      data="statuses=OPEN"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="3"
+      name="Sonar Open issues"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRootUuids=ABCD|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+  <issue_filters
+      id="4"
+      name="Bad component roots fields"
+      user_login="michael"
+      shared="[true]"
+      description="All open issues on Sonar"
+      data="statuses=CLOSED|componentRootUuids=|resolution=FIXED"
+      created_at="2013-06-10"
+      updated_at="2013-06-10" />
+
+</dataset>