]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Correctly delete OrgQualityGates when deleting organization
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Dec 2017 12:48:16 +0000 (13:48 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java

index 1529d4f7fa61c9774b5e4cbe1714c7efbc04ab1b..2c17e8bdf9f4b5de55f1e3da6df21d4d99e03639 100644 (file)
@@ -79,10 +79,11 @@ public class QualityGateDao implements Dao {
 
   public void deleteByUuids(DbSession session, Collection<String> uuids) {
     QualityGateMapper mapper = mapper(session);
-    DatabaseUtils.executeLargeUpdates(uuids, partitionUuids -> {
-      mapper.deleteByUuids(partitionUuids);
-      mapper.deleteOrgQualityGatesByQualityGateUuids(partitionUuids);
-    });
+    DatabaseUtils.executeLargeUpdates(uuids, mapper::deleteByUuids);
+  }
+
+  public void deleteOrgQualityGatesByOrganization(DbSession session, OrganizationDto organization) {
+    mapper(session).deleteOrgQualityGatesByOrganization(organization.getUuid());
   }
 
   public void update(QualityGateDto qGate, DbSession session) {
index 783d32bc65073680621f70187576fcd568fe1587..46c86d25a3e96991195caa4314758ec912af561e 100644 (file)
@@ -51,7 +51,7 @@ public interface QualityGateMapper {
 
   void deleteOrgQualityGatesByQualityGateUuid(String uuid);
 
-  void deleteOrgQualityGatesByQualityGateUuids(@Param("uuids") Collection<String> uuids);
+  void deleteOrgQualityGatesByOrganization(@Param("organizationUuid") String organizationUuid);
 
   void update(QualityGateDto qGate);
 
index a235cefcf9076234e16a320c39e1386c4d4030d5..f7ea50278a6d1540488a6b588a10cebe6c1bef04 100644 (file)
     delete from org_quality_gates where quality_gate_uuid=#{uuid}
   </update>
 
-  <update id="deleteOrgQualityGatesByQualityGateUuids" parameterType="map">
+  <update id="deleteOrgQualityGatesByOrganization" parameterType="String">
     delete from org_quality_gates
-    where quality_gate_uuid in
-    <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
+    where organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
   </update>
 
   <update id="update" parameterType="QualityGate">
index 380ff784281baf508c9c0d366412436d002277cb..5255d7b969c9ae22b7eaaa3709de14e60ef22aa9 100644 (file)
@@ -199,6 +199,21 @@ public class QualityGateDaoTest {
     assertThat(db.countRowsOfTable(dbSession, "quality_gates")).isEqualTo(nbOfQualityGates);
   }
 
+  @Test
+  public void deleteOrgQualityGatesByOrganization() {
+    OrganizationDto organization = db.organizations().insert();
+    qualityGateDbTester.insertQualityGate(organization);
+    OrganizationDto otherOrganization = db.organizations().insert();
+    qualityGateDbTester.insertQualityGate(otherOrganization);
+
+    underTest.deleteOrgQualityGatesByOrganization(dbSession, organization);
+    dbSession.commit();
+
+    assertThat(db.select("select organization_uuid as \"organizationUuid\" from org_quality_gates"))
+      .extracting(row -> (String) row.get("organizationUuid"))
+      .containsOnly(otherOrganization.getUuid());
+  }
+
   @Test
   public void update() {
     OrganizationDto organization = db.organizations().insert();
index 7bae477af3a88aa7655e53e9d95fefaef86ce9b7..ea143056c4e3cb3baafc02bd46ee8e9debce133c 100644 (file)
@@ -56,16 +56,6 @@ public class QualityGateDbTester {
     return builtin;
   }
 
-  public void setBuiltInAsDefaultOn(OrganizationDto organizationDto) {
-    QualityGateDto builtinQG = dbClient.qualityGateDao().selectBuiltIn(dbSession);
-    dbClient.organizationDao().update(dbSession, organizationDto.setDefaultQualityGateUuid(builtinQG.getUuid()));
-    if (dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organizationDto, builtinQG.getUuid()) == null) {
-      dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organizationDto, builtinQG);
-      dbSession.commit();
-    }
-    dbSession.commit();
-  }
-
   @SafeVarargs
   public final QGateWithOrgDto insertQualityGate(OrganizationDto organization, Consumer<QualityGateDto>... dtoPopulators) {
     QualityGateDto qualityGate = new QualityGateDto()
@@ -87,6 +77,11 @@ public class QualityGateDbTester {
     db.commit();
   }
 
+  public void associateQualityGateToOrganization(QualityGateDto qualityGate, OrganizationDto organization) {
+    dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organization, qualityGate);
+    db.commit();
+  }
+
   @SafeVarargs
   public final QualityGateDto createDefaultQualityGate(OrganizationDto organization, Consumer<QualityGateDto>... dtoPopulators) {
     QualityGateDto defaultQGate = insertQualityGate(organization, dtoPopulators);
index 0e25904e76aa4ac07e962f9c374cdc7eee4cc5a2..9a3bd8b79d8506f2f892c5a820739c44c5bc1cdc 100644 (file)
@@ -143,6 +143,7 @@ public class DeleteAction implements OrganizationsWsAction {
       .filter(q -> !q.isBuiltIn())
       .map(QualityGateDto::getUuid)
       .collect(MoreCollectors.toList()));
+    dbClient.qualityGateDao().deleteOrgQualityGatesByOrganization(dbSession, organization);
   }
 
   private void deleteOrganization(DbSession dbSession, OrganizationDto organization) {
index b8e037b600b60426487c12de50ccd9caf605696a..31c33ec135d616de389f1ff33adbee6afb36f4cf 100644 (file)
@@ -36,6 +36,7 @@ import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.qualitygate.QGateWithOrgDto;
+import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.db.qualityprofile.QProfileDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
@@ -352,25 +353,27 @@ public class DeleteActionTest {
 
   @Test
   public void delete_quality_gates() {
-    OrganizationDto org = db.organizations().insert();
-    OrganizationDto otherOrg = db.organizations().insert();
-    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(org);
-    QGateWithOrgDto qualityGateInOtherOrg = db.qualityGates().insertQualityGate(otherOrg);
-    db.qualityGates().insertBuiltInQualityGate();
-    logInAsAdministrator(org);
+    QualityGateDto builtInQualityGate = db.qualityGates().insertBuiltInQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    db.qualityGates().associateQualityGateToOrganization(builtInQualityGate, organization);
+    OrganizationDto otherOrganization = db.organizations().insert();
+    db.qualityGates().associateQualityGateToOrganization(builtInQualityGate, otherOrganization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    QGateWithOrgDto qualityGateInOtherOrg = db.qualityGates().insertQualityGate(otherOrganization);
+    logInAsAdministrator(organization);
 
-    sendRequest(org);
+    sendRequest(organization);
 
-    verifyOrganizationDoesNotExist(org);
+    verifyOrganizationDoesNotExist(organization);
     assertThat(db.select("select uuid as \"uuid\" from quality_gates"))
       .extracting(row -> (String) row.get("uuid"))
-      .containsOnly(qualityGateInOtherOrg.getUuid(), qualityGateFinder.getBuiltInQualityGate(db.getSession()).getUuid());
-
-    // Built-in quality gate must not be destroyed
-    assertThat(qualityGateFinder.getBuiltInQualityGate(db.getSession())).isNotNull();
+      .containsExactlyInAnyOrder(qualityGateInOtherOrg.getUuid(), builtInQualityGate.getUuid());
     assertThat(db.select("select organization_uuid as \"organizationUuid\" from org_quality_gates"))
       .extracting(row -> (String) row.get("organizationUuid"))
-      .containsOnly(otherOrg.getUuid());
+      .containsOnly(otherOrganization.getUuid());
+
+    // Check built-in quality gate is still available in other organization
+    assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndName(db.getSession(), otherOrganization, "Sonar way")).isNotNull();
   }
 
   private void verifyOrganizationDoesNotExist(OrganizationDto organization) {