aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2018-01-25 16:43:00 +0100
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>2018-02-07 11:32:38 +0100
commit4e1b01c0139c80bba0c8b1075010394fe4eeb3d4 (patch)
treea89948cec0d499b209826fcbb5bc234cdd9c418f /server/sonar-db-migration/src
parent144d9754a2d0e0e8a57ae3567359eedcf08a3cc5 (diff)
downloadsonarqube-4e1b01c0139c80bba0c8b1075010394fe4eeb3d4.tar.gz
sonarqube-4e1b01c0139c80bba0c8b1075010394fe4eeb3d4.zip
SONAR-10321 Store rule scope in DB
Diffstat (limited to 'server/sonar-db-migration/src')
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScope.java26
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java5
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRules.java46
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMain.java31
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest.java35
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java2
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest.java56
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest.java88
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest/rules.sql24
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest/rules.sql25
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest/rules.sql25
11 files changed, 361 insertions, 2 deletions
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScope.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScope.java
new file mode 100644
index 00000000000..8bb0fc15d4a
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScope.java
@@ -0,0 +1,26 @@
+package org.sonar.server.platform.db.migration.version.v71;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+public class AddRuleScope extends DdlChange {
+
+ public AddRuleScope(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AddColumnsBuilder(getDialect(), "rules")
+ .addColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
+ .setColumnName("scope")
+ .setIsNullable(true)
+ .setLimit(20)
+ .build())
+ .build());
+ }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java
index 2705f099de4..bb67a4d88ed 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71.java
@@ -27,6 +27,9 @@ public class DbVersion71 implements DbVersion {
@Override
public void addSteps(MigrationStepRegistry registry) {
registry
- .add(2000, "Delete settings defined in sonar.properties from PROPERTIES table", DeleteSettingsDefinedInSonarDotProperties.class);
+ .add(2000, "Delete settings defined in sonar.properties from PROPERTIES table", DeleteSettingsDefinedInSonarDotProperties.class)
+ .add(2001, "Add scope to rules", AddRuleScope.class)
+ .add(2002, "Set rules scope to MAIN", SetRuleScopeToMain.class)
+ .add(2003, "Make scope not nullable in rules", MakeScopeNotNullableInRules.class);
}
}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRules.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRules.java
new file mode 100644
index 00000000000..9bd077747e0
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRules.java
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.v71;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class MakeScopeNotNullableInRules extends DdlChange {
+ private static final String TABLE_NAME = "rules";
+
+ public MakeScopeNotNullableInRules(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME)
+ .updateColumn(newVarcharColumnDefBuilder()
+ .setColumnName("scope")
+ .setLimit(20)
+ .setIsNullable(false)
+ .build())
+ .build());
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMain.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMain.java
new file mode 100644
index 00000000000..b2fb71e1b62
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMain.java
@@ -0,0 +1,31 @@
+package org.sonar.server.platform.db.migration.version.v71;
+
+import java.sql.SQLException;
+import org.sonar.api.rule.RuleScope;
+import org.sonar.api.utils.System2;
+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 SetRuleScopeToMain extends DataChange {
+ private final System2 system2;
+
+ public SetRuleScopeToMain(Database db, System2 system2) {
+ super(db);
+ this.system2 = system2;
+ }
+
+ @Override
+ protected void execute(Context context) throws SQLException {
+ long now = system2.now();
+ MassUpdate massUpdate = context.prepareMassUpdate();
+ massUpdate.select("select id from rules where scope is NULL");
+ massUpdate.rowPluralName("rules");
+ massUpdate.update("update rules set scope=?, updated_at=? where scope is NULL");
+ massUpdate.execute((row, update) -> {
+ update.setString(1, RuleScope.MAIN.name());
+ update.setLong(2, now);
+ return true;
+ });
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest.java
new file mode 100644
index 00000000000..30856ed1819
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest.java
@@ -0,0 +1,35 @@
+package org.sonar.server.platform.db.migration.version.v71;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.VARCHAR;
+
+public class AddRuleScopeTest {
+ @Rule
+ public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddRuleScopeTest.class, "rules.sql");
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private AddRuleScope underTest = new AddRuleScope(dbTester.database());
+
+ @Test
+ public void column_is_added_to_table() throws SQLException {
+ underTest.execute();
+
+ dbTester.assertColumnDefinition("rules", "scope", VARCHAR, null, true);
+ }
+
+ @Test
+ public void migration_is_not_reentrant() throws SQLException {
+ underTest.execute();
+
+ expectedException.expect(IllegalStateException.class);
+
+ underTest.execute();
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java
index 47b980bb2fa..c65e93d43a8 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/DbVersion71Test.java
@@ -36,7 +36,7 @@ public class DbVersion71Test {
@Test
public void verify_migration_count() {
- verifyMigrationCount(underTest, 1);
+ verifyMigrationCount(underTest, 4);
}
}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest.java
new file mode 100644
index 00000000000..192abdcc2b1
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest.java
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.v71;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.rule.RuleScope;
+import org.sonar.db.CoreDbTester;
+
+public class MakeScopeNotNullableInRulesTest {
+ @Rule
+ public CoreDbTester db = CoreDbTester.createForSchema(MakeScopeNotNullableInRulesTest.class, "rules.sql");
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private MakeScopeNotNullableInRules underTest = new MakeScopeNotNullableInRules(db.database());
+
+ @Test
+ public void execute_makes_column_not_null() throws SQLException {
+ db.assertColumnDefinition("rules", "scope", Types.VARCHAR, null, true);
+ insertRow(1);
+ insertRow(2);
+
+ underTest.execute();
+
+ db.assertColumnDefinition("rules", "scope", Types.VARCHAR, null, false);
+ }
+
+ private void insertRow(int id) {
+ db.executeInsert(
+ "RULES",
+ "PLUGIN_RULE_KEY", "key_" + id,
+ "PLUGIN_NAME", "name_" + id,
+ "SCOPE", RuleScope.MAIN.name());
+ }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest.java
new file mode 100644
index 00000000000..0a8c8b1398c
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest.java
@@ -0,0 +1,88 @@
+package org.sonar.server.platform.db.migration.version.v71;
+
+import java.sql.SQLException;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.rule.RuleScope;
+import org.sonar.api.utils.System2;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SetRuleScopeToMainTest {
+ @Rule
+ public final CoreDbTester dbTester = CoreDbTester.createForSchema(SetRuleScopeToMainTest.class, "rules.sql");
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private System2 system = new System2();
+
+ private SetRuleScopeToMain underTest = new SetRuleScopeToMain(dbTester.database(), system);
+
+ @Test
+ public void has_no_effect_if_table_rules_is_empty() throws SQLException {
+ underTest.execute();
+
+ assertThat(dbTester.countRowsOfTable("rules")).isEqualTo(0);
+ }
+
+ @Test
+ public void updates_rows_with_null_is_build_in_column_to_false() throws SQLException {
+ insertRow(1, null);
+ insertRow(2, null);
+
+ assertThat(countRowsWithValue(null)).isEqualTo(2);
+ assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(0);
+
+ underTest.execute();
+
+ assertThat(countRowsWithValue(null)).isEqualTo(0);
+ assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(2);
+ }
+
+ @Test
+ public void support_large_number_of_rows() throws SQLException {
+ for (int i = 0; i < 2_000; i++) {
+ insertRow(i, null);
+ }
+
+ assertThat(countRowsWithValue(null)).isEqualTo(2000);
+ assertThat(countRowsWithValue(RuleScope.MAIN)).isZero();
+
+ underTest.execute();
+
+ assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(2_000);
+ assertThat(countRowsWithValue(null)).isEqualTo(0);
+ }
+
+ @Test
+ public void execute_is_reentreant() throws SQLException {
+ insertRow(1, null);
+ insertRow(2, RuleScope.MAIN);
+
+ underTest.execute();
+
+ underTest.execute();
+
+ assertThat(countRowsWithValue(null)).isEqualTo(0);
+ assertThat(countRowsWithValue(RuleScope.MAIN)).isEqualTo(2);
+ }
+
+ private int countRowsWithValue(@Nullable RuleScope value) {
+ if (value == null) {
+ return dbTester.countSql("select count(1) from rules where scope is null");
+ }
+ return dbTester.countSql("select count(1) from rules where scope='" + value + "'");
+ }
+
+ private void insertRow(int id, @Nullable RuleScope scope) {
+ dbTester.executeInsert(
+ "RULES",
+ "PLUGIN_RULE_KEY", "key_" + id,
+ "PLUGIN_NAME", "name_" + id,
+ "SCOPE", scope == null ? null : scope.name());
+ }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest/rules.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest/rules.sql
new file mode 100644
index 00000000000..7bc032ceb37
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/AddRuleScopeTest/rules.sql
@@ -0,0 +1,24 @@
+CREATE TABLE "RULES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "PLUGIN_KEY" VARCHAR(200),
+ "PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL,
+ "PLUGIN_NAME" VARCHAR(255) NOT NULL,
+ "DESCRIPTION" VARCHAR(16777215),
+ "DESCRIPTION_FORMAT" VARCHAR(20),
+ "PRIORITY" INTEGER,
+ "IS_TEMPLATE" BOOLEAN DEFAULT FALSE,
+ "TEMPLATE_ID" INTEGER,
+ "PLUGIN_CONFIG_KEY" VARCHAR(200),
+ "NAME" VARCHAR(200),
+ "STATUS" VARCHAR(40),
+ "LANGUAGE" VARCHAR(20),
+ "DEF_REMEDIATION_FUNCTION" VARCHAR(20),
+ "DEF_REMEDIATION_GAP_MULT" VARCHAR(20),
+ "DEF_REMEDIATION_BASE_EFFORT" VARCHAR(20),
+ "GAP_DESCRIPTION" VARCHAR(4000),
+ "SYSTEM_TAGS" VARCHAR(4000),
+ "RULE_TYPE" TINYINT,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT
+);
+CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest/rules.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest/rules.sql
new file mode 100644
index 00000000000..1708db814f4
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/MakeScopeNotNullableInRulesTest/rules.sql
@@ -0,0 +1,25 @@
+CREATE TABLE "RULES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "PLUGIN_KEY" VARCHAR(200),
+ "PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL,
+ "PLUGIN_NAME" VARCHAR(255) NOT NULL,
+ "DESCRIPTION" VARCHAR(16777215),
+ "DESCRIPTION_FORMAT" VARCHAR(20),
+ "PRIORITY" INTEGER,
+ "IS_TEMPLATE" BOOLEAN DEFAULT FALSE,
+ "TEMPLATE_ID" INTEGER,
+ "PLUGIN_CONFIG_KEY" VARCHAR(200),
+ "NAME" VARCHAR(200),
+ "STATUS" VARCHAR(40),
+ "LANGUAGE" VARCHAR(20),
+ "SCOPE" VARCHAR(20),
+ "DEF_REMEDIATION_FUNCTION" VARCHAR(20),
+ "DEF_REMEDIATION_GAP_MULT" VARCHAR(20),
+ "DEF_REMEDIATION_BASE_EFFORT" VARCHAR(20),
+ "GAP_DESCRIPTION" VARCHAR(4000),
+ "SYSTEM_TAGS" VARCHAR(4000),
+ "RULE_TYPE" TINYINT,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT
+);
+CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest/rules.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest/rules.sql
new file mode 100644
index 00000000000..1708db814f4
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v71/SetRuleScopeToMainTest/rules.sql
@@ -0,0 +1,25 @@
+CREATE TABLE "RULES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "PLUGIN_KEY" VARCHAR(200),
+ "PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL,
+ "PLUGIN_NAME" VARCHAR(255) NOT NULL,
+ "DESCRIPTION" VARCHAR(16777215),
+ "DESCRIPTION_FORMAT" VARCHAR(20),
+ "PRIORITY" INTEGER,
+ "IS_TEMPLATE" BOOLEAN DEFAULT FALSE,
+ "TEMPLATE_ID" INTEGER,
+ "PLUGIN_CONFIG_KEY" VARCHAR(200),
+ "NAME" VARCHAR(200),
+ "STATUS" VARCHAR(40),
+ "LANGUAGE" VARCHAR(20),
+ "SCOPE" VARCHAR(20),
+ "DEF_REMEDIATION_FUNCTION" VARCHAR(20),
+ "DEF_REMEDIATION_GAP_MULT" VARCHAR(20),
+ "DEF_REMEDIATION_BASE_EFFORT" VARCHAR(20),
+ "GAP_DESCRIPTION" VARCHAR(4000),
+ "SYSTEM_TAGS" VARCHAR(4000),
+ "RULE_TYPE" TINYINT,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT
+);
+CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES" ("PLUGIN_NAME", "PLUGIN_RULE_KEY");