Skip to content

Commit

Permalink
[DDING-000] FileMetaData의 삭제 상태관리 방법 수정 (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoSeonJe authored Nov 27, 2024
1 parent fcff6b4 commit 8fdd631
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "file_meta_data",indexes = {@Index(columnList = "domainType,entityId,fileStatus")})
@SQLDelete(sql = "update file_meta_data set deleted_at = CURRENT_TIMESTAMP where id=?")
@SQLRestriction("deleted_at IS NULL")
@SQLRestriction("file_status != 'DELETED'")
public class FileMetaData extends BaseEntity {

@Id
Expand All @@ -47,9 +44,6 @@ public class FileMetaData extends BaseEntity {
@Enumerated(EnumType.STRING)
private FileCategory fileCategory;

@Column(name = "deleted_at", columnDefinition = "TIMESTAMP")
private LocalDateTime deletedAt;

@Builder
private FileMetaData(UUID id, String fileKey, String fileName, DomainType domainType, Long entityId,
FileStatus fileStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@

public interface FileMetaDataRepository extends JpaRepository<FileMetaData, UUID> {

@Query("select fmd from FileMetaData fmd where fmd.domainType = :domainType and fmd.entityId = :entityId and fmd.fileStatus = :fileStatus")
@Query("""
select fmd from FileMetaData fmd
where fmd.domainType = :domainType
and fmd.entityId = :entityId
and fmd.fileStatus = :fileStatus
and fmd.fileStatus != 'DELETED'
""")
List<FileMetaData> findAllByDomainTypeAndEntityIdWithFileStatus(
@Param("domainType") DomainType domainType,
@Param("entityId") Long entityId,
@Param("fileStatus") FileStatus fileStatus
@Param("domainType") DomainType domainType,
@Param("entityId") Long entityId,
@Param("fileStatus") FileStatus fileStatus
);

List<FileMetaData> findAllByDomainTypeAndEntityId(DomainType domainType, Long entityId);
@Query(value = """
select * from file_meta_data
where domain_type = :#{#domainType.name()}
and entity_id = :entityId
and file_status != 'DELETED'
""", nativeQuery = true)
List<FileMetaData> findAllByDomainTypeAndEntityId(
@Param("domainType") DomainType domainType,
@Param("entityId") Long entityId
);

List<FileMetaData> findByIdIn(List<UUID> ids);
@Query(value = """
select *
from file_meta_data
where id in (:ids)
and file_status != 'DELETED'
""", nativeQuery = true)
List<FileMetaData> findByIdIn(@Param("ids") List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ public void updateStatusToDelete(DomainType domainType, Long entityId) {
fileMetaDatas.forEach(fileMetaData -> {
fileMetaData.updateStatus(DELETED);
});
entityManager.flush();
fileMetaDataRepository.deleteAll(fileMetaDatas);
}

private List<String> getNewIds(List<String> ids) {
List<FileMetaData> fileMetaDatas = fileMetaDataRepository.findByIdIn(toUUIDs(ids));
return fileMetaDatas.stream()
.filter(FileMetaData::isPending)
.map(fileMetaData -> String.valueOf(fileMetaData.getId()))
.toList();
}

private List<String> getNewIds(List<String> ids) {
Expand All @@ -122,8 +128,6 @@ private void deleteOldIds(List<String> ids, DomainType domainType, Long entityId
.filter(fileMetaData -> !ids.contains(String.valueOf(fileMetaData.getId())))
.toList();
deleteTarget.forEach(target -> target.updateStatus(DELETED));
entityManager.flush();
fileMetaDataRepository.deleteAll(deleteTarget);
}

private FileMetaData findById(UUID id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE file_meta_data
DROP COLUMN deleted_at;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ddingdong.ddingdongBE.domain.filemetadata.repository;

import ddingdong.ddingdongBE.common.support.DataJpaTestSupport;
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileStatus;
import jakarta.persistence.EntityManager;
import java.util.List;
import java.util.UUID;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

class FileMetaDataRepositoryTest extends DataJpaTestSupport {

@Autowired
private FileMetaDataRepository fileMetaDataRepository;

@Autowired
private EntityManager entityManager;

@Test
void findAllByDomainTypeAndEntityId() {
DomainType domainType = DomainType.CLUB_PROFILE;
Long id = 1L;
FileMetaData fileMetaData = FileMetaData.builder()
.id(UUID.randomUUID())
.fileKey("123")
.fileName("1234.png")
.domainType(domainType)
.entityId(id)
.fileStatus(FileStatus.COUPLED)
.build();
fileMetaDataRepository.save(fileMetaData);

List<FileMetaData> fileMetaDataList = fileMetaDataRepository.findAllByDomainTypeAndEntityId(domainType, id);

Assertions.assertThat(fileMetaDataList.size()).isEqualTo(1);
Assertions.assertThat(fileMetaDataList.get(0).getFileKey()).isEqualTo("123");
}

@DisplayName("여러 개 UUID를 사용하여 fileMetaData를 조회한다.")
@Test
void findByIn() {
// given
DomainType domainType = DomainType.CLUB_PROFILE;
Long entityId = 1L;
Long entityId2 = 2L;
UUID id = UUID.randomUUID();
UUID id2 = UUID.randomUUID();
FileMetaData fileMetaData = FileMetaData.builder()
.id(id)
.fileKey("123")
.fileName("1234.png")
.domainType(domainType)
.entityId(entityId)
.fileStatus(FileStatus.COUPLED)
.build();
FileMetaData fileMetaData2 = FileMetaData.builder()
.id(id2)
.fileKey("12344")
.fileName("123455.png")
.domainType(domainType)
.entityId(entityId2)
.fileStatus(FileStatus.DELETED)
.build();
fileMetaDataRepository.saveAll(List.of(fileMetaData, fileMetaData2));
// when
List<FileMetaData> fileMetaDataList = fileMetaDataRepository.findByIdIn(List.of(id, id2));
// then
Assertions.assertThat(fileMetaDataList.size()).isEqualTo(1);
Assertions.assertThat(fileMetaDataList.get(0).getFileKey()).isEqualTo("123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ void updateAllToActivatedAndAttached() {
List<FileMetaData> result = fileMetaDataRepository.findAllByDomainTypeAndEntityIdWithFileStatus(
domainType, entityId, FileStatus.COUPLED);

FileMetaData attachedFileMetaData = fileMetaDataRepository.findById(id2).orElse(null);
assertThat(result).hasSize(2)
.extracting("id", "fileStatus")
.contains(tuple(id1, FileStatus.COUPLED), tuple(id3, FileStatus.COUPLED));
assertThat(attachedFileMetaData).isEqualTo(null);
}

@DisplayName("FileMetaData 수정 - COUPLED & DELETED 기존 아이디를 그대로 입력할 경우")
Expand Down Expand Up @@ -199,7 +197,6 @@ void updateAllToAttached() {
.set("fileStatus", FileStatus.COUPLED)
.sample()
));

//when
fileMetaDataService.updateStatusToDelete(domainType, entityId);
em.flush();
Expand Down

0 comments on commit 8fdd631

Please sign in to comment.