]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12347 Migrate properties
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 22 Aug 2019 20:33:17 +0000 (15:33 -0500)
committerSonarTech <sonartech@sonarsource.com>
Tue, 24 Sep 2019 18:21:15 +0000 (20:21 +0200)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/DbVersion80.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodProperties.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/DbVersion80Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest/schema.sql [new file with mode: 0644]

index d8a80401af0964182d577161aa2fd9dcaee7abf2..5bb2ab5e9e4ba5ff1587afb765eea06276e94b28 100644 (file)
@@ -34,6 +34,7 @@ public class DbVersion80 implements DbVersion {
       .add(3005, "Remove default quality gate property from Properties table", RemoveDefaultQualityGateFromPropertiesTable.class)
       .add(3006, "Create NEW_CODE_PERIOD table", CreateNewCodePeriodTable.class)
       .add(3007, "Populate NEW_CODE_PERIOD table", PopulateNewCodePeriodTable.class)
+      .add(3008, "Remove leak period properties", RemoveLeakPeriodProperties.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodProperties.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodProperties.java
new file mode 100644 (file)
index 0000000..e5e8856
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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.v80;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+
+public class RemoveLeakPeriodProperties extends DataChange {
+  private static final String LEAK_PERIOD_PROP_KEY = "sonar.leak.period";
+
+  public RemoveLeakPeriodProperties(Database db) {
+    super(db);
+  }
+
+  @Override
+  protected void execute(DataChange.Context context) throws SQLException {
+    context.prepareUpsert("delete from properties where prop_key='" + LEAK_PERIOD_PROP_KEY + "'")
+      .execute()
+      .commit();
+  }
+}
index f373bc8074cc5c876e48a5a1fac4777e1001109a..a4a6ecde6fa1d002c3cfc45405df21db7230543f 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion80Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 8);
+    verifyMigrationCount(underTest, 9);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest.java
new file mode 100644 (file)
index 0000000..3ec8bf6
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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.v80;
+
+import java.sql.SQLException;
+import java.time.Instant;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+public class RemoveLeakPeriodPropertiesTest {
+  private static final String PROPERTIES_TABLE_NAME = "properties";
+  private static final int TOTAL_NUMBER_OF_PROPERTIES = 10;
+
+  @Rule
+  public CoreDbTester dbTester = CoreDbTester.createForSchema(RemoveLeakPeriodPropertiesTest.class, "schema.sql");
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private RemoveLeakPeriodProperties underTest = new RemoveLeakPeriodProperties(dbTester.database());
+
+  @Test
+  public void remove_default_quality_gate_property() throws SQLException {
+    for (long i = 1; i <= TOTAL_NUMBER_OF_PROPERTIES; i++) {
+      insertQualityGateProperty(i, i + 100);
+    }
+
+    int propertiesCount = dbTester.countRowsOfTable(PROPERTIES_TABLE_NAME);
+    Assert.assertEquals(TOTAL_NUMBER_OF_PROPERTIES, propertiesCount);
+
+    underTest.execute();
+
+    //should delete properties
+    propertiesCount = dbTester.countRowsOfTable(PROPERTIES_TABLE_NAME);
+    Assert.assertEquals(0, propertiesCount);
+
+    //should not fail if executed twice
+    underTest.execute();
+  }
+
+  private void insertQualityGateProperty(Long projectId, Long qualityGateId) {
+    dbTester.executeInsert(PROPERTIES_TABLE_NAME,
+      "prop_key", "sonar.leak.period",
+      "resource_id", projectId,
+      "is_empty", false,
+      "text_value", Long.toString(qualityGateId),
+      "created_at", Instant.now().toEpochMilli());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v80/RemoveLeakPeriodPropertiesTest/schema.sql
new file mode 100644 (file)
index 0000000..d84c238
--- /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,
+  "CREATED_AT" BIGINT
+);
+CREATE INDEX "PROPERTIES_KEY" ON "PROPERTIES" ("PROP_KEY");