]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8100 add PermissionTemplateDao#deleteByOrganization
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Oct 2016 16:08:00 +0000 (18:08 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 15:17:54 +0000 (17:17 +0200)
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml
sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java
sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDbTester.java

index d8e648487e3768e93804ae6710bcf73ef8dc2333..c4b777f9bfa0630b8037bc110bf6ab21a4935578 100644 (file)
@@ -36,4 +36,6 @@ public interface PermissionTemplateCharacteristicMapper {
   void update(PermissionTemplateCharacteristicDto templatePermissionDto);
 
   void deleteByTemplateId(long id);
+
+  void deleteByTemplateIds(@Param("templateIds") List<Long> subList);
 }
index 49d96caaf2516d134fa94c212ac63d634e24e54b..9beeb044d2c489137c497d98700446ef93d62a21 100644 (file)
@@ -162,10 +162,10 @@ public class PermissionTemplateDao implements Dao {
 
   public void deleteById(DbSession session, long templateId) {
     PermissionTemplateMapper mapper = mapper(session);
-    mapper.deleteUserPermissions(templateId);
-    mapper.deleteGroupPermissions(templateId);
+    mapper.deleteUserPermissionsByTemplateId(templateId);
+    mapper.deleteGroupPermissionsByTemplateId(templateId);
     session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
-    mapper.delete(templateId);
+    mapper.deleteById(templateId);
   }
 
   public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) {
@@ -239,4 +239,17 @@ public class PermissionTemplateDao implements Dao {
   private static PermissionTemplateMapper mapper(DbSession session) {
     return session.getMapper(PermissionTemplateMapper.class);
   }
+
+  public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
+    PermissionTemplateMapper templateMapper = mapper(dbSession);
+    PermissionTemplateCharacteristicMapper templateCharacteristicMapper = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class);
+    List<Long> templateIds = templateMapper.selectTemplateIdsByOrganization(organizationUuid);
+    executeLargeInputsWithoutOutput(templateIds, subList -> {
+      templateCharacteristicMapper.deleteByTemplateIds(subList);
+      templateMapper.deleteGroupPermissionsByTemplateIds(subList);
+      templateMapper.deleteUserPermissionsByTemplateIds(subList);
+      templateMapper.deleteByIds(subList);
+      return null;
+    });
+  }
 }
index 7cd947b94f482baa72ffbc8b580201c3e7241d19..64d4626e7b3d947e55e92c4d1e6ce8c7d3931b28 100644 (file)
@@ -36,13 +36,19 @@ public interface PermissionTemplateMapper {
 
   void update(PermissionTemplateDto permissionTemplate);
 
-  void delete(long templateId);
+  void deleteById(long templateId);
 
-  void deleteUserPermissions(long templateId);
+  void deleteByIds(@Param("templateIds") List<Long> templateIds);
+
+  void deleteUserPermissionsByTemplateId(long templateId);
+
+  void deleteUserPermissionsByTemplateIds(@Param("templateIds") List<Long> templateIds);
 
   void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
 
-  void deleteGroupPermissions(long templateId);
+  void deleteGroupPermissionsByTemplateId(long templateId);
+  
+  void deleteGroupPermissionsByTemplateIds(@Param("templateIds") List<Long> templateIds);
 
   void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
 
@@ -77,4 +83,7 @@ public interface PermissionTemplateMapper {
   List<String> selectPotentialPermissionsByUserIdAndTemplateId(@Param("userId") @Nullable Long currentUserId, @Param("templateId") long templateId);
 
   int countGroupsWithPermission(@Param("templateId") long templateId, @Param("permission") String permission, @Nullable @Param("groupId") Long groupId);
+
+  List<Long> selectTemplateIdsByOrganization(@Param("organizationUuid") String organizationUuid);
+
 }
index cd60828514f5884225b7c64b06fc690c0f38d3ee..c065eeb92bd187cad9933fc419db3d9b11728abd 100644 (file)
     DELETE FROM perm_tpl_characteristics
     WHERE template_id = #{permissionTemplateId}
   </delete>
+
+  <delete id="deleteByTemplateIds" parameterType="long">
+    delete from
+      perm_tpl_characteristics
+    where
+      template_id in
+      <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+        #{templateId}
+      </foreach>
+  </delete>
 </mapper>
index a930b0bb31bd4fcc0e6a9e9344c5785437f0e32a..e60f18a5a82bb7d1d3c75e77541a6afeb7e099bc 100644 (file)
     WHERE id = #{id}
   </update>
 
-  <delete id="delete" parameterType="long">
+  <delete id="deleteById" parameterType="long">
     DELETE FROM permission_templates
     WHERE id = #{templateId}
   </delete>
 
-  <delete id="deleteUserPermissions" parameterType="long">
-    DELETE FROM perm_templates_users
-    WHERE template_id = #{templateId}
+  <delete id="deleteByIds" parameterType="long">
+    delete from
+      permission_templates
+    where
+      id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+        #{templateId,jdbcType=BIGINT}
+      </foreach>
   </delete>
 
-  <delete id="deleteGroupPermissions" parameterType="long">
-    DELETE FROM perm_templates_groups
-    WHERE template_id = #{templateId}
+  <delete id="deleteUserPermissionsByTemplateId" parameterType="long">
+    delete from
+      perm_templates_users
+    where
+      template_id = #{templateId,jdbcType=BIGINT}
   </delete>
 
-  <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
-    INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
-    VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
-  </insert>
+  <delete id="deleteUserPermissionsByTemplateIds">
+    delete from
+      perm_templates_users
+    where
+      template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+        #{templateId,jdbcType=BIGINT}
+      </foreach>
+  </delete>
 
   <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
     DELETE FROM perm_templates_users
     AND permission_reference = #{permission}
   </delete>
 
-  <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
-    INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
-    VALUES (
-    #{templateId,jdbcType=BIGINT},
-    #{groupId,jdbcType=BIGINT},
-    #{permission,jdbcType=VARCHAR},
-    #{createdAt,jdbcType=TIMESTAMP},
-    #{updatedAt,jdbcType=TIMESTAMP}
-    )
-  </insert>
+  <delete id="deleteGroupPermissionsByTemplateId" parameterType="long">
+    delete from
+      perm_templates_groups
+    where
+      template_id = #{templateId,jdbcType=BIGINT}
+  </delete>
+
+  <delete id="deleteGroupPermissionsByTemplateIds">
+    delete from
+      perm_templates_groups
+    where
+      template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+        #{templateId,jdbcType=BIGINT}
+      </foreach>
+  </delete>
 
   <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
     DELETE FROM perm_templates_groups
     </choose>
   </delete>
 
+  <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
+    INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
+    VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
+  </insert>
+
+  <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
+    INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
+    VALUES (
+    #{templateId,jdbcType=BIGINT},
+    #{groupId,jdbcType=BIGINT},
+    #{permission,jdbcType=VARCHAR},
+    #{createdAt,jdbcType=TIMESTAMP},
+    #{updatedAt,jdbcType=TIMESTAMP}
+    )
+  </insert>
+
   <delete id="deleteByGroupId" parameterType="long">
     delete from perm_templates_groups
     where group_id = #{groupId,jdbcType=BIGINT}
       ptg.group_id = #{groupId}
     </if>
   </select>
+
+  <select id="selectTemplateIdsByOrganization" resultType="Long">
+    select
+      id
+    from
+      permission_templates
+    where
+      organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+  </select>
 </mapper>
index b799080d1951ab6a5f45c7701e8e1579c7363ec3..0cc55cc8092f4178c3840a5e458dda242c595f89 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 
@@ -55,6 +56,7 @@ public class PermissionTemplateDaoTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public DbTester db = DbTester.create(system);
+
   private DbSession dbSession = db.getSession();
   private PermissionTemplateDbTester templateDb = db.permissionTemplates();
 
@@ -286,6 +288,79 @@ public class PermissionTemplateDaoTest {
     assertThat(resultWithoutUser).containsOnly(UserRole.ISSUE_ADMIN);
   }
 
+  @Test
+  public void deleteByOrganization_does_not_fail_on_empty_db() {
+    underTest.deleteByOrganization(dbSession, "some uuid");
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_does_not_fail_when_organization_has_no_template() {
+    OrganizationDto organization = db.organizations().insert();
+
+    underTest.deleteByOrganization(dbSession, organization.getUuid());
+    dbSession.commit();
+  }
+
+  @Test
+  public void deleteByOrganization_delete_all_templates_of_organization_and_content_of_child_tables() {
+    OrganizationDto organization1 = db.organizations().insert();
+    OrganizationDto organization2 = db.organizations().insert();
+    OrganizationDto organization3 = db.organizations().insert();
+
+    PermissionTemplateDto[] templates = {
+      createTemplate(organization1),
+      createTemplate(organization2),
+      createTemplate(organization3),
+      createTemplate(organization1),
+      createTemplate(organization2)
+    };
+
+    verifyTemplateIdsInDb(templates[0].getId(), templates[1].getId(), templates[2].getId(), templates[3].getId(), templates[4].getId());
+
+    underTest.deleteByOrganization(dbSession, organization2.getUuid());
+    dbSession.commit();
+    verifyTemplateIdsInDb(templates[0].getId(), templates[2].getId(), templates[3].getId());
+
+    underTest.deleteByOrganization(dbSession, organization3.getUuid());
+    dbSession.commit();
+    verifyTemplateIdsInDb(templates[0].getId(), templates[3].getId());
+
+    underTest.deleteByOrganization(dbSession, organization1.getUuid());
+    dbSession.commit();
+    verifyTemplateIdsInDb();
+  }
+
+  private PermissionTemplateDto createTemplate(OrganizationDto organization) {
+    UserDto user = db.users().insertUser();
+    GroupDto group = db.users().insertGroup();
+    db.users().insertMember(group, user);
+    PermissionTemplateDto template = templateDb.insertTemplate(organization);
+    templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
+    templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.ADMIN);
+    templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
+    templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.ADMIN);
+    templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
+    templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
+    templateDb.addGroupToTemplate(template.getId(), null, UserRole.ISSUE_ADMIN);
+    return template;
+  }
+
+  private void verifyTemplateIdsInDb(Long... expectedTemplateIds) {
+    assertThat(db.select("select distinct template_id as \"templateId\" from perm_templates_groups"))
+      .extracting((row) -> (Long) row.get("templateId"))
+      .containsOnly(expectedTemplateIds);
+    assertThat(db.select("select distinct template_id as \"templateId\" from perm_templates_users"))
+      .extracting((row) -> (Long) row.get("templateId"))
+      .containsOnly(expectedTemplateIds);
+    assertThat(db.select("select distinct template_id as \"templateId\" from perm_tpl_characteristics"))
+      .extracting((row) -> (Long) row.get("templateId"))
+      .containsOnly(expectedTemplateIds);
+    assertThat(db.select("select distinct id as \"templateId\" from permission_templates"))
+      .extracting((row) -> (Long) row.get("templateId"))
+      .containsOnly(expectedTemplateIds);
+  }
+
   private void commit() {
     dbSession.commit();
   }
index cfebcbed05601204219e27f0eb20adb6e1386a01..b2677425ef9e7a23d662cbe35c4090f866f2cc06 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
 
 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
@@ -43,6 +44,10 @@ public class PermissionTemplateDbTester {
     return insertTemplate(newPermissionTemplateDto());
   }
 
+  public PermissionTemplateDto insertTemplate(OrganizationDto organizationDto) {
+    return insertTemplate(newPermissionTemplateDto().setOrganizationUuid(organizationDto.getUuid()));
+  }
+
   public PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
     PermissionTemplateDto templateInDb = dbClient.permissionTemplateDao().insert(dbSession, template);
     db.commit();