Added method to delete orphaned episodeFiles and also cleanup episodes that have invalid episodeFiles.

Cleanup will run aforementioned methods before doing cleanup.
pull/7/merge
Mark McDowall 14 years ago
parent 208feb7d19
commit 68b7ba9209

@ -359,5 +359,47 @@ namespace NzbDrone.Core.Test
//Assert
Assert.AreEqual(expectedName, result);
}
[Test]
public void CleanEpisodesWithNonExistantFiles()
{
//Setup
var episodes = Builder<Episode>.CreateListOfSize(10).Build();
var mocker = new AutoMoqer();
var database = MockLib.GetEmptyDatabase(true);
mocker.SetConstant(database);
database.InsertMany(episodes);
//Act
mocker.Resolve<MediaFileProvider>().CleanEpisodesWithNonExistantFiles();
var result = database.Fetch<Episode>();
//Assert
result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0);
}
[Test]
public void DeleteOrphanedEpisodeFiles()
{
//Setup
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10).Build();
var episodes = Builder<Episode>.CreateListOfSize(5).Build();
var mocker = new AutoMoqer();
var database = MockLib.GetEmptyDatabase(true);
mocker.SetConstant(database);
database.InsertMany(episodes);
database.InsertMany(episodeFiles);
//Act
mocker.Resolve<MediaFileProvider>().DeleteOrphanedEpisodeFiles();
var result = database.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(5);
result.Should().OnlyContain(e => e.EpisodeFileId > 0);
}
}
}

@ -196,7 +196,9 @@ namespace NzbDrone.Core.Providers
/// <param name = "files">list of files to verify</param>
public virtual void CleanUp(List<EpisodeFile> files)
{
//TODO: remove orphaned files. in files table but not linked to from episode table.
_mediaFileProvider.CleanEpisodesWithNonExistantFiles();
_mediaFileProvider.DeleteOrphanedEpisodeFiles();
foreach (var episodeFile in files)
{
if (!_diskProvider.FileExists(episodeFile.Path))

@ -74,6 +74,26 @@ namespace NzbDrone.Core.Providers
return new FileInfo(path);
}
public virtual void CleanEpisodesWithNonExistantFiles()
{
_database.Execute(@"UPDATE Episodes SET EpisodeFileId = 0
WHERE EpisodeFileId IN
(SELECT Episodes.EpisodeFileId FROM Episodes
LEFT OUTER JOIN EpisodeFiles
ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.EpisodeFileId > 0 AND EpisodeFiles.EpisodeFileId IS null)");
}
public virtual void DeleteOrphanedEpisodeFiles()
{
_database.Execute(@"DELETE FROM EpisodeFiles
WHERE EpisodeFileId IN
(SELECT EpisodeFiles.EpisodeFileId FROM EpisodeFiles
LEFT OUTER JOIN Episodes
ON EpisodeFiles.EpisodeFileId = Episodes.EpisodeFileId
WHERE Episodes.EpisodeFileId IS null)");
}
public virtual string GetNewFilename(IList<Episode> episodes, string seriesTitle, QualityTypes quality)
{
var separatorStyle = EpisodeSortingHelper.GetSeparatorStyle(_configProvider.SeparatorStyle);

Loading…
Cancel
Save