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/sonar-db-migration | |
parent | bf2b385a2e8b9bfbc5cc66efb1356394c431b3e2 (diff) | |
download | sonarqube-f3de142860a4ebcc91f9f80771964a105f8ed982.tar.gz sonarqube-f3de142860a4ebcc91f9f80771964a105f8ed982.zip |
SONAR-9142 Remove 'previous_analysis' option
Diffstat (limited to 'server/sonar-db-migration')
5 files changed, 166 insertions, 1 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"); |