]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Add migration on measures on requirements to measures on rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 13 Mar 2014 07:24:39 +0000 (08:24 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 13 Mar 2014 07:24:39 +0000 (08:24 +0100)
sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java
sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql
sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java
sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/521_update_requirement_measures.rb [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements_result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/schema.sql [new file with mode: 0644]

index a4ad3c2eb75c41a2c876f4d70ae60ca9b26d2b65..ec82ef2498bcee1496c7aac11f6c81d04e76be36 100644 (file)
@@ -33,7 +33,7 @@ import java.util.List;
  */
 public class DatabaseVersion implements BatchComponent, ServerComponent {
 
-  public static final int LAST_VERSION = 520;
+  public static final int LAST_VERSION = 521;
 
   public static enum Status {
     UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
index 01b91d42ffc6567401a8489ac649208dc40802d5..18024e5406e4e73b82bf55852fcbbbaec6937ba4 100644 (file)
@@ -218,6 +218,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('517');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('518');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('519');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('520');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('521');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null);
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index 4566f1b188e5603e0edbaed0d04ff6ff3a89ef1e..b2690c3f3d8f1ce75baaf91422e673a39a00b1c6 100644 (file)
 package org.sonar.server.db.migrations;
 
 import com.google.common.collect.ImmutableList;
-import org.sonar.server.db.migrations.v43.DevelopmentCostMeasuresMigration;
-import org.sonar.server.db.migrations.v43.IssueChangelogMigration;
-import org.sonar.server.db.migrations.v43.IssueMigration;
-import org.sonar.server.db.migrations.v43.TechnicalDebtMeasuresMigration;
+import org.sonar.server.db.migrations.v36.ViolationMigration;
 import org.sonar.server.db.migrations.v42.CompleteIssueMessageMigration;
 import org.sonar.server.db.migrations.v42.PackageKeysMigration;
-import org.sonar.server.db.migrations.v36.ViolationMigration;
+import org.sonar.server.db.migrations.v43.*;
 
 import java.util.List;
 
@@ -38,6 +35,7 @@ public interface DatabaseMigrations {
     IssueChangelogMigration.class,
     TechnicalDebtMeasuresMigration.class,
     DevelopmentCostMeasuresMigration.class,
+    RequirementMeasuresMigration.class,
 
     // 4.2
     PackageKeysMigration.class, CompleteIssueMessageMigration.class
diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java
new file mode 100644 (file)
index 0000000..adb2de1
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.migrations.v43;
+
+import org.sonar.core.persistence.Database;
+import org.sonar.server.db.migrations.DatabaseMigration;
+import org.sonar.server.db.migrations.MassUpdater;
+import org.sonar.server.db.migrations.SqlUtil;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Used in the Active Record Migration 521
+ *
+ * @since 4.3
+ */
+public class RequirementMeasuresMigration implements DatabaseMigration {
+
+  private final Database db;
+
+  public RequirementMeasuresMigration(Database database) {
+    this.db = database;
+  }
+
+  @Override
+  public void execute() {
+    new MassUpdater(db).execute(
+      new MassUpdater.InputLoader<Row>() {
+        @Override
+        public String selectSql() {
+          return "SELECT project_measures.id,characteristics.rule_id FROM project_measures " +
+            "INNER JOIN characteristics ON characteristics.id = project_measures.characteristic_id " +
+            "WHERE characteristics.rule_id IS NOT NULL";
+        }
+
+        @Override
+        public Row load(ResultSet rs) throws SQLException {
+          Row row = new Row();
+          row.id = SqlUtil.getLong(rs, 1);
+          row.ruleId = SqlUtil.getInt(rs, 2);
+          return row;
+        }
+      },
+      new MassUpdater.InputConverter<Row>() {
+        @Override
+        public String updateSql() {
+          return "UPDATE project_measures SET characteristic_id=null,rule_id=? WHERE id=?";
+        }
+
+        @Override
+        public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+          updateStatement.setInt(1, row.ruleId);
+          updateStatement.setLong(2, row.id);
+          return true;
+        }
+      }
+    );
+  }
+
+  private static class Row {
+    private Long id;
+    private Integer ruleId;
+  }
+
+}
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/521_update_requirement_measures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/521_update_requirement_measures.rb
new file mode 100644 (file)
index 0000000..d9632e4
--- /dev/null
@@ -0,0 +1,30 @@
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 4.3
+# SONAR-5056
+#
+class UpdateRequirementMeasures < ActiveRecord::Migration
+
+  def self.up
+    Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration('org.sonar.server.db.migrations.v43.RequirementMeasuresMigration')
+  end
+end
diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest.java
new file mode 100644 (file)
index 0000000..9c42c13
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.migrations.v43;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.core.persistence.TestDatabase;
+
+public class RequirementMeasuresMigrationTest {
+
+  @ClassRule
+  public static TestDatabase db = new TestDatabase().schema(RequirementMeasuresMigrationTest.class, "schema.sql");
+
+  RequirementMeasuresMigration migration;
+
+  @Before
+  public void setUp() throws Exception {
+    migration = new RequirementMeasuresMigration(db.database());
+  }
+
+  @Test
+  public void migrate_measures_on_requirements() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_measures_on_requirements.xml");
+
+    migration.execute();
+
+    db.assertDbUnit(getClass(), "migrate_measures_on_requirements_result.xml", "project_measures");
+  }
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements.xml
new file mode 100644 (file)
index 0000000..a5249ee
--- /dev/null
@@ -0,0 +1,44 @@
+<dataset>
+
+  <!-- Root characteristic -->
+  <characteristics id="1" kee="PORTABILITY" name="Portability" parent_id="[null]" root_id="[null]" rule_id="[null]" characteristic_order="1"
+                   function_key="[null]" factor_value="[null]" factor_unit="[null]" offset_value="[null]" offset_unit="[null]" enabled="[true]"
+                   created_at="2013-11-20" updated_at="2013-11-22"/>
+
+  <!-- Characteristic -->
+  <characteristics id="2" kee="COMPILER_RELATED_PORTABILITY" name="Compiler related portability" parent_id="1" root_id="1" rule_id="[null]" characteristic_order="[null]"
+                   function_key="[null]" factor_value="[null]" factor_unit="[null]" offset_value="[null]" offset_unit="[null]" enabled="[true]"
+                   created_at="2013-11-20" updated_at="2013-11-22"/>
+
+  <!-- Requirement -->
+  <characteristics id="3" kee="[null]" name="[null]" parent_id="2" root_id="1" rule_id="10"
+                   function_key="linear_offset" factor_value="20.0" factor_unit="mn" offset_value="30.0" offset_unit="h" enabled="[true]"
+                   created_at="2013-11-20" updated_at="[null]"/>
+
+
+
+  <!-- Measure linked on requirement -> to be updated -->
+  <project_measures id="1" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="3" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure linked on characteristic -> not to be updated -->
+  <project_measures id="2" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure linked on rule -> not to be updated -->
+  <project_measures id="3" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="10" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure not linked on rule or requirement -> not to be updated -->
+  <project_measures id="4" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements_result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements_result.xml
new file mode 100644 (file)
index 0000000..df20f63
--- /dev/null
@@ -0,0 +1,27 @@
+<dataset>
+
+  <!-- Measure linked on requirement -> to be updated -->
+  <project_measures id="1" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="10" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure linked on characteristic -> not to be updated -->
+  <project_measures id="2" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure linked on rule -> not to be updated -->
+  <project_measures id="3" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="10" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+  <!-- Measure not linked on rule or requirement -> not to be updated -->
+  <project_measures id="4" VALUE="1.0" METRIC_ID="1" SNAPSHOT_ID="1000" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+                    rule_id="[null]" text_value="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+                    alert_status="[null]" description="[null]" rule_priority="[null]" characteristic_id="[null]" url="[null]" person_id="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/schema.sql
new file mode 100644 (file)
index 0000000..a60b5de
--- /dev/null
@@ -0,0 +1,44 @@
+-- 4.3
+
+CREATE TABLE "PROJECT_MEASURES" (
+  "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "VALUE" DOUBLE,
+  "METRIC_ID" INTEGER NOT NULL,
+  "SNAPSHOT_ID" INTEGER,
+  "RULE_ID" INTEGER,
+  "RULES_CATEGORY_ID" INTEGER,
+  "TEXT_VALUE" VARCHAR(96),
+  "TENDENCY" INTEGER,
+  "MEASURE_DATE" TIMESTAMP,
+  "PROJECT_ID" INTEGER,
+  "ALERT_STATUS" VARCHAR(5),
+  "ALERT_TEXT" VARCHAR(4000),
+  "URL" VARCHAR(2000),
+  "DESCRIPTION" VARCHAR(4000),
+  "RULE_PRIORITY" INTEGER,
+  "CHARACTERISTIC_ID" INTEGER,
+  "PERSON_ID" INTEGER,
+  "VARIATION_VALUE_1" DOUBLE,
+  "VARIATION_VALUE_2" DOUBLE,
+  "VARIATION_VALUE_3" DOUBLE,
+  "VARIATION_VALUE_4" DOUBLE,
+  "VARIATION_VALUE_5" DOUBLE
+);
+
+CREATE TABLE "CHARACTERISTICS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "KEE" VARCHAR(100),
+  "NAME" VARCHAR(100),
+  "PARENT_ID" INTEGER,
+  "ROOT_ID" INTEGER,
+  "RULE_ID" INTEGER,
+  "FUNCTION_KEY" VARCHAR(100),
+  "FACTOR_VALUE" DOUBLE,
+  "FACTOR_UNIT" VARCHAR(100),
+  "OFFSET_VALUE" DOUBLE,
+  "OFFSET_UNIT" VARCHAR(100),
+  "CHARACTERISTIC_ORDER" INTEGER,
+  "ENABLED" BOOLEAN,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP
+);