1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2025-01-03 05:25:10 +00:00

Fixed: Cleanup Tracks Orphaned in History

This commit is contained in:
Qstick 2020-01-01 13:02:03 -05:00
parent e2c1d57d8c
commit f6fd675ad4
2 changed files with 68 additions and 0 deletions

View file

@ -13,6 +13,7 @@ public class CleanupOrphanedHistoryItemsFixture : DbTest<CleanupOrphanedHistoryI
{
private Artist _artist;
private Album _album;
private Track _track;
[SetUp]
public void Setup()
@ -22,6 +23,9 @@ public void Setup()
_album = Builder<Album>.CreateNew()
.BuildNew();
_track = Builder<Track>.CreateNew()
.BuildNew();
}
private void GivenArtist()
@ -34,14 +38,21 @@ private void GivenAlbum()
Db.Insert(_album);
}
private void GivenTrack()
{
Db.Insert(_track);
}
[Test]
public void should_delete_orphaned_items_by_artist()
{
GivenAlbum();
GivenTrack();
var history = Builder<History.History>.CreateNew()
.With(h => h.Quality = new QualityModel())
.With(h => h.AlbumId = _album.Id)
.With(h => h.TrackId = _track.Id)
.BuildNew();
Db.Insert(history);
@ -53,10 +64,29 @@ public void should_delete_orphaned_items_by_artist()
public void should_delete_orphaned_items_by_album()
{
GivenArtist();
GivenTrack();
var history = Builder<History.History>.CreateNew()
.With(h => h.Quality = new QualityModel())
.With(h => h.ArtistId = _artist.Id)
.With(h => h.TrackId = _track.Id)
.BuildNew();
Db.Insert(history);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_delete_orphaned_items_by_track()
{
GivenArtist();
GivenAlbum();
var history = Builder<History.History>.CreateNew()
.With(h => h.Quality = new QualityModel())
.With(h => h.ArtistId = _artist.Id)
.With(h => h.AlbumId = _album.Id)
.BuildNew();
Db.Insert(history);
@ -69,6 +99,7 @@ public void should_not_delete_unorphaned_data_by_artist()
{
GivenArtist();
GivenAlbum();
GivenTrack();
var history = Builder<History.History>.CreateListOfSize(2)
.All()
@ -90,6 +121,7 @@ public void should_not_delete_unorphaned_data_by_album()
{
GivenArtist();
GivenAlbum();
GivenTrack();
var history = Builder<History.History>.CreateListOfSize(2)
.All()
@ -105,5 +137,28 @@ public void should_not_delete_unorphaned_data_by_album()
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(h => h.AlbumId == _album.Id);
}
[Test]
public void should_not_delete_unorphaned_data_by_track()
{
GivenArtist();
GivenAlbum();
GivenTrack();
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.Quality = new QualityModel())
.With(h => h.ArtistId = _artist.Id)
.With(h => h.AlbumId = _album.Id)
.TheFirst(1)
.With(h => h.TrackId = _track.Id)
.BuildListOfNew();
Db.InsertMany(history);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(h => h.TrackId == _track.Id);
}
}
}

View file

@ -15,6 +15,7 @@ public void Clean()
{
CleanupOrphanedByArtist();
CleanupOrphanedByAlbum();
CleanupOrphanedByTrack();
}
private void CleanupOrphanedByArtist()
@ -40,5 +41,17 @@ LEFT OUTER JOIN Albums
ON History.AlbumId = Albums.Id
WHERE Albums.Id IS NULL)");
}
private void CleanupOrphanedByTrack()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Tracks
ON History.TrackId = Tracks.Id
WHERE Tracks.Id IS NULL)");
}
}
}