]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9302 do not change current default profiles 2180/head
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 16 Jun 2017 15:18:43 +0000 (17:18 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 16 Jun 2017 21:22:44 +0000 (23:22 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/DefaultQProfileMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/DefaultQProfileMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/DefaultQProfileDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImpl.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQProfileInsertImplTest.java

index 0f62f0b296236766f82831d882359abadd10ca05..ee87776537253f5f6739410b15637a019fc18822 100644 (file)
@@ -20,6 +20,7 @@
 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;
@@ -50,13 +51,17 @@ public class DefaultQProfileDao implements 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);
   }
index 6f69965c89684544e7e3a5069a37d3eb5d7a513b..580ea4028e289845f6125b4233f101946c012873 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.db.qualityprofile;
 
 import java.util.Collection;
+import java.util.List;
 import java.util.Set;
 import org.apache.ibatis.annotations.Param;
 
@@ -30,7 +31,9 @@ public interface DefaultQProfileMapper {
 
   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);
 }
index 71c6c32e22552f5fc24f545071c38dd37a9284a2..60c6d8cc2acb4bcb7ea55ef47701d74fa1eb8c19 100644 (file)
         #{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>
 
index f317d828e080489ef1fcba3d8ca307d4737e70b7..720487158def7bad3d5faa2865ceb57dd0dca999 100644 (file)
@@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 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();
@@ -116,6 +116,18 @@ public class DefaultQProfileDaoTest {
     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();
index f69f0877b094bdeeee1720d8b022190b2caeb610..4fc3efa1bda0294c3d0d465d74c041eb30464cc1 100644 (file)
@@ -96,8 +96,9 @@ public class BuiltInQProfileInsertImpl implements BuiltInQProfileInsert {
     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 -> {
@@ -106,13 +107,13 @@ public class BuiltInQProfileInsertImpl implements BuiltInQProfileInsert {
         .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);
index 1398f9e4fb6977d4b240dd59501a430fdf4b48ce..3d85ade5b77ee59501dc735f972a6deabcb6f1ca 100644 (file)
@@ -120,7 +120,7 @@ public class BuiltInQProfileInsertImplTest {
   }
 
   @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);
@@ -134,7 +134,23 @@ public class BuiltInQProfileInsertImplTest {
   }
 
   @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);