Fixed: Cleanup orphaned import list movies by movie metadata

pull/9499/head
Bogdan 1 year ago
parent c8d30fd214
commit 119cdf6f09

@ -4,6 +4,7 @@ using NUnit.Framework;
using NzbDrone.Core.Housekeeping.Housekeepers; using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.ImportLists; using NzbDrone.Core.ImportLists;
using NzbDrone.Core.ImportLists.ImportListMovies; using NzbDrone.Core.ImportLists.ImportListMovies;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Housekeeping.Housekeepers namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
@ -42,8 +43,13 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
{ {
GivenImportList(); GivenImportList();
var movieMetadata = Builder<MovieMetadata>.CreateNew().BuildNew();
Db.Insert(movieMetadata);
var status = Builder<ImportListMovie>.CreateNew() var status = Builder<ImportListMovie>.CreateNew()
.With(h => h.ListId = _importList.Id) .With(h => h.ListId = _importList.Id)
.With(b => b.MovieMetadataId = movieMetadata.Id)
.BuildNew(); .BuildNew();
Db.Insert(status); Db.Insert(status);

@ -13,15 +13,33 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
} }
public void Clean() public void Clean()
{
CleanupOrphanedByImportLists();
CleanupOrphanedByMovieMetadata();
}
private void CleanupOrphanedByImportLists()
{
using var mapper = _database.OpenConnection();
mapper.Execute(@"DELETE FROM ""ImportListMovies""
WHERE ""Id"" IN (
SELECT ""ImportListMovies"".""Id""
FROM ""ImportListMovies""
LEFT OUTER JOIN ""ImportLists"" ON ""ImportListMovies"".""ListId"" = ""ImportLists"".""Id""
WHERE ""ImportLists"".""Id"" IS NULL)");
}
private void CleanupOrphanedByMovieMetadata()
{ {
using var mapper = _database.OpenConnection(); using var mapper = _database.OpenConnection();
mapper.Execute(@"DELETE FROM ""ImportListMovies"" mapper.Execute(@"DELETE FROM ""ImportListMovies""
WHERE ""Id"" IN ( WHERE ""Id"" IN (
SELECT ""ImportListMovies"".""Id"" FROM ""ImportListMovies"" SELECT ""ImportListMovies"".""Id""
LEFT OUTER JOIN ""ImportLists"" FROM ""ImportListMovies""
ON ""ImportListMovies"".""ListId"" = ""ImportLists"".""Id"" LEFT OUTER JOIN ""MovieMetadata"" ON ""ImportListMovies"".""MovieMetadataId"" = ""MovieMetadata"".""Id""
WHERE ""ImportLists"".""Id"" IS NULL)"); WHERE ""MovieMetadata"".""Id"" IS NULL)");
} }
} }
} }

Loading…
Cancel
Save