From: Julien Lancelot Date: Thu, 23 Oct 2014 14:13:46 +0000 (+0200) Subject: SONAR-5753 Do no migrate already migrated components X-Git-Tag: 5.0-RC1~618 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fdf072f716d462797a26ceb3981ad85f234c1ddd;p=sonarqube.git SONAR-5753 Do no migrate already migrated components --- 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 91d5ef962fb..f7705679eff 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 @@ -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 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 uuidByComponentId) { - String uuid = uuidByComponentId.get(componentId); + private static String getOrCreateUuid(Component component, Map 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; 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 8e774925936..a2e05857f27 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 @@ -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 index 00000000000..6a9087fc776 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/not_migrate_already_migrated_projects.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 dcc9b35d21d..ac818ec1150 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 @@ -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 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 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 selectDisabledComponentChildrenForProjects(@Param("id") Long projectId);