2013-09-18 05:28:05 +00:00
|
|
|
|
using NLog;
|
2013-09-19 00:28:25 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-18 05:28:05 +00:00
|
|
|
|
using NzbDrone.Core.History;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Housekeeping.Housekeepers
|
|
|
|
|
{
|
|
|
|
|
public class CleanupOrphanedHistoryItems : IHousekeepingTask
|
|
|
|
|
{
|
2013-09-19 00:28:25 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2013-09-18 05:28:05 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-09-19 00:28:25 +00:00
|
|
|
|
public CleanupOrphanedHistoryItems(IDatabase database, Logger logger)
|
2013-09-18 05:28:05 +00:00
|
|
|
|
{
|
2013-09-19 00:28:25 +00:00
|
|
|
|
_database = database;
|
2013-09-18 05:28:05 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clean()
|
|
|
|
|
{
|
|
|
|
|
_logger.Trace("Running orphaned history cleanup");
|
2013-09-19 00:28:25 +00:00
|
|
|
|
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)");
|
2013-09-18 05:28:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|