Skip to content

Commit

Permalink
SONAR-9304 Create migration for QPROFILE
Browse files Browse the repository at this point in the history
  • Loading branch information
ehartmann committed Jun 14, 2017
1 parent b376b9d commit 90c44d0
Show file tree
Hide file tree
Showing 79 changed files with 688 additions and 581 deletions.
49 changes: 0 additions & 49 deletions server/sonar-db-dao/src/main/java/org/sonar/db/Dto.java

This file was deleted.

Expand Up @@ -79,10 +79,7 @@ public void insert(DbSession session, RulesProfileDto profile, RulesProfileDto..


private void doInsert(QualityProfileMapper mapper, RulesProfileDto profile) { private void doInsert(QualityProfileMapper mapper, RulesProfileDto profile) {
Preconditions.checkArgument(profile.getId() == null, "Quality profile is already persisted (got id %d)", profile.getId()); Preconditions.checkArgument(profile.getId() == null, "Quality profile is already persisted (got id %d)", profile.getId());
Date now = new Date(system.now()); mapper.insert(profile, new Date(system.now()));
profile.setCreatedAt(now);
profile.setUpdatedAt(now);
mapper.insert(profile);
} }


public void update(DbSession session, RulesProfileDto profile, RulesProfileDto... otherProfiles) { public void update(DbSession session, RulesProfileDto profile, RulesProfileDto... otherProfiles) {
Expand All @@ -95,12 +92,11 @@ public void update(DbSession session, RulesProfileDto profile, RulesProfileDto..


private void doUpdate(QualityProfileMapper mapper, RulesProfileDto profile) { private void doUpdate(QualityProfileMapper mapper, RulesProfileDto profile) {
Preconditions.checkArgument(profile.getId() != null, "Quality profile is not persisted"); Preconditions.checkArgument(profile.getId() != null, "Quality profile is not persisted");
profile.setUpdatedAt(new Date(system.now())); mapper.update(profile, new Date(system.now()));
mapper.update(profile);
} }


public List<RulesProfileDto> selectDefaultProfiles(DbSession session, OrganizationDto organization, Collection<String> languageKeys) { public List<RulesProfileDto> selectDefaultProfiles(DbSession session, OrganizationDto organization, Collection<String> languageKeys) {
return executeLargeInputs(languageKeys, chunk -> mapper(session).selectDefaultProfiles(organization.getUuid(), chunk)); return mapper(session).selectDefaultProfiles(organization.getUuid(), languageKeys);
} }


@CheckForNull @CheckForNull
Expand All @@ -109,12 +105,12 @@ public RulesProfileDto selectDefaultProfile(DbSession session, OrganizationDto o
} }


@CheckForNull @CheckForNull
public RulesProfileDto selectByProjectAndLanguage(DbSession session, String projectKey, String language) { public RulesProfileDto selectAssociatedToProjectAndLanguage(DbSession session, ComponentDto project, String language) {
return mapper(session).selectByProjectAndLanguage(projectKey, language); return mapper(session).selectAssociatedToProjectUuidAndLanguage(project.getOrganizationUuid(), project.projectUuid(), language);
} }


public List<RulesProfileDto> selectByProjectAndLanguages(DbSession session, OrganizationDto organization, ComponentDto project, Collection<String> languageKeys) { public List<RulesProfileDto> selectAssociatedToProjectUuidAndLanguages(DbSession session, ComponentDto project, Collection<String> languages) {
return executeLargeInputs(languageKeys, input -> mapper(session).selectByProjectAndLanguages(organization.getUuid(), project.getKey(), input)); return mapper(session).selectAssociatedToProjectUuidAndLanguages(project.getOrganizationUuid(), project.uuid(), languages);
} }


public List<RulesProfileDto> selectByLanguage(DbSession dbSession, OrganizationDto organization, String language) { public List<RulesProfileDto> selectByLanguage(DbSession dbSession, OrganizationDto organization, String language) {
Expand All @@ -132,7 +128,7 @@ public List<RulesProfileDto> selectDescendants(DbSession session, String key) {
List<RulesProfileDto> descendants = Lists.newArrayList(); List<RulesProfileDto> descendants = Lists.newArrayList();
for (RulesProfileDto child : selectChildren(session, key)) { for (RulesProfileDto child : selectChildren(session, key)) {
descendants.add(child); descendants.add(child);
descendants.addAll(selectDescendants(session, child.getKey())); descendants.addAll(selectDescendants(session, child.getKee()));
} }
return descendants; return descendants;
} }
Expand All @@ -143,23 +139,23 @@ public RulesProfileDto selectByNameAndLanguage(OrganizationDto organization, Str
} }


public List<RulesProfileDto> selectByNameAndLanguages(OrganizationDto organization, String name, Collection<String> languageKeys, DbSession session) { public List<RulesProfileDto> selectByNameAndLanguages(OrganizationDto organization, String name, Collection<String> languageKeys, DbSession session) {
return executeLargeInputs(languageKeys, input -> mapper(session).selectByNameAndLanguages(organization.getUuid(), name, input)); return mapper(session).selectByNameAndLanguages(organization.getUuid(), name, languageKeys);
} }


public Map<String, Long> countProjectsByProfileKey(DbSession dbSession, OrganizationDto organization) { public Map<String, Long> countProjectsByProfileKey(DbSession dbSession, OrganizationDto organization) {
return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileKey(organization.getUuid())); return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileKey(organization.getUuid()));
} }


public void insertProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) { public void insertProjectProfileAssociation(DbSession dbSession, ComponentDto project, RulesProfileDto profile) {
mapper(session).insertProjectProfileAssociation(projectUuid, profileKey); mapper(dbSession).insertProjectProfileAssociation(project.uuid(), profile.getKee());
} }


public void deleteProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) { public void deleteProjectProfileAssociation(DbSession dbSession, ComponentDto project, RulesProfileDto profile) {
mapper(session).deleteProjectProfileAssociation(projectUuid, profileKey); mapper(dbSession).deleteProjectProfileAssociation(project.uuid(), profile.getKee());
} }


public void updateProjectProfileAssociation(String projectUuid, String newProfileKey, String oldProfileKey, DbSession session) { public void updateProjectProfileAssociation(DbSession dbSession, ComponentDto project, String newProfileKey, String oldProfileKey) {
mapper(session).updateProjectProfileAssociation(projectUuid, newProfileKey, oldProfileKey); mapper(dbSession).updateProjectProfileAssociation(project.uuid(), newProfileKey, oldProfileKey);
} }


public void deleteProjectAssociationsByProfileKeys(DbSession dbSession, Collection<String> profileKeys) { public void deleteProjectAssociationsByProfileKeys(DbSession dbSession, Collection<String> profileKeys) {
Expand Down
Expand Up @@ -28,9 +28,9 @@


public interface QualityProfileMapper { public interface QualityProfileMapper {


void insert(RulesProfileDto dto); void insert(@Param("dto") RulesProfileDto dto, @Param("now") Date now);


void update(RulesProfileDto dto); void update(@Param("dto") RulesProfileDto dto, @Param("now") Date now);


void deleteByKeys(@Param("profileKeys") Collection<String> profileKeys); void deleteByKeys(@Param("profileKeys") Collection<String> profileKeys);


Expand All @@ -39,12 +39,17 @@ public interface QualityProfileMapper {
@CheckForNull @CheckForNull
RulesProfileDto selectDefaultProfile(@Param("organizationUuid") String organizationUuid, @Param("language") String language); RulesProfileDto selectDefaultProfile(@Param("organizationUuid") String organizationUuid, @Param("language") String language);


List<RulesProfileDto> selectDefaultProfiles(@Param("organizationUuid") String organizationUuid, @Param("languages") List<String> languages); List<RulesProfileDto> selectDefaultProfiles(
@Param("organizationUuid") String organizationUuid,
@Param("languages") Collection<String> languages);


@CheckForNull @CheckForNull
RulesProfileDto selectByNameAndLanguage(@Param("organizationUuid") String organizationUuid, @Param("name") String name, @Param("language") String language); RulesProfileDto selectByNameAndLanguage(@Param("organizationUuid") String organizationUuid, @Param("name") String name, @Param("language") String language);


List<RulesProfileDto> selectByNameAndLanguages(@Param("organizationUuid") String organizationUuid, @Param("name") String name, @Param("languages") List<String> languages); List<RulesProfileDto> selectByNameAndLanguages(
@Param("organizationUuid") String organizationUuid,
@Param("name") String name,
@Param("languages") Collection<String> languages);


@CheckForNull @CheckForNull
RulesProfileDto selectByKey(String key); RulesProfileDto selectByKey(String key);
Expand All @@ -61,14 +66,25 @@ public interface QualityProfileMapper {


List<KeyLongValue> countProjectsByProfileKey(@Param("organizationUuid") String organizationUuid); List<KeyLongValue> countProjectsByProfileKey(@Param("organizationUuid") String organizationUuid);


RulesProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language); @CheckForNull
RulesProfileDto selectAssociatedToProjectUuidAndLanguage(
@Param("organizationUuid") String organizationUuid,
@Param("projectUuid") String projectUuid,
@Param("language") String language);


List<RulesProfileDto> selectByProjectAndLanguages(@Param("organizationUuid") String organizationUuid, @Param("projectKey") String projectKey, List<RulesProfileDto> selectAssociatedToProjectUuidAndLanguages(
@Param("languages") List<String> input); @Param("organizationUuid") String organizationUuid,
@Param("projectUuid") String projectUuid,
@Param("languages") Collection<String> languages);


void insertProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey); void insertProjectProfileAssociation(
@Param("projectUuid") String projectUuid,
@Param("profileUuid") String profileUuid);


void updateProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey, @Param("oldProfileKey") String oldProfileKey); void updateProjectProfileAssociation(
@Param("projectUuid") String projectUuid,
@Param("profileUuid") String profileUuid,
@Param("oldProfileUuid") String oldProfileUuid);


void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey); void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);


Expand Down
Expand Up @@ -23,13 +23,12 @@
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.core.util.UtcDateUtils; import org.sonar.core.util.UtcDateUtils;
import org.sonar.db.Dto;
import org.sonar.db.organization.OrganizationDto; import org.sonar.db.organization.OrganizationDto;


/** /**
* Represents the table "rules_profiles" * Represents the table "rules_profiles"
*/ */
public class RulesProfileDto extends Dto<String> { public class RulesProfileDto {


private Integer id; private Integer id;
/** /**
Expand All @@ -56,11 +55,6 @@ public RulesProfileDto setOrganizationUuid(String organizationUuid) {
return this; return this;
} }


@Override
public String getKey() {
return kee;
}

public RulesProfileDto setKey(String s) { public RulesProfileDto setKey(String s) {
return setKee(s); return setKee(s);
} }
Expand Down
Expand Up @@ -10,42 +10,40 @@
p.name as name, p.name as name,
p.language as language, p.language as language,
p.parent_kee as parentKee, p.parent_kee as parentKee,
p.created_at as createdAt,
p.updated_at as updatedAt,
p.rules_updated_at as rulesUpdatedAt, p.rules_updated_at as rulesUpdatedAt,
p.last_used as lastUsed, p.last_used as lastUsed,
p.user_updated_at as userUpdatedAt, p.user_updated_at as userUpdatedAt,
p.is_built_in as isBuiltIn p.is_built_in as isBuiltIn
</sql> </sql>


<insert id="insert" parameterType="RulesProfile" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> <insert id="insert" parameterType="map" keyColumn="id" useGeneratedKeys="true" keyProperty="dto.id">
INSERT INTO rules_profiles (organization_uuid, kee, parent_kee, name, language, created_at, updated_at, rules_updated_at, last_used, user_updated_at, is_built_in) INSERT INTO rules_profiles (organization_uuid, kee, parent_kee, name, language, created_at, updated_at, rules_updated_at, last_used, user_updated_at, is_built_in)
VALUES ( VALUES (
#{organizationUuid, jdbcType=VARCHAR}, #{dto.organizationUuid, jdbcType=VARCHAR},
#{kee, jdbcType=VARCHAR}, #{dto.kee, jdbcType=VARCHAR},
#{parentKee, jdbcType=VARCHAR}, #{dto.parentKee, jdbcType=VARCHAR},
#{name, jdbcType=VARCHAR}, #{dto.name, jdbcType=VARCHAR},
#{language, jdbcType=VARCHAR}, #{dto.language, jdbcType=VARCHAR},
#{createdAt, jdbcType=TIMESTAMP}, #{now, jdbcType=TIMESTAMP},
#{updatedAt, jdbcType=TIMESTAMP}, #{now, jdbcType=TIMESTAMP},
#{rulesUpdatedAt, jdbcType=VARCHAR}, #{dto.rulesUpdatedAt, jdbcType=VARCHAR},
#{lastUsed, jdbcType=BIGINT}, #{dto.lastUsed, jdbcType=BIGINT},
#{userUpdatedAt, jdbcType=BIGINT}, #{dto.userUpdatedAt, jdbcType=BIGINT},
#{isBuiltIn, jdbcType=BOOLEAN} #{dto.isBuiltIn, jdbcType=BOOLEAN}
) )
</insert> </insert>


<update id="update" parameterType="RulesProfile"> <update id="update" parameterType="map">
UPDATE rules_profiles SET UPDATE rules_profiles SET
name=#{name, jdbcType=VARCHAR}, name=#{dto.name, jdbcType=VARCHAR},
language=#{language, jdbcType=VARCHAR}, language=#{dto.language, jdbcType=VARCHAR},
parent_kee=#{parentKee, jdbcType=VARCHAR}, parent_kee=#{dto.parentKee, jdbcType=VARCHAR},
updated_at=#{updatedAt, jdbcType=TIMESTAMP}, updated_at=#{now, jdbcType=TIMESTAMP},
rules_updated_at=#{rulesUpdatedAt, jdbcType=VARCHAR}, rules_updated_at=#{dto.rulesUpdatedAt, jdbcType=VARCHAR},
last_used=#{lastUsed, jdbcType=BIGINT}, last_used=#{dto.lastUsed, jdbcType=BIGINT},
user_updated_at=#{userUpdatedAt, jdbcType=BIGINT}, user_updated_at=#{dto.userUpdatedAt, jdbcType=BIGINT},
is_built_in=#{isBuiltIn, jdbcType=BOOLEAN} is_built_in=#{dto.isBuiltIn, jdbcType=BOOLEAN}
WHERE id=#{id} WHERE id=#{dto.id}
</update> </update>


<update id="deleteByKeys" parameterType="String"> <update id="deleteByKeys" parameterType="String">
Expand Down Expand Up @@ -180,35 +178,45 @@
group by pp.profile_key group by pp.profile_key
</select> </select>


<select id="selectByProjectAndLanguage" parameterType="map" resultType="RulesProfile"> <select id="selectAssociatedToProjectUuidAndLanguage" parameterType="map" resultType="RulesProfile">
SELECT select
<include refid="profilesColumns"/> <include refid="profilesColumns"/>
FROM rules_profiles p from rules_profiles p
JOIN project_qprofiles pp ON pp.profile_key=p.kee inner join project_qprofiles pp ON pp.profile_key = p.kee
JOIN projects project ON pp.project_uuid=project.uuid AND project.kee=#{projectKey} where
WHERE p.language=#{language} p.language = #{language, jdbcType=VARCHAR}
and pp.project_uuid = #{projectUuid, jdbcType=VARCHAR}
and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
</select> </select>


<select id="selectByProjectAndLanguages" parameterType="map" resultType="RulesProfile"> <select id="selectAssociatedToProjectUuidAndLanguages" parameterType="map" resultType="RulesProfile">
SELECT select
<include refid="profilesColumns"/> <include refid="profilesColumns"/>
FROM rules_profiles p from rules_profiles p
JOIN project_qprofiles pp ON pp.profile_key=p.kee inner join project_qprofiles pq ON pq.profile_key = p.kee
JOIN projects project ON pp.project_uuid=project.uuid AND project.kee=#{projectKey, jdbcType=VARCHAR} where
JOIN organizations org ON project.organization_uuid = org.uuid AND p.organization_uuid = org.uuid p.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
WHERE p.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach> and pq.project_uuid = #{projectUuid, jdbcType=VARCHAR}
AND org.uuid = #{organizationUuid, jdbcType=VARCHAR} and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
</select> </select>


<insert id="insertProjectProfileAssociation" keyColumn="id" useGeneratedKeys="true"> <insert id="insertProjectProfileAssociation" useGeneratedKeys="false">
INSERT INTO project_qprofiles (project_uuid, profile_key) insert into project_qprofiles (
VALUES (#{projectUuid, jdbcType=VARCHAR}, #{profileKey, jdbcType=VARCHAR}) project_uuid,
profile_key
) values (
#{projectUuid, jdbcType=VARCHAR},
#{profileUuid, jdbcType=VARCHAR}
)
</insert> </insert>


<update id="updateProjectProfileAssociation"> <update id="updateProjectProfileAssociation">
UPDATE project_qprofiles update project_qprofiles
SET profile_key=#{profileKey, jdbcType=VARCHAR} set
WHERE project_uuid=#{projectUuid, jdbcType=VARCHAR} AND profile_key=#{oldProfileKey, jdbcType=VARCHAR} profile_key = #{profileUuid, jdbcType=VARCHAR}
where
project_uuid = #{projectUuid, jdbcType=VARCHAR}
and profile_key = #{oldProfileUuid, jdbcType=VARCHAR}
</update> </update>


<update id="deleteProjectProfileAssociation"> <update id="deleteProjectProfileAssociation">
Expand Down

0 comments on commit 90c44d0

Please sign in to comment.