From 8d0aafd422f93d3b32ac8d07aeab6eb12e0aa0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 16 Aug 2016 19:08:25 +0200 Subject: [PATCH] SONAR-7842 add error columns to CE_ACTIVITY --- .../1307_add_error_columns_to_ce_activity.rb | 29 +++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 4 +- .../v61/AddErrorColumnsToCeActivity.java | 46 ++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../org/sonar/db/version/schema-h2.ddl | 4 +- .../db/version/MigrationStepModuleTest.java | 2 +- .../v61/AddErrorColumnsToCeActivityTest.java | 87 +++++++++++++++++++ .../old_ce_activity.sql | 17 ++++ 9 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1307_add_error_columns_to_ce_activity.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivity.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest/old_ce_activity.sql diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1307_add_error_columns_to_ce_activity.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1307_add_error_columns_to_ce_activity.rb new file mode 100644 index 000000000000..1e7f47c5e9c0 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1307_add_error_columns_to_ce_activity.rb @@ -0,0 +1,29 @@ +# +# 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 6.1 +# +class AddErrorColumnsToCeActivity < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v61.AddErrorColumnsToCeActivity') + end +end 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 f3b429dc19e2..bb811799f642 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 @@ -30,7 +30,7 @@ public class DatabaseVersion { - public static final int LAST_VERSION = 1_306; + public static final int LAST_VERSION = 1_307; /** * 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 634e51128b80..b30051b2c988 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 @@ -145,6 +145,7 @@ import org.sonar.db.version.v60.PopulateUuidPathColumnOnProjects; import org.sonar.db.version.v60.RemoveUsersPasswordWhenNotLocal; import org.sonar.db.version.v61.AddBUuidPathToProjects; +import org.sonar.db.version.v61.AddErrorColumnsToCeActivity; import org.sonar.db.version.v61.DeleteProjectDashboards; import org.sonar.db.version.v61.DeleteReportsFromCeQueue; import org.sonar.db.version.v61.DropIsGlobalFromDashboards; @@ -316,6 +317,7 @@ protected void configureModule() { DropIsGlobalFromDashboards.class, DeleteReportsFromCeQueue.class, ShrinkModuleUuidPathOfProjects.class, - AddBUuidPathToProjects.class); + AddBUuidPathToProjects.class, + AddErrorColumnsToCeActivity.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivity.java b/sonar-db/src/main/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivity.java new file mode 100644 index 000000000000..6fbe1e434609 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivity.java @@ -0,0 +1,46 @@ +/* + * 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.v61; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.AddColumnsBuilder; +import org.sonar.db.version.DdlChange; + +import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder; +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AddErrorColumnsToCeActivity extends DdlChange { + + private static final String TABLE_CE_ACTIVITY = "ce_activity"; + + public AddErrorColumnsToCeActivity(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDatabase().getDialect(), TABLE_CE_ACTIVITY) + .addColumn(newVarcharColumnDefBuilder().setColumnName("error_message").setLimit(1000).setIsNullable(true).build()) + .addColumn(newClobColumnDefBuilder().setColumnName("error_stacktrace").setIsNullable(true).build()) + .build()); + } + +} 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 ca2bef32e9c9..bb91bf092de5 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 @@ -493,6 +493,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1303'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1304'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1305'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1306'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1307'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index a581e7edf716..43d5caa4c100 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -531,7 +531,9 @@ CREATE TABLE "CE_ACTIVITY" ( "EXECUTED_AT" BIGINT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL, - "EXECUTION_TIME_MS" BIGINT NULL + "EXECUTION_TIME_MS" BIGINT NULL, + "ERROR_MESSAGE" VARCHAR(1000), + "ERROR_STACKTRACE" CLOB(2147483647) ); CREATE TABLE "CE_TASK_INPUT" ( 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 634666429cce..dffa71a16b94 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(131); + assertThat(container.size()).isEqualTo(132); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest.java new file mode 100644 index 000000000000..31085a733593 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest.java @@ -0,0 +1,87 @@ +/* + * 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.v61; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static java.lang.String.valueOf; + +public class AddErrorColumnsToCeActivityTest { + + private static final String TABLE = "CE_ACTIVITY"; + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddErrorColumnsToCeActivityTest.class, "old_ce_activity.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddErrorColumnsToCeActivity underTest = new AddErrorColumnsToCeActivity(db.database()); + + @Test + public void migration_adds_column_to_empty_table() throws SQLException { + underTest.execute(); + + verifyAddedColumns(); + } + + @Test + public void migration_adds_columns_to_populated_table() throws SQLException { + for (int i = 0; i < 9; i++) { + db.executeInsert( + TABLE, + "uuid", valueOf(i), + "task_type", "PROJECT", + "component_uuid", valueOf(i + 20), + "analysis_uuid", valueOf(i + 30), + "status", "ok", + "is_last", "true", + "is_last_key", "aa", + "submitted_at", valueOf(84654), + "created_at", valueOf(9512), + "updated_at", valueOf(45120)); + } + db.commit(); + + underTest.execute(); + + verifyAddedColumns(); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Fail to execute "); + underTest.execute(); + } + + private void verifyAddedColumns() { + db.assertColumnDefinition(TABLE, "error_message", Types.VARCHAR, 1000, true); + db.assertColumnDefinition(TABLE, "error_stacktrace", Types.CLOB, null, true); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest/old_ce_activity.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest/old_ce_activity.sql new file mode 100644 index 000000000000..a519e005821c --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v61/AddErrorColumnsToCeActivityTest/old_ce_activity.sql @@ -0,0 +1,17 @@ +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, + "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 +);