From d0efb4c3a0b9161df8290a993145827637320eba Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 27 Oct 2017 16:22:12 +0200 Subject: [PATCH] SONAR-9863 Drop old licenses --- .../db/migration/version/v67/DbVersion67.java | 1 + .../version/v67/DropOldLicenses.java | 52 ++++++++++++ .../version/v67/DbVersion67Test.java | 2 +- .../version/v67/DropOldLicensesTest.java | 80 +++++++++++++++++++ .../v67/DropOldLicensesTest/properties.sql | 11 +++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicenses.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest/properties.sql 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 0f06e00377e..f72c75bbed5 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 @@ -33,6 +33,7 @@ public class DbVersion67 implements DbVersion { .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) + .add(1837, "Drop old licenses", DropOldLicenses.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicenses.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicenses.java new file mode 100644 index 00000000000..8fed77fdf96 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicenses.java @@ -0,0 +1,52 @@ +/* + * 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 DropOldLicenses extends DataChange { + + private static final String LICENSE_HASH_SECURED_SUFFIX = ".licenseHash.secured"; + private static final String LICENSE_SECURED_SUFFIX = ".license.secured"; + + public DropOldLicenses(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("select prop_key from properties where prop_key like ?") + .setString(1, "%" + LICENSE_HASH_SECURED_SUFFIX); + massUpdate.update("delete from properties where prop_key = ? or prop_key = ?"); + massUpdate.rowPluralName("old license properties"); + massUpdate.execute((row, update) -> { + String licenseHashKey = row.getString(1); + String licenseKey = licenseHashKey.replace(LICENSE_HASH_SECURED_SUFFIX, "") + LICENSE_SECURED_SUFFIX; + update.setString(1, licenseHashKey); + update.setString(2, licenseKey); + 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 de678701f0b..c418c66ef62 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, 7); + verifyMigrationCount(underTest, 8); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest.java new file mode 100644 index 00000000000..0f35f73dddd --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest.java @@ -0,0 +1,80 @@ +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.stream.Collectors; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThat; + +public class DropOldLicensesTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropOldLicensesTest.class, "properties.sql"); + + private DropOldLicenses underTest = new DropOldLicenses(db.database()); + + @Test + public void remove_old_licenses() throws SQLException { + insertProperty("sonar.cpp.license.secured"); + insertProperty("sonar.cpp.licenseHash.secured"); + insertProperty("sonar.objc.license.secured"); + insertProperty("sonar.objc.licenseHash.secured"); + + underTest.execute(); + + assertPropertiesIsEmpty(); + } + + @Test + public void ignore_existing_none_related_licenses_settings() throws SQLException { + insertProperty("my.property"); + insertProperty("custom.license"); + + underTest.execute(); + + assertProperties("my.property", "custom.license"); + } + + private void assertPropertiesIsEmpty() { + assertProperties(); + } + + private void assertProperties(String... expectedSettingKeys) { + assertThat(db.select("SELECT PROP_KEY FROM PROPERTIES") + .stream() + .map(map -> map.get("PROP_KEY")) + .collect(Collectors.toList())) + .containsExactlyInAnyOrder(expectedSettingKeys); + } + + public void insertProperty(String propertyKey) { + db.executeInsert( + "properties", + "prop_key", propertyKey, + "is_empty", "false", + "text_value", randomAlphabetic(2)); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest/properties.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest/properties.sql new file mode 100644 index 00000000000..dfc39d8d285 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest/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(2147483647), + "CREATED_AT" BIGINT +); +CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY"); -- 2.39.5