diff --git a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedMetadataFilesFixture.cs b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedMetadataFilesFixture.cs index 0e6bb9fc6..4ea0046c3 100644 --- a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedMetadataFilesFixture.cs +++ b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedMetadataFilesFixture.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using NzbDrone.Core.Housekeeping.Housekeepers; using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.Metadata; using NzbDrone.Core.Metadata.Files; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; @@ -81,5 +82,43 @@ public void should_not_delete_metadata_files_that_have_a_coresponding_episode_fi Subject.Clean(); AllStoredModels.Should().HaveCount(1); } + + [Test] + public void should_delete_episode_metadata_files_that_have_episodefileid_of_zero() + { + var series = Builder.CreateNew() + .BuildNew(); + + Db.Insert(series); + + var metadataFile = Builder.CreateNew() + .With(m => m.SeriesId = series.Id) + .With(m => m.Type = MetadataType.EpisodeMetadata) + .With(m => m.EpisodeFileId = 0) + .BuildNew(); + + Db.Insert(metadataFile); + Subject.Clean(); + AllStoredModels.Should().HaveCount(0); + } + + [Test] + public void should_delete_episode_image_files_that_have_episodefileid_of_zero() + { + var series = Builder.CreateNew() + .BuildNew(); + + Db.Insert(series); + + var metadataFile = Builder.CreateNew() + .With(m => m.SeriesId = series.Id) + .With(m => m.Type = MetadataType.EpisodeImage) + .With(m => m.EpisodeFileId = 0) + .BuildNew(); + + Db.Insert(metadataFile); + Subject.Clean(); + AllStoredModels.Should().HaveCount(0); + } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs index 88ee87ad3..e889b3214 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs @@ -20,6 +20,7 @@ public void Clean() DeleteOrphanedBySeries(); DeleteOrphanedByEpisodeFile(); + DeleteWhereEpisodeFileIsZero(); } private void DeleteOrphanedBySeries() @@ -46,5 +47,16 @@ LEFT OUTER JOIN EpisodeFiles WHERE MetadataFiles.EpisodeFileId > 0 AND EpisodeFiles.Id IS NULL)"); } + + private void DeleteWhereEpisodeFileIsZero() + { + var mapper = _database.GetDataMapper(); + + mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT Id FROM MetadataFiles + WHERE Type IN (2, 5) + AND EpisodeFileId = 0)"); + } } }