Housekeepers talk to the DB directly now

This commit is contained in:
Mark McDowall 2013-09-18 17:28:25 -07:00
parent 6c91639d64
commit f716a6d9fd
11 changed files with 161 additions and 141 deletions

View File

@ -60,8 +60,6 @@ namespace NzbDrone.Core.Test.Framework
}
}
[Category("DbTest")]
public abstract class DbTest : CoreTest
{

View File

@ -42,71 +42,5 @@ namespace NzbDrone.Core.Test.HistoryTests
StoredModel.Data.Should().HaveCount(2);
}
[Test]
public void should_delete_orphaned_items_by_series()
{
var history = Builder<History.History>.CreateNew().BuildNew();
Subject.Insert(history);
Subject.CleanupOrphanedBySeries();
Subject.All().Should().BeEmpty();
}
[Test]
public void should_delete_orphaned_items_by_episode()
{
var history = Builder<History.History>.CreateNew().BuildNew();
Subject.Insert(history);
Subject.CleanupOrphanedByEpisode();
Subject.All().Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_data_by_series()
{
var series = Builder<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.Id = 0)
.TheFirst(1)
.With(h => h.SeriesId = series.Id)
.Build();
Subject.InsertMany(history);
Subject.CleanupOrphanedBySeries();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(h => h.SeriesId == series.Id);
}
[Test]
public void should_not_delete_unorphaned_data_by_episode()
{
var episode = Builder<Episode>.CreateNew()
.BuildNew();
Db.Insert(episode);
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.Id = 0)
.TheFirst(1)
.With(h => h.EpisodeId = episode.Id)
.Build();
Subject.InsertMany(history);
Subject.CleanupOrphanedByEpisode();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(h => h.EpisodeId == episode.Id);
}
}
}

View File

@ -1,13 +1,14 @@
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.TvTests.EpisodeRepositoryTests
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{
[TestFixture]
public class CleanupOrphanedEpisodesFixture : DbTest<EpisodeRepository, Episode>
public class CleanupOrphanedEpisodesFixture : DbTest<CleanupOrphanedEpisodes, Episode>
{
[Test]
public void should_delete_orphaned_episodes()
@ -15,9 +16,9 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
var episode = Builder<Episode>.CreateNew()
.BuildNew();
Subject.Insert(episode);
Subject.CleanupOrphanedEpisodes();
Subject.All().Should().BeEmpty();
Db.Insert(episode);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
@ -29,16 +30,14 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
Db.Insert(series);
var episodes = Builder<Episode>.CreateListOfSize(2)
.All()
.With(e => e.Id = 0)
.TheFirst(1)
.With(e => e.SeriesId = series.Id)
.Build();
.BuildListOfNew();
Subject.InsertMany(episodes);
Subject.CleanupOrphanedEpisodes();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(e => e.SeriesId == series.Id);
Db.InsertMany(episodes);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(e => e.SeriesId == series.Id);
}
}
}

View File

@ -0,0 +1,105 @@
using System;
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 CleanupOrphanedHistoryItemsFixture : DbTest<CleanupOrphanedHistoryItems, History.History>
{
private Series _series;
private Episode _episode;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew()
.BuildNew();
_episode = Builder<Episode>.CreateNew()
.BuildNew();
}
private void GivenSeries()
{
Db.Insert(_series);
}
private void GivenEpisode()
{
Db.Insert(_episode);
}
[Test]
public void should_delete_orphaned_items_by_series()
{
GivenEpisode();
var history = Builder<History.History>.CreateNew()
.With(h => h.EpisodeId = _episode.Id)
.BuildNew();
Db.Insert(history);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_delete_orphaned_items_by_episode()
{
GivenSeries();
var history = Builder<History.History>.CreateNew()
.With(h => h.SeriesId = _series.Id)
.BuildNew();
Db.Insert(history);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_data_by_series()
{
GivenSeries();
GivenEpisode();
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.EpisodeId = _episode.Id)
.TheFirst(1)
.With(h => h.SeriesId = _series.Id)
.BuildListOfNew();
Db.InsertMany(history);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(h => h.SeriesId == _series.Id);
}
[Test]
public void should_not_delete_unorphaned_data_by_episode()
{
GivenSeries();
GivenEpisode();
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.SeriesId = _series.Id)
.TheFirst(1)
.With(h => h.EpisodeId = _episode.Id)
.BuildListOfNew();
Db.InsertMany(history);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(h => h.EpisodeId == _episode.Id);
}
}
}

View File

@ -125,6 +125,7 @@
<Compile Include="Framework\CoreTest.cs" />
<Compile Include="Framework\DbTest.cs" />
<Compile Include="Framework\NBuilderExtensions.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
<Compile Include="IndexerSearchTests\SearchDefinitionFixture.cs" />
<Compile Include="IndexerTests\BasicRssParserFixture.cs" />
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
@ -179,7 +180,7 @@
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteFileFixture.cs" />
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteDirectoryFixture.cs" />
<Compile Include="NotificationTests\PlexProviderTest.cs" />
<Compile Include="TvTests\EpisodeRepositoryTests\CleanupOrphanedEpisodesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodesFixture.cs" />
<Compile Include="TvTests\RefreshEpisodeServiceFixture.cs" />
<Compile Include="TvTests\EpisodeProviderTests\HandleEpisodeFileDeletedFixture.cs" />
<Compile Include="TvTests\EpisodeRepositoryTests\FindEpisodeFixture.cs" />

View File

@ -12,8 +12,6 @@ namespace NzbDrone.Core.History
{
void Trim();
List<QualityModel> GetEpisodeHistory(int episodeId);
void CleanupOrphanedBySeries();
void CleanupOrphanedByEpisode();
}
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
@ -39,30 +37,6 @@ namespace NzbDrone.Core.History
return history.Select(h => h.Quality).ToList();
}
public 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)");
}
public 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)");
}
public override PagingSpec<History> GetPaged(PagingSpec<History> pagingSpec)
{
var pagingQuery = Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)

View File

@ -17,7 +17,6 @@ namespace NzbDrone.Core.History
void Trim();
QualityModel GetBestQualityInHistory(int episodeId);
PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
void CleanupOrphaned();
}
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>
@ -41,12 +40,6 @@ namespace NzbDrone.Core.History
return _historyRepository.GetPaged(pagingSpec);
}
public void CleanupOrphaned()
{
_historyRepository.CleanupOrphanedBySeries();
_historyRepository.CleanupOrphanedByEpisode();
}
public void Purge()
{
_historyRepository.Purge();

View File

@ -1,23 +1,32 @@
using NLog;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEpisodes : IHousekeepingTask
{
private readonly IEpisodeService _episodeService;
private readonly IDatabase _database;
private readonly Logger _logger;
public CleanupOrphanedEpisodes(IEpisodeService episodeService, Logger logger)
public CleanupOrphanedEpisodes(IDatabase database, Logger logger)
{
_episodeService = episodeService;
_database = database;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned episodes cleanup");
_episodeService.CleanupOrphanedEpisodes();
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

@ -1,23 +1,49 @@
using NLog;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.History;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedHistoryItems : IHousekeepingTask
{
private readonly IHistoryService _historyService;
private readonly IDatabase _database;
private readonly Logger _logger;
public CleanupOrphanedHistoryItems(IHistoryService historyService, Logger logger)
public CleanupOrphanedHistoryItems(IDatabase database, Logger logger)
{
_historyService = historyService;
_database = database;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned history cleanup");
_historyService.CleanupOrphaned();
CleanupOrphanedBySeries();
CleanupOrphanedByEpisode();
}
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 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

@ -22,7 +22,6 @@ namespace NzbDrone.Core.Tv
void SetMonitoredFlat(Episode episode, bool monitored);
void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
void SetFileId(int episodeId, int fileId);
void CleanupOrphanedEpisodes();
}
public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository
@ -124,18 +123,6 @@ namespace NzbDrone.Core.Tv
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
}
public void CleanupOrphanedEpisodes()
{
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)");
}
private SortBuilder<Episode> GetEpisodesWithoutFilesQuery(PagingSpec<Episode> pagingSpec, DateTime currentTime, int startingSeasonNumber)
{
return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id)

View File

@ -29,7 +29,6 @@ namespace NzbDrone.Core.Tv
void UpdateMany(List<Episode> episodes);
void DeleteMany(List<Episode> episodes);
void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
void CleanupOrphanedEpisodes();
}
public class EpisodeService : IEpisodeService,
@ -113,11 +112,6 @@ namespace NzbDrone.Core.Tv
_episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored);
}
public void CleanupOrphanedEpisodes()
{
_episodeRepository.CleanupOrphanedEpisodes();
}
public bool IsFirstOrLastEpisodeOfSeason(int episodeId)
{
var episode = GetEpisode(episodeId);