forked from SonarSource/sonarqube
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-7824 Populate DB column ACTIVITIES.PROFILE_KEY
- Loading branch information
Showing
12 changed files
with
221 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...r/sonar-web/src/main/webapp/WEB-INF/db/migrate/1260_populate_profile_key_of_activities.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 PopulateProfileKeyOfActivities < ActiveRecord::Migration | ||
def self.up | ||
execute_java_migration('org.sonar.db.version.v60.PopulateProfileKeyOfActivities') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sonar-db/src/main/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.util.Map; | ||
import org.sonar.api.utils.KeyValueFormat; | ||
import org.sonar.db.Database; | ||
import org.sonar.db.version.BaseDataChange; | ||
import org.sonar.db.version.MassUpdate; | ||
import org.sonar.db.version.Select; | ||
import org.sonar.db.version.SqlStatement; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static org.apache.commons.lang.StringUtils.isNotBlank; | ||
|
||
public class PopulateProfileKeyOfActivities extends BaseDataChange { | ||
|
||
public PopulateProfileKeyOfActivities(Database db) { | ||
super(db); | ||
} | ||
|
||
@Override | ||
public void execute(Context context) throws SQLException { | ||
MassUpdate massUpdate = context.prepareMassUpdate(); | ||
massUpdate.select("select id, data_field from activities where profile_key is null"); | ||
massUpdate.update("update activities set profile_key=?, data_field=? where id=?"); | ||
massUpdate.rowPluralName("activities"); | ||
massUpdate.execute(PopulateProfileKeyOfActivities::handle); | ||
} | ||
|
||
private static boolean handle(Select.Row row, SqlStatement update) throws SQLException { | ||
int id = row.getInt(1); | ||
String data = row.getString(2); | ||
Map<String, String> fields = KeyValueFormat.parse(data); | ||
String profileKey = fields.remove("profileKey"); | ||
checkState(isNotBlank(profileKey), "No profile key found in db row of activities.data_field", id); | ||
|
||
update.setString(1, profileKey); | ||
update.setString(2, KeyValueFormat.format(fields)); | ||
update.setInt(3, id); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
sonar-db/src/test/java/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.sonar.api.utils.System2; | ||
import org.sonar.db.DbTester; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PopulateProfileKeyOfActivitiesTest { | ||
private static final String ACTIVITIES_TABLE = "activities"; | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
@Rule | ||
public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateProfileKeyOfActivitiesTest.class, "activities.sql"); | ||
|
||
PopulateProfileKeyOfActivities underTest = new PopulateProfileKeyOfActivities(db.database()); | ||
|
||
@Test | ||
public void migration_has_no_effect_on_empty_tables() throws SQLException { | ||
underTest.execute(); | ||
|
||
assertThat(db.countRowsOfTable(ACTIVITIES_TABLE)).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void migration_update_activities_profile_key() throws SQLException { | ||
insertActivity("first-profile-key"); | ||
insertActivity("first-profile-key"); | ||
insertActivity("first-profile-key"); | ||
insertActivity("second-profile-key"); | ||
insertActivity("third-profile-key"); | ||
|
||
underTest.execute(); | ||
|
||
assertCountActivitiesWithProfile("first-profile-key", 3); | ||
assertCountActivitiesWithProfile("second-profile-key", 1); | ||
assertCountActivitiesWithProfile("third-profile-key", 1); | ||
} | ||
|
||
@Test | ||
public void migration_is_reentrant() throws SQLException { | ||
insertActivity("profile-key"); | ||
underTest.execute(); | ||
assertCountActivitiesWithProfile("profile-key", 1); | ||
|
||
underTest.execute(); | ||
assertCountActivitiesWithProfile("profile-key", 1); | ||
} | ||
|
||
@Test | ||
public void fail_if_activity_data_is_not_well_formatted() throws SQLException { | ||
expectedException.expect(IllegalStateException.class); | ||
expectedException.expectMessage("Error during processing of row: "); | ||
|
||
db.executeInsert(ACTIVITIES_TABLE, "data_field", "key=fakeKey"); | ||
|
||
underTest.execute(); | ||
} | ||
|
||
private void assertCountActivitiesWithProfile(String profileKey, int expectedNumberOfActivities) { | ||
assertThat(countActivitiesWithProfile(profileKey)).isEqualTo(expectedNumberOfActivities); | ||
} | ||
|
||
private int countActivitiesWithProfile(String qualityProfileKey) { | ||
// profile key is removed from data_field | ||
return db.countSql(String.format("select count(1) from activities where profile_key='%s' and data_field='key=fakeKey'", qualityProfileKey)); | ||
} | ||
|
||
private void insertActivity(String profileKey) { | ||
db.executeInsert(ACTIVITIES_TABLE, | ||
"data_field", "key=fakeKey;profileKey=" + profileKey); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...test/resources/org/sonar/db/version/v60/PopulateProfileKeyOfActivitiesTest/activities.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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), | ||
"LOG_ACTION" VARCHAR(250), | ||
"LOG_MESSAGE" VARCHAR(250), | ||
"DATA_FIELD" CLOB(2147483647) | ||
); |