You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs

50 lines
1.6 KiB

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