diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-23 14:16:06 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-23 15:09:57 +0200 |
commit | f454ddf26181f113c8b37f55dfacbb98e6aa26fd (patch) | |
tree | 914aa55c474cfff356241155d96654092b85e4e2 | |
parent | 39b8e7628f1c1763ba6db389fa83264dd425ef7c (diff) | |
download | sonarqube-f454ddf26181f113c8b37f55dfacbb98e6aa26fd.tar.gz sonarqube-f454ddf26181f113c8b37f55dfacbb98e6aa26fd.zip |
SONAR-5753 Migrate disabled components
4 files changed, 189 insertions, 41 deletions
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 4cbd1c62997..eda2906dfcb 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 @@ -62,45 +62,10 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { try { Migration50Mapper mapper = session.getMapper(Migration50Mapper.class); - for (Component project : mapper.selectEnabledRootTrkProjects()) { + for (Component project : mapper.selectRootProjects()) { Map<Long, String> uuidByComponentId = newHashMap(); - Map<Long, Component> componentsBySnapshotId = newHashMap(); - - List<Component> components = mapper.selectComponentChildrenForProjects(project.getId()); - components.add(project); - for (Component component : components) { - componentsBySnapshotId.put(component.getSnapshotId(), component); - - component.setUuid(getOrCreateUuid(component.getId(), uuidByComponentId)); - component.setProjectUuid(getOrCreateUuid(project.getId(), uuidByComponentId)); - } - - for (Component component : components) { - String snapshotPath = component.getSnapshotPath(); - StringBuilder moduleUuidPath = new StringBuilder(); - Component lastModule = null; - if (!Strings.isNullOrEmpty(snapshotPath)) { - for (String s : Splitter.on(".").omitEmptyStrings().split(snapshotPath)) { - Long snapshotId = Long.valueOf(s); - Component currentComponent = componentsBySnapshotId.get(snapshotId); - if (currentComponent.getScope().equals(Scopes.PROJECT)) { - lastModule = currentComponent; - moduleUuidPath.append(currentComponent.getUuid()).append("."); - } - } - } - if (moduleUuidPath.length() > 0) { - component.setModuleUuidPath(moduleUuidPath.toString()); - } - - // 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)); - } - - mapper.updateComponentUuids(component); - counter.getAndIncrement(); - } + migrateEnabledComponents(session, mapper, project, uuidByComponentId); + migrateDisabledComponents(session, mapper, project, uuidByComponentId); } session.commit(); @@ -114,6 +79,56 @@ public class PopulateProjectsUuidColumnsMigration implements DatabaseMigration { } } + private void migrateEnabledComponents(DbSession session, Migration50Mapper mapper, Component project, Map<Long, String> uuidByComponentId) { + Map<Long, Component> componentsBySnapshotId = newHashMap(); + + List<Component> components = mapper.selectComponentChildrenForProjects(project.getId()); + components.add(project); + for (Component component : components) { + componentsBySnapshotId.put(component.getSnapshotId(), component); + + component.setUuid(getOrCreateUuid(component.getId(), uuidByComponentId)); + component.setProjectUuid(getOrCreateUuid(project.getId(), uuidByComponentId)); + } + + for (Component component : components) { + String snapshotPath = component.getSnapshotPath(); + StringBuilder moduleUuidPath = new StringBuilder(); + Component lastModule = null; + if (!Strings.isNullOrEmpty(snapshotPath)) { + for (String s : Splitter.on(".").omitEmptyStrings().split(snapshotPath)) { + Long snapshotId = Long.valueOf(s); + Component currentComponent = componentsBySnapshotId.get(snapshotId); + if (currentComponent.getScope().equals(Scopes.PROJECT)) { + lastModule = currentComponent; + moduleUuidPath.append(currentComponent.getUuid()).append("."); + } + } + } + if (moduleUuidPath.length() > 0) { + component.setModuleUuidPath(moduleUuidPath.toString()); + } + + // 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)); + } + + mapper.updateComponentUuids(component); + counter.getAndIncrement(); + } + } + + 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)); + + mapper.updateComponentUuids(component); + counter.getAndIncrement(); + } + } + private static String getOrCreateUuid(Long componentId, Map<Long, String> uuidByComponentId) { String uuid = uuidByComponentId.get(componentId); if (uuid == null) { 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 9c1fcdb67b5..8e774925936 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,6 +104,45 @@ public class PopulateProjectsUuidColumnsMigrationTest { } @Test + public void migrate_disable_components() throws Exception { + db.prepareDbUnit(getClass(), "migrate_disable_components.xml"); + + migration.execute(); + session.commit(); + + Component root = mapper.selectComponentByKey("org.struts:struts"); + assertThat(root.getUuid()).isNotNull(); + + Component module = mapper.selectComponentByKey("org.struts:struts-core"); + assertThat(module.getUuid()).isNotNull(); + assertThat(module.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(module.getModuleUuid()).isNull(); + assertThat(module.getModuleUuidPath()).isNull(); + + Component subModule = mapper.selectComponentByKey("org.struts:struts-db"); + assertThat(subModule.getUuid()).isNotNull(); + assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(subModule.getModuleUuid()).isNull(); + assertThat(subModule.getModuleUuidPath()).isNull(); + + Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts"); + assertThat(directory.getUuid()).isNotNull(); + assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(directory.getModuleUuid()).isNull(); + assertThat(directory.getModuleUuidPath()).isNull(); + + Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java"); + assertThat(file.getUuid()).isNotNull(); + assertThat(file.getProjectUuid()).isEqualTo(root.getUuid()); + // Module and module path will always be null for removed components + assertThat(file.getModuleUuid()).isNull(); + assertThat(file.getModuleUuidPath()).isNull(); + } + + @Test public void migrate_provisioned_project() throws Exception { db.prepareDbUnit(getClass(), "migrate_provisioned_project.xml"); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/migrate_disable_components.xml b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/migrate_disable_components.xml new file mode 100644 index 00000000000..58e6564ebe5 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/db/migrations/v50/PopulateProjectsUuidColumnsMigrationTest/migrate_disable_components.xml @@ -0,0 +1,82 @@ +<dataset> + + <!-- root project --> + <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts" + uuid="[null]" project_uuid="[null]" 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=""/> + + <!-- removed module --> + <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core" + uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + scope="PRJ" qualifier="BRC" long_name="Struts Core" deprecated_kee="[null]" + description="[null]" enabled="[false]" 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="[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="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."/> + + <!--removed sub module --> + <projects id="3" root_id="2" kee="org.struts:struts-db" name="Struts Db" + uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + scope="PRJ" qualifier="BRC" long_name="Struts Db" deprecated_kee="[null]" + description="[null]" enabled="[false]" 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="[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="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."/> + + <!-- removed directory --> + <projects long_name="org.struts" id="4" scope="DIR" qualifier="DIR" kee="org.struts:struts-core:src/org/struts" + uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + name="src/org/struts" root_id="2" + description="[null]" deprecated_kee="[null]" + enabled="[false]" 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="[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="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."/> + + <!-- removed file --> + <projects long_name="org.struts.RequestContext" id="5" scope="FIL" qualifier="FIL" kee="org.struts:struts-core:src/org/struts/RequestContext.java" + uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + 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="5" project_id="5" 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> 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 0c7267c7d7b..dcc9b35d21d 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 @@ -29,7 +29,7 @@ import java.util.List; public interface Migration50Mapper { /** - * Return enabled root projects (Views and Developers are NOT returned) + * Return root projects (Views and Developers are NOT returned) */ @Select("SELECT " + " p.id AS \"id\", " + @@ -39,10 +39,10 @@ public interface Migration50Mapper { " 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 AND p.enabled=${_true}") + " WHERE p.scope = 'PRJ' AND p.qualifier <> 'VW' AND p.qualifier <> 'DEV' AND p.root_id IS NULL") @Result(javaType = Component.class) @Options(statementType = StatementType.PREPARED, resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 200) - List<Component> selectEnabledRootTrkProjects(); + List<Component> selectRootProjects(); @Select("SELECT " + " p.id AS \"id\", " + @@ -58,6 +58,18 @@ public interface Migration50Mapper { @Result(javaType = Component.class) List<Component> selectComponentChildrenForProjects(@Param("id") Long projectId); + /** + * Return disabled children + */ + @Select("SELECT " + + " p.id AS \"id\" " + + "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}") + @Result(javaType = Component.class) + List<Component> selectDisabledComponentChildrenForProjects(@Param("id") Long projectId); + @Select("SELECT " + " p.id AS \"id\", " + " p.uuid AS \"uuid\", " + |