diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-12-12 13:48:16 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-12-14 17:03:35 +0100 |
commit | 4889f6cea0b2b0cc8d2b043de68a25b2aec2442e (patch) | |
tree | 3e5ddaa8104cd08ce19e443a065e3ab5b2d37e74 | |
parent | cfc286fa9fe61a781578e3a78e6563aa5750dbad (diff) | |
download | sonarqube-4889f6cea0b2b0cc8d2b043de68a25b2aec2442e.tar.gz sonarqube-4889f6cea0b2b0cc8d2b043de68a25b2aec2442e.zip |
SONAR-10134 Correctly delete OrgQualityGates when deleting organization
7 files changed, 45 insertions, 31 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java index 1529d4f7fa6..2c17e8bdf9f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateDao.java @@ -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) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java index 783d32bc650..46c86d25a3e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java @@ -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); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml index a235cefcf90..f7ea50278a6 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml @@ -113,10 +113,9 @@ 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"> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java index 380ff784281..5255d7b969c 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java @@ -200,6 +200,21 @@ public class QualityGateDaoTest { } @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(); QGateWithOrgDto qualityGate = qualityGateDbTester.insertQualityGate(organization, qg -> qg.setName("old name")); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java index 7bae477af3a..ea143056c4e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java @@ -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); diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java index 0e25904e76a..9a3bd8b79d8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java @@ -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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java index b8e037b600b..31c33ec135d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java @@ -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) { |