From baa107dc6d0b0e2a1359b607ac659b68741c45a0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 29 Apr 2014 12:56:48 +0200 Subject: [PATCH] SONAR-5218 Add a migration to update issues action plan key linked on removed action plan --- .../core/persistence/DatabaseVersion.java | 2 +- .../org/sonar/core/persistence/rows-h2.sql | 1 + .../db/migrations/DatabaseMigrations.java | 6 +- .../v44/IssueActionPlanKeyMigration.java | 96 +++++++++++++++++++ ..._action_plan_key_on_removed_action_plan.rb | 31 ++++++ .../v44/IssueActionPlanKeyMigrationTest.java | 62 ++++++++++++ .../migrate_issues_action_plan_key.xml | 22 +++++ .../migrate_issues_action_plan_key_result.xml | 19 ++++ .../schema.sql | 41 ++++++++ 9 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/531_update_issues_action_plan_key_on_removed_action_plan.rb create mode 100644 sonar-server/src/test/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest.java create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key_result.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/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 41ca1b31a1e..0bec63db4ee 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 = 530; + public static final int LAST_VERSION = 531; 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 968d26bed97..b1a4dd82bb5 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 @@ -226,6 +226,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('524'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('525'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('526'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('530'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('531'); 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 18662efe7b4..68bda116223 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 @@ -24,6 +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 java.util.List; @@ -42,7 +43,10 @@ public interface DatabaseMigrations { TechnicalDebtMeasuresMigration.class, DevelopmentCostMeasuresMigration.class, RequirementMeasuresMigration.class, - NotResolvedIssuesOnRemovedComponentsMigration.class + NotResolvedIssuesOnRemovedComponentsMigration.class, + + // 4.4 + IssueActionPlanKeyMigration.class ); } diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java new file mode 100644 index 00000000000..b085c9ee119 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java @@ -0,0 +1,96 @@ +/* + * 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 com.google.common.annotations.VisibleForTesting; +import org.sonar.api.utils.System2; +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; +import java.sql.Timestamp; + +/** + * SONAR-5218 + * Update all issues having action plan linked on removed action plan. + * + * Used in the Active Record Migration 531. + * @since 4.4 + */ +public class IssueActionPlanKeyMigration implements DatabaseMigration { + + private final System2 system2; + private final Database db; + + public IssueActionPlanKeyMigration(Database database) { + this(database, System2.INSTANCE); + } + + @VisibleForTesting + IssueActionPlanKeyMigration(Database database, System2 system2) { + this.db = database; + this.system2 = system2; + } + + @Override + public void execute() { + new MassUpdater(db).execute( + new MassUpdater.InputLoader() { + @Override + public String selectSql() { + return "SELECT i.id FROM issues i " + + "LEFT OUTER JOIN action_plans ap ON ap.kee=i.action_plan_key " + + "WHERE i.action_plan_key IS NOT NULL " + + "AND ap.kee is null "; + } + + @Override + public Row load(ResultSet rs) throws SQLException { + Row row = new Row(); + row.id = SqlUtil.getLong(rs, 1); + return row; + } + }, + new MassUpdater.InputConverter() { + @Override + public String updateSql() { + return "UPDATE issues SET action_plan_key=NULL,updated_at=? WHERE id=?"; + } + + @Override + public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException { + updateStatement.setTimestamp(1, new Timestamp(system2.now())); + updateStatement.setLong(2, row.id); + return true; + } + } + ); + } + + private static class Row { + private Long id; + } + +} diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/531_update_issues_action_plan_key_on_removed_action_plan.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/531_update_issues_action_plan_key_on_removed_action_plan.rb new file mode 100644 index 00000000000..a6859443e2a --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/531_update_issues_action_plan_key_on_removed_action_plan.rb @@ -0,0 +1,31 @@ +# +# 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.4 +# SONAR-5218 +# +class UpdateIssuesActionPlanKeyOnRemovedActionPlan < ActiveRecord::Migration + + def self.up + Java::OrgSonarServerUi::JRubyFacade.getInstance().databaseMigrator().executeMigration('org.sonar.server.db.migrations.v44.IssueActionPlanKeyMigration') + end + +end diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest.java new file mode 100644 index 00000000000..0a806250a1d --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest.java @@ -0,0 +1,62 @@ +/* + * 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.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.core.persistence.TestDatabase; + +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class IssueActionPlanKeyMigrationTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(IssueActionPlanKeyMigrationTest.class, "schema.sql"); + + @Mock + System2 system2; + + IssueActionPlanKeyMigration migration; + + @Before + public void setUp() throws Exception { + when(system2.now()).thenReturn(DateUtils.parseDate("2014-04-28").getTime()); + + migration = new IssueActionPlanKeyMigration(db.database(), system2); + } + + @Test + public void migrate_issues_action_plan_key() throws Exception { + db.prepareDbUnit(getClass(), "migrate_issues_action_plan_key.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "migrate_issues_action_plan_key_result.xml", "issues"); + } + +} diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key.xml new file mode 100644 index 00000000000..51271c7c2d3 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key_result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key_result.xml new file mode 100644 index 00000000000..6d10f936015 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key_result.xml @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/schema.sql new file mode 100644 index 00000000000..1ad49679f5c --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/schema.sql @@ -0,0 +1,41 @@ +-- 4.4 + +CREATE TABLE "ISSUES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "KEE" VARCHAR(50) UNIQUE NOT NULL, + "COMPONENT_ID" INTEGER NOT NULL, + "ROOT_COMPONENT_ID" INTEGER, + "RULE_ID" INTEGER, + "SEVERITY" VARCHAR(10), + "MANUAL_SEVERITY" BOOLEAN NOT NULL, + "MESSAGE" VARCHAR(4000), + "LINE" INTEGER, + "EFFORT_TO_FIX" DOUBLE, + "STATUS" VARCHAR(20), + "RESOLUTION" VARCHAR(20), + "CHECKSUM" VARCHAR(1000), + "REPORTER" VARCHAR(40), + "ASSIGNEE" VARCHAR(40), + "AUTHOR_LOGIN" VARCHAR(100), + "ACTION_PLAN_KEY" VARCHAR(50) NULL, + "ISSUE_ATTRIBUTES" VARCHAR(4000), + "ISSUE_CREATION_DATE" TIMESTAMP, + "ISSUE_CLOSE_DATE" TIMESTAMP, + "ISSUE_UPDATE_DATE" TIMESTAMP, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, + "TECHNICAL_DEBT" INTEGER +); + +CREATE TABLE "ACTION_PLANS" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "KEE" VARCHAR(100), + "NAME" VARCHAR(200), + "DESCRIPTION" VARCHAR(1000), + "DEADLINE" TIMESTAMP, + "USER_LOGIN" VARCHAR(40), + "PROJECT_ID" INTEGER, + "STATUS" VARCHAR(10), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); -- 2.39.5