]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9863 Drop old licenses
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Oct 2017 14:22:12 +0000 (16:22 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 1 Nov 2017 13:40:29 +0000 (14:40 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicenses.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/DropOldLicensesTest/properties.sql [new file with mode: 0644]

index 0f06e00377e7ad0f87a5c07b9da5fa9841b6cb71..f72c75bbed56d6355ba49b30130b0144c7d9f91d 100644 (file)
@@ -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 (file)
index 0000000..8fed77f
--- /dev/null
@@ -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;
+    });
+  }
+}
index de678701f0bf525031df4c0961dd02601c30ba10..c418c66ef6243c161b03039e14b15166bff0ae51 100644 (file)
@@ -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 (file)
index 0000000..0f35f73
--- /dev/null
@@ -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 (file)
index 0000000..dfc39d8
--- /dev/null
@@ -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");