]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5233 Fix potential resource key conflict during 4.2 migration
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 9 Jun 2014 12:11:45 +0000 (14:11 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 9 Jun 2014 12:12:52 +0000 (14:12 +0200)
sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java

index 997a66d36459cf1717c875b8e05cca119200a9b8..99eeb05cac9a51e853ecc5c6eba442fe29041c8c 100644 (file)
@@ -127,7 +127,7 @@ public class ResourceKeyMigration implements BatchComponent {
           logger.warn("Directory with key " + parentOldKey + " matches both " + deprecatedDirectoryKeyMapper.get(parentOldKey) + " and "
             + parentNewKey + ". First match is arbitrary chosen.");
         }
-        resourceModel.setKey(newEffectiveKey);
+        updateKey(resourceModel, newEffectiveKey);
         resourceModel.setDeprecatedKey(oldEffectiveKey);
         logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey);
       } else {
@@ -136,6 +136,24 @@ public class ResourceKeyMigration implements BatchComponent {
     }
   }
 
+  private void updateKey(ResourceModel resourceModel, String newEffectiveKey) {
+    // Look for disabled resource with conflicting key
+    List<ResourceModel> duplicateDisabledResources = session.createQuery(new StringBuilder().append("from ")
+      .append(ResourceModel.class.getSimpleName())
+      .append(" where enabled = false ")
+      .append(" and kee = :kee ")
+      .append(" and qualifier = :qualifier ").toString())
+      .setParameter("kee", newEffectiveKey)
+      .setParameter("qualifier", resourceModel.getQualifier()).getResultList();
+    if (duplicateDisabledResources.size() > 0) {
+      ResourceModel duplicateDisabledResource = duplicateDisabledResources.get(0);
+      String disabledKey = newEffectiveKey + "_renamed_by_resource_key_migration";
+      duplicateDisabledResource.setKey(disabledKey);
+      logger.info(COMPONENT_CHANGED_TO, newEffectiveKey, disabledKey);
+    }
+    resourceModel.setKey(newEffectiveKey);
+  }
+
   private StringBuilder newResourceQuery() {
     return new StringBuilder().append("from ")
       .append(ResourceModel.class.getSimpleName())
@@ -160,7 +178,7 @@ public class ResourceKeyMigration implements BatchComponent {
       String oldEffectiveKey = resourceModel.getKey();
       if (deprecatedDirectoryKeyMapper.containsKey(oldEffectiveKey)) {
         String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey);
-        resourceModel.setKey(newEffectiveKey);
+        updateKey(resourceModel, newEffectiveKey);
         resourceModel.setDeprecatedKey(oldEffectiveKey);
         logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey);
       } else {