From 0832a517c9b7cb70d51ba6557094a605a8a9efdc Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Mon, 23 Jan 2017 16:08:49 +0100 Subject: SONAR-8238 when updating a project, clear ALL components from index --- .../java/org/sonar/db/component/ComponentDao.java | 11 +- .../org/sonar/db/component/ComponentMapper.java | 2 +- .../org/sonar/db/component/ComponentMapper.xml | 13 +- .../org/sonar/db/component/ComponentDaoTest.java | 35 ++++ .../ComponentDaoTest/selectForIndexing.xml | 185 +++++++++++++++++++++ 5 files changed, 238 insertions(+), 8 deletions(-) create mode 100644 sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml (limited to 'sonar-db') diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java index 8ec27d1c2a6..0bc027afb7a 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import javax.annotation.Nullable; import org.apache.ibatis.session.ResultHandler; @@ -230,10 +231,14 @@ public class ComponentDao implements Dao { } /** - * Selects all enabled components. The result is not returned (since it is usually too big), but handed over to the handler + * Selects all components that are relevant for indexing. The result is not returned (since it is usually too big), but handed over to the handler + * @param session the database session + * @param projectUuid the project uuid, which is selected with all of its children + * @param handler the action to be applied to every result */ - public void selectAll(DbSession session, ResultHandler handler) { - mapper(session).selectAll(handler); + public void selectForIndexing(DbSession session, @Nullable String projectUuid, ResultHandler handler) { + Objects.requireNonNull(handler); + mapper(session).selectForIndexing(projectUuid, handler); } /** diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java index 7b2e26eab24..a32a4177b58 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -116,7 +116,7 @@ public interface ComponentMapper { long countGhostProjects(Map parameters); - void selectAll(ResultHandler handler); + void selectForIndexing(@Param("projectUuid") @Nullable String projectUuid, ResultHandler handler); void insert(ComponentDto componentDto); diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml index a9d12f01871..92c23601b28 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -414,11 +414,16 @@ - + select + from projects p - where p.enabled=${_true} + where + p.enabled=${_true} + and p.copy_component_uuid is null + + and p.project_uuid=#{projectUuid} + diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java index 039891ac0a1..8d2b2d83c66 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -20,9 +20,14 @@ package org.sonar.db.component; import com.google.common.base.Optional; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; +import org.assertj.core.api.ListAssert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -563,6 +568,36 @@ public class ComponentDaoTest { assertThat(components).extracting("id").containsOnly(1L, 2L, 3L, 4L); } + @Test + public void selectForIndexing_all() { + assertSelectForIndexing(null) + .doesNotContain("DIS7") + .doesNotContain("COPY8") + .containsOnly("U1", "U2", "U3", "U4", "U5", "U6"); + } + + @Test + public void selectForIndexing_project() { + assertSelectForIndexing("U1") + .doesNotContain("DIS7") + .doesNotContain("COPY8") + .containsOnly("U1", "U2", "U3", "U4"); + } + + private ListAssert assertSelectForIndexing(@Nullable String projectUuid) { + db.prepareDbUnit(getClass(), "selectForIndexing.xml"); + + List components = new ArrayList<>(); + underTest.selectForIndexing(dbSession, projectUuid, new ResultHandler() { + + @Override + public void handleResult(ResultContext context) { + components.add((ComponentDto) context.getResultObject()); + } + }); + return assertThat(components).extracting(ComponentDto::uuid); + } + @Test public void insert() { db.prepareDbUnit(getClass(), "empty.xml"); diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml new file mode 100644 index 00000000000..7c4f24eda93 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3