diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-02-25 23:31:28 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-02-26 16:27:24 +0100 |
commit | 8dd5740bdeb71b8fcabbfb7d428f2e73b2e0d727 (patch) | |
tree | 2d50de1b3b52af8cb9371fa62f0c76e8a670edae /sonar-db | |
parent | 41c79688c9760806fb15cf2a6b97cf98d852f18c (diff) | |
download | sonarqube-8dd5740bdeb71b8fcabbfb7d428f2e73b2e0d727.tar.gz sonarqube-8dd5740bdeb71b8fcabbfb7d428f2e73b2e0d727.zip |
SONAR-7333 DB migration to set ISSUES.ISSUE_TYPE
Diffstat (limited to 'sonar-db')
10 files changed, 210 insertions, 6 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 113ee4f5fdd..744620671a6 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -29,7 +29,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1106; + public static final int LAST_VERSION = 1107; /** * The minimum supported version which can be upgraded. Lower diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 7df3779ced9..ac74da47d49 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -72,6 +72,7 @@ import org.sonar.db.version.v54.RemovePreviewPermission; import org.sonar.db.version.v55.AddIssuesType; import org.sonar.db.version.v55.AddRulesLongDateColumns; import org.sonar.db.version.v55.DeleteMeasuresWithCharacteristicId; +import org.sonar.db.version.v55.FeedIssueTypes; import org.sonar.db.version.v55.FeedRulesLongDateColumns; public class MigrationStepModule extends Module { @@ -143,7 +144,8 @@ public class MigrationStepModule extends Module { AddRulesLongDateColumns.class, FeedRulesLongDateColumns.class, DeleteMeasuresWithCharacteristicId.class, - AddIssuesType.class + AddIssuesType.class, + FeedIssueTypes.class ); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v55/FeedIssueTypes.java b/sonar-db/src/main/java/org/sonar/db/version/v55/FeedIssueTypes.java new file mode 100644 index 00000000000..233e2e63ff6 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v55/FeedIssueTypes.java @@ -0,0 +1,91 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.db.version.v55; + +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import java.sql.SQLException; +import java.util.List; +import org.sonar.api.utils.System2; +import org.sonar.core.issue.IssueType; +import org.sonar.db.Database; +import org.sonar.db.version.BaseDataChange; +import org.sonar.db.version.MassUpdate; +import org.sonar.db.version.MassUpdate.Handler; +import org.sonar.db.version.Select.Row; +import org.sonar.db.version.SqlStatement; + +import static com.google.common.collect.Lists.newArrayList; +import static org.apache.commons.lang.StringUtils.defaultString; + +/** + * Duplicates RuleTagsToTypeConverter from API on purpose. Db migration must be isolated and must not + * depend on external code that may evolve over time. + * + * https://jira.sonarsource.com/browse/MMF-141 + */ +public class FeedIssueTypes extends BaseDataChange { + + private final long now; + + public FeedIssueTypes(Database db, System2 system) { + super(db); + this.now = system.now(); + } + + @Override + public void execute(Context context) throws SQLException { + final Splitter tagSplitter = Splitter.on(','); + final Joiner tagJoiner = Joiner.on(',').skipNulls(); + + MassUpdate update = context.prepareMassUpdate().rowPluralName("issues"); + update.select("SELECT id, tags FROM issues WHERE issue_type IS NULL OR issue_type=0"); + update.update("UPDATE issues SET issue_type=?, tags=?, updated_at=? WHERE id=?"); + update.execute(new Handler() { + @Override + public boolean handle(Row row, SqlStatement update) throws SQLException { + long id = row.getLong(1); + + // See algorithm to deduce type from tags in RuleTagsToTypeConverter + List<String> tags = newArrayList(tagSplitter.split(defaultString(row.getNullableString(2)))); + IssueType type = tagsToType(tags); + tags.remove("bug"); + tags.remove("security"); + + update.setInt(1, type.getDbConstant()); + update.setString(2, tagJoiner.join(tags)); + update.setLong(3, now); + update.setLong(4, id); + return true; + } + }); + } + + static IssueType tagsToType(List<String> tags) { + IssueType type = IssueType.CODE_SMELL; + if (tags.contains("bug")) { + type = IssueType.BUG; + } else if (tags.contains("security")) { + type = IssueType.VULNERABILITY; + } + return type; + } + +} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 2016f764ae9..3c87dc8ed22 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -381,7 +381,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1100'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1101'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1102'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1103'); -INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1106'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1107'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java index ddd3920af68..cbda4d662bc 100644 --- a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java @@ -106,6 +106,7 @@ public class IssueDaoTest { assertThat(issue.getRuleId()).isEqualTo(500); assertThat(issue.getLanguage()).isEqualTo("java"); assertThat(issue.getSeverity()).isEqualTo("BLOCKER"); + assertThat(issue.getType()).isEqualTo(2); assertThat(issue.isManualSeverity()).isFalse(); assertThat(issue.getMessage()).isNull(); assertThat(issue.getLine()).isEqualTo(200); diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index 033448a921e..e81f6085362 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -29,6 +29,6 @@ public class MigrationStepModuleTest { public void verify_count_of_added_MigrationStep_types() { ComponentContainer container = new ComponentContainer(); new MigrationStepModule().configure(container); - assertThat(container.size()).isEqualTo(55); + assertThat(container.size()).isEqualTo(56); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/FeedIssueTypesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedIssueTypesTest.java new file mode 100644 index 00000000000..0299c30b39a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedIssueTypesTest.java @@ -0,0 +1,79 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.db.version.v55; + +import java.util.Arrays; +import java.util.Collections; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.core.issue.IssueType; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.issue.IssueDto; +import org.sonar.db.version.MigrationStep; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.sonar.db.version.v55.FeedIssueTypes.tagsToType; + +public class FeedIssueTypesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, FeedIssueTypesTest.class, "schema.sql"); + + @Test + public void test_tagsToType() { + assertThat(tagsToType(asList("misra", "bug"))).isEqualTo(IssueType.BUG); + assertThat(tagsToType(asList("misra", "security"))).isEqualTo(IssueType.VULNERABILITY); + + // "bug" has priority on "security" + assertThat(tagsToType(asList("security", "bug"))).isEqualTo(IssueType.BUG); + + // default is "code smell" + assertThat(tagsToType(asList("clumsy", "spring"))).isEqualTo(IssueType.CODE_SMELL); + assertThat(tagsToType(Collections.<String>emptyList())).isEqualTo(IssueType.CODE_SMELL); + } + + @Test + public void test_migration() throws Exception { + try (DbSession dbSession = db.getSession()) { + IssueDto codeSmell = new IssueDto().setKee("code_smell").setTags(Arrays.asList("clumsy", "spring")); + IssueDto withoutTags = new IssueDto().setKee("without_tags"); + IssueDto bug = new IssueDto().setKee("bug").setTags(Arrays.asList("clumsy", "bug")); + db.getDbClient().issueDao().insert(dbSession, codeSmell, withoutTags, bug); + dbSession.commit(); + + MigrationStep underTest = new FeedIssueTypes(db.database(), mock(System2.class)); + underTest.execute(); + + assertType("code_smell", IssueType.CODE_SMELL); + assertType("without_tags", IssueType.CODE_SMELL); + assertType("bug", IssueType.BUG); + } + } + + private void assertType(String issueKey, IssueType expectedType) { + Number type = (Number)db.selectFirst("select * from issues where kee='" + issueKey + "'").get("ISSUE_TYPE"); + assertThat(type.intValue()).isEqualTo(expectedType.getDbConstant()); + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/v55/FeedRulesLongDateColumnsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedRulesLongDateColumnsTest.java index 84da34783be..240c0e79345 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v55/FeedRulesLongDateColumnsTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v55/FeedRulesLongDateColumnsTest.java @@ -39,7 +39,7 @@ public class FeedRulesLongDateColumnsTest { System2 system = mock(System2.class); - MigrationStep migration = new FeedRulesLongDateColumns(db.database(), system); + MigrationStep underTest = new FeedRulesLongDateColumns(db.database(), system); @Before public void setUp() throws Exception { @@ -50,7 +50,7 @@ public class FeedRulesLongDateColumnsTest { public void execute() throws Exception { db.prepareDbUnit(getClass(), "execute.xml"); - migration.execute(); + underTest.execute(); assertThat(db.countSql("select count(*) from rules where created_at_ms is not null and updated_at_ms is not null")).isEqualTo(3); // Only 1 rules not updated diff --git a/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml b/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml index 0b261a3252d..07ddc4b1729 100644 --- a/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml +++ b/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml @@ -24,6 +24,7 @@ created_at="1400000000000" updated_at="1450000000000" locations="[null]" + issue_type="2" /> <issues @@ -50,6 +51,7 @@ created_at="1400000000000" updated_at="1450000000000" locations="[null]" + issue_type="2" /> </dataset> diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedIssueTypesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedIssueTypesTest/schema.sql new file mode 100644 index 00000000000..b7526a5d2bb --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v55/FeedIssueTypesTest/schema.sql @@ -0,0 +1,29 @@ +CREATE TABLE "ISSUES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "KEE" VARCHAR(50) UNIQUE NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "PROJECT_UUID" VARCHAR(50), + "RULE_ID" INTEGER, + "SEVERITY" VARCHAR(10), + "MANUAL_SEVERITY" BOOLEAN NOT NULL, + "MESSAGE" VARCHAR(4000), + "LINE" INTEGER, + "EFFORT_TO_FIX" DOUBLE, + "TECHNICAL_DEBT" INTEGER, + "STATUS" VARCHAR(20), + "RESOLUTION" VARCHAR(20), + "CHECKSUM" VARCHAR(1000), + "REPORTER" VARCHAR(255), + "ASSIGNEE" VARCHAR(255), + "AUTHOR_LOGIN" VARCHAR(255), + "ACTION_PLAN_KEY" VARCHAR(50) NULL, + "ISSUE_ATTRIBUTES" VARCHAR(4000), + "TAGS" VARCHAR(4000), + "ISSUE_CREATION_DATE" BIGINT, + "ISSUE_CLOSE_DATE" BIGINT, + "ISSUE_UPDATE_DATE" BIGINT, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT, + "LOCATIONS" BLOB(167772150), + "ISSUE_TYPE" TINYINT +); |