From 3e31fbf20e786c7e9b506d1c2cf2c67e4bfe80a0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 13 Mar 2014 08:24:39 +0100 Subject: [PATCH] SONAR-5056 Add migration on measures on requirements to measures on rules --- .../core/persistence/DatabaseVersion.java | 2 +- .../org/sonar/core/persistence/rows-h2.sql | 1 + .../db/migrations/DatabaseMigrations.java | 8 +- .../v43/RequirementMeasuresMigration.java | 85 +++++++++++++++++++ .../521_update_requirement_measures.rb | 30 +++++++ .../v43/RequirementMeasuresMigrationTest.java | 48 +++++++++++ .../migrate_measures_on_requirements.xml | 44 ++++++++++ ...igrate_measures_on_requirements_result.xml | 27 ++++++ .../schema.sql | 44 ++++++++++ 9 files changed, 283 insertions(+), 6 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/521_update_requirement_measures.rb create mode 100644 sonar-server/src/test/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest.java create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements_result.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/schema.sql 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 a4ad3c2eb75..ec82ef2498b 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 = 520; + public static final int LAST_VERSION = 521; 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 01b91d42ffc..18024e5406e 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 @@ -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; diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java index 4566f1b188e..b2690c3f3d8 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java @@ -20,13 +20,10 @@ 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 index 00000000000..adb2de1286d --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigration.java @@ -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() { + @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() { + @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 index 00000000000..d9632e4e20b --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/521_update_requirement_measures.rb @@ -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 index 00000000000..9c42c138295 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest.java @@ -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 index 00000000000..a5249ee19f0 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + 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 index 00000000000..df20f63c880 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/migrate_measures_on_requirements_result.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + 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 index 00000000000..a60b5de5b5d --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v43/RequirementMeasuresMigrationTest/schema.sql @@ -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 +); -- 2.39.5