From 082949f81baed8e98867d6e752a6378cec4b2960 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 24 Jun 2016 16:36:23 +0200 Subject: [PATCH] SONAR-7824 Add DB column ACTIVITIES.PROFILE_KEY --- .../org/sonar/server/activity/Activity.java | 17 ++++- .../server/activity/ActivityService.java | 14 +++- .../qualityprofile/ActiveRuleChange.java | 1 + .../server/activity/ActivityServiceTest.java | 6 +- .../index/ActivityResultSetIteratorTest.java | 1 - .../activity/ws/ActivitiesWsMediumTest.java | 27 +++++-- .../traverse.xml | 4 +- .../1259_add_profile_key_to_activities.rb | 28 +++++++ .../org/sonar/db/activity/ActivityDao.java | 10 --- .../org/sonar/db/activity/ActivityDto.java | 10 +++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 2 + .../v60/AddProfileKeyToActivities.java | 45 +++++++++++ .../org/sonar/db/activity/ActivityMapper.xml | 4 +- .../org/sonar/db/version/schema-h2.ddl | 1 + .../sonar/db/activity/ActivityDaoTest.java | 19 +++-- .../db/version/MigrationStepModuleTest.java | 2 +- ...moveAnalysisReportsFromActivitiesTest.java | 10 ++- .../v60/AddProfileKeyToActivitiesTest.java | 75 +++++++++++++++++++ .../activities.sql | 10 +++ 20 files changed, 249 insertions(+), 39 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1259_add_profile_key_to_activities.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v60/AddProfileKeyToActivities.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest/activities.sql diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/Activity.java b/server/sonar-server/src/main/java/org/sonar/server/activity/Activity.java index f4e8939f933..67069330b30 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/activity/Activity.java +++ b/server/sonar-server/src/main/java/org/sonar/server/activity/Activity.java @@ -19,11 +19,10 @@ */ package org.sonar.server.activity; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - import java.util.LinkedHashMap; import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; public class Activity { @@ -34,7 +33,8 @@ public class Activity { private Type type; private String action; private String message; - private final Map data = new LinkedHashMap<>(); + private String profileKey; + private final Map data = new LinkedHashMap<>(); public Type getType() { return type; @@ -69,4 +69,13 @@ public class Activity { this.data.put(key, val); return this; } + + public String getProfileKey() { + return profileKey; + } + + public Activity setProfileKey(String profileKey) { + this.profileKey = profileKey; + return this; + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/ActivityService.java b/server/sonar-server/src/main/java/org/sonar/server/activity/ActivityService.java index 0f1f013b759..bf3885e353e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/activity/ActivityService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/activity/ActivityService.java @@ -21,9 +21,10 @@ package org.sonar.server.activity; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.util.Uuids; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.activity.ActivityDto; import org.sonar.server.activity.index.ActivityIndexer; -import org.sonar.db.DbClient; import org.sonar.server.user.UserSession; public class ActivityService { @@ -45,8 +46,15 @@ public class ActivityService { .setAction(activity.getAction()) .setMessage(activity.getMessage()) .setData(KeyValueFormat.format(activity.getData())) + .setProfileKey(activity.getProfileKey()) .setType(activity.getType().name()); - dbClient.activityDao().insert(dto); - indexer.index(); + DbSession dbSession = dbClient.openSession(false); + try { + dbClient.activityDao().insert(dbSession, dto); + dbSession.commit(); + indexer.index(); + } finally { + dbClient.closeSession(dbSession); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java index 8c910f5140c..87350de9d01 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java @@ -92,6 +92,7 @@ public class ActiveRuleChange { Activity activity = new Activity(); activity.setType(Activity.Type.QPROFILE); activity.setAction(type.name()); + activity.setProfileKey(getKey().qProfile()); activity.setData("key", getKey().toString()); activity.setData("ruleKey", getKey().ruleKey().toString()); activity.setData("profileKey", getKey().qProfile()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/ActivityServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/ActivityServiceTest.java index 9904058376c..69618927959 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/activity/ActivityServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/activity/ActivityServiceTest.java @@ -71,20 +71,22 @@ public class ActivityServiceTest { activity.setAction("THE_ACTION"); activity.setMessage("THE_MSG"); activity.setData("foo", "bar"); + activity.setData("profileKey", "PROFILE_KEY"); + activity.setProfileKey("PROFILE_KEY"); service.save(activity); Map dbMap = db.selectFirst("select log_type as \"type\", log_action as \"action\", log_message as \"msg\", data_field as \"data\" from activities"); assertThat(dbMap).containsEntry("type", "ANALYSIS_REPORT"); assertThat(dbMap).containsEntry("action", "THE_ACTION"); assertThat(dbMap).containsEntry("msg", "THE_MSG"); - assertThat(dbMap.get("data")).isEqualTo("foo=bar"); + assertThat(dbMap.get("data")).isEqualTo("foo=bar;profileKey=PROFILE_KEY"); List docs = es.getDocuments("activities", "activity", ActivityDoc.class); assertThat(docs).hasSize(1); assertThat(docs.get(0).getKey()).isNotEmpty(); assertThat(docs.get(0).getAction()).isEqualTo("THE_ACTION"); assertThat(docs.get(0).getMessage()).isEqualTo("THE_MSG"); - assertThat(docs.get(0).getDetails()).containsOnly(MapEntry.entry("foo", "bar")); + assertThat(docs.get(0).getDetails()).containsOnly(MapEntry.entry("foo", "bar"), MapEntry.entry("profileKey", "PROFILE_KEY")); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/index/ActivityResultSetIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/index/ActivityResultSetIteratorTest.java index ee56231d389..7a20cd28de2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/activity/index/ActivityResultSetIteratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/activity/index/ActivityResultSetIteratorTest.java @@ -30,7 +30,6 @@ import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; - public class ActivityResultSetIteratorTest { @Rule diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java index 196a6e100c0..895b3cf2331 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java @@ -31,6 +31,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.test.JsonAssert.assertJson; public class ActivitiesWsMediumTest { @@ -68,12 +69,28 @@ public class ActivitiesWsMediumTest { activity.setAction("THE_ACTION"); activity.setMessage("THE_MSG"); activity.setData("foo", "bar"); - service.save(activity); + activity.setProfileKey("PROFILE_KEY"); + service.save(activity); WsTester.TestRequest request = tester.wsTester().newGetRequest("api/activities", "search"); - WsTester.Result result = request.execute(); - assertThat(result.outputAsString()).contains("\"total\":1"); - assertThat(result.outputAsString()).contains("\"type\":\"ANALYSIS_REPORT\""); - assertThat(result.outputAsString()).contains("\"details\":{\"foo\":\"bar\"}"); + + String result = request.execute().outputAsString(); + assertJson(result).isSimilarTo( + "{" + + " \"total\": 1," + + " \"p\": 1," + + " \"ps\": 10," + + " \"logs\": [" + + " {" + + " \"type\": \"ANALYSIS_REPORT\"," + + " \"action\": \"THE_ACTION\"," + + " \"message\": \"THE_MSG\"," + + " \"details\": {" + + " \"profileKey\": \"PROFILE_KEY\"," + + " \"foo\": \"bar\"" + + " }" + + " }" + + " ]" + + "}"); } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/activity/index/ActivityResultSetIteratorTest/traverse.xml b/server/sonar-server/src/test/resources/org/sonar/server/activity/index/ActivityResultSetIteratorTest/traverse.xml index f92f7678379..1a480fc228f 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/activity/index/ActivityResultSetIteratorTest/traverse.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/activity/index/ActivityResultSetIteratorTest/traverse.xml @@ -1,7 +1,7 @@ + created_at="2014-01-01" data_field="foo=bar" user_login="THE_AUTHOR" profile_key="PROFILE_KEY"/> + created_at="2015-01-01" data_field="foo=bar" user_login="THE_AUTHOR" profile_key="PROFILE_KEY"/> diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1259_add_profile_key_to_activities.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1259_add_profile_key_to_activities.rb new file mode 100644 index 00000000000..e8797025bc8 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1259_add_profile_key_to_activities.rb @@ -0,0 +1,28 @@ +# +# 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.0 +# SONAR-7794 +# +class AddProfileKeyToActivities < ActiveRecord::Migration + def self.up + execute_java_migration('org.sonar.db.version.v60.AddProfileKeyToActivities') + end +end diff --git a/sonar-db/src/main/java/org/sonar/db/activity/ActivityDao.java b/sonar-db/src/main/java/org/sonar/db/activity/ActivityDao.java index d180c138b72..7c8a7e27416 100644 --- a/sonar-db/src/main/java/org/sonar/db/activity/ActivityDao.java +++ b/sonar-db/src/main/java/org/sonar/db/activity/ActivityDao.java @@ -31,16 +31,6 @@ public class ActivityDao extends AbstractDao { super(mybatis, system); } - public void insert(ActivityDto dto) { - DbSession session = myBatis().openSession(false); - try { - insert(session, dto); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } - public void insert(DbSession session, ActivityDto dto) { dto.setCreatedAt(new Date(now())); session.getMapper(ActivityMapper.class).insert(dto); diff --git a/sonar-db/src/main/java/org/sonar/db/activity/ActivityDto.java b/sonar-db/src/main/java/org/sonar/db/activity/ActivityDto.java index a50be420e2f..70b8b7a4c00 100644 --- a/sonar-db/src/main/java/org/sonar/db/activity/ActivityDto.java +++ b/sonar-db/src/main/java/org/sonar/db/activity/ActivityDto.java @@ -27,6 +27,7 @@ import org.apache.commons.lang.builder.ToStringStyle; public class ActivityDto { private String key; + private String profileKey; private String message; private String type; private String action; @@ -43,6 +44,15 @@ public class ActivityDto { return key; } + public String getProfileKey() { + return profileKey; + } + + public ActivityDto setProfileKey(String profileKey) { + this.profileKey = profileKey; + return this; + } + public Date getCreatedAt() { return createdAt; } 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 0099f81fdb8..ec5a26be020 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 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1_258; + public static final int LAST_VERSION = 1_259; /** * 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 064eedfa295..b8c9c9e3eb4 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 @@ -90,6 +90,7 @@ import org.sonar.db.version.v60.AddComponentUuidColumnToDuplicationsIndex; import org.sonar.db.version.v60.AddComponentUuidColumnToMeasures; import org.sonar.db.version.v60.AddComponentUuidColumnsToSnapshots; import org.sonar.db.version.v60.AddLastUsedColumnToRulesProfiles; +import org.sonar.db.version.v60.AddProfileKeyToActivities; import org.sonar.db.version.v60.AddUuidColumnToSnapshots; import org.sonar.db.version.v60.AddUuidColumnsToProjects; import org.sonar.db.version.v60.AddUuidColumnsToResourceIndex; @@ -241,6 +242,7 @@ public class MigrationStepModule extends Module { DropIdColumnsFromProjects.class, AddLastUsedColumnToRulesProfiles.class, PopulateLastUsedColumnOfRulesProfiles.class, + AddProfileKeyToActivities.class, // SNAPSHOTS.UUID AddUuidColumnToSnapshots.class, diff --git a/sonar-db/src/main/java/org/sonar/db/version/v60/AddProfileKeyToActivities.java b/sonar-db/src/main/java/org/sonar/db/version/v60/AddProfileKeyToActivities.java new file mode 100644 index 00000000000..54b37121834 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v60/AddProfileKeyToActivities.java @@ -0,0 +1,45 @@ +/* + * 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.v60; + +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.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AddProfileKeyToActivities extends DdlChange { + + private static final String TABLE_ACTIVITIES = "activities"; + + public AddProfileKeyToActivities(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDatabase().getDialect(), TABLE_ACTIVITIES) + .addColumn(newVarcharColumnDefBuilder().setColumnName("profile_key").setLimit(255).setIsNullable(true).build()) + .build()); + } + +} diff --git a/sonar-db/src/main/resources/org/sonar/db/activity/ActivityMapper.xml b/sonar-db/src/main/resources/org/sonar/db/activity/ActivityMapper.xml index 0014464aa2c..63084a23551 100644 --- a/sonar-db/src/main/resources/org/sonar/db/activity/ActivityMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/activity/ActivityMapper.xml @@ -5,10 +5,10 @@ insert into activities - (created_at, log_key, log_type, log_action, user_login, data_field, log_message) + (created_at, log_key, log_type, log_action, user_login, data_field, log_message, profile_key) values (#{createdAt,jdbcType=TIMESTAMP}, #{key,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{action,jdbcType=VARCHAR}, - #{author,jdbcType=VARCHAR}, #{data,jdbcType=VARCHAR}, #{message,jdbcType=VARCHAR}) + #{author,jdbcType=VARCHAR}, #{data,jdbcType=VARCHAR}, #{message,jdbcType=VARCHAR}, #{profileKey, jdbcType=VARCHAR}) 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 72e3aa398c7..b9cafa5b94a 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 @@ -476,6 +476,7 @@ CREATE TABLE "PERM_TEMPLATES_GROUPS" ( CREATE TABLE "ACTIVITIES" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "LOG_KEY" VARCHAR(250), + "PROFILE_KEY" VARCHAR(255), "CREATED_AT" TIMESTAMP, "USER_LOGIN" VARCHAR(255), "LOG_TYPE" VARCHAR(250), diff --git a/sonar-db/src/test/java/org/sonar/db/activity/ActivityDaoTest.java b/sonar-db/src/test/java/org/sonar/db/activity/ActivityDaoTest.java index d43fb92738f..eb410ab599e 100644 --- a/sonar-db/src/test/java/org/sonar/db/activity/ActivityDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/activity/ActivityDaoTest.java @@ -23,19 +23,20 @@ import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class ActivityDaoTest { System2 system = mock(System2.class); @Rule public DbTester dbTester = DbTester.create(system); + DbSession dbSession = dbTester.getSession(); ActivityDao underTest = dbTester.getDbClient().activityDao(); @@ -43,15 +44,23 @@ public class ActivityDaoTest { public void insert() { when(system.now()).thenReturn(1_500_000_000_000L); ActivityDto dto = new ActivityDto() - .setKey("UUID_1").setAction("THE_ACTION").setType("THE_TYPE") - .setAuthor("THE_AUTHOR").setData("THE_DATA"); - underTest.insert(dto); + .setKey("UUID_1") + .setAction("THE_ACTION") + .setType("THE_TYPE") + .setAuthor("THE_AUTHOR") + .setData("THE_DATA") + .setProfileKey("PROFILE_KEY"); + underTest.insert(dbSession, dto); + dbSession.commit(); - Map map = dbTester.selectFirst("select created_at as \"createdAt\", log_action as \"action\", data_field as \"data\" from activities where log_key='UUID_1'"); + Map map = dbTester.selectFirst("select created_at as \"createdAt\", log_action as \"action\", " + + "data_field as \"data\", profile_key as \"profileKey\" " + + "from activities where log_key='UUID_1'"); assertThat(map.get("action")).isEqualTo("THE_ACTION"); // not possible to check exact date yet. dbTester#selectFirst() uses ResultSet#getObject(), which returns // non-JDBC interface in Oracle driver. assertThat(map.get("createdAt")).isNotNull(); assertThat(map.get("data")).isEqualTo("THE_DATA"); + assertThat(map.get("profileKey")).isEqualTo("PROFILE_KEY"); } } 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 6c85da727e3..00e8e84fecb 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(114); + assertThat(container.size()).isEqualTo(115); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java index 85a415da6b8..b1502e86b5f 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v52/RemoveAnalysisReportsFromActivitiesTest.java @@ -22,6 +22,8 @@ package org.sonar.db.version.v52; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.activity.ActivityDto; import org.sonar.db.version.MigrationStep; @@ -31,14 +33,16 @@ import static org.assertj.core.api.Assertions.assertThat; public class RemoveAnalysisReportsFromActivitiesTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); MigrationStep underTest = new RemoveAnalysisReportsFromActivities(db.database()); @Test public void test() throws Exception { - db.getDbClient().activityDao().insert(new ActivityDto().setType("ANALYSIS_REPORT").setKey("1")); - db.getDbClient().activityDao().insert(new ActivityDto().setType("ANALYSIS_REPORT").setKey("2")); - db.getDbClient().activityDao().insert(new ActivityDto().setType("PROFILE_CHANGE").setKey("3")); + dbClient.activityDao().insert(dbSession, new ActivityDto().setType("ANALYSIS_REPORT").setKey("1")); + dbClient.activityDao().insert(dbSession, new ActivityDto().setType("ANALYSIS_REPORT").setKey("2")); + dbClient.activityDao().insert(dbSession, new ActivityDto().setType("PROFILE_CHANGE").setKey("3")); underTest.execute(); diff --git a/sonar-db/src/test/java/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest.java new file mode 100644 index 00000000000..f5c9d354856 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest.java @@ -0,0 +1,75 @@ +/* + * 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.v60; + +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; + +public class AddProfileKeyToActivitiesTest { + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, AddProfileKeyToActivitiesTest.class, "activities.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddProfileKeyToActivities underTest = new AddProfileKeyToActivities(db.database()); + + @Test + public void migration_adds_column_to_empty_table() throws SQLException { + underTest.execute(); + + verifyAddedColumns(); + } + + @Test + public void migration_adds_column_to_populated_table() throws SQLException { + for (int i = 0; i < 9; i++) { + db.executeInsert( + "activities", + "log_key", "LOG_KEY_" + i, + "user_login", "login"); + } + 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("activities", "profile_key", Types.VARCHAR, 255, true); + } + +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest/activities.sql b/sonar-db/src/test/resources/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest/activities.sql new file mode 100644 index 00000000000..338d203db7d --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v60/AddProfileKeyToActivitiesTest/activities.sql @@ -0,0 +1,10 @@ +CREATE TABLE "ACTIVITIES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "LOG_KEY" VARCHAR(250), + "CREATED_AT" TIMESTAMP, + "USER_LOGIN" VARCHAR(255), + "LOG_TYPE" VARCHAR(250), + "LOG_ACTION" VARCHAR(250), + "LOG_MESSAGE" VARCHAR(250), + "DATA_FIELD" CLOB(2147483647) +); -- 2.39.5