From 90920c321540b5109a1bde775b0a0b26f16ba15d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 25 Nov 2013 10:46:18 +0100 Subject: [PATCH] SONAR-4895 Add migration to populate new characteristics columns --- .../core/persistence/DatabaseVersion.java | 2 +- .../org/sonar/core/persistence/rows-h2.sql | 1 + .../db/migrate/462_migrate_characteristics.rb | 122 ++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/462_migrate_characteristics.rb diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index 79dace471d8..7af4c474942 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -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 diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 4e2b6c9aa60..b268b1e4bb5 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -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 index 00000000000..5c94516d038 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/462_migrate_characteristics.rb @@ -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 + -- 2.39.5