From a795aa9c302449a3444f96842ab4084e50c8f49c Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 28 Sep 2017 13:20:32 +0200 Subject: [PATCH] Add db column ce_activity.error_type --- .../org/sonar/db/version/schema-h2.ddl | 3 +- .../java/org/sonar/db/ce/CeActivityDto.java | 17 ++++++ .../org/sonar/db/ce/CeActivityMapper.xml | 7 ++- .../org/sonar/db/ce/CeActivityDaoTest.java | 3 + .../AddErrorTypeColumnToCeActivityTable.java | 45 +++++++++++++++ .../db/migration/version/v66/DbVersion66.java | 4 +- ...dErrorTypeColumnToCeActivityTableTest.java | 55 +++++++++++++++++++ .../version/v66/DbVersion66Test.java | 2 +- .../ce_activity_6_5.sql | 21 +++++++ 9 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest/ce_activity_6_5.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 760b11ce89d..19e5a27b81f 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -609,7 +609,8 @@ CREATE TABLE "CE_ACTIVITY" ( "UPDATED_AT" BIGINT NOT NULL, "EXECUTION_TIME_MS" BIGINT NULL, "ERROR_MESSAGE" VARCHAR(1000), - "ERROR_STACKTRACE" CLOB(2147483647) + "ERROR_STACKTRACE" CLOB(2147483647), + "ERROR_TYPE" VARCHAR(20) ); CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID"); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java index 0183618a348..692244129fd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java @@ -69,6 +69,13 @@ public class CeActivityDto { * @see CeActivityDao#selectByUuid(DbSession, String) */ private String errorStacktrace; + + /** + * Optional free-text type of error. It may be set only when {@link #errorMessage} is not null. + */ + @Nullable + private String errorType; + /** * Flag indicating whether the analysis of the current activity has a scanner context or not. *

@@ -247,6 +254,16 @@ public class CeActivityDto { return this; } + @CheckForNull + public String getErrorType() { + return errorType; + } + + public CeActivityDto setErrorType(@Nullable String s) { + this.errorType = ensureNotTooBig(s, 20); + return this; + } + @CheckForNull private static String ensureNotTooBig(@Nullable String str, int maxSize) { if (str == null) { diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml index 59f459c8df3..3b1b34b6a33 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml @@ -34,6 +34,7 @@ ca.is_last_key as isLastKey, ca.execution_time_ms as executionTimeMs, ca.error_message as errorMessage, + ca.error_type as errorType, @@ -150,7 +151,8 @@ updated_at, execution_time_ms, error_message, - error_stacktrace + error_stacktrace, + error_type ) values ( #{uuid,jdbcType=VARCHAR}, @@ -170,7 +172,8 @@ #{updatedAt,jdbcType=BIGINT}, #{executionTimeMs,jdbcType=BIGINT}, #{errorMessage,jdbcType=VARCHAR}, - #{errorStacktrace,jdbcType=CLOB} + #{errorStacktrace,jdbcType=CLOB}, + #{errorType,jdbcType=VARCHAR} ) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java index f11864bb66b..f887e5f49f0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java @@ -77,6 +77,7 @@ public class CeActivityDaoTest { assertThat(dto.toString()).isNotEmpty(); assertThat(dto.getErrorMessage()).isNull(); assertThat(dto.getErrorStacktrace()).isNull(); + assertThat(dto.getErrorType()).isNull(); assertThat(dto.isHasScannerContext()).isFalse(); } @@ -111,6 +112,7 @@ public class CeActivityDaoTest { CeActivityDto read = saved.get(); assertThat(read.getErrorMessage()).isEqualTo(dto.getErrorMessage()); assertThat(read.getErrorStacktrace()).isEqualTo(dto.getErrorStacktrace()); + assertThat(read.getErrorType()).isNotNull().isEqualTo(dto.getErrorType()); } @Test @@ -360,6 +362,7 @@ public class CeActivityDaoTest { dto.setAnalysisUuid(uuid + "_2"); if (status == FAILED) { dto.setErrorMessage("error msg for " + uuid); + dto.setErrorType("anErrorType"); } return dto; } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTable.java new file mode 100644 index 00000000000..c423bb0af11 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTable.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v66; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddErrorTypeColumnToCeActivityTable extends DdlChange { + + public AddErrorTypeColumnToCeActivityTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), "ce_activity") + .addColumn(VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("error_type") + .setLimit(20) + .setIsNullable(true) + .build()) + .build()); + } +} + diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java index 3bfe2ec422f..3cb9ff5735c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66.java @@ -35,6 +35,8 @@ public class DbVersion66 implements DbVersion { .add(1806, "Create table project_branches", CreateTableProjectBranches.class) .add(1807, "Add on project_branches key", AddIndexOnProjectBranchesKey.class) .add(1808, "Add branch column to projects table", AddBranchColumnToProjectsTable.class) - .add(1809, "Populate project_branches with existing main branches", PopulateMainProjectBranches.class); + .add(1809, "Populate project_branches with existing main branches", PopulateMainProjectBranches.class) + .add(1810, "Add ce_activity.error_type", AddErrorTypeColumnToCeActivityTable.class) + ; } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest.java new file mode 100644 index 00000000000..f46ba8e1a9e --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v66; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + + +public class AddErrorTypeColumnToCeActivityTableTest { + + @Rule + public final CoreDbTester dbTester = CoreDbTester.createForSchema(AddErrorTypeColumnToCeActivityTableTest.class, "ce_activity_6_5.sql"); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddErrorTypeColumnToCeActivityTable underTest = new AddErrorTypeColumnToCeActivityTable(dbTester.database()); + + @Test + public void column_is_added_to_table() throws SQLException { + underTest.execute(); + + dbTester.assertColumnDefinition("ce_activity", "error_type", java.sql.Types.VARCHAR, 20, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java index 76a9365740c..6ebcf647366 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/DbVersion66Test.java @@ -36,7 +36,7 @@ public class DbVersion66Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 9); + verifyMigrationCount(underTest, 10); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest/ce_activity_6_5.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest/ce_activity_6_5.sql new file mode 100644 index 00000000000..588109c67ff --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/AddErrorTypeColumnToCeActivityTableTest/ce_activity_6_5.sql @@ -0,0 +1,21 @@ +CREATE TABLE "CE_ACTIVITY" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40) NOT NULL, + "TASK_TYPE" VARCHAR(15) NOT NULL, + "COMPONENT_UUID" VARCHAR(40) NULL, + "ANALYSIS_UUID" VARCHAR(50) NULL, + "STATUS" VARCHAR(15) NOT NULL, + "IS_LAST" BOOLEAN NOT NULL, + "IS_LAST_KEY" VARCHAR(55) NOT NULL, + "SUBMITTER_LOGIN" VARCHAR(255) NULL, + "WORKER_UUID" VARCHAR(40) NULL, + "EXECUTION_COUNT" INTEGER NOT NULL, + "SUBMITTED_AT" BIGINT NOT NULL, + "STARTED_AT" BIGINT NULL, + "EXECUTED_AT" BIGINT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "EXECUTION_TIME_MS" BIGINT NULL, + "ERROR_MESSAGE" VARCHAR(1000), + "ERROR_STACKTRACE" CLOB(2147483647) +); -- 2.39.5