]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11463 Don't migrate root folders and fix component key migration
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 3 Dec 2018 15:56:30 +0000 (16:56 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 16 Jan 2019 08:43:05 +0000 (09:43 +0100)
12 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/ComponentUuidFactory.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistComponentsStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/ComponentUuidFactoryTest.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentUpdateDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentWithModuleUuidDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDtoTest.java
server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java

index 11459ba43a77b01f5420f5f0ca109b7079a89a56..ccd812b6f14242df09a38041be6cd0ed87d3ce78 100644 (file)
@@ -23,10 +23,12 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.function.Supplier;
 import java.util.stream.Collectors;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
 import org.sonar.core.component.ComponentKeys;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
@@ -38,28 +40,65 @@ import org.sonar.db.component.KeyWithUuidDto;
 public class ComponentUuidFactory {
   private final Map<String, String> uuidsByKey = new HashMap<>();
 
-  public ComponentUuidFactory(DbClient dbClient, DbSession dbSession, String rootKey, Supplier<Map<String, String>> reportModulesPath) {
-    Map<String, String> modulePathsByUuid = loadModulePathsByUuid(dbClient, dbSession, rootKey, reportModulesPath);
+  public ComponentUuidFactory(DbClient dbClient, DbSession dbSession, String rootKey, Map<String, String> reportModulesPath) {
+    Map<String, String> modulePathsByUuid;
 
-    if (modulePathsByUuid.isEmpty()) {
-      // only contains root project or we don't have relative paths for other modules anyway
-      List<KeyWithUuidDto> keys = dbClient.componentDao().selectUuidsByKeyFromProjectKey(dbSession, rootKey);
-      keys.forEach(dto -> uuidsByKey.put(dto.key(), dto.uuid()));
+    if (reportModulesPath.isEmpty()) {
+      noMigration(dbClient, dbSession, rootKey);
     } else {
-      List<ComponentWithModuleUuidDto> dtos = loadComponentsWithModuleUuid(dbClient, dbSession, rootKey);
-      for (ComponentWithModuleUuidDto dto : dtos) {
-        String pathFromRootProject = modulePathsByUuid.get(dto.moduleUuid());
-        String componentPath = StringUtils.isEmpty(pathFromRootProject) ? dto.path() : (pathFromRootProject + "/" + dto.path());
+      modulePathsByUuid = loadModulePathsByUuid(dbClient, dbSession, rootKey, reportModulesPath);
+
+      if (modulePathsByUuid.isEmpty()) {
+        noMigration(dbClient, dbSession, rootKey);
+      } else {
+        doMigration(dbClient, dbSession, rootKey, modulePathsByUuid);
+      }
+    }
+  }
+
+  private void noMigration(DbClient dbClient, DbSession dbSession, String rootKey) {
+    List<KeyWithUuidDto> keys = dbClient.componentDao().selectUuidsByKeyFromProjectKey(dbSession, rootKey);
+    keys.forEach(dto -> uuidsByKey.put(dto.key(), dto.uuid()));
+  }
+
+  private void doMigration(DbClient dbClient, DbSession dbSession, String rootKey, Map<String, String> modulePathsByUuid) {
+    List<ComponentWithModuleUuidDto> dtos = loadComponentsWithModuleUuid(dbClient, dbSession, rootKey);
+    for (ComponentWithModuleUuidDto dto : dtos) {
+      if ("/".equals(dto.path())) {
+        // skip root folders
+        continue;
+      }
+
+      if (Scopes.PROJECT.equals(dto.scope())) {
+        String modulePathFromRootProject = modulePathsByUuid.get(dto.uuid());
+        uuidsByKey.put(ComponentKeys.createEffectiveKey(rootKey, modulePathFromRootProject), dto.uuid());
+      } else {
+        String modulePathFromRootProject = modulePathsByUuid.get(dto.moduleUuid());
+        String componentPath = createComponentPath(dto, modulePathFromRootProject);
         uuidsByKey.put(ComponentKeys.createEffectiveKey(rootKey, componentPath), dto.uuid());
       }
     }
   }
 
+  @CheckForNull
+  private static String createComponentPath(ComponentWithModuleUuidDto dto, @Nullable String modulePathFromRootProject) {
+    if (StringUtils.isEmpty(modulePathFromRootProject)) {
+      return dto.path();
+    }
+
+    if (StringUtils.isEmpty(dto.path())) {
+      // will be the case for modules
+      return modulePathFromRootProject;
+    }
+
+    return modulePathFromRootProject + "/" + dto.path();
+  }
+
   private static List<ComponentWithModuleUuidDto> loadComponentsWithModuleUuid(DbClient dbClient, DbSession dbSession, String rootKey) {
     return dbClient.componentDao().selectComponentsWithModuleUuidFromProjectKey(dbSession, rootKey);
   }
 
-  private static Map<String, String> loadModulePathsByUuid(DbClient dbClient, DbSession dbSession, String rootKey, Supplier<Map<String, String>> reportModulesPath) {
+  private static Map<String, String> loadModulePathsByUuid(DbClient dbClient, DbSession dbSession, String rootKey, Map<String, String> pathByModuleKey) {
     List<ComponentDto> moduleDtos = dbClient.componentDao()
       .selectModulesFromProjectKey(dbSession, rootKey, false).stream()
       .filter(c -> Qualifiers.MODULE.equals(c.qualifier()))
@@ -69,7 +108,6 @@ public class ComponentUuidFactory {
       return Collections.emptyMap();
     }
 
-    Map<String, String> pathByModuleKey = reportModulesPath.get();
     Map<String, String> modulePathByUuid = new HashMap<>();
     for (ComponentDto dto : moduleDtos) {
       String relativePath = pathByModuleKey.get(dto.getKey());
index b1511df95ab0f9b7cb6c275f919fd6d4b9fb9837..9bd4a18fac58cad5336fbc3357dfa8b782cd1d11 100644 (file)
@@ -79,7 +79,7 @@ public class BuildComponentTreeStep implements ComputationStep {
       String rootKey = keyGenerator.generateKey(reportProject, null);
 
       // loads the UUIDs from database. If they don't exist, then generate new ones
-      ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey, reportModulesPath);
+      ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey, reportModulesPath.get());
 
       String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
       SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
index 64c7c45b83869aee076bb51f7d1d0a60136063a0..9ee2cc9c165b095b84e9928401e448462a8d546e 100644 (file)
@@ -100,16 +100,16 @@ public class PersistComponentsStep implements ComputationStep {
       // safeguard, reset all rows to b-changed=false
       dbClient.componentDao().resetBChangedForRootComponentUuid(dbSession, projectUuid);
 
-      Map<String, ComponentDto> existingDtosByKeys = indexExistingDtosByKey(dbSession);
-      boolean isRootPrivate = isRootPrivate(treeRootHolder.getRoot(), existingDtosByKeys);
+      Map<String, ComponentDto> existingDtosByUuids = indexExistingDtosByUuids(dbSession);
+      boolean isRootPrivate = isRootPrivate(treeRootHolder.getRoot(), existingDtosByUuids);
       String mainBranchProjectUuid = loadProjectUuidOfMainBranch();
 
-      // Insert or update the components in database. They are removed from existingDtosByKeys
+      // Insert or update the components in database. They are removed from existingDtosByUuids
       // at the same time.
-      new PathAwareCrawler<>(new PersistComponentStepsVisitor(existingDtosByKeys, dbSession, mainBranchProjectUuid))
+      new PathAwareCrawler<>(new PersistComponentStepsVisitor(existingDtosByUuids, dbSession, mainBranchProjectUuid))
         .visit(treeRootHolder.getRoot());
 
-      disableRemainingComponents(dbSession, existingDtosByKeys.values());
+      disableRemainingComponents(dbSession, existingDtosByUuids.values());
       ensureConsistentVisibility(dbSession, projectUuid, isRootPrivate);
 
       dbSession.commit();
@@ -141,14 +141,13 @@ public class PersistComponentsStep implements ComputationStep {
     dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, projectUuid, isRootPrivate);
   }
 
-  private static boolean isRootPrivate(Component root, Map<String, ComponentDto> existingDtosByKeys) {
-    String rootKey = root.getDbKey();
-    ComponentDto rootDto = existingDtosByKeys.get(rootKey);
+  private static boolean isRootPrivate(Component root, Map<String, ComponentDto> existingDtosByUuids) {
+    ComponentDto rootDto = existingDtosByUuids.get(root.getUuid());
     if (rootDto == null) {
       if (Component.Type.VIEW == root.getType()) {
         return false;
       }
-      throw new IllegalStateException(String.format("The project '%s' is not stored in the database, during a project analysis.", rootKey));
+      throw new IllegalStateException(String.format("The project '%s' is not stored in the database, during a project analysis.", root.getDbKey()));
     }
     return rootDto.isPrivate();
   }
@@ -157,20 +156,20 @@ public class PersistComponentsStep implements ComputationStep {
    * Returns a mutable map of the components currently persisted in database for the project, including
    * disabled components.
    */
-  private Map<String, ComponentDto> indexExistingDtosByKey(DbSession session) {
+  private Map<String, ComponentDto> indexExistingDtosByUuids(DbSession session) {
     return dbClient.componentDao().selectAllComponentsFromProjectKey(session, treeRootHolder.getRoot().getDbKey())
       .stream()
-      .collect(Collectors.toMap(ComponentDto::getDbKey, Function.identity()));
+      .collect(Collectors.toMap(ComponentDto::uuid, Function.identity()));
   }
 
   private class PersistComponentStepsVisitor extends PathAwareVisitorAdapter<ComponentDtoHolder> {
 
-    private final Map<String, ComponentDto> existingComponentDtosByKey;
+    private final Map<String, ComponentDto> existingComponentDtosByUuids;
     private final DbSession dbSession;
     @Nullable
     private final String mainBranchProjectUuid;
 
-    PersistComponentStepsVisitor(Map<String, ComponentDto> existingComponentDtosByKey, DbSession dbSession, @Nullable String mainBranchProjectUuid) {
+    PersistComponentStepsVisitor(Map<String, ComponentDto> existingComponentDtosByUuids, DbSession dbSession, @Nullable String mainBranchProjectUuid) {
       super(
         CrawlerDepthLimit.LEAVES,
         PRE_ORDER,
@@ -192,7 +191,7 @@ public class PersistComponentsStep implements ComputationStep {
             return null;
           }
         });
-      this.existingComponentDtosByKey = existingComponentDtosByKey;
+      this.existingComponentDtosByUuids = existingComponentDtosByUuids;
       this.dbSession = dbSession;
       this.mainBranchProjectUuid = mainBranchProjectUuid;
     }
@@ -240,7 +239,7 @@ public class PersistComponentsStep implements ComputationStep {
     }
 
     private ComponentDto persistComponent(ComponentDto componentDto) {
-      ComponentDto existingComponent = existingComponentDtosByKey.remove(componentDto.getDbKey());
+      ComponentDto existingComponent = existingComponentDtosByUuids.remove(componentDto.uuid());
       if (existingComponent == null) {
         dbClient.componentDao().insert(dbSession, componentDto);
         return componentDto;
@@ -252,6 +251,7 @@ public class PersistComponentsStep implements ComputationStep {
 
         // update the fields in memory in order the PathAwareVisitor.Path
         // to be up-to-date
+        existingComponent.setDbKey(updateDto.getBKey());
         existingComponent.setCopyComponentUuid(updateDto.getBCopyComponentUuid());
         existingComponent.setDescription(updateDto.getBDescription());
         existingComponent.setEnabled(updateDto.isBEnabled());
@@ -371,7 +371,6 @@ public class PersistComponentsStep implements ComputationStep {
       componentDto.setOrganizationUuid(analysisMetadataHolder.getOrganization().getUuid());
       componentDto.setUuid(componentUuid);
       componentDto.setDbKey(componentKey);
-      componentDto.setDeprecatedKey(componentKey);
       componentDto.setMainBranchProjectUuid(mainBranchProjectUuid);
       componentDto.setEnabled(true);
       componentDto.setCreatedAt(new Date(system2.now()));
@@ -415,6 +414,7 @@ public class PersistComponentsStep implements ComputationStep {
   private static Optional<ComponentUpdateDto> compareForUpdate(ComponentDto existing, ComponentDto target) {
     boolean hasDifferences = !StringUtils.equals(existing.getCopyResourceUuid(), target.getCopyResourceUuid()) ||
       !StringUtils.equals(existing.description(), target.description()) ||
+      !StringUtils.equals(existing.getDbKey(), target.getDbKey()) ||
       !existing.isEnabled() ||
       !StringUtils.equals(existing.getUuidPath(), target.getUuidPath()) ||
       !StringUtils.equals(existing.language(), target.language()) ||
index 3d84801d17a3ebf37ba58c1af4de76d24e8e3870..76e1acd6d01f159609577044f76b25e999e951c6 100644 (file)
@@ -30,26 +30,21 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 public class ComponentUuidFactoryTest {
 
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
-  private ReportModulesPath reportModulesPath = mock(ReportModulesPath.class);
-
   @Test
   public void load_uuids_from_existing_components_in_db() {
     ComponentDto project = db.components().insertPrivateProject();
-    ComponentDto module = db.components().insertComponent(ComponentTesting
-      .newModuleDto(project));
-    when(reportModulesPath.get()).thenReturn(Collections.singletonMap(module.getKey(), "module1_path"));
+    ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
+    Map<String, String> reportModulesPath = Collections.singletonMap(module.getKey(), "module1_path");
     ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), reportModulesPath);
 
     assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid());
-    assertThat(underTest.getOrCreateForKey(module.getDbKey())).isNotEqualTo(module.uuid());
+    assertThat(underTest.getOrCreateForKey(module.getDbKey())).isNotIn(project.uuid(), module.uuid());
   }
 
   @Test
@@ -70,8 +65,7 @@ public class ComponentUuidFactoryTest {
     Map<String, String> modulesRelativePaths = new HashMap<>();
     modulesRelativePaths.put("project:module1", "module1_path");
     modulesRelativePaths.put("project:module1:module2", "module1_path/module2_path");
-    when(reportModulesPath.get()).thenReturn(modulesRelativePaths);
-    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), reportModulesPath);
+    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), modulesRelativePaths);
 
     // migrated files
     assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
@@ -81,10 +75,32 @@ public class ComponentUuidFactoryTest {
     assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid());
 
     // old keys with modules don't exist
-    assertThat(underTest.getOrCreateForKey(module1.getDbKey())).isNotEqualTo(module1.uuid());
-    assertThat(underTest.getOrCreateForKey(module2.getDbKey())).isNotEqualTo(module2.uuid());
-    assertThat(underTest.getOrCreateForKey(file1.getDbKey())).isNotEqualTo(file1.uuid());
-    assertThat(underTest.getOrCreateForKey(file2.getDbKey())).isNotEqualTo(file2.uuid());
+    assertThat(underTest.getOrCreateForKey(module1.getDbKey())).isNotIn(project.uuid(), module1.uuid(), module2.uuid(), file1.uuid(), file2.uuid());
+    assertThat(underTest.getOrCreateForKey(module2.getDbKey())).isNotIn(project.uuid(), module1.uuid(), module2.uuid(), file1.uuid(), file2.uuid());
+    assertThat(underTest.getOrCreateForKey(file1.getDbKey())).isNotIn(project.uuid(), module1.uuid(), module2.uuid(), file1.uuid(), file2.uuid());
+    assertThat(underTest.getOrCreateForKey(file2.getDbKey())).isNotIn(project.uuid(), module1.uuid(), module2.uuid(), file1.uuid(), file2.uuid());
+  }
+
+  @Test
+  public void migrate_project_with_root_folders() {
+    ComponentDto project = db.components().insertPrivateProject(dto -> dto.setDbKey("project"));
+    ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
+      .setDbKey("project:module1"));
+    ComponentDto dir1 = db.components().insertComponent(ComponentTesting.newDirectory(module1, "/")
+      .setDbKey("project:module1:/"));
+
+    Map<String, String> modulesRelativePaths = Collections.singletonMap("project:module1", "module1_path");
+    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), modulesRelativePaths);
+
+    // project remains the same
+    assertThat(underTest.getOrCreateForKey(project.getDbKey())).isEqualTo(project.uuid());
+
+    // module migrated to folder
+    assertThat(underTest.getOrCreateForKey("project:module1_path")).isEqualTo(module1.uuid());
+
+    // doesnt exist
+    assertThat(underTest.getOrCreateForKey("project:module1_path/")).isNotIn(project.uuid(), module1.uuid(), dir1.uuid());
+    assertThat(underTest.getOrCreateForKey("project:module1")).isNotIn(project.uuid(), module1.uuid(), dir1.uuid());
   }
 
   @Test
@@ -97,18 +113,17 @@ public class ComponentUuidFactoryTest {
       .setDbKey("project:file1")
       .setEnabled(false)
       .setPath("file1_path"));
-    when(reportModulesPath.get()).thenReturn(Collections.singletonMap("project:module1", "module1_path"));
+    Map<String, String> modulesRelativePaths = Collections.singletonMap("project:module1", "module1_path");
 
-    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), reportModulesPath);
+    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), project.getDbKey(), modulesRelativePaths);
 
-    // migrated files
+    // migrated file
     assertThat(underTest.getOrCreateForKey("project:module1_path/file1_path")).isEqualTo(file1.uuid());
   }
 
   @Test
   public void generate_uuid_if_it_does_not_exist_in_db() {
-    when(reportModulesPath.get()).thenReturn(Collections.emptyMap());
-    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), "theProjectKey", reportModulesPath);
+    ComponentUuidFactory underTest = new ComponentUuidFactory(db.getDbClient(), db.getSession(), "theProjectKey", Collections.emptyMap());
 
     String generatedKey = underTest.getOrCreateForKey("foo");
     assertThat(generatedKey).isNotEmpty();
index f0612417e5f94585e2e86c11e57728395df860d2..a6c6e823bb3151fae15b62d5a23e5b68cf779e5f 100644 (file)
@@ -147,7 +147,6 @@ public class ComponentDto {
   private String scope;
   private String qualifier;
   private String path;
-  private String deprecatedKey;
   private String name;
   private String longName;
   private String language;
@@ -264,16 +263,6 @@ public class ComponentDto {
     return this;
   }
 
-  @CheckForNull
-  public String deprecatedKey() {
-    return deprecatedKey;
-  }
-
-  public ComponentDto setDeprecatedKey(@Nullable String deprecatedKey) {
-    this.deprecatedKey = deprecatedKey;
-    return this;
-  }
-
   /**
    * Return the root project uuid. On a root project, return itself
    */
@@ -483,7 +472,6 @@ public class ComponentDto {
       .append("mainBranchProjectUuid", mainBranchProjectUuid)
       .append("copyComponentUuid", copyComponentUuid)
       .append("path", path)
-      .append("deprecatedKey", deprecatedKey)
       .append("name", name)
       .append("longName", longName)
       .append("language", language)
@@ -509,7 +497,6 @@ public class ComponentDto {
     copy.scope = scope;
     copy.qualifier = qualifier;
     copy.path = path;
-    copy.deprecatedKey = deprecatedKey;
     copy.name = name;
     copy.longName = longName;
     copy.language = language;
index 357f478e61cf61d1fc3b0ac64ceddfbeda3736fd..1c485731437dbf1b44daf54b6d972505a8138370 100644 (file)
@@ -30,6 +30,10 @@ public class ComponentUpdateDto {
    * See https://jira.sonarsource.com/browse/SONAR-7700
    */
   private boolean bChanged;
+  /**
+   * Component keys are normally immutable. But in SQ 7.6 we have to migrate component keys to drop modules.
+   */
+  private String bKey;
   private String bCopyComponentUuid;
   private String bDescription;
   private boolean bEnabled;
@@ -55,6 +59,10 @@ public class ComponentUpdateDto {
     return bChanged;
   }
 
+  public String getBKey() {
+    return bKey;
+  }
+
   @CheckForNull
   public String getBCopyComponentUuid() {
     return bCopyComponentUuid;
@@ -113,6 +121,11 @@ public class ComponentUpdateDto {
     return this;
   }
 
+  public ComponentUpdateDto setBKey(String s) {
+    this.bKey = s;
+    return this;
+  }
+
   public ComponentUpdateDto setBCopyComponentUuid(@Nullable String s) {
     this.bCopyComponentUuid = s;
     return this;
@@ -175,6 +188,7 @@ public class ComponentUpdateDto {
     return new ComponentUpdateDto()
       .setUuid(from.uuid())
       .setBChanged(false)
+      .setBKey(from.getDbKey())
       .setBCopyComponentUuid(from.getCopyResourceUuid())
       .setBDescription(from.description())
       .setBEnabled(from.isEnabled())
index 3c2045f4873f0a1ec695037a72a3afc4823eb568..b7157a0840e881e9a570cfb5e73ba9eecce5b8e7 100644 (file)
@@ -26,11 +26,13 @@ public class ComponentWithModuleUuidDto {
   private final String uuid;
   private final String moduleUuid;
   private final String path;
+  private final String scope;
 
-  public ComponentWithModuleUuidDto(String uuid, String moduleUuid, String path) {
+  public ComponentWithModuleUuidDto(String uuid, String moduleUuid, String path, String scope) {
     this.uuid = uuid;
     this.moduleUuid = moduleUuid;
     this.path = path;
+    this.scope = scope;
   }
 
   public String path() {
@@ -44,4 +46,8 @@ public class ComponentWithModuleUuidDto {
   public String uuid() {
     return uuid;
   }
+
+  public String scope() {
+    return scope;
+  }
 }
index 919e317a67c94014b2b1d47f53c0ce9d8446dc5f..ca0db872a8ab2daeb1ba44f7e671f1454faaa7b9 100644 (file)
@@ -12,7 +12,6 @@
     p.module_uuid_path as moduleUuidPath,
     p.main_branch_project_uuid as mainBranchProjectUuid,
     p.kee as kee,
-    p.deprecated_kee as deprecatedKey,
     p.name as name,
     p.long_name as longName,
     p.description as description,
 
   <select id="selectComponentsWithModuleUuidFromProjectKey" resultType="ComponentWithModuleUuid">
     SELECT
-      p.uuid as uuid, p.module_uuid as moduleUuid, p.path as path
+      p.uuid as uuid, p.module_uuid as moduleUuid, p.path as path, p.scope as scope
     FROM
       projects p
     INNER JOIN
     INSERT INTO projects (
       organization_uuid,
       kee,
-      deprecated_kee,
       uuid,
       uuid_path,
       project_uuid,
       enabled,
       created_at,
       b_changed,
+      deprecated_kee,
       b_copy_component_uuid,
       b_description,
       b_enabled,
     VALUES (
     #{organizationUuid,jdbcType=VARCHAR},
     #{kee,jdbcType=VARCHAR},
-    #{deprecatedKey,jdbcType=VARCHAR},
     #{uuid,jdbcType=VARCHAR},
     #{uuidPath,jdbcType=VARCHAR},
     #{projectUuid,jdbcType=VARCHAR},
     ${_false},
     null,
     null,
+    null,
     ${_false},
     null,
     null,
   <update id="update" parameterType="org.sonar.db.component.ComponentUpdateDto" useGeneratedKeys="false">
     update projects set
     b_changed = #{bChanged,jdbcType=BOOLEAN},
+    <!-- Component key is normally immutable, but since 7.6 deprecated_kee is used as a b_kee to migrate component keys after the drop of modules -->
+    deprecated_kee = #{bKey,jdbcType=VARCHAR},
     b_copy_component_uuid = #{bCopyComponentUuid,jdbcType=VARCHAR},
     b_description = #{bDescription,jdbcType=VARCHAR},
     b_enabled = #{bEnabled,jdbcType=BOOLEAN},
   <update id="updateBEnabledToFalse" parameterType="org.sonar.db.component.ComponentUpdateDto" useGeneratedKeys="false">
     update projects set
     b_changed = ${_true},
+    <!-- Component key is normally immutable, but since 7.6 deprecated_kee is used as a b_kee to migrate component keys after the drop of modules -->
+    deprecated_kee = kee,
     b_copy_component_uuid = copy_component_uuid,
     b_description = description,
     b_enabled = ${_false},
 
   <update id="applyBChangesForRootComponentUuid" parameterType="string" useGeneratedKeys="false">
     update projects set
+    <!-- Component key is normally immutable, but since 7.6 deprecated_kee is used as a b_kee to migrate component keys after the drop of modules -->
+    kee = deprecated_kee,
     copy_component_uuid = b_copy_component_uuid,
     description = b_description,
     enabled = b_enabled,
     path = b_path,
     qualifier = b_qualifier,
     b_changed = ${_false},
+    <!-- Component key is normally immutable, but since 7.6 deprecated_kee is used as a b_kee to migrate component keys after the drop of modules -->
+    deprecated_kee = null,
     b_copy_component_uuid = null,
     b_description = null,
     b_enabled = ${_false},
 
   <update id="resetBChangedForRootComponentUuid" parameterType="map" >
     update projects
-    set b_changed = ${_false}
+    set b_changed = ${_false},
+    <!-- Component key is normally immutable, but since 7.6 deprecated_kee is used as a b_kee to migrate component keys after the drop of modules -->
+    deprecated_kee = kee
     where
     project_uuid = #{projectUuid,jdbcType=VARCHAR} and
     b_changed = ${_true}
index 76ae9566b0c3e544afffcd703266c06dcf90148c..a2a8964e82e957f0275445725d42f6c3aa49fef1 100644 (file)
@@ -1338,6 +1338,7 @@ public class ComponentDaoTest {
 
     underTest.update(dbSession, new ComponentUpdateDto()
       .setUuid("U1")
+      .setBKey("key")
       .setBCopyComponentUuid("copy")
       .setBChanged(true)
       .setBDescription("desc")
@@ -1354,6 +1355,7 @@ public class ComponentDaoTest {
 
     Map<String, Object> row = selectBColumnsForUuid("U1");
     assertThat(row.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
+    assertThat(row.get("bKey")).isEqualTo("key");
     assertThat(row.get("bCopyComponentUuid")).isEqualTo("copy");
     assertThat(row.get("bDescription")).isEqualTo("desc");
     assertThat(row.get("bEnabled")).isIn(true, /* for Oracle */1L, 1);
@@ -1379,6 +1381,7 @@ public class ComponentDaoTest {
 
     Map<String, Object> row1 = selectBColumnsForUuid("U1");
     assertThat(row1.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
+    assertThat(row1.get("bKey")).isEqualTo(dto1.getDbKey());
     assertThat(row1.get("bCopyComponentUuid")).isEqualTo(dto1.getCopyResourceUuid());
     assertThat(row1.get("bDescription")).isEqualTo(dto1.description());
     assertThat(row1.get("bEnabled")).isIn(false, /* for Oracle */0L, 0);
@@ -1393,6 +1396,7 @@ public class ComponentDaoTest {
 
     Map<String, Object> row2 = selectBColumnsForUuid("U2");
     assertThat(row2.get("bChanged")).isIn(true, /* for Oracle */1L, 1);
+    assertThat(row2.get("bKey")).isEqualTo(dto2.getDbKey());
     assertThat(row2.get("bCopyComponentUuid")).isEqualTo(dto2.getCopyResourceUuid());
     assertThat(row2.get("bDescription")).isEqualTo(dto2.description());
     assertThat(row2.get("bEnabled")).isIn(false, /* for Oracle */0L, 0);
@@ -1411,7 +1415,7 @@ public class ComponentDaoTest {
 
   private Map<String, Object> selectBColumnsForUuid(String uuid) {
     return db.selectFirst(
-      "select b_changed as \"bChanged\", b_copy_component_uuid as \"bCopyComponentUuid\", b_description as \"bDescription\", " +
+      "select b_changed as \"bChanged\", deprecated_kee as \"bKey\", b_copy_component_uuid as \"bCopyComponentUuid\", b_description as \"bDescription\", " +
         "b_enabled as \"bEnabled\", b_uuid_path as \"bUuidPath\", b_language as \"bLanguage\", b_long_name as \"bLongName\"," +
         "b_module_uuid as \"bModuleUuid\", b_module_uuid_path as \"bModuleUuidPath\", b_name as \"bName\", " +
         "b_path as \"bPath\", b_qualifier as \"bQualifier\" " +
index a22bd5f90ec32dd93bafadc1218258bdd8fde798..3702e046ca63dcb39da44a029e1a830870993d98 100644 (file)
@@ -34,7 +34,6 @@ public class ComponentDtoTest {
     ComponentDto componentDto = new ComponentDto()
       .setId(1L)
       .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java")
-      .setDeprecatedKey("org.struts:struts-core:src/org/struts/RequestContext.java")
       .setName("RequestContext.java")
       .setLongName("org.struts.RequestContext")
       .setQualifier("FIL")
@@ -47,7 +46,6 @@ public class ComponentDtoTest {
 
     assertThat(componentDto.getId()).isEqualTo(1L);
     assertThat(componentDto.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
-    assertThat(componentDto.deprecatedKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
     assertThat(componentDto.getBranch()).isNull();
     assertThat(componentDto.name()).isEqualTo("RequestContext.java");
     assertThat(componentDto.longName()).isEqualTo("org.struts.RequestContext");
index 0902d3269995defa376fb5984587b285d1c098b4..5ec5c494bdd9362a34b29276c85d3652d701f227 100644 (file)
@@ -115,7 +115,6 @@ public class ComponentUpdater {
       .setModuleUuidPath(ComponentDto.UUID_PATH_SEPARATOR + uuid + ComponentDto.UUID_PATH_SEPARATOR)
       .setProjectUuid(uuid)
       .setDbKey(keyWithBranch)
-      .setDeprecatedKey(keyWithBranch)
       .setName(newComponent.name())
       .setLongName(newComponent.name())
       .setScope(Scopes.PROJECT)
index 96d6351dcf9d1340601f14a9463f1d2e8aeb999b..b759ca71db1857382a3e332b291232f06de242a2 100644 (file)
@@ -82,7 +82,6 @@ public class ComponentUpdaterTest {
 
     ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
     assertThat(loaded.getDbKey()).isEqualTo(DEFAULT_PROJECT_KEY);
-    assertThat(loaded.deprecatedKey()).isEqualTo(DEFAULT_PROJECT_KEY);
     assertThat(loaded.name()).isEqualTo(DEFAULT_PROJECT_NAME);
     assertThat(loaded.longName()).isEqualTo(DEFAULT_PROJECT_NAME);
     assertThat(loaded.qualifier()).isEqualTo(Qualifiers.PROJECT);