From 0dc65cd83f7ba3946372c7dade945b701dbce65b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 6 Dec 2017 14:22:26 +0100 Subject: [PATCH] SONAR-10116 disable clean-up migration on SonarCloud --- .../v70/DeletePersonAndFileMeasures.java | 12 +++++++- .../v70/DeletePersonAndFileMeasuresTest.java | 30 ++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasures.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasures.java index 92ba449f7cd..69ee2bb4881 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasures.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasures.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.db.migration.version.v70; import java.sql.SQLException; +import org.sonar.api.config.Configuration; import org.sonar.db.Database; import org.sonar.db.dialect.H2; import org.sonar.db.dialect.MsSql; @@ -30,12 +31,21 @@ import org.sonar.server.platform.db.migration.step.DataChange; import org.sonar.server.platform.db.migration.step.MassUpdate; public class DeletePersonAndFileMeasures extends DataChange { - public DeletePersonAndFileMeasures(Database db) { + + private final Configuration configuration; + + public DeletePersonAndFileMeasures(Database db, Configuration configuration) { super(db); + this.configuration = configuration; } @Override protected void execute(Context context) throws SQLException { + if (configuration.getBoolean("sonar.sonarcloud.enabled").orElse(false)) { + // clean-up will be done in background so that interruption of service + // is reduced during upgrade + return; + } MassUpdate massUpdate = context.prepareMassUpdate(); massUpdate.select("select uuid from snapshots"); massUpdate.rowPluralName("snapshots"); diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasuresTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasuresTest.java index 2019003f8e0..0908bd3aa24 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasuresTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DeletePersonAndFileMeasuresTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang.math.RandomUtils; import org.junit.Rule; import org.junit.Test; +import org.sonar.api.config.internal.MapSettings; import org.sonar.db.CoreDbTester; import org.sonar.server.platform.db.migration.step.DataChange; @@ -34,8 +35,6 @@ public class DeletePersonAndFileMeasuresTest { @Rule public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonAndFileMeasuresTest.class, "initial.sql"); - private DataChange underTest = new DeletePersonAndFileMeasures(db.database()); - @Test public void delete_file_and_person_measures() throws SQLException { insertComponent("P1", "PRJ", "TRK"); @@ -59,19 +58,42 @@ public class DeletePersonAndFileMeasuresTest { long m11 = insertPersonMeasure("P1", "S2"); long m12 = insertPersonMeasure("F1", "S2"); - underTest.execute(); + run(false); assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4); assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2); assertThatMeasuresAreExactly(m1, m2, m7, m8); // migration is re-entrant - underTest.execute(); + run(false); assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4); assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2); assertThatMeasuresAreExactly(m1, m2, m7, m8); } + @Test + public void migration_is_disabled_on_sonarcloud() throws SQLException { + insertComponent("F1", "FIL", "FIL"); + insertSnapshot("S1", "P1", false); + insertMeasure("F1", "S1"); + insertPersonMeasure("F1", "S1"); + + run(true); + + assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(1); + assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(1); + assertThat(db.countRowsOfTable("PROJECT_MEASURES")).isEqualTo(2); + } + + private void run(boolean sonarCloud) throws SQLException { + MapSettings settings = new MapSettings(); + if (sonarCloud) { + settings.setProperty("sonar.sonarcloud.enabled", true); + } + DataChange underTest = new DeletePersonAndFileMeasures(db.database(), settings.asConfig()); + underTest.execute(); + } + private void assertThatMeasuresAreExactly(long... expectedMeasureIds) { long[] ids = db.select("select id as \"id\" from project_measures") .stream() -- 2.39.5