diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2019-11-14 14:50:50 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-12-09 20:46:16 +0100 |
commit | 19fc8cae2da5245cacaccc9d503030d64ade0499 (patch) | |
tree | 8a86675828abee8e037b54979b95dee9241ea9af | |
parent | 9e9d0ad028680e6fc4ef28f1d5985f74e411af95 (diff) | |
download | sonarqube-19fc8cae2da5245cacaccc9d503030d64ade0499.tar.gz sonarqube-19fc8cae2da5245cacaccc9d503030d64ade0499.zip |
SONAR-12661 rework configuration setting "sonar.dbcleaner.daysBeforeDeletingInactiveShortLivingBranches"
5 files changed, 154 insertions, 2 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java index 912861e3c6f..d6ee2292a57 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java @@ -37,6 +37,8 @@ public class DbVersion81 implements DbVersion { .add(3107, "Migrate default branches to keep global setting", MigrateDefaultBranchesToKeepSetting.class) .add(3108, "Add EXCLUDE_FROM_PURGE column", AddExcludeBranchFromPurgeColumn.class) .add(3109, "Populate EXCLUDE_FROM_PURGE column", PopulateExcludeBranchFromPurgeColumn.class) - .add(3110, "Remove 'sonar.branch.longLivedBranches.regex'", RemoveLLBRegexSetting.class); + .add(3110, "Remove 'sonar.branch.longLivedBranches.regex'", RemoveLLBRegexSetting.class) + .add(3111, "Rename 'sonar.dbcleaner.daysBeforeDeletingInactiveShortLivingBranches' setting", + RenameDaysBeforeDeletingInactiveSLBSetting.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSetting.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSetting.java new file mode 100644 index 00000000000..575104ba86b --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSetting.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.v81; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.SupportsBlueGreen; +import org.sonar.server.platform.db.migration.step.DataChange; + +@SupportsBlueGreen +public class RenameDaysBeforeDeletingInactiveSLBSetting extends DataChange { + static final String TABLE = "properties"; + static final String OLD_PROPERTY_NAME = "sonar.dbcleaner.daysBeforeDeletingInactiveShortLivingBranches"; + static final String NEW_PROPERTY_NAME = "sonar.dbcleaner.daysBeforeDeletingInactiveBranchesAndPRs"; + + public RenameDaysBeforeDeletingInactiveSLBSetting(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + context.prepareUpsert("update " + TABLE + " set prop_key='" + NEW_PROPERTY_NAME + "' where prop_key='" + OLD_PROPERTY_NAME + "'") + .execute() + .commit(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java index cb64c2f6f88..9d37c435f77 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java @@ -37,7 +37,7 @@ public class DbVersion81Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 11); + verifyMigrationCount(underTest, 12); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest.java new file mode 100644 index 00000000000..ea6c7957524 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest.java @@ -0,0 +1,96 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.v81; + +import java.sql.SQLException; +import java.time.Instant; +import javax.annotation.Nullable; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.sonar.server.platform.db.migration.version.v81.RenameDaysBeforeDeletingInactiveSLBSetting.NEW_PROPERTY_NAME; +import static org.sonar.server.platform.db.migration.version.v81.RenameDaysBeforeDeletingInactiveSLBSetting.OLD_PROPERTY_NAME; +import static org.sonar.server.platform.db.migration.version.v81.RenameDaysBeforeDeletingInactiveSLBSetting.TABLE; + +public class RenameDaysBeforeDeletingInactiveSLBSettingTest { + + private static final int TOTAL_NUMBER_OF_PROJECT_LEVEL_PROPERTIES = 10; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(RenameDaysBeforeDeletingInactiveSLBSettingTest.class, "schema.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private RenameDaysBeforeDeletingInactiveSLBSetting underTest = new RenameDaysBeforeDeletingInactiveSLBSetting(dbTester.database()); + + @Before + public void setup() { + insertProperty(null, OLD_PROPERTY_NAME, "xyz"); + insertProperty(null, "sonar.unrelated.global.property", "xyz"); + for (long i = 1; i <= TOTAL_NUMBER_OF_PROJECT_LEVEL_PROPERTIES; i++) { + insertProperty(i, OLD_PROPERTY_NAME, format("xyz-%s", i)); + insertProperty(i, "sonar.unrelated.project.property", format("xyz-%s", i)); + } + + int propertiesCount = dbTester.countRowsOfTable(TABLE); + assertEquals(2 * TOTAL_NUMBER_OF_PROJECT_LEVEL_PROPERTIES + 2, propertiesCount); + } + + @Test + public void migrates_old_prop_key_to_new_prop_key() throws SQLException { + underTest.execute(); + + verifyResult(); + } + + @Test + public void migration_is_re_entrant() throws SQLException { + underTest.execute(); + underTest.execute(); + + verifyResult(); + } + + private void verifyResult() { + int propertiesCount = dbTester.countRowsOfTable(TABLE); + assertEquals(2 * TOTAL_NUMBER_OF_PROJECT_LEVEL_PROPERTIES + 2, propertiesCount); + + int numberOfPropsWithNewName = dbTester.countSql("select count(*) from " + TABLE + " where prop_key = '" + NEW_PROPERTY_NAME + "'"); + int numberOfPropsWithOldName = dbTester.countSql("select count(*) from " + TABLE + " where prop_key = '" + OLD_PROPERTY_NAME + "'"); + assertThat(numberOfPropsWithNewName).isEqualTo(TOTAL_NUMBER_OF_PROJECT_LEVEL_PROPERTIES + 1); + assertThat(numberOfPropsWithOldName).isEqualTo(0); + } + + private void insertProperty(@Nullable Long projectId, String propertyKey, String propertyValue) { + dbTester.executeInsert(TABLE, + "prop_key", propertyKey, + "resource_id", projectId, + "is_empty", false, + "text_value", propertyValue, + "created_at", Instant.now().toEpochMilli()); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest/schema.sql new file mode 100644 index 00000000000..367029ea6ba --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/RenameDaysBeforeDeletingInactiveSLBSettingTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "PROPERTIES" ( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,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"); |