]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4895 Add migration to populate new characteristics columns
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 25 Nov 2013 09:46:18 +0000 (10:46 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 25 Nov 2013 09:46:18 +0000 (10:46 +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/webapp/WEB-INF/db/migrate/462_migrate_characteristics.rb [new file with mode: 0644]

index 79dace471d8145b68a5fffb04cab25d92a33c20b..7af4c474942a6a978c0487ee1d6c5586bdb8a121 100644 (file)
@@ -33,7 +33,7 @@ import java.util.List;
  */
 public class DatabaseVersion implements BatchComponent, ServerComponent {
 
-  public static final int LAST_VERSION = 461;
+  public static final int LAST_VERSION = 462;
 
   public static enum Status {
     UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
index 4e2b6c9aa60cc812fc89c71ab130c9da12e9d0ff..b268b1e4bb5fd1d747f3222c27c3828eaff0959f 100644 (file)
@@ -184,6 +184,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('443');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('444');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('460');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('461');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('462');
 
 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;
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/462_migrate_characteristics.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/462_migrate_characteristics.rb
new file mode 100644 (file)
index 0000000..5c94516
--- /dev/null
@@ -0,0 +1,122 @@
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2008-2013 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.
+#
+
+#
+# Sonar 4.1
+# SONAR-4831
+# SONAR-4895
+#
+class MigrateCharacteristics < ActiveRecord::Migration
+
+  class QualityModel < ActiveRecord::Base
+  end
+
+  class Characteristic < ActiveRecord::Base
+  end
+
+  class CharacteristicEdge < ActiveRecord::Base
+  end
+
+  class CharacteristicProperty < ActiveRecord::Base
+  end
+
+  def self.up
+    now = Time.now
+
+    QualityModel.reset_column_information
+    Characteristic.reset_column_information
+    CharacteristicEdge.reset_column_information
+    CharacteristicProperty.reset_column_information
+
+    sqale_model = QualityModel.first(:conditions => ['name=?', 'SQALE'])
+    characteristics = Characteristic.all(:conditions => ['quality_model_id=?', sqale_model.id])
+    characteristic_edges = CharacteristicEdge.all
+    parent_ids_by_characteristic_id = {}
+    characteristic_edges.each do |edge|
+      parent_ids_by_characteristic_id[edge.child_id] = edge.parent_id
+    end
+
+    properties = CharacteristicProperty.all
+    properties_by_characteristic_id = {}
+    properties.each do |prop|
+      char_properties = properties_by_characteristic_id[prop.characteristic_id] || []
+      char_properties << prop
+      properties_by_characteristic_id[prop.characteristic_id] = char_properties
+    end
+
+    requirements_to_delete = []
+
+    characteristics.each do |characteristic|
+      # Requirement
+      if characteristic.rule_id
+        char_properties = properties_by_characteristic_id[characteristic.id]
+        function = char_properties.find { |prop| prop.kee == 'remediationFunction'} if char_properties
+        if char_properties && function
+          factor = char_properties.find { |prop| prop.kee == 'remediationFactor' }
+          offset = char_properties.find { |prop| prop.kee == 'offset' }
+
+          case function.text_value
+            when 'linear'
+              characteristic.function_key = 'linear'
+              characteristic.factor_value = factor.value
+              characteristic.factor_unit = factor.text_value
+              characteristic.offset_value = 0.0
+              characteristic.offset_unit = 'mn'
+
+            when 'linear_offset'
+              characteristic.function_key = 'linear_offset'
+              characteristic.factor_value = factor.value
+              characteristic.factor_unit = factor.text_value
+              characteristic.offset_value = offset.value
+              characteristic.offset_unit = offset.text_value
+
+            # linear_threshold is depreciated and is replaced by linear
+            when 'linear_threshold'
+              characteristic.function_key = 'linear'
+              characteristic.factor_value = factor.value
+              characteristic.factor_unit = factor.text_value
+              characteristic.offset_value = 0.0
+              characteristic.offset_unit = 'mn'
+
+            # constant_resource is no more managed anymore, it has to be deleted
+            when 'constant_resource'
+              requirements_to_delete << characteristic
+          end
+          # requirement without properties or without remediationFunction has to be deleted
+        else
+          requirements_to_delete << characteristic
+        end
+      end
+
+      characteristic.parent_id = parent_ids_by_characteristic_id[characteristic.id]
+      characteristic.created_at = now
+      characteristic.updated_at = now
+      characteristic.save
+    end
+
+    requirements_to_delete.each do |requirement|
+      CharacteristicProperty.delete_all(['characteristic_id=?', requirement.id])
+      CharacteristicEdge.delete_all(['child_id=?', requirement.id])
+      requirement.delete
+    end
+  end
+
+end
+