Create OrphanedTracks Housekeeper (#81)

pull/82/head
Qstick 7 years ago committed by GitHub
parent c0e922e4a2
commit 43677e8481

@ -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);
}
}
}

@ -0,0 +1,43 @@
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Music;
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{
[TestFixture]
public class CleanupOrphanedTracksFixture : DbTest<CleanupOrphanedTracks, Track>
{
[Test]
public void should_delete_orphaned_tracks()
{
var track = Builder<Track>.CreateNew()
.BuildNew();
Db.Insert(track);
Subject.Clean();
AllStoredModels.Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_tracks()
{
var album = Builder<Album>.CreateNew()
.BuildNew();
Db.Insert(album);
var tracks = Builder<Track>.CreateListOfSize(2)
.TheFirst(1)
.With(e => e.AlbumId = album.Id)
.BuildListOfNew();
Db.InsertMany(tracks);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(e => e.AlbumId == album.Id);
}
}
}

@ -237,7 +237,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupDuplicateMetadataFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedBlacklistFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedTrackFilesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodesFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedTracksFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedIndexerStatusFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItemsFixture.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFilesFixture.cs" />

@ -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)");
}
}
}

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

@ -591,7 +591,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupDuplicateMetadataFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedBlacklist.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedTrackFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodes.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedTracks.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedIndexerStatus.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFiles.cs" />

Loading…
Cancel
Save