Cleanup orphaned episode files if the series was deleted

Fixed: Cleanup orphaned episode files after a series is deleted
pull/34/head
Mark McDowall 11 years ago
parent 036c1f888f
commit c926775719

@ -0,0 +1,45 @@
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{
[TestFixture]
public class CleanupOrphanedEpisodeFilesFixture : DbTest<CleanupOrphanedEpisodeFiles, EpisodeFile>
{
[Test]
public void should_delete_orphaned_episode_files()
{
var episodeFile = Builder<EpisodeFile>.CreateNew()
.BuildNew();
Db.Insert(episodeFile);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_episode_files()
{
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(2)
.BuildListOfNew();
Db.InsertMany(episodeFiles);
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFiles.First().Id)
.BuildNew();
Db.Insert(episode);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
Db.All<Episode>().Should().Contain(e => e.EpisodeFileId == AllStoredModels.First().Id);
}
}
}

@ -128,6 +128,7 @@
<Compile Include="Framework\DbTest.cs" />
<Compile Include="Framework\NBuilderExtensions.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFilesFixture.cs" />
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />

@ -0,0 +1,31 @@
using NLog;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEpisodeFiles : IHousekeepingTask
{
private readonly IDatabase _database;
private readonly Logger _logger;
public CleanupOrphanedEpisodeFiles(IDatabase database, Logger logger)
{
_database = database;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned episode files cleanup");
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM EpisodeFiles
WHERE Id IN (
SELECT EpisodeFiles.Id FROM EpisodeFiles
LEFT OUTER JOIN Episodes
ON EpisodeFiles.Id = Episodes.EpisodeFileId
WHERE Episodes.Id IS NULL)");
}
}
}

@ -228,6 +228,7 @@
<Compile Include="Exceptions\StatusCodeToExceptions.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodes.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
<Compile Include="Housekeeping\HousekeepingService.cs" />
<Compile Include="Housekeeping\IHousekeepingTask.cs" />

Loading…
Cancel
Save