Skip to content

Commit

Permalink
#432 fix expunge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kagemomiji committed May 14, 2024
1 parent ff4b937 commit f38306f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ private void expunge() {
}

LOG.debug("Cleaning database...");
LOG.debug("Deleting non-present cover art...");
coverArtService.expunge();
LOG.debug("Deleting non-present artists...");
artistService.expunge();
LOG.debug("Deleting non-present albums...");
albumService.expunge();
LOG.debug("Deleting non-present media files...");
mediaFileService.expunge();
LOG.debug("Deleting non-present cover art...");
coverArtService.expunge();
LOG.debug("Deleting non-present media folders...");
mediaFolderService.expunge();
LOG.debug("Refreshing playlist stats...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public class CoverArt {
@Column(name = "updated")
private Instant updated = created;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entity_id", insertable = false, updatable = false)
private Artist artist;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entity_id", insertable = false, updatable = false)
private Album album;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entity_id", insertable = false, updatable = false)
private MediaFile mediaFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,18 @@ public void delete(EntityType type, Integer id) {
public void expunge() {
coverArtCache.clear();
List<CoverArt> expungeCoverArts = coverArtRepository.findAll().stream()
.filter(art ->
(art.getEntityType() == EntityType.ALBUM && art.getAlbum() == null) ||
(art.getEntityType() == EntityType.ARTIST && art.getArtist() == null) ||
(art.getEntityType() == EntityType.MEDIA_FILE && (art.getMediaFile() == null || !art.getMediaFile().isPresent())))
.filter(art -> {
switch (art.getEntityType()) {
case ALBUM:
return art.getAlbum() == null || !art.getAlbum().isPresent();
case ARTIST:
return art.getArtist() == null || !art.getArtist().isPresent();
case MEDIA_FILE:
return art.getMediaFile() == null || !art.getMediaFile().isPresent();
default:
return false;
}
})
.collect(Collectors.toList());
coverArtRepository.deleteAll(expungeCoverArts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,28 @@ void testExpunge() {
new CoverArt(1, EntityType.ALBUM, "path/to/image.jpg", null, false),
new CoverArt(2, EntityType.ARTIST, "path/to/image.jpg", null, false),
new CoverArt(3, EntityType.MEDIA_FILE, "path/to/image.jpg", null, false),
new CoverArt(4, EntityType.MEDIA_FILE, "path/to/image.jpg", null, false),
new CoverArt(5, EntityType.ALBUM, "path/to/image.jpg", null, false),
new CoverArt(6, EntityType.ARTIST, "path/to/image.jpg", null, false),
new CoverArt(7, EntityType.MEDIA_FILE, "path/to/image.jpg", null, false)
new CoverArt(4, EntityType.ALBUM, "path/to/image.jpg", null, false),
new CoverArt(5, EntityType.ARTIST, "path/to/image.jpg", null, false),
new CoverArt(6, EntityType.MEDIA_FILE, "path/to/image.jpg", null, false),
new CoverArt(7, EntityType.ALBUM, "path/to/image.jpg", null, false),
new CoverArt(8, EntityType.ARTIST, "path/to/image.jpg", null, false),
new CoverArt(9, EntityType.MEDIA_FILE, "path/to/image.jpg", null, false)
).map(art -> {
if (art.getEntityId() < 5) {
if (art.getEntityId() < 7) {
switch (art.getEntityType()) {
case ALBUM:
art.setAlbum(new Album());
Album album = new Album();
album.setPresent(art.getEntityId() <= 3); // only album with id 3 has an album present
art.setAlbum(album);
break;
case ARTIST:
art.setArtist(new Artist());
Artist artist = new Artist();
artist.setPresent(art.getEntityId() <= 3); // only artist with id 3 has an artist present
art.setArtist(artist);
break;
case MEDIA_FILE:
MediaFile mediaFile = new MediaFile();
mediaFile.setPresent(art.getEntityId() == 3); // only media file with id 3 has a media file present
mediaFile.setPresent(art.getEntityId() <= 3); // only media file with id 3 has a media file present
art.setMediaFile(mediaFile);
break;
case NONE:
Expand All @@ -174,7 +180,7 @@ void testExpunge() {
coverArtService.expunge();
verify(coverArtRepository, times(1)).deleteAll(coverArtListCaptor.capture());
List<CoverArt> deletedCoverArt = coverArtListCaptor.getValue();
assertEquals(4, deletedCoverArt.size());
assertEquals(6, deletedCoverArt.size());
assertTrue(deletedCoverArt.stream().allMatch(art -> art.getEntityId() > 3));
}

Expand Down

0 comments on commit f38306f

Please sign in to comment.