From 3e5626f8942fb78e1f79997cc220ef654d5a07af Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 24 May 2020 22:36:40 -0400 Subject: [PATCH] Fixed: Housekeeper for Credits (Left behind from deleted caused later add issues) --- .../CleanupOrphanedCreditsFixture.cs | 46 +++++++++++++++++++ .../Housekeepers/CleanupOrphanedCredits.cs | 28 +++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedCreditsFixture.cs create mode 100644 src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedCredits.cs diff --git a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedCreditsFixture.cs b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedCreditsFixture.cs new file mode 100644 index 000000000..219b47fb5 --- /dev/null +++ b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedCreditsFixture.cs @@ -0,0 +1,46 @@ +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Housekeeping.Housekeepers; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Movies.Credits; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Housekeeping.Housekeepers +{ + [TestFixture] + public class CleanupOrphanedCreditsFixture : DbTest + { + [Test] + public void should_delete_orphaned_credit_items() + { + var credit = Builder.CreateNew() + .With(h => h.MovieId = default) + .With(h => h.Name = "Some Credit") + .BuildNew(); + + Db.Insert(credit); + Subject.Clean(); + AllStoredModels.Should().BeEmpty(); + } + + [Test] + public void should_not_delete_unorphaned_credit_items() + { + var movie = Builder.CreateNew().BuildNew(); + + Db.Insert(movie); + + var credit = Builder.CreateNew() + .With(h => h.MovieId = default) + .With(h => h.Name = "Some Credit") + .With(b => b.MovieId = movie.Id) + .BuildNew(); + + Db.Insert(credit); + + Subject.Clean(); + AllStoredModels.Should().HaveCount(1); + } + } +} diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedCredits.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedCredits.cs new file mode 100644 index 000000000..2bd61c5a3 --- /dev/null +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedCredits.cs @@ -0,0 +1,28 @@ +using Dapper; +using NzbDrone.Core.Datastore; + +namespace NzbDrone.Core.Housekeeping.Housekeepers +{ + public class CleanupOrphanedCredits : IHousekeepingTask + { + private readonly IMainDatabase _database; + + public CleanupOrphanedCredits(IMainDatabase database) + { + _database = database; + } + + public void Clean() + { + using (var mapper = _database.OpenConnection()) + { + mapper.Execute(@"DELETE FROM Credits + WHERE Id IN ( + SELECT Credits.Id FROM Credits + LEFT OUTER JOIN Movies + ON Credits.MovieId = Movies.Id + WHERE Movies.Id IS NULL)"); + } + } + } +}