Skip to content

Commit

Permalink
Remove dependency of migration on core persistence classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Mar 26, 2015
1 parent ddc22b4 commit a61164d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
Expand Up @@ -25,8 +25,7 @@
import org.sonar.core.UtcDateUtils;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.migration.v44.Migration44Mapper;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.qualityprofile.db.QualityProfileMapper;
import org.sonar.core.persistence.migration.v44.QProfileDto44;
import org.sonar.server.db.DbClient;
import org.sonar.server.db.migrations.DatabaseMigration;

Expand All @@ -53,9 +52,8 @@ public void execute() {
try {
Date now = new Date(system.now());
int i = 0;
QualityProfileMapper profileMapper = session.getMapper(QualityProfileMapper.class);
Migration44Mapper migrationMapper = session.getMapper(Migration44Mapper.class);
for (QualityProfileDto profile : profileMapper.selectAll()) {
for (QProfileDto44 profile : migrationMapper.selectAllProfiles()) {
Date createdAt = (Date) ObjectUtils.defaultIfNull(migrationMapper.selectProfileCreatedAt(profile.getId()), now);
Date updatedAt = (Date) ObjectUtils.defaultIfNull(migrationMapper.selectProfileUpdatedAt(profile.getId()), now);

Expand Down
Expand Up @@ -22,13 +22,12 @@

import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.core.persistence.DbTester;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class FeedQProfileKeysMigrationTest {

Expand All @@ -41,29 +40,29 @@ public void feed_keys() throws Exception {

new FeedQProfileKeysMigration(db.database()).execute();

QualityProfileDao dao = new QualityProfileDao(db.myBatis(), mock(System2.class));
List<Map<String, Object>> profiles = db.select("SELECT kee, name, language, parent_kee FROM rules_profiles ORDER BY id ASC");

QualityProfileDto parentProfile = dao.getById(10);
assertThat(parentProfile.getKey()).startsWith("java-sonar-way-");
assertThat(parentProfile.getName()).isEqualTo("Sonar Way");
assertThat(parentProfile.getLanguage()).isEqualTo("java");
assertThat(parentProfile.getParentKee()).isNull();
Map<String, Object> parentProfile = profiles.get(0);
assertThat((String) parentProfile.get("KEE")).startsWith("java-sonar-way-");
assertThat(parentProfile.get("NAME")).isEqualTo("Sonar Way");
assertThat(parentProfile.get("LANGUAGE")).isEqualTo("java");
assertThat(parentProfile.get("PARENT_KEE")).isNull();

QualityProfileDto differentCaseProfile = dao.getById(11);
assertThat(differentCaseProfile.getKey()).startsWith("java-sonar-way-").isNotEqualTo(parentProfile.getKey());
assertThat(differentCaseProfile.getName()).isEqualTo("Sonar way");
assertThat(differentCaseProfile.getParentKee()).isNull();
Map<String, Object> differentCaseProfile = profiles.get(1);
assertThat((String) differentCaseProfile.get("KEE")).startsWith("java-sonar-way-").isNotEqualTo(parentProfile.get("KEE"));
assertThat(differentCaseProfile.get("NAME")).isEqualTo("Sonar way");
assertThat(differentCaseProfile.get("PARENT_KEE")).isNull();

QualityProfileDto childProfile = dao.getById(12);
assertThat(childProfile.getKey()).startsWith("java-child-");
assertThat(childProfile.getName()).isEqualTo("Child");
assertThat(childProfile.getParentKee()).isEqualTo(parentProfile.getKey());
Map<String, Object> childProfile = profiles.get(2);
assertThat((String) childProfile.get("KEE")).startsWith("java-child-");
assertThat(childProfile.get("NAME")).isEqualTo("Child");
assertThat(childProfile.get("PARENT_KEE")).isEqualTo(parentProfile.get("KEE"));

QualityProfileDto phpProfile = dao.getById(13);
assertThat(phpProfile.getKey()).startsWith("php-sonar-way-");
assertThat(phpProfile.getName()).isEqualTo("Sonar Way");
assertThat(phpProfile.getLanguage()).isEqualTo("php");
assertThat(phpProfile.getParentKee()).isNull();
Map<String, Object> phpProfile = profiles.get(3);
assertThat((String) phpProfile.get("KEE")).startsWith("php-sonar-way-");
assertThat(phpProfile.get("NAME")).isEqualTo("Sonar Way");
assertThat(phpProfile.get("LANGUAGE")).isEqualTo("php");
assertThat(phpProfile.get("PARENT_KEE")).isNull();
}

}
Expand Up @@ -46,6 +46,8 @@ public interface Migration44Mapper {
QProfileDto44 selectProfileById(int id);

// creation of columns RULES_PROFILES.CREATED_AT and UPDATED_AT
List<QProfileDto44> selectAllProfiles();

@CheckForNull
Date selectProfileCreatedAt(int profileId);

Expand Down
Expand Up @@ -47,6 +47,10 @@
and pm.value is not null
</select>

<select id="selectAllProfiles" resultType="org.sonar.core.persistence.migration.v44.QProfileDto44">
select id, kee as key, name, language from rules_profiles
</select>

<select id="selectProfileUpdatedAt" resultType="date" parameterType="int">
select max(change_date) from active_rule_changes
where profile_id=#{id}
Expand Down

0 comments on commit a61164d

Please sign in to comment.