Skip to content

Commit

Permalink
Fixed: Correctly delete trackfiles on AlbumDeletedEvent
Browse files Browse the repository at this point in the history
GetFilesByAlbum performs a join on the album releases under the hood,
which won't be populated once the album is deleted.  Fix by providing
a special delete method which omits the join and just looks at albumId.
  • Loading branch information
ta264 committed Jun 27, 2019
1 parent 3ebbf6f commit 447bf63
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/NzbDrone.Core.Test/MediaFiles/MediaFileRepositoryFixture.cs
Expand Up @@ -6,6 +6,7 @@
using NzbDrone.Core.Music;
using NzbDrone.Core.Test.Framework;
using System.Collections.Generic;
using System.Linq;

namespace NzbDrone.Core.Test.MediaFiles
{
Expand Down Expand Up @@ -140,5 +141,14 @@ private void VerifyEagerLoaded(List<TrackFile> files)
file.Artist.Value.Metadata.Value.Should().NotBeNull();
}
}

[Test]
public void delete_files_by_album_should_work_if_join_fails()
{
Db.Delete(album);
Subject.DeleteFilesByAlbum(album.Id);

Db.All<TrackFile>().Where(x => x.AlbumId == album.Id).Should().HaveCount(0);
}
}
}
7 changes: 7 additions & 0 deletions src/NzbDrone.Core/MediaFiles/MediaFileRepository.cs
Expand Up @@ -14,6 +14,7 @@ public interface IMediaFileRepository : IBasicRepository<TrackFile>
List<TrackFile> GetFilesByRelease(int releaseId);
List<TrackFile> GetFilesWithBasePath(string path);
TrackFile GetFileWithPath(string path);
void DeleteFilesByAlbum(int albumId);
}


Expand Down Expand Up @@ -51,6 +52,12 @@ public List<TrackFile> GetFilesByAlbum(int albumId)
.ToList();
}

public void DeleteFilesByAlbum(int albumId)
{
var ids = DataMapper.Query<TrackFile>().Where(x => x.AlbumId == albumId);
DeleteMany(ids);
}

public List<TrackFile> GetFilesByRelease(int releaseId)
{
return Query
Expand Down
3 changes: 1 addition & 2 deletions src/NzbDrone.Core/MediaFiles/MediaFileService.cs
Expand Up @@ -148,8 +148,7 @@ public TrackFile GetFileWithPath(string path)

public void HandleAsync(AlbumDeletedEvent message)
{
var files = GetFilesByAlbum(message.Album.Id);
_mediaFileRepository.DeleteMany(files);
_mediaFileRepository.DeleteFilesByAlbum(message.Album.Id);
}

public List<TrackFile> GetFilesByArtist(int artistId)
Expand Down

0 comments on commit 447bf63

Please sign in to comment.