diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-17 17:40:02 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-22 10:25:43 +0200 |
commit | e2b192341cea719265c7d0860e4eb12032b1e6e2 (patch) | |
tree | 7993602f3e8b3d7a69da8f680718259b9cbd0c50 /sonar-db | |
parent | 2dbe71c53c39b3b4cf41b2faf0ecec3d8e888809 (diff) | |
download | sonarqube-e2b192341cea719265c7d0860e4eb12032b1e6e2.tar.gz sonarqube-e2b192341cea719265c7d0860e4eb12032b1e6e2.zip |
SONAR-7911 purge scanner_context table when analysis is deleted
Diffstat (limited to 'sonar-db')
7 files changed, 51 insertions, 7 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java index f77dfc2cdc8..03c7abd8b8b 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -166,6 +166,11 @@ class PurgeCommands { session.commit(); profiler.stop(); + profiler.start("deleteAnalyses (scanner_context)"); + analysisUuidsPartitions.forEach(purgeMapper::deleteScannerContexts); + session.commit(); + profiler.stop(); + profiler.start("deleteAnalyses (snapshots)"); analysisUuidsPartitions.forEach(purgeMapper::deleteAnalyses); session.commit(); diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java index 91cb547e29f..aef0971294b 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -38,6 +38,8 @@ public interface PurgeMapper { void deleteAnalysisEvents(@Param("analysisUuids") List<String> analysisUuids); + void deleteScannerContexts(@Param("analysisUuids") List<String> analysisUuids); + void deleteAnalysisMeasures(@Param("analysisUuids") List<String> analysisUuids); void deleteSnapshotMeasures(@Param("analysisUuids") List<String> analysisUuids); diff --git a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index fa9fd4b78c5..8bd1b400caf 100644 --- a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -104,6 +104,15 @@ </foreach> </delete> + <delete id="deleteScannerContexts" parameterType="map"> + delete from scanner_context + where + analysis_uuid in + <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=","> + #{analysisUuid} + </foreach> + </delete> + <delete id="deleteAnalyses" parameterType="map"> delete from snapshots where diff --git a/sonar-db/src/test/java/org/sonar/db/DbTester.java b/sonar-db/src/test/java/org/sonar/db/DbTester.java index 04e229a2d3f..15b62f2d1f6 100644 --- a/sonar-db/src/test/java/org/sonar/db/DbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/DbTester.java @@ -57,6 +57,7 @@ import org.dbunit.operation.DatabaseOperation; import org.junit.rules.ExternalResource; import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.Logger; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Lists.newArrayList; @@ -380,6 +381,7 @@ public class DbTester extends ExternalResource { } } } catch (DatabaseUnitException e) { + e.printStackTrace(); fail(e.getMessage()); } catch (Exception e) { throw translateException("Error while checking results", e); diff --git a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index c49dff51a20..55dd7318bdc 100644 --- a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -24,11 +24,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; +import org.sonar.core.util.CloseableIterator; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -38,6 +40,7 @@ import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; +import static java.util.Collections.singleton; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -49,23 +52,26 @@ public class PurgeDaoTest { private static final String THE_PROJECT_UUID = "P1"; private static final long THE_PROJECT_ID = 1L; - System2 system2 = mock(System2.class); + private System2 system2 = mock(System2.class); @Rule public DbTester dbTester = DbTester.create(system2); - DbClient dbClient = dbTester.getDbClient(); - - DbSession dbSession = dbTester.getSession(); - - PurgeDao underTest = dbTester.getDbClient().purgeDao(); + private DbClient dbClient = dbTester.getDbClient(); + private DbSession dbSession = dbTester.getSession(); + private PurgeDao underTest = dbTester.getDbClient().purgeDao(); @Test public void shouldDeleteAbortedBuilds() { dbTester.prepareDbUnit(getClass(), "shouldDeleteAbortedBuilds.xml"); + insertScannerContext("u1", "bla!"); + insertScannerContext("u2", "blo!"); + underTest.purge(dbSession, newConfigurationWith30Days(), PurgeListener.EMPTY, new PurgeProfiler()); dbSession.commit(); + dbTester.assertDbUnit(getClass(), "shouldDeleteAbortedBuilds-result.xml", "snapshots"); + assertSingleScannerContext("u1"); } @Test @@ -102,8 +108,12 @@ public class PurgeDaoTest { @Test public void shouldDeleteAnalyses() { dbTester.prepareDbUnit(getClass(), "shouldDeleteAnalyses.xml"); + insertScannerContext("u1", "pop"); + insertScannerContext("u3", "pup"); + underTest.deleteAnalyses(dbSession, new PurgeProfiler(), ImmutableList.of(new IdUuidPair(3, "u3"))); dbTester.assertDbUnit(getClass(), "shouldDeleteAnalyses-result.xml", "snapshots"); + assertSingleScannerContext("u1"); } @Test @@ -123,13 +133,17 @@ public class PurgeDaoTest { @Test public void delete_project_and_associated_data() { dbTester.prepareDbUnit(getClass(), "shouldDeleteProject.xml"); + insertScannerContext("u1", "ppppp"); + underTest.deleteProject(dbSession, "A"); dbSession.commit(); + assertThat(dbTester.countRowsOfTable("projects")).isZero(); assertThat(dbTester.countRowsOfTable("snapshots")).isZero(); assertThat(dbTester.countRowsOfTable("issues")).isZero(); assertThat(dbTester.countRowsOfTable("issue_changes")).isZero(); assertThat(dbTester.countRowsOfTable("file_sources")).isZero(); + assertThat(dbTester.countRowsOfTable("scanner_context")).isZero(); } @Test @@ -238,4 +252,15 @@ public class PurgeDaoTest { private static PurgeConfiguration newConfigurationWith30Days(System2 system2, String... disabledComponentUuids) { return new PurgeConfiguration(new IdUuidPair(THE_PROJECT_ID, THE_PROJECT_UUID), new String[0], 30, system2, Arrays.asList(disabledComponentUuids)); } + + private void insertScannerContext(String analysisUuid, String scannerContext) { + dbTester.getDbClient().scannerContextDao().insert(dbSession, analysisUuid, CloseableIterator.from(singleton(scannerContext).iterator())); + dbTester.commit(); + } + + private void assertSingleScannerContext(String expected) { + List<Map<String, Object>> rows = dbTester.select("select analysis_uuid as \"analysisUuid\" from scanner_context"); + assertThat(rows).hasSize(1); + assertThat(rows.iterator().next().get("analysisUuid")).isEqualTo(expected); + } } diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAbortedBuilds-result.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAbortedBuilds-result.xml index bef7ebe6aff..e0dd2801321 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAbortedBuilds-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAbortedBuilds-result.xml @@ -21,7 +21,7 @@ Snapshot 2 has been deleted authorization_updated_at="[null]" id="1" enabled="[true]" - root_id="[null]"/> + root_uuid="P1"/> <!-- past snapshot with status "processed" and already purged --> <snapshots id="1" diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAnalyses.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAnalyses.xml index 1596238b8d1..d4cdd256532 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAnalyses.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldDeleteAnalyses.xml @@ -80,4 +80,5 @@ build_date="1228222680000" version="[null]" /> + </dataset> |