]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5218 Add a migration to update issues action plan key linked on removed action...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 29 Apr 2014 10:56:48 +0000 (12:56 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 29 Apr 2014 10:56:48 +0000 (12:56 +0200)
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/java/org/sonar/server/db/migrations/DatabaseMigrations.java
sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/db/migrate/531_update_issues_action_plan_key_on_removed_action_plan.rb [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/migrate_issues_action_plan_key_result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigrationTest/schema.sql [new file with mode: 0644]

index 41ca1b31a1e49579c38f7b964245fbcf06c95d21..0bec63db4ee50a749821b4eeffe8fbee172a3a2e 100644 (file)
@@ -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
index 968d26bed9726fdd74546404e4e8a1c73925f72a..b1a4dd82bb5244773582cad535c4d704bdb5456c 100644 (file)
@@ -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;
index 18662efe7b4d635917e6102869c6794fcec7c98c..68bda1162239fd9cd0177e373065ed12fe125cc3 100644 (file)
@@ -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 (file)
index 0000000..b085c9e
--- /dev/null
@@ -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<Row>() {
+        @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<Row>() {
+        @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 (file)
index 0000000..a685944
--- /dev/null
@@ -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 (file)
index 0000000..0a80625
--- /dev/null
@@ -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 (file)
index 0000000..51271c7
--- /dev/null
@@ -0,0 +1,22 @@
+<dataset>
+
+  <action_plans id="1" kee="ABCD" project_id="1" name="SHORT_TERM" description="[null]" deadline="[null]"
+                user_login="igor" status="[null]" created_at="[null]" updated_at="[null]" />
+
+  <!-- linked on removed action plan -->
+  <issues ID="1" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="1"
+          ACTION_PLAN_KEY="REMOVED" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+          TECHNICAL_DEBT="10"/>
+
+  <!-- linked on not removed action plan -->
+  <issues ID="2" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="2"
+          ACTION_PLAN_KEY="ABCD" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+          TECHNICAL_DEBT="10"/>
+
+</dataset>
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 (file)
index 0000000..6d10f93
--- /dev/null
@@ -0,0 +1,19 @@
+<dataset>
+
+  <!-- linked on removed action plan -->
+  <issues ID="1" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="1"
+          ACTION_PLAN_KEY="[null]" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2014-04-28 00:00:00.0"
+          TECHNICAL_DEBT="10"/>
+
+  <!-- linked on not removed action plan -->
+  <issues ID="2" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="2"
+          ACTION_PLAN_KEY="ABCD" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+          TECHNICAL_DEBT="10"/>
+
+</dataset>
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 (file)
index 0000000..1ad4967
--- /dev/null
@@ -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
+);