aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-08-28 12:58:21 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-08-29 17:52:09 +0200
commitf15ecf379d92b749f3d4bd8f7ec694be600b6784 (patch)
tree69c00d0d60f6f0d78c682675125568732a188bd0 /server/sonar-db-migration
parent1f30a68520775329a76d4dd7d369fa23a02d13b3 (diff)
downloadsonarqube-f15ecf379d92b749f3d4bd8f7ec694be600b6784.tar.gz
sonarqube-f15ecf379d92b749f3d4bd8f7ec694be600b6784.zip
SONAR-8979 Drop leak period setting values on views
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java4
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViews.java48
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java6
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest.java140
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest/settings_and_projects.sql57
5 files changed, 251 insertions, 4 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java
index 9ea2becfe20..8a040e89ee4 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java
@@ -28,6 +28,8 @@ public class DbVersion66 implements DbVersion {
public void addSteps(MigrationStepRegistry registry) {
registry
.add(1800, "Add incremental column to snapthots table", AddIncrementalColumnToSnapshotsTable.class)
- .add(1801, "Create table CE task characteristics", CreateTableCeTaskCharacteristics.class);
+ .add(1801, "Create table CE task characteristics", CreateTableCeTaskCharacteristics.class)
+ .add(1802, "Delete leak settings on views", DeleteLeakSettingsOnViews.class)
+ ;
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViews.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViews.java
new file mode 100644
index 00000000000..60cdc23e1a1
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViews.java
@@ -0,0 +1,48 @@
+/*
+ * 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.v66;
+
+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 DeleteLeakSettingsOnViews extends DataChange {
+
+ public DeleteLeakSettingsOnViews(Database db) {
+ super(db);
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("SELECT prop.id FROM properties prop " +
+ "INNER JOIN projects p ON p.id=prop.resource_id AND p.qualifier NOT IN ('TRK') " +
+ "WHERE prop.prop_key='sonar.leak.period' or prop.prop_key='sonar.timemachine.period1' ");
+ massUpdate.update("DELETE FROM properties WHERE id=?");
+ massUpdate.rowPluralName("properties");
+ 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/v66/DbVersion66Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java
index 57008442615..407ed300852 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java
@@ -19,11 +19,11 @@
*/
package org.sonar.server.platform.db.migration.version.v66;
+import org.junit.Test;
+
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationCount;
import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
-import org.junit.Test;
-
public class DbVersion66Test {
private DbVersion66 underTest = new DbVersion66();
@@ -34,6 +34,6 @@ public class DbVersion66Test {
@Test
public void verify_migration_count() {
- verifyMigrationCount(underTest, 2);
+ verifyMigrationCount(underTest, 3);
}
}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest.java
new file mode 100644
index 00000000000..4582b3c9f55
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest.java
@@ -0,0 +1,140 @@
+/*
+ * 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.v66;
+
+import java.sql.SQLException;
+import java.util.Random;
+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.junit.rules.ExpectedException;
+import org.sonar.api.resources.Scopes;
+import org.sonar.core.util.Uuids;
+import org.sonar.db.CoreDbTester;
+
+import static java.lang.String.valueOf;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
+import static org.sonar.api.resources.Qualifiers.SUBVIEW;
+import static org.sonar.api.resources.Qualifiers.VIEW;
+
+public class DeleteLeakSettingsOnViewsTest {
+
+ @Rule
+ public CoreDbTester db = CoreDbTester.createForSchema(DeleteLeakSettingsOnViewsTest.class, "settings_and_projects.sql");
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private DeleteLeakSettingsOnViews underTest = new DeleteLeakSettingsOnViews(db.database());
+
+ @Test
+ public void migrate_leak_settings_on_views() throws SQLException {
+ long viewId = insertComponent(Scopes.PROJECT, VIEW);
+ long subViewId = insertComponent(Scopes.PROJECT, SUBVIEW);
+ insertProperty("sonar.leak.period", viewId);
+ insertProperty("sonar.leak.period", subViewId);
+
+ underTest.execute();
+
+ assertPropertiesIsEmpty();
+ }
+
+ @Test
+ public void migrate_old_leak_settings_on_views() throws SQLException {
+ long viewId = insertComponent(Scopes.PROJECT, VIEW);
+ long subViewId = insertComponent(Scopes.PROJECT, SUBVIEW);
+ insertProperty("sonar.timemachine.period1", viewId);
+ insertProperty("sonar.timemachine.period1", subViewId);
+
+ underTest.execute();
+
+ assertPropertiesIsEmpty();
+ }
+
+ @Test
+ public void does_nothing_on_leak_settings_not_on_views() throws SQLException {
+ long projectId = insertComponent(Scopes.PROJECT, PROJECT);
+ insertProperty("sonar.leak.period", projectId);
+ insertProperty("sonar.leak.period", null);
+
+ underTest.execute();
+
+ assertProperties(
+ tuple("sonar.leak.period", projectId),
+ tuple("sonar.leak.period", null));
+ }
+
+ @Test
+ public void does_nothing_on_non_leak_settings() throws SQLException {
+ long projectId = insertComponent(Scopes.PROJECT, PROJECT);
+ insertProperty("sonar.component", projectId);
+ insertProperty("sonar.global", null);
+
+ underTest.execute();
+
+ assertProperties(
+ tuple("sonar.component", projectId),
+ tuple("sonar.global", null));
+ }
+
+ private void assertPropertiesIsEmpty(){
+ assertThat(db.countRowsOfTable("properties")).isZero();
+ }
+
+ private void assertProperties(Tuple... expectedTuples) {
+ assertThat(db.select("SELECT PROP_KEY, RESOURCE_ID FROM PROPERTIES")
+ .stream()
+ .map(map -> new Tuple(map.get("PROP_KEY"), map.get("RESOURCE_ID")))
+ .collect(Collectors.toList()))
+ .containsExactlyInAnyOrder(expectedTuples);
+ }
+
+ public void insertProperty(String propertyName, @Nullable Long componentId) {
+ db.executeInsert(
+ "properties",
+ "prop_key", propertyName,
+ "resource_id", componentId,
+ "is_empty", "false",
+ "text_value", randomAlphabetic(2));
+ }
+
+ private long insertComponent(@Nullable String scope, @Nullable String qualifier) {
+ String uuid = Uuids.createFast();
+ db.executeInsert(
+ "projects",
+ "organization_uuid", randomAlphanumeric(3),
+ "uuid", uuid,
+ "uuid_path", "path_of_" + uuid,
+ "root_uuid", uuid,
+ "project_uuid", uuid,
+ "scope", scope,
+ "qualifier", qualifier,
+ "private", valueOf(new Random().nextBoolean()),
+ "enabled", valueOf(new Random().nextBoolean()));
+ return ((Long) db.selectFirst("select id as \"ID\" from projects where uuid='" + uuid + "'").get("ID")).intValue();
+ }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest/settings_and_projects.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest/settings_and_projects.sql
new file mode 100644
index 00000000000..6d62b229804
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/DeleteLeakSettingsOnViewsTest/settings_and_projects.sql
@@ -0,0 +1,57 @@
+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");
+
+CREATE TABLE "PROJECTS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "ORGANIZATION_UUID" VARCHAR(40) NOT NULL,
+ "KEE" VARCHAR(400),
+ "UUID" VARCHAR(50) NOT NULL,
+ "UUID_PATH" VARCHAR(1500) NOT NULL,
+ "ROOT_UUID" VARCHAR(50) NOT NULL,
+ "PROJECT_UUID" VARCHAR(50) NOT NULL,
+ "MODULE_UUID" VARCHAR(50),
+ "MODULE_UUID_PATH" VARCHAR(1500),
+ "NAME" VARCHAR(2000),
+ "DESCRIPTION" VARCHAR(2000),
+ "PRIVATE" BOOLEAN NOT NULL,
+ "TAGS" VARCHAR(500),
+ "ENABLED" BOOLEAN NOT NULL DEFAULT TRUE,
+ "SCOPE" VARCHAR(3),
+ "QUALIFIER" VARCHAR(10),
+ "DEPRECATED_KEE" VARCHAR(400),
+ "PATH" VARCHAR(2000),
+ "LANGUAGE" VARCHAR(20),
+ "COPY_COMPONENT_UUID" VARCHAR(50),
+ "LONG_NAME" VARCHAR(2000),
+ "DEVELOPER_UUID" VARCHAR(50),
+ "CREATED_AT" TIMESTAMP,
+ "AUTHORIZATION_UPDATED_AT" BIGINT,
+ "B_CHANGED" BOOLEAN,
+ "B_COPY_COMPONENT_UUID" VARCHAR(50),
+ "B_DESCRIPTION" VARCHAR(2000),
+ "B_ENABLED" BOOLEAN,
+ "B_UUID_PATH" VARCHAR(1500),
+ "B_LANGUAGE" VARCHAR(20),
+ "B_LONG_NAME" VARCHAR(500),
+ "B_MODULE_UUID" VARCHAR(50),
+ "B_MODULE_UUID_PATH" VARCHAR(1500),
+ "B_NAME" VARCHAR(500),
+ "B_PATH" VARCHAR(2000),
+ "B_QUALIFIER" VARCHAR(10)
+);
+CREATE INDEX "PROJECTS_ORGANIZATION" ON "PROJECTS" ("ORGANIZATION_UUID");
+CREATE UNIQUE INDEX "PROJECTS_KEE" ON "PROJECTS" ("KEE");
+CREATE INDEX "PROJECTS_ROOT_UUID" ON "PROJECTS" ("ROOT_UUID");
+CREATE UNIQUE INDEX "PROJECTS_UUID" ON "PROJECTS" ("UUID");
+CREATE INDEX "PROJECTS_PROJECT_UUID" ON "PROJECTS" ("PROJECT_UUID");
+CREATE INDEX "PROJECTS_MODULE_UUID" ON "PROJECTS" ("MODULE_UUID");
+CREATE INDEX "PROJECTS_QUALIFIER" ON "PROJECTS" ("QUALIFIER");