diff options
4 files changed, 58 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java index 30ad7a49f28..a88029e17fb 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java @@ -71,6 +71,10 @@ public class GroupDao implements Dao { mapper(dbSession).deleteById(groupId); } + public void deleteByOrganization(DbSession dbSession, String organizationUuid) { + mapper(dbSession).deleteByOrganization(organizationUuid); + } + public int countByQuery(DbSession session, String organizationUuid, @Nullable String query) { return mapper(session).countByQuery(organizationUuid, groupSearchToSql(query)); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java b/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java index d056931638a..cbb3e0dff7c 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java @@ -44,6 +44,8 @@ public interface GroupMapper { void deleteById(long groupId); + void deleteByOrganization(@Param("organizationUuid") String organizationUuid); + @CheckForNull GroupDto selectByName(@Param("organizationUuid") String organizationUuid, @Param("name") String name); diff --git a/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml index 45d71a4d7b6..bc74e334d7e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml @@ -46,6 +46,13 @@ </where> </delete> + <delete id="deleteByOrganization" parameterType="String"> + delete from + groups + where + organization_uuid=#{organizationUuid,jdbcType=VARCHAR} + </delete> + <select id="selectByUserLogin" parameterType="string" resultType="Group"> select <include refid="groupColumns"/> diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java index 99ee0be3115..e5482dcfedc 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java @@ -408,6 +408,51 @@ public class GroupDaoTest { stream(adminPerGroupPermissionUsers).forEach(user -> db.rootFlag().verify(user, false, DATE_1)); } + @Test + public void deleteByOrganization_does_not_fail_when_table_is_empty() { + underTest.deleteByOrganization(dbSession, "some uuid"); + dbSession.commit(); + } + + @Test + public void deleteByOrganization_does_not_fail_when_organization_has_no_group() { + OrganizationDto organization = db.organizations().insert(); + + underTest.deleteByOrganization(dbSession, organization.getUuid()); + dbSession.commit(); + } + + @Test + public void deleteByOrganization_deletes_all_groups_in_specified_organization() { + OrganizationDto organization1 = db.organizations().insert(); + OrganizationDto organization2 = db.organizations().insert(); + OrganizationDto organization3 = db.organizations().insert(); + + db.users().insertGroup(organization1); + db.users().insertGroup(organization2); + db.users().insertGroup(organization3); + db.users().insertGroup(organization3); + db.users().insertGroup(organization2); + + underTest.deleteByOrganization(dbSession, organization2.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(organization1.getUuid(), organization3.getUuid()); + + underTest.deleteByOrganization(dbSession, organization1.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(organization3.getUuid()); + + underTest.deleteByOrganization(dbSession, organization3.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(); + } + + private void verifyOrganizationUuidsInTable(String... organizationUuids) { + assertThat(db.select("select distinct organization_uuid as \"organizationUuid\" from groups")) + .extracting(row -> (String) row.get("organizationUuid")) + .containsOnly(organizationUuids); + } + private void call_updateRootFlagFromPermissions(GroupDto groupDto, long now) { when(system2.now()).thenReturn(now); underTest.updateRootFlagOfUsersInGroupFromPermissions(db.getSession(), groupDto.getId(), db.getDefaultOrganization().getUuid()); |