1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-29 11:25:26 +00:00

Clean up the cleanup'er (housekeeping)

This commit is contained in:
Devin Buhl 2017-03-17 20:39:50 -04:00
parent b137fc075a
commit c03c13d924
13 changed files with 133 additions and 230 deletions

View file

@ -1,43 +0,0 @@
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{
[TestFixture]
public class CleanupOrphanedEpisodesFixture : DbTest<CleanupOrphanedEpisodes, Episode>
{
[Test]
public void should_delete_orphaned_episodes()
{
var episode = Builder<Episode>.CreateNew()
.BuildNew();
Db.Insert(episode);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_episodes()
{
var series = Builder<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var episodes = Builder<Episode>.CreateListOfSize(2)
.TheFirst(1)
.With(e => e.SeriesId = series.Id)
.BuildListOfNew();
Db.InsertMany(episodes);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(e => e.SeriesId == series.Id);
}
}
}

View file

@ -11,12 +11,12 @@
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{
[TestFixture]
public class CleanupOrphanedEpisodeFilesFixture : DbTest<CleanupOrphanedEpisodeFiles, EpisodeFile>
public class CleanupOrphanedMovieFilesFixture : DbTest<CleanupOrphanedMovieFiles, MovieFile>
{
[Test]
public void should_delete_orphaned_episode_files()
{
var episodeFile = Builder<EpisodeFile>.CreateNew()
var episodeFile = Builder<MovieFile>.CreateNew()
.With(h => h.Quality = new QualityModel())
.BuildNew();
@ -28,22 +28,22 @@ public void should_delete_orphaned_episode_files()
[Test]
public void should_not_delete_unorphaned_episode_files()
{
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(2)
var episodeFiles = Builder<MovieFile>.CreateListOfSize(2)
.All()
.With(h => h.Quality = new QualityModel())
.BuildListOfNew();
Db.InsertMany(episodeFiles);
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFiles.First().Id)
var episode = Builder<Movie>.CreateNew()
.With(e => e.MovieFileId = episodeFiles.First().Id)
.BuildNew();
Db.Insert(episode);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
Db.All<Episode>().Should().Contain(e => e.EpisodeFileId == AllStoredModels.First().Id);
Db.All<Movie>().Should().Contain(e => e.MovieFileId == AllStoredModels.First().Id);
}
}
}

View file

@ -230,8 +230,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupAbsolutePathMetadataFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupDuplicateMetadataFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedBlacklistFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMovieFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedIndexerStatusFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFilesFixture.cs" />

View file

@ -13,48 +13,48 @@ public CleanupDuplicateMetadataFiles(IMainDatabase database)
public void Clean()
{
DeleteDuplicateSeriesMetadata();
DeleteDuplicateEpisodeMetadata();
DeleteDuplicateEpisodeImages();
//DeleteDuplicateSeriesMetadata();
//DeleteDuplicateEpisodeMetadata();
//DeleteDuplicateEpisodeImages();
}
private void DeleteDuplicateSeriesMetadata()
{
var mapper = _database.GetDataMapper();
//private void DeleteDuplicateSeriesMetadata()
//{
// var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 1
GROUP BY SeriesId, Consumer
HAVING COUNT(SeriesId) > 1
)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT Id FROM MetadataFiles
// WHERE Type = 1
// GROUP BY SeriesId, Consumer
// HAVING COUNT(SeriesId) > 1
// )");
//}
private void DeleteDuplicateEpisodeMetadata()
{
var mapper = _database.GetDataMapper();
//private void DeleteDuplicateEpisodeMetadata()
//{
// var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 2
GROUP BY EpisodeFileId, Consumer
HAVING COUNT(EpisodeFileId) > 1
)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT Id FROM MetadataFiles
// WHERE Type = 2
// GROUP BY EpisodeFileId, Consumer
// HAVING COUNT(EpisodeFileId) > 1
// )");
//}
private void DeleteDuplicateEpisodeImages()
{
var mapper = _database.GetDataMapper();
//private void DeleteDuplicateEpisodeImages()
//{
// var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 5
GROUP BY EpisodeFileId, Consumer
HAVING COUNT(EpisodeFileId) > 1
)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT Id FROM MetadataFiles
// WHERE Type = 5
// GROUP BY EpisodeFileId, Consumer
// HAVING COUNT(EpisodeFileId) > 1
// )");
//}
}
}

View file

@ -1,26 +0,0 @@
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEpisodeFiles : IHousekeepingTask
{
private readonly IMainDatabase _database;
public CleanupOrphanedEpisodeFiles(IMainDatabase database)
{
_database = database;
}
public void Clean()
{
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)");
}
}
}

View file

@ -1,26 +0,0 @@
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEpisodes : IHousekeepingTask
{
private readonly IMainDatabase _database;
public CleanupOrphanedEpisodes(IMainDatabase database)
{
_database = database;
}
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM Episodes
WHERE Id IN (
SELECT Episodes.Id FROM Episodes
LEFT OUTER JOIN Series
ON Episodes.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
}
}

View file

@ -13,23 +13,9 @@ public CleanupOrphanedHistoryItems(IMainDatabase database)
public void Clean()
{
//CleanupOrphanedBySeries();
//CleanupOrphanedByEpisode();
CleanupOrphanedByMovie();
}
private void CleanupOrphanedBySeries()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Series
ON History.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
private void CleanupOrphanedByMovie()
{
var mapper = _database.GetDataMapper();
@ -41,17 +27,5 @@ LEFT OUTER JOIN Movies
ON History.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
private void CleanupOrphanedByEpisode()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Episodes
ON History.EpisodeId = Episodes.Id
WHERE Episodes.Id IS NULL)");
}
}
}

View file

@ -13,45 +13,45 @@ public CleanupOrphanedMetadataFiles(IMainDatabase database)
public void Clean()
{
DeleteOrphanedBySeries();
DeleteOrphanedByEpisodeFile();
DeleteWhereEpisodeFileIsZero();
//DeleteOrphanedBySeries();
//DeleteOrphanedByEpisodeFile();
//DeleteWhereEpisodeFileIsZero();
}
private void DeleteOrphanedBySeries()
{
var mapper = _database.GetDataMapper();
//private void DeleteOrphanedBySeries()
//{
// var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN Series
ON MetadataFiles.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT MetadataFiles.Id FROM MetadataFiles
// LEFT OUTER JOIN Series
// ON MetadataFiles.SeriesId = Series.Id
// WHERE Series.Id IS NULL)");
//}
private void DeleteOrphanedByEpisodeFile()
{
var mapper = _database.GetDataMapper();
//private void DeleteOrphanedByEpisodeFile()
//{
// var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN EpisodeFiles
ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id
WHERE MetadataFiles.EpisodeFileId > 0
AND EpisodeFiles.Id IS NULL)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT MetadataFiles.Id FROM MetadataFiles
// LEFT OUTER JOIN EpisodeFiles
// ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id
// WHERE MetadataFiles.EpisodeFileId > 0
// AND EpisodeFiles.Id IS NULL)");
//}
private void DeleteWhereEpisodeFileIsZero()
{
var mapper = _database.GetDataMapper();
//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)");
}
// mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
// WHERE Id IN (
// SELECT Id FROM MetadataFiles
// WHERE Type IN (2, 5)
// AND EpisodeFileId = 0)");
//}
}
}

View file

@ -0,0 +1,26 @@
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedMovieFiles : IHousekeepingTask
{
private readonly IMainDatabase _database;
public CleanupOrphanedMovieFiles(IMainDatabase database)
{
_database = database;
}
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MovieFiles
WHERE Id IN (
SELECT MovieFiles.Id FROM MovieFiles
LEFT OUTER JOIN Movies
ON MovieFiles.Id = Movies.MovieFileId
WHERE Movies.Id IS NULL)");
}
}
}

View file

@ -19,7 +19,7 @@ public void Clean()
{
var mapper = _database.GetDataMapper();
var usedTags = new[] { "Series", "Notifications", "DelayProfiles", "Restrictions" }
var usedTags = new[] { "Movies", "Series", "Notifications", "DelayProfiles", "Restrictions" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();

View file

@ -0,0 +1,27 @@
using System.Linq;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class UpdateCleanTitleForMovies : IHousekeepingTask
{
private readonly IMovieRepository _movieRepository;
public UpdateCleanTitleForMovies(IMovieRepository movieRepository)
{
_movieRepository = movieRepository;
}
public void Clean()
{
var movies = _movieRepository.All().ToList();
movies.ForEach(s =>
{
s.CleanTitle = s.CleanTitle.CleanSeriesTitle();
_movieRepository.Update(s);
});
}
}
}

View file

@ -1,27 +0,0 @@
using System.Linq;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class UpdateCleanTitleForSeries : IHousekeepingTask
{
private readonly ISeriesRepository _seriesRepository;
public UpdateCleanTitleForSeries(ISeriesRepository seriesRepository)
{
_seriesRepository = seriesRepository;
}
public void Clean()
{
var series = _seriesRepository.All().ToList();
series.ForEach(s =>
{
s.CleanTitle = s.CleanTitle.CleanSeriesTitle();
_seriesRepository.Update(s);
});
}
}
}

View file

@ -639,8 +639,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupAbsolutePathMetadataFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupDuplicateMetadataFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedBlacklist.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodes.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMovieFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedIndexerStatus.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFiles.cs" />
@ -649,7 +648,7 @@
<Compile Include="Housekeeping\Housekeepers\DeleteBadMediaCovers.cs" />
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasks.cs" />
<Compile Include="Housekeeping\Housekeepers\TrimLogDatabase.cs" />
<Compile Include="Housekeeping\Housekeepers\UpdateCleanTitleForSeries.cs" />
<Compile Include="Housekeeping\Housekeepers\UpdateCleanTitleForMovies.cs" />
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
<Compile Include="Housekeeping\HousekeepingService.cs" />
<Compile Include="Housekeeping\IHousekeepingTask.cs" />
@ -1347,4 +1346,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>