Cleanup episode metadata/image files that aren't properly attached to files

pull/2/head
Mark McDowall 11 years ago
parent 78ded70b39
commit 9c858445a3

@ -3,6 +3,7 @@ using FluentAssertions;
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 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
}
[Test]
public void should_delete_episode_metadata_files_that_have_episodefileid_of_zero()
{
var series = Builder<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var metadataFile = Builder<MetadataFile>.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<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var metadataFile = Builder<MetadataFile>.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);
}
}
}

@ -20,6 +20,7 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
DeleteOrphanedBySeries();
DeleteOrphanedByEpisodeFile();
DeleteWhereEpisodeFileIsZero();
}
private void DeleteOrphanedBySeries()
@ -46,5 +47,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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)");
}
}
}

Loading…
Cancel
Save