]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5753 Do no migrate already migrated components
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 23 Oct 2014 14:13:46 +0000 (16:13 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 23 Oct 2014 14:13:46 +0000 (16:13 +0200)
server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java
server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest.java
server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_projects.xml [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java

index 91d5ef962fb0b9841802c663eda483100e7ecd88..f7705679effb79d59f0bc1071447664603561b27 100644 (file)
@@ -87,8 +87,8 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration {
       for (Component component : components) {
         componentsBySnapshotId.put(component.getSnapshotId(), component);
 
-        component.setUuid(getOrCreateUuid(component.getId(), uuidByComponentId));
-        component.setProjectUuid(getOrCreateUuid(project.getId(), uuidByComponentId));
+        component.setUuid(getOrCreateUuid(component, uuidByComponentId));
+        component.setProjectUuid(getOrCreateUuid(project, uuidByComponentId));
       }
 
       for (Component component : components) {
@@ -118,25 +118,26 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration {
 
     // Module UUID should contains direct module of a component, but it should be null on the first module
     if (lastModule != null && !lastModule.getId().equals(project.getId())) {
-      component.setModuleUuid(getOrCreateUuid(lastModule.getId(), uuidByComponentId));
+      component.setModuleUuid(getOrCreateUuid(lastModule, uuidByComponentId));
     }
   }
 
   private void migrateDisabledComponents(DbSession session, Migration50Mapper mapper, Component project, Map<Long, String> uuidByComponentId) {
     for (Component component : mapper.selectDisabledComponentChildrenForProjects(project.getId())) {
-      component.setUuid(getOrCreateUuid(component.getId(), uuidByComponentId));
-      component.setProjectUuid(getOrCreateUuid(project.getId(), uuidByComponentId));
+      component.setUuid(getOrCreateUuid(component, uuidByComponentId));
+      component.setProjectUuid(getOrCreateUuid(project, uuidByComponentId));
 
       mapper.updateComponentUuids(component);
       counter.getAndIncrement();
     }
   }
 
-  private static String getOrCreateUuid(Long componentId, Map<Long, String> uuidByComponentId) {
-    String uuid = uuidByComponentId.get(componentId);
+  private static String getOrCreateUuid(Component component, Map<Long, String> uuidByComponentId) {
+    String existingUuid = component.getUuid();
+    String uuid = existingUuid == null ? uuidByComponentId.get(component.getId()) : existingUuid;
     if (uuid == null) {
       String newUuid = UUID.randomUUID().toString();
-      uuidByComponentId.put(componentId, newUuid);
+      uuidByComponentId.put(component.getId(), newUuid);
       return newUuid;
     }
     return uuid;
index 8e77492593620a9d07911dbe55ee69139087cc1d..a2e05857f27e9351e8a3fd77c44a369ca94f1325 100644 (file)
@@ -103,6 +103,29 @@ public class PopulateProjectsUuidColumnsMigrationTest {
     assertThat(ImmutableSet.of(root.getUuid(), module.getUuid(), subModule.getUuid(), directory.getUuid(), file.getUuid())).hasSize(5);
   }
 
+  @Test
+  public void not_migrate_already_migrated_projects() throws Exception {
+    db.prepareDbUnit(getClass(), "not_migrate_already_migrated_projects.xml");
+    session.commit();
+
+    Component root = mapper.selectComponentByKey("org.struts:struts");
+    Component module = mapper.selectComponentByKey("org.struts:struts-core");
+    Component subModule = mapper.selectComponentByKey("org.struts:struts-db");
+    Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts");
+    Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
+    Component removedFile = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext2.java");
+
+    migration.execute();
+    session.commit();
+
+    assertThat(mapper.selectComponentByKey("org.struts:struts").getUuid()).isEqualTo(root.getUuid());
+    assertThat(mapper.selectComponentByKey("org.struts:struts-core").getUuid()).isEqualTo(module.getUuid());
+    assertThat(mapper.selectComponentByKey("org.struts:struts-db").getUuid()).isEqualTo(subModule.getUuid());
+    assertThat(mapper.selectComponentByKey("org.struts:struts-core:src/org/struts").getUuid()).isEqualTo(directory.getUuid());
+    assertThat(mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java").getUuid()).isEqualTo(file.getUuid());
+    assertThat(mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext2.java").getUuid()).isEqualTo(removedFile.getUuid());
+  }
+
   @Test
   public void migrate_disable_components() throws Exception {
     db.prepareDbUnit(getClass(), "migrate_disable_components.xml");
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_projects.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_projects.xml
new file mode 100644 (file)
index 0000000..6a9087f
--- /dev/null
@@ -0,0 +1,99 @@
+<dataset>
+
+  <!-- root project -->
+  <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" />
+  <snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
+             status="P" islast="[true]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path=""/>
+
+  <!-- module -->
+  <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
+            uuid="BCDE" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="ABCD"
+            scope="PRJ" qualifier="BRC" long_name="Struts Core" deprecated_kee="[null]"
+            description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" created_at="2014-06-18" authorization_updated_at="[null]" />
+  <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1"
+             status="P" islast="[true]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="PRJ" qualifier="BRC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path="1."/>
+
+  <!-- sub module -->
+  <projects id="3" root_id="2" kee="org.struts:struts-db" name="Struts Db"
+            uuid="CDEF" project_uuid="ABCD" module_uuid="BCDE" module_uuid_path="ABCD.BCDE."
+            scope="PRJ" qualifier="BRC" long_name="Struts Db" deprecated_kee="[null]"
+            description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" created_at="2014-06-18" authorization_updated_at="[null]" />
+  <snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
+             status="P" islast="[true]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="PRJ" qualifier="BRC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path="1.2."/>
+
+  <!-- directory -->
+  <projects long_name="org.struts" id="4" scope="DIR" qualifier="DIR" kee="org.struts:struts-core:src/org/struts"
+            uuid="DEFG" project_uuid="ABCD" module_uuid="CDEF" module_uuid_path="ABCD.BCDE.CDEF."
+            name="src/org/struts" root_id="2"
+            description="[null]" deprecated_kee="[null]"
+            enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/org/struts" created_at="2014-06-18" authorization_updated_at="[null]" />
+  <snapshots id="4" project_id="4" parent_snapshot_id="3" root_project_id="1" root_snapshot_id="1"
+             status="P" islast="[true]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="DIR" qualifier="PAC" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path="1.2.3."/>
+
+  <!-- file -->
+  <projects long_name="org.struts.RequestContext" id="5" scope="FIL" qualifier="FIL" kee="org.struts:struts-core:src/org/struts/RequestContext.java"
+            uuid="EFGH" project_uuid="ABCD" module_uuid="CDEF" module_uuid_path="ABCD.BCDE.CDEF."
+            name="RequestContext.java" root_id="2"
+            description="[null]" deprecated_kee="[null]"
+            enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java" created_at="2014-06-18" authorization_updated_at="[null]" />
+
+  <snapshots id="5" project_id="5" parent_snapshot_id="4" root_project_id="1" root_snapshot_id="1"
+             status="P" islast="[true]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="FIL" qualifier="CLA" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path="1.2.3.4."/>
+
+  <!-- removed file -->
+  <projects long_name="org.struts.RequestContext2" id="6" scope="FIL" qualifier="FIL" kee="org.struts:struts-core:src/org/struts/RequestContext2.java"
+            uuid="GHIJ" project_uuid="ABCD" module_uuid="CDEF" module_uuid_path="ABCD.BCDE.CDEF."
+            name="RequestContext.java" root_id="2"
+            description="[null]" deprecated_kee="[null]"
+            enabled="[false]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java" created_at="2014-06-18" authorization_updated_at="[null]" />
+
+  <snapshots id="6" project_id="6" parent_snapshot_id="4" root_project_id="1" root_snapshot_id="1"
+             status="P" islast="[false]" purge_status="[null]"
+             period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+             period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+             period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+             period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+             period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+             depth="[null]" scope="FIL" qualifier="CLA" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+             version="[null]" path="1.2.3.4."/>
+
+</dataset>
index dcc9b35d21d985142a7a89960e553b8e490db9a1..ac818ec1150d029d3334df6f2dd521766b6cd87b 100644 (file)
@@ -33,19 +33,24 @@ public interface Migration50Mapper {
    */
   @Select("SELECT " +
     "  p.id AS \"id\", " +
+    "  p.uuid AS \"uuid\", " +
     "  s.root_project_id AS \"projectId\", " +
     "  s.id AS \"snapshotId\", " +
     "  s.path AS \"snapshotPath\", " +
     "  p.scope AS \"scope\" " +
     "FROM projects p " +
     "  LEFT OUTER JOIN snapshots s ON s.project_id = p.id AND s.islast = ${_true} " +
-    "  WHERE p.scope = 'PRJ' AND p.qualifier <> 'VW' AND p.qualifier <> 'DEV' AND p.root_id IS NULL")
+    "  WHERE " +
+    "   p.scope = 'PRJ' " +
+    "   AND p.root_id IS NULL " +
+    "   AND p.qualifier <> 'VW' AND p.qualifier <> 'DEV' ")
   @Result(javaType = Component.class)
   @Options(statementType = StatementType.PREPARED, resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 200)
   List<Component> selectRootProjects();
 
   @Select("SELECT " +
     "  p.id AS \"id\", " +
+    "  p.uuid AS \"uuid\", " +
     "  s.root_project_id AS \"projectId\", " +
     "  s.id AS \"snapshotId\", " +
     "  s.path AS \"snapshotPath\", " +
@@ -53,8 +58,9 @@ public interface Migration50Mapper {
     "FROM projects root " +
     "  INNER JOIN snapshots root_snapshot ON root_snapshot.project_id = root.id AND root_snapshot.islast = ${_true} " +
     "  INNER JOIN snapshots s ON s.root_snapshot_id = root_snapshot.id AND s.islast = ${_true} " +
-    "  INNER JOIN projects p ON p.id = s.project_id" +
-    "  WHERE root.id = #{id}")
+    "  INNER JOIN projects p ON p.id = s.project_id " +
+    "  WHERE root.id = #{id} " +
+    "   AND p.uuid IS NULL ")
   @Result(javaType = Component.class)
   List<Component> selectComponentChildrenForProjects(@Param("id") Long projectId);
 
@@ -62,11 +68,14 @@ public interface Migration50Mapper {
    * Return disabled children
    */
   @Select("SELECT " +
-    "  p.id AS \"id\" " +
+    "  p.id AS \"id\", " +
+    "  p.uuid AS \"uuid\" " +
     "FROM projects p " +
     "  LEFT OUTER JOIN projects root_one ON root_one.id = p.root_id " +
     "  LEFT OUTER JOIN projects root_two ON root_two.id = root_one.root_id " +
-    "  WHERE (root_one.id = #{id} OR root_two.id=#{id}) AND p.enabled=${_false}")
+    "  WHERE (root_one.id = #{id} OR root_two.id=#{id}) " +
+    "   AND p.uuid IS NULL " +
+    "   AND p.enabled=${_false} ")
   @Result(javaType = Component.class)
   List<Component> selectDisabledComponentChildrenForProjects(@Param("id") Long projectId);