]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5249 Use a Java migration to merge measure data into project_measures
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 29 Apr 2014 14:54:34 +0000 (16:54 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 29 Apr 2014 14:55:17 +0000 (16:55 +0200)
sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java
sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/530_merge_measure_data_into_project_measures.rb
sonar-server/src/test/java/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data-result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/schema.sql [new file with mode: 0644]

index 68bda1162239fd9cd0177e373065ed12fe125cc3..4cb30bd9a561d7ac526300ff8c393f72a68b23a0 100644 (file)
@@ -24,7 +24,7 @@ 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.v43.*;
-import org.sonar.server.db.migrations.v44.IssueActionPlanKeyMigration;
+import org.sonar.server.db.migrations.v44.*;
 
 import java.util.List;
 
@@ -46,7 +46,8 @@ public interface DatabaseMigrations {
     NotResolvedIssuesOnRemovedComponentsMigration.class,
 
     // 4.4
-    IssueActionPlanKeyMigration.class
+    IssueActionPlanKeyMigration.class,
+    MeasureDataMigration.class
   );
 
 }
diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/MeasureDataMigration.java
new file mode 100644 (file)
index 0000000..d6ef252
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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.v44;
+
+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.Blob;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * SONAR-5249
+ * Merge measure data table into project_measure
+ *
+ * Used in the Active Record Migration 530.
+ * @since 4.4
+ */
+public class MeasureDataMigration implements DatabaseMigration {
+
+  private final Database db;
+
+  public MeasureDataMigration(Database database) {
+    this.db = database;
+  }
+
+  @Override
+  public void execute() {
+    new MassUpdater(db).execute(
+      new MassUpdater.InputLoader<Row>() {
+        @Override
+        public String selectSql() {
+          return "SELECT md.measure_id, md.data FROM measure_data md";
+        }
+
+        @Override
+        public Row load(ResultSet rs) throws SQLException {
+          Row row = new Row();
+          row.measure_id = SqlUtil.getLong(rs, 1);
+          row.data = rs.getBlob(2);
+          return row;
+        }
+      },
+      new MassUpdater.InputConverter<Row>() {
+        @Override
+        public String updateSql() {
+          return "UPDATE project_measures SET measure_data=? WHERE id=?";
+        }
+
+        @Override
+        public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+          updateStatement.setBlob(1, row.data);
+          updateStatement.setLong(2, row.measure_id);
+          return true;
+        }
+      }
+      );
+  }
+
+  private static class Row {
+    private Long measure_id;
+    private Blob data;
+  }
+
+}
index 75c0a8063547cdc7ea76e29fc37e63d8510573d1..0500c775f5422200cff4f34a6cf4ee0753c4b191 100644 (file)
@@ -32,7 +32,7 @@ class MergeMeasureDataIntoProjectMeasures < ActiveRecord::Migration
       add_column :project_measures, 'measure_data', :binary, :null => true
     end
     ProjectMeasure.reset_column_information
-    execute_ddl('move measure data', 'UPDATE project_measures m SET m.measure_data = (SELECT md.data FROM measure_data md WHERE md.measure_id = m.id)')
+    Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration('org.sonar.server.db.migrations.v44.MeasureDataMigration')
     drop_table(:measure_data)
   end
   
diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest.java
new file mode 100644 (file)
index 0000000..a0aaae4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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.v44;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.core.persistence.TestDatabase;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MeasureDataMigrationTest {
+
+  @ClassRule
+  public static TestDatabase db = new TestDatabase().schema(MeasureDataMigrationTest.class, "schema.sql");
+
+  MeasureDataMigration migration;
+
+  @Before
+  public void setUp() throws Exception {
+    migration = new MeasureDataMigration(db.database());
+  }
+
+  @Test
+  public void migrate_issues_action_plan_key() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_measure_data.xml");
+
+    migration.execute();
+
+    db.assertDbUnit(getClass(), "migrate_measure_data-result.xml", "project_measures");
+  }
+
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data-result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data-result.xml
new file mode 100644 (file)
index 0000000..657d854
--- /dev/null
@@ -0,0 +1,11 @@
+<dataset>
+
+   <project_measures id="1" VALUE="[null]" METRIC_ID="1" SNAPSHOT_ID="3001" 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_data="MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OQ=="/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/migrate_measure_data.xml
new file mode 100644 (file)
index 0000000..523f61d
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+
+   <project_measures id="1" VALUE="[null]" METRIC_ID="1" SNAPSHOT_ID="3001" 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_data="[null]"/>
+
+   <measure_data id="1" measure_id="1" snapshot_id="3001" data="MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OQ=="/>
+
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/MeasureDataMigrationTest/schema.sql
new file mode 100644 (file)
index 0000000..84a3706
--- /dev/null
@@ -0,0 +1,34 @@
+-- 4.4
+
+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,
+  "MEASURE_DATA" BINARY(167772150)
+);
+
+CREATE TABLE "MEASURE_DATA" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "MEASURE_ID" BIGINT,
+  "SNAPSHOT_ID" INTEGER,
+  "DATA" BINARY(167772150)
+);