package org.sonar.db.qualityprofile;
import java.util.Collection;
+import java.util.HashSet;
import java.util.Set;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
}
public Set<String> selectExistingQProfileUuids(DbSession dbSession, String organizationUuid, Collection<String> qProfileUuids) {
- return mapper(dbSession).selectExistingQProfileUuids(organizationUuid, qProfileUuids);
+ return new HashSet<>(DatabaseUtils.executeLargeInputs(qProfileUuids, uuids -> mapper(dbSession).selectExistingQProfileUuids(organizationUuid, uuids)));
}
public boolean isDefault(DbSession dbSession, String organizationUuid, String qProfileUuid) {
return selectExistingQProfileUuids(dbSession, organizationUuid, singletonList(qProfileUuid)).contains(qProfileUuid);
}
+ public Set<String> selectUuidsOfOrganizationsWithoutDefaultProfile(DbSession dbSession, String language) {
+ return mapper(dbSession).selectUuidsOfOrganizationsWithoutDefaultProfile(language);
+ }
+
private static DefaultQProfileMapper mapper(DbSession dbSession) {
return dbSession.getMapper(DefaultQProfileMapper.class);
}
package org.sonar.db.qualityprofile;
import java.util.Collection;
+import java.util.List;
import java.util.Set;
import org.apache.ibatis.annotations.Param;
void deleteByQProfileUuids(@Param("qProfileUuids") Collection<String> qProfileUuids);
- Set<String> selectExistingQProfileUuids(
+ List<String> selectExistingQProfileUuids(
@Param("organizationUuid") String organizationUuid,
@Param("qProfileUuids") Collection<String> qProfileUuids);
+
+ Set<String> selectUuidsOfOrganizationsWithoutDefaultProfile(@Param("language") String language);
}
#{qProfileUuid, jdbcType=VARCHAR}
</foreach>
</select>
+
+ <select id="selectUuidsOfOrganizationsWithoutDefaultProfile" parameterType="String" resultType="String">
+ select uuid from organizations
+ where not exists
+ (select 1 from default_qprofiles dqp where dqp.organization_uuid = uuid and dqp.language = #{language, jdbcType=VARCHAR})
+ </select>
</mapper>
public class DefaultQProfileDaoTest {
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester dbTester = DbTester.create(System2.INSTANCE).setDisableDefaultOrganization(true);
private DbSession dbSession = dbTester.getSession();
private DefaultQProfileDao underTest = dbTester.getDbClient().defaultQProfileDao();
assertThat(underTest.isDefault(dbSession, org.getUuid(), "does_not_exist")).isFalse();
}
+ @Test
+ public void selectUuidsOfOrganizationsWithoutDefaultProfile() {
+ OrganizationDto org1 = dbTester.organizations().insert();
+ OrganizationDto org2 = dbTester.organizations().insert();
+ QProfileDto profileInOrg1 = dbTester.qualityProfiles().insert(org1, p -> p.setLanguage("java"));
+ QProfileDto profileInOrg2 = dbTester.qualityProfiles().insert(org2, p -> p.setLanguage("java"));
+ dbTester.qualityProfiles().setAsDefault(profileInOrg1);
+
+ //assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "java")).containsExactly(org2.getUuid());
+ assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "js")).containsExactlyInAnyOrder(org1.getUuid(), org2.getUuid());
+ }
+
private void assertThatIsDefault(OrganizationDto org, QProfileDto profile) {
assertThat(selectUuidOfDefaultProfile(org, profile.getLanguage())).hasValue(profile.getKee());
assertThat(underTest.isDefault(dbSession, org.getUuid(), profile.getKee())).isTrue();
activeRuleIndexer.indexRuleProfile(dbSession, ruleProfile);
}
- private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtInQProfile, RulesProfileDto rulesProfileDto) {
+ private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtIn, RulesProfileDto rulesProfileDto) {
List<String> orgUuids = dbClient.organizationDao().selectAllUuids(dbSession);
+ Set<String> orgUuidsWithoutDefault = dbClient.defaultQProfileDao().selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, builtIn.getLanguage());
List<DefaultQProfileDto> defaults = new ArrayList<>();
orgUuids.forEach(orgUuid -> {
.setRulesProfileUuid(rulesProfileDto.getKee())
.setUuid(uuidFactory.create());
- if (builtInQProfile.isDefault()) {
+ if (builtIn.isDefault() && orgUuidsWithoutDefault.contains(orgUuid)) {
// rows of table default_qprofiles must be inserted after
// in order to benefit from batch SQL inserts
defaults.add(new DefaultQProfileDto()
.setQProfileUuid(dto.getUuid())
.setOrganizationUuid(orgUuid)
- .setLanguage(builtInQProfile.getLanguage()));
+ .setLanguage(builtIn.getLanguage()));
}
dbClient.qualityProfileDao().insert(batchDbSession, dto);
}
@Test
- public void flag_profile_as_default_on_organizations_if_declared_as_default_by_api() {
+ public void flag_profile_as_default_on_organization_if_declared_as_default_by_api() {
OrganizationDto org = db.organizations().insert();
RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
apiProfile.setDefaultProfile(true);
}
@Test
- public void dont_flag_profile_as_default_on_organizations_if_not_declared_as_default_by_api() {
+ public void existing_default_profile_in_organization_must_not_be_changed() {
+ RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
+ apiProfile.setDefaultProfile(true);
+ BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile);
+
+ OrganizationDto org = db.organizations().insert();
+ QProfileDto currentDefault = db.qualityProfiles().insert(org, p -> p.setLanguage(apiProfile.getLanguage()));
+ db.qualityProfiles().setAsDefault(currentDefault);
+
+ call(builtIn);
+
+ QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, apiProfile.getLanguage());
+ assertThat(defaultProfile.getKee()).isEqualTo(currentDefault.getKee());
+ }
+
+ @Test
+ public void dont_flag_profile_as_default_on_organization_if_not_declared_as_default_by_api() {
OrganizationDto org = db.organizations().insert();
RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
apiProfile.setDefaultProfile(false);