From 0b22d7fce3229722e597c586a9b93c6fd5b537d8 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 2 Sep 2017 06:39:02 -0400 Subject: [PATCH] Create Orphaned Albums Housekeeper (#82) --- .../CleanupOrphanedAlbumsFixture.cs | 43 +++++++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../Housekeepers/CleanupOrphanedAlbums.cs | 26 +++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 4 files changed, 71 insertions(+) create mode 100644 src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedAlbumsFixture.cs create mode 100644 src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlbums.cs diff --git a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedAlbumsFixture.cs b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedAlbumsFixture.cs new file mode 100644 index 000000000..cfe479c81 --- /dev/null +++ b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedAlbumsFixture.cs @@ -0,0 +1,43 @@ +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Housekeeping.Housekeepers; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Music; + +namespace NzbDrone.Core.Test.Housekeeping.Housekeepers +{ + [TestFixture] + public class CleanupOrphanedAlbumsFixture : DbTest + { + [Test] + public void should_delete_orphaned_albums() + { + var album = Builder.CreateNew() + .BuildNew(); + + Db.Insert(album); + Subject.Clean(); + AllStoredModels.Should().BeEmpty(); + } + + [Test] + public void should_not_delete_unorphaned_albums() + { + var artist = Builder.CreateNew() + .BuildNew(); + + Db.Insert(artist); + + var albums = Builder.CreateListOfSize(2) + .TheFirst(1) + .With(e => e.ArtistId = artist.Id) + .BuildListOfNew(); + + Db.InsertMany(albums); + Subject.Clean(); + AllStoredModels.Should().HaveCount(1); + AllStoredModels.Should().Contain(e => e.ArtistId == artist.Id); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 9c4973c75..d296761bb 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -235,6 +235,7 @@ + diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlbums.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlbums.cs new file mode 100644 index 000000000..f9f2d3e87 --- /dev/null +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAlbums.cs @@ -0,0 +1,26 @@ +using NzbDrone.Core.Datastore; + +namespace NzbDrone.Core.Housekeeping.Housekeepers +{ + public class CleanupOrphanedAlbums : IHousekeepingTask + { + private readonly IMainDatabase _database; + + public CleanupOrphanedAlbums(IMainDatabase database) + { + _database = database; + } + + public void Clean() + { + var mapper = _database.GetDataMapper(); + + mapper.ExecuteNonQuery(@"DELETE FROM Albums + WHERE Id IN ( + SELECT Albums.Id FROM Albums + LEFT OUTER JOIN Artists + ON Albums.ArtistId = Artists.Id + WHERE Artists.Id IS NULL)"); + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 9bb8c692d..534145063 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -589,6 +589,7 @@ +