diff options
author | Eric Hartmann <hartmann.eric@gmail.com> | 2017-10-19 13:47:56 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.Com> | 2017-10-20 12:26:58 +0200 |
commit | f3de142860a4ebcc91f9f80771964a105f8ed982 (patch) | |
tree | a84e8a2eceb72d0af73fb05a5b62c8f950767087 /server | |
parent | bf2b385a2e8b9bfbc5cc66efb1356394c431b3e2 (diff) | |
download | sonarqube-f3de142860a4ebcc91f9f80771964a105f8ed982.tar.gz sonarqube-f3de142860a4ebcc91f9f80771964a105f8ed982.zip |
SONAR-9142 Remove 'previous_analysis' option
Diffstat (limited to 'server')
9 files changed, 174 insertions, 68 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java index c6eddef0eda..0f06e00377e 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java @@ -32,6 +32,7 @@ public class DbVersion67 implements DbVersion { .add(1833, "Cleanup disabled users", CleanupDisabledUsers.class) .add(1834, "Set WEBHOOK_DELIVERIES.CE_TASK_UUID as nullable", UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.class) .add(1835, "Populate WEBHOOK_DELIVERIES.ANALYSIS_UUID", PopulateAnalysisUuidColumnOnWebhookDeliveries.class) + .add(1836, "Migrate 'previous_analysis' leak periods to 'previous_version'", MigratePreviousAnalysisToPreviousVersion.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersion.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersion.java new file mode 100644 index 00000000000..8f3e954967c --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersion.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.platform.db.migration.version.v67; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class MigratePreviousAnalysisToPreviousVersion extends DataChange { + + public MigratePreviousAnalysisToPreviousVersion(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select id from properties " + + " where prop_key = 'sonar.leak.period' and text_value='previous_analysis'"); + massUpdate.update("update properties " + + " set text_value='previous_version', " + + " clob_value = null " + + " where id = ?"); + massUpdate.rowPluralName("leak periods"); + massUpdate.execute((row, update) -> { + update.setLong(1, row.getLong(1)); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java index dc54ab65e84..de678701f0b 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java @@ -36,7 +36,7 @@ public class DbVersion67Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 6); + verifyMigrationCount(underTest, 7); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest.java new file mode 100644 index 00000000000..11de20ca3ad --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest.java @@ -0,0 +1,104 @@ +package org.sonar.server.platform.db.migration.version.v67;/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.assertj.core.groups.Tuple; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class MigratePreviousAnalysisToPreviousVersionTest { + + private final static String SELECT_PROPERTIES = "SELECT prop_key, is_empty, text_value, clob_value FROM properties"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MigratePreviousAnalysisToPreviousVersionTest.class, "properties.sql"); + + private MigratePreviousAnalysisToPreviousVersion underTest = new MigratePreviousAnalysisToPreviousVersion(db.database()); + + @Test + public void migration_must_update_the_database() throws SQLException { + insertProperty("sonar.leak.period", "any.value_here", null, false); + insertProperty("sonar.leak.period", "previous_version", null, false); + insertProperty("sonar.leak.period", "previous_analysis", null, false); + insertProperty("whatever.property", "nothingspecial", null, false); + insertProperty("whatever.property", null, "nothing.special", false); + + underTest.execute(); + + assertPropertyContainsInAnyOrder( + tuple("sonar.leak.period", "any.value_here", null, false), + tuple("sonar.leak.period", "previous_version", null, false), + tuple("sonar.leak.period", "previous_version", null, false), // Single change + tuple("whatever.property", "nothingspecial", null, false), + tuple("whatever.property", null, "nothing.special", false) + ); + } + + @Test + public void migration_must_be_reentrant() throws SQLException { + insertProperty("sonar.leak.period", "any.value_here", null, false); + insertProperty("sonar.leak.period", "previous_version", null, false); + insertProperty("sonar.leak.period", "previous_analysis", null, false); + insertProperty("whatever.property", "nothingspecial", null, false); + insertProperty("whatever.property", null, "nothing.special", false); + + underTest.execute(); + underTest.execute(); + + assertPropertyContainsInAnyOrder( + tuple("sonar.leak.period", "any.value_here", null, false), + tuple("sonar.leak.period", "previous_version", null, false), + tuple("sonar.leak.period", "previous_version", null, false), // Single change + tuple("whatever.property", "nothingspecial", null, false), + tuple("whatever.property", null, "nothing.special", false) + ); + } + + @Test + public void migration_is_doing_nothing_when_no_data() throws SQLException { + assertThat(db.countRowsOfTable("properties")).isEqualTo(0); + underTest.execute(); + assertThat(db.countRowsOfTable("properties")).isEqualTo(0); + } + + private void insertProperty(String propKey, @Nullable String textValue, @Nullable String clobValue, boolean isEmpty) { + HashMap<String, Object> map = new HashMap<>(); + map.put("PROP_KEY", propKey); + map.put("TEXT_VALUE", textValue); + map.put("CLOB_VALUE", clobValue); + map.put("IS_EMPTY", isEmpty); + db.executeInsert("PROPERTIES", map); + } + + private void assertPropertyContainsInAnyOrder(Tuple... tuples) { + assertThat(db.select(SELECT_PROPERTIES) + .stream() + .map(p -> new Tuple(p.get("PROP_KEY"), p.get("TEXT_VALUE"), p.get("CLOB_VALUE"), p.get("IS_EMPTY"))) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder(tuples); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest/properties.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest/properties.sql new file mode 100644 index 00000000000..d84c238cd48 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/MigratePreviousAnalysisToPreviousVersionTest/properties.sql @@ -0,0 +1,11 @@ +CREATE TABLE "PROPERTIES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "PROP_KEY" VARCHAR(512) NOT NULL, + "RESOURCE_ID" INTEGER, + "USER_ID" INTEGER, + "IS_EMPTY" BOOLEAN NOT NULL, + "TEXT_VALUE" VARCHAR(4000), + "CLOB_VALUE" CLOB, + "CREATED_AT" BIGINT +); +CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java index 7a21affa5ac..606cdcaca78 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java @@ -38,7 +38,6 @@ import org.sonar.server.computation.task.projectanalysis.period.Period; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS; -import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION; import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED; @@ -70,10 +69,6 @@ public class PeriodResolver { if (StringUtils.isBlank(propertyValue)) { return null; } - if (propertyValue.equals(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS)) { - LOG.warn("Leak period is set to deprecated value '{}'. This value will be removed in next SonarQube LTS, please use another one instead.", - LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS); - } Period period = resolve(propertyValue); if (period == null && StringUtils.isNotBlank(propertyValue)) { LOG.debug("Property " + LEAK_PERIOD + " is not valid: " + propertyValue); @@ -91,9 +86,6 @@ public class PeriodResolver { if (date != null) { return findByDate(date); } - if (StringUtils.equals(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, property)) { - return findByPreviousAnalysis(); - } if (StringUtils.equals(LEAK_PERIOD_MODE_PREVIOUS_VERSION, property)) { return findByPreviousVersion(); } @@ -122,16 +114,6 @@ public class PeriodResolver { } @CheckForNull - private Period findByPreviousAnalysis() { - SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setCreatedBefore(analysisDate).setIsLast(true).setSort(BY_DATE, DESC)); - if (snapshot == null) { - return null; - } - LOG.debug("Compare to previous analysis ({})", formatDate(snapshot.getCreatedAt())); - return new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, formatDate(snapshot.getCreatedAt()), snapshot.getCreatedAt(), snapshot.getUuid()); - } - - @CheckForNull private Period findByPreviousVersion() { if (currentVersion == null) { return null; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/NewEffortAggregatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/NewEffortAggregatorTest.java index f567083f309..64a19ba9da0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/NewEffortAggregatorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/NewEffortAggregatorTest.java @@ -45,11 +45,11 @@ import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY; import static org.sonar.api.rules.RuleType.BUG; import static org.sonar.api.rules.RuleType.CODE_SMELL; import static org.sonar.api.rules.RuleType.VULNERABILITY; -import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; public class NewEffortAggregatorTest { - private static final Period PERIOD = new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, null, 1_500_000_000L, "U1"); + private static final Period PERIOD = new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, 1_500_000_000L, "U1"); private static final long[] OLD_ISSUES_DATES = new long[] { PERIOD.getSnapshotDate(), PERIOD.getSnapshotDate() - 1, diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java index 8d45b82f8a7..9a1a708bc6e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java @@ -24,6 +24,7 @@ import java.text.SimpleDateFormat; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.System2; @@ -46,7 +47,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS; -import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION; @@ -66,6 +66,8 @@ public class LoadPeriodsStepTest extends BaseStepTest { public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); @Rule public LogTester logTester = new LogTester(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); private PeriodHolderImpl periodsHolder = new PeriodHolderImpl(); private DbClient dbClient = dbTester.getDbClient(); @@ -232,49 +234,6 @@ public class LoadPeriodsStepTest extends BaseStepTest { } @Test - public void feed_period_by_previous_analysis() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "previous_analysis"); - - underTest.execute(); - - // return analysis from 2008-11-29 - Period period = periodsHolder.getPeriod(); - assertThat(period).isNotNull(); - assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS); - assertThat(period.getModeParameter()).isNotNull(); - assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1004"); - - assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); - assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous analysis ("); - } - - @Test - public void no_period_by_previous_analysis() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.leak.period", "previous_analysis"); - - underTest.execute(); - - assertThat(periodsHolder.getPeriod()).isNull(); - } - - @Test - public void display_warning_log_when_using_previous_analysis() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "previous_analysis"); - - underTest.execute(); - - assertThat(logTester.logs(LoggerLevel.WARN)) - .containsOnly("Leak period is set to deprecated value 'previous_analysis'. This value will be removed in next SonarQube LTS, please use another one instead."); - } - - @Test public void feed_period_by_previous_version() { setupRoot(PROJECT_ROOT); dbTester.prepareDbUnit(getClass(), "shared.xml"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistAnalysisStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistAnalysisStepTest.java index 3a3bb64bdd8..aa1ba90c9a4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistAnalysisStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistAnalysisStepTest.java @@ -46,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE; -import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; public class ReportPersistAnalysisStepTest extends BaseStepTest { @@ -157,7 +157,7 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { @Test public void only_persist_snapshots_with_leak_period_on_project_and_module() { - periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1")); + periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, analysisDate, "u1")); OrganizationDto organizationDto = dbTester.organizations().insert(); ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project"); @@ -190,7 +190,7 @@ public class ReportPersistAnalysisStepTest extends BaseStepTest { underTest.execute(); SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid()); - assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS); + assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION); } @Test |