From: Julien Lancelot Date: Tue, 28 Oct 2014 09:24:00 +0000 (+0100) Subject: SONAR-5755 Improve migration re-entrance X-Git-Tag: 5.0-RC1~547 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=37676376879a327a222aded681a3b75c1aacc45b;p=sonarqube.git SONAR-5755 Improve migration re-entrance --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java index 9619825745c..098b4a6ace7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigration.java @@ -36,6 +36,7 @@ import java.util.Timer; import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; +import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; /** @@ -85,14 +86,19 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { List components = mapper.selectComponentChildrenForProjects(project.getId()); components.add(project); + List componentsToMigrate = newArrayList(); for (Component component : components) { componentsBySnapshotId.put(component.getSnapshotId(), component); - component.setUuid(getOrCreateUuid(component, uuidByComponentId)); - component.setProjectUuid(getOrCreateUuid(project, uuidByComponentId)); + // Not migrate components already having an UUID + if (component.getUuid() == null) { + component.setUuid(getOrCreateUuid(component, uuidByComponentId)); + component.setProjectUuid(getOrCreateUuid(project, uuidByComponentId)); + componentsToMigrate.add(component); + } } - for (Component component : components) { + for (Component component : componentsToMigrate) { updateComponent(component, project, componentsBySnapshotId, uuidByComponentId); mapper.updateComponentUuids(component); counter.getAndIncrement(); @@ -115,7 +121,7 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { } if (moduleUuidPath.length() > 0) { // Remove last '.' - moduleUuidPath.deleteCharAt(moduleUuidPath.length()-1); + moduleUuidPath.deleteCharAt(moduleUuidPath.length() - 1); component.setModuleUuidPath(moduleUuidPath.toString()); } @@ -126,9 +132,17 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { } private void migrateDisabledComponents(DbSession session, Migration50Mapper mapper, Component project, Map uuidByComponentId) { - for (Component component : mapper.selectDisabledComponentChildrenForProjects(project.getId())) { + String projectUuid = getOrCreateUuid(project, uuidByComponentId); + for (Component component : mapper.selectDisabledDirectComponentChildrenForProjects(project.getId())) { + component.setUuid(getOrCreateUuid(component, uuidByComponentId)); + component.setProjectUuid(projectUuid); + + mapper.updateComponentUuids(component); + counter.getAndIncrement(); + } + for (Component component : mapper.selectDisabledNoneDirectComponentChildrenForProjects(project.getId())) { component.setUuid(getOrCreateUuid(component, uuidByComponentId)); - component.setProjectUuid(getOrCreateUuid(project, uuidByComponentId)); + component.setProjectUuid(projectUuid); mapper.updateComponentUuids(component); counter.getAndIncrement(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest.java b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest.java index fe4b669c320..53250c0b561 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest.java @@ -104,26 +104,47 @@ public class PopulateProjectsUuidColumnsMigrationTest { } @Test - public void not_migrate_already_migrated_projects() throws Exception { - db.prepareDbUnit(getClass(), "not_migrate_already_migrated_projects.xml"); + public void not_migrate_already_migrated_components() throws Exception { + db.prepareDbUnit(getClass(), "not_migrate_already_migrated_components.xml"); + + migration.execute(); session.commit(); Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isEqualTo("ABCD"); + assertThat(root.getProjectUuid()).isEqualTo("ABCD"); + assertThat(root.getModuleUuid()).isNull(); + assertThat(root.getModuleUuidPath()).isNull(); + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isEqualTo("BCDE"); + assertThat(module.getProjectUuid()).isEqualTo("ABCD"); + assertThat(module.getModuleUuid()).isEqualTo("ABCD"); + assertThat(module.getModuleUuidPath()).isEqualTo("ABCD"); + Component subModule = mapper.selectComponentByKey("org.struts:struts-db"); + assertThat(subModule.getUuid()).isNotNull(); + assertThat(subModule.getProjectUuid()).isEqualTo("ABCD"); + assertThat(subModule.getModuleUuid()).isEqualTo("BCDE"); + assertThat(subModule.getModuleUuidPath()).isEqualTo("ABCD.BCDE"); + 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"); + assertThat(directory.getUuid()).isNotNull(); + assertThat(directory.getProjectUuid()).isEqualTo("ABCD"); + assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(directory.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid()); - migration.execute(); - session.commit(); + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo("ABCD"); + assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid()); + assertThat(file.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid()); - 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()); + Component removedFile = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext2.java"); + assertThat(removedFile.getUuid()).isEqualTo("DCBA"); + assertThat(removedFile.getProjectUuid()).isEqualTo("ABCD"); + assertThat(removedFile.getModuleUuid()).isEqualTo("BCDE"); + assertThat(removedFile.getModuleUuidPath()).isEqualTo("ABCD.BCDE"); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_components.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_components.xml new file mode 100644 index 00000000000..c558f41f9b2 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_components.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 deleted file mode 100644 index 6a9087fc776..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_projects.xml +++ /dev/null @@ -1,99 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java index 1a914314fdf..61c1c43fb91 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/migration/v50/Migration50Mapper.java @@ -34,6 +34,7 @@ public interface Migration50Mapper { @Select("SELECT " + " p.id AS \"id\", " + " p.uuid AS \"uuid\", " + + " p.project_uuid AS \"projectUuid\", " + " s.root_project_id AS \"projectId\", " + " s.id AS \"snapshotId\", " + " s.path AS \"snapshotPath\", " + @@ -50,6 +51,9 @@ public interface Migration50Mapper { @Select("SELECT " + " p.id AS \"id\", " + " p.uuid AS \"uuid\", " + + " p.project_uuid AS \"projectUuid\", " + + " p.module_uuid AS \"moduleUuid\", " + + " p.module_uuid_path AS \"moduleUuidPath\", " + " s.root_project_id AS \"projectId\", " + " s.id AS \"snapshotId\", " + " s.path AS \"snapshotPath\", " + @@ -58,25 +62,40 @@ public interface Migration50Mapper { " 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} " + - " AND p.uuid IS NULL ") + " WHERE root.id = #{id} ") @Result(javaType = Component.class) List selectComponentChildrenForProjects(@Param("id") Long projectId); /** - * Return disabled children + * Return disabled direct children from a project (1st level modules, files on single module project) + * For migration re-entrance, ignore components already having UUID + */ + @Select("SELECT " + + " p.id AS \"id\", " + + " p.uuid AS \"uuid\" " + + "FROM projects p " + + " INNER JOIN projects root ON root.id = p.root_id " + + " WHERE root.id = #{id} " + + " AND p.uuid IS NULL " + + " AND p.enabled=${_false} ") + @Result(javaType = Component.class) + List selectDisabledDirectComponentChildrenForProjects(@Param("id") Long projectId); + + /** + * Return disabled none direct children (2nd level modules and more, files on modules, etc.) + * For migration re-entrance, ignore components already having UUID */ @Select("SELECT " + " 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}) " + + " INNER JOIN projects root_one ON root_one.id = p.root_id " + + " INNER JOIN projects root_two ON root_two.id = root_one.root_id " + + " WHERE root_two.id=#{id} " + " AND p.uuid IS NULL " + " AND p.enabled=${_false} ") @Result(javaType = Component.class) - List selectDisabledComponentChildrenForProjects(@Param("id") Long projectId); + List selectDisabledNoneDirectComponentChildrenForProjects(@Param("id") Long projectId); /** * Return not migrated components