Convert to 'using' declaration in Housekeeping Tasks

Closes #8723
pull/8726/head
Bogdan 2 years ago
parent d008768fff
commit 41492efd2e

@ -14,34 +14,32 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{ {
if (_database.DatabaseType == DatabaseType.PostgreSQL) mapper.Execute(@"DELETE FROM ""MetadataFiles""
{ WHERE ""Id"" = ANY (
mapper.Execute(@"DELETE FROM ""MetadataFiles"" SELECT ""Id"" FROM ""MetadataFiles""
WHERE ""Id"" = ANY ( WHERE ""RelativePath""
SELECT ""Id"" FROM ""MetadataFiles"" LIKE '_:\\%'
WHERE ""RelativePath"" OR ""RelativePath""
LIKE '_:\\%' LIKE '\\%'
OR ""RelativePath"" OR ""RelativePath""
LIKE '\\%' LIKE '/%'
OR ""RelativePath"" )");
LIKE '/%' }
)"); else
} {
else mapper.Execute(@"DELETE FROM ""MetadataFiles""
{ WHERE ""Id"" IN (
mapper.Execute(@"DELETE FROM ""MetadataFiles"" SELECT ""Id"" FROM ""MetadataFiles""
WHERE ""Id"" IN ( WHERE ""RelativePath""
SELECT ""Id"" FROM ""MetadataFiles"" LIKE '_:\%'
WHERE ""RelativePath"" OR ""RelativePath""
LIKE '_:\%' LIKE '\%'
OR ""RelativePath"" OR ""RelativePath""
LIKE '\%' LIKE '/%'
OR ""RelativePath"" )");
LIKE '/%'
)");
}
} }
} }
} }

@ -14,13 +14,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""NamingConfig""
mapper.Execute(@"DELETE FROM ""NamingConfig"" WHERE ""Id"" NOT IN (
WHERE ""Id"" NOT IN ( SELECT ""Id"" FROM ""NamingConfig""
SELECT ""Id"" FROM ""NamingConfig"" LIMIT 1)");
LIMIT 1)");
}
} }
} }
} }

@ -14,13 +14,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""Users""
mapper.Execute(@"DELETE FROM ""Users"" WHERE ""Id"" NOT IN (
WHERE ""Id"" NOT IN ( SELECT ""Id"" FROM ""Users""
SELECT ""Id"" FROM ""Users"" LIMIT 1)");
LIMIT 1)");
}
} }
} }
} }

@ -16,30 +16,28 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{ {
if (_database.DatabaseType == DatabaseType.PostgreSQL) mapper.Execute(@"DELETE FROM ""PendingReleases""
{
mapper.Execute(@"DELETE FROM ""PendingReleases""
WHERE ""Added"" < @TwoWeeksAgo WHERE ""Added"" < @TwoWeeksAgo
AND ""Reason"" = ANY (@Reasons)", AND ""Reason"" = ANY (@Reasons)",
new new
{ {
TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), TwoWeeksAgo = DateTime.UtcNow.AddDays(-14),
Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback }
}); });
} }
else else
{ {
mapper.Execute(@"DELETE FROM ""PendingReleases"" mapper.Execute(@"DELETE FROM ""PendingReleases""
WHERE ""Added"" < @TwoWeeksAgo WHERE ""Added"" < @TwoWeeksAgo
AND ""REASON"" IN @Reasons", AND ""REASON"" IN @Reasons",
new new
{ {
TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), TwoWeeksAgo = DateTime.UtcNow.AddDays(-14),
Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback }
}); });
}
} }
} }
} }

@ -20,30 +20,26 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteDuplicateMovieMetadata() private void DeleteDuplicateMovieMetadata()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MetadataFiles""
mapper.Execute(@"DELETE FROM ""MetadataFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT MIN(""Id"") FROM ""MetadataFiles""
SELECT MIN(""Id"") FROM ""MetadataFiles"" WHERE ""Type"" = 1
WHERE ""Type"" = 1 GROUP BY ""MovieId"", ""Consumer""
GROUP BY ""MovieId"", ""Consumer"" HAVING COUNT(""MovieId"") > 1
HAVING COUNT(""MovieId"") > 1 )");
)");
}
} }
private void DeleteDuplicateMovieFileMetadata() private void DeleteDuplicateMovieFileMetadata()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MetadataFiles""
mapper.Execute(@"DELETE FROM ""MetadataFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT MIN(""Id"") FROM ""MetadataFiles""
SELECT MIN(""Id"") FROM ""MetadataFiles"" WHERE ""Type"" = 1
WHERE ""Type"" = 1 GROUP BY ""MovieFileId"", ""Consumer""
GROUP BY ""MovieFileId"", ""Consumer"" HAVING COUNT(""MovieFileId"") > 1
HAVING COUNT(""MovieFileId"") > 1 )");
)");
}
} }
} }
} }

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

@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""Blocklist""
mapper.Execute(@"DELETE FROM ""Blocklist"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""Blocklist"".""Id"" FROM ""Blocklist""
SELECT ""Blocklist"".""Id"" FROM ""Blocklist"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""Blocklist"".""MovieId"" = ""Movies"".""Id""
ON ""Blocklist"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,13 +14,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""Collections"" WHERE ""TmdbId"" IN (SELECT ""X"".""TmdbId"" FROM (SELECT ""Collections"".""TmdbId"", COUNT(""Movies"".""Id"") as ""MovieCount"" FROM ""Collections""
mapper.Execute(@"DELETE FROM ""Collections"" WHERE ""TmdbId"" IN (SELECT ""X"".""TmdbId"" FROM (SELECT ""Collections"".""TmdbId"", COUNT(""Movies"".""Id"") as ""MovieCount"" FROM ""Collections"" LEFT OUTER JOIN ""MovieMetadata"" ON ""Collections"".""TmdbId"" = ""MovieMetadata"".""CollectionTmdbId""
LEFT OUTER JOIN ""MovieMetadata"" ON ""Collections"".""TmdbId"" = ""MovieMetadata"".""CollectionTmdbId"" LEFT OUTER JOIN ""Movies"" ON ""Movies"".""MovieMetadataId"" = ""MovieMetadata"".""Id""
LEFT OUTER JOIN ""Movies"" ON ""Movies"".""MovieMetadataId"" = ""MovieMetadata"".""Id"" GROUP BY ""Collections"".""Id"") AS ""X"" WHERE ""X"".""MovieCount"" = 0)");
GROUP BY ""Collections"".""Id"") AS ""X"" WHERE ""X"".""MovieCount"" = 0)");
}
} }
} }
} }

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

@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""DownloadClientStatus""
mapper.Execute(@"DELETE FROM ""DownloadClientStatus"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus""
SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus"" LEFT OUTER JOIN ""DownloadClients""
LEFT OUTER JOIN ""DownloadClients"" ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id""
ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id"" WHERE ""DownloadClients"".""Id"" IS NULL)");
WHERE ""DownloadClients"".""Id"" IS NULL)");
}
} }
} }
} }

@ -20,29 +20,25 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedByMovie() private void DeleteOrphanedByMovie()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""ExtraFiles""
mapper.Execute(@"DELETE FROM ""ExtraFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""ExtraFiles"".""Id"" FROM ""ExtraFiles""
SELECT ""ExtraFiles"".""Id"" FROM ""ExtraFiles"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""ExtraFiles"".""MovieId"" = ""Movies"".""Id""
ON ""ExtraFiles"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
private void DeleteOrphanedByMovieFile() private void DeleteOrphanedByMovieFile()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""ExtraFiles""
mapper.Execute(@"DELETE FROM ""ExtraFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""ExtraFiles"".""Id"" FROM ""ExtraFiles""
SELECT ""ExtraFiles"".""Id"" FROM ""ExtraFiles"" LEFT OUTER JOIN ""MovieFiles""
LEFT OUTER JOIN ""MovieFiles"" ON ""ExtraFiles"".""MovieFileId"" = ""MovieFiles"".""Id""
ON ""ExtraFiles"".""MovieFileId"" = ""MovieFiles"".""Id"" WHERE ""ExtraFiles"".""MovieFileId"" > 0
WHERE ""ExtraFiles"".""MovieFileId"" > 0 AND ""MovieFiles"".""Id"" IS NULL)");
AND ""MovieFiles"".""Id"" IS NULL)");
}
} }
} }
} }

@ -19,15 +19,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void CleanupOrphanedByMovie() private void CleanupOrphanedByMovie()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""History""
mapper.Execute(@"DELETE FROM ""History"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""History"".""Id"" FROM ""History""
SELECT ""History"".""Id"" FROM ""History"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""History"".""MovieId"" = ""Movies"".""Id""
ON ""History"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""IndexerStatus""
mapper.Execute(@"DELETE FROM ""IndexerStatus"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""IndexerStatus"".""Id"" FROM ""IndexerStatus""
SELECT ""IndexerStatus"".""Id"" FROM ""IndexerStatus"" LEFT OUTER JOIN ""Indexers""
LEFT OUTER JOIN ""Indexers"" ON ""IndexerStatus"".""ProviderId"" = ""Indexers"".""Id""
ON ""IndexerStatus"".""ProviderId"" = ""Indexers"".""Id"" WHERE ""Indexers"".""Id"" IS NULL)");
WHERE ""Indexers"".""Id"" IS NULL)");
}
} }
} }
} }

@ -21,41 +21,35 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedByMovie() private void DeleteOrphanedByMovie()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MetadataFiles""
mapper.Execute(@"DELETE FROM ""MetadataFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles""
SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""MetadataFiles"".""MovieId"" = ""Movies"".""Id""
ON ""MetadataFiles"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
private void DeleteOrphanedByMovieFile() private void DeleteOrphanedByMovieFile()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MetadataFiles""
mapper.Execute(@"DELETE FROM ""MetadataFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles""
SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" LEFT OUTER JOIN ""MovieFiles""
LEFT OUTER JOIN ""MovieFiles"" ON ""MetadataFiles"".""MovieFileId"" = ""MovieFiles"".""Id""
ON ""MetadataFiles"".""MovieFileId"" = ""MovieFiles"".""Id"" WHERE ""MetadataFiles"".""MovieFileId"" > 0
WHERE ""MetadataFiles"".""MovieFileId"" > 0 AND ""MovieFiles"".""Id"" IS NULL)");
AND ""MovieFiles"".""Id"" IS NULL)");
}
} }
private void DeleteWhereMovieFileIsZero() private void DeleteWhereMovieFileIsZero()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MetadataFiles""
mapper.Execute(@"DELETE FROM ""MetadataFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""Id"" FROM ""MetadataFiles""
SELECT ""Id"" FROM ""MetadataFiles"" WHERE ""Type"" IN (1, 2)
WHERE ""Type"" IN (1, 2) AND ""MovieFileId"" = 0)");
AND ""MovieFileId"" = 0)");
}
} }
} }
} }

@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MovieFiles""
mapper.Execute(@"DELETE FROM ""MovieFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""MovieFiles"".""Id"" FROM ""MovieFiles""
SELECT ""MovieFiles"".""Id"" FROM ""MovieFiles"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""MovieFiles"".""Id"" = ""Movies"".""MovieFileId""
ON ""MovieFiles"".""Id"" = ""Movies"".""MovieFileId"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,16 +14,14 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""MovieMetadata""
mapper.Execute(@"DELETE FROM ""MovieMetadata"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""MovieMetadata"".""Id"" FROM ""MovieMetadata""
SELECT ""MovieMetadata"".""Id"" FROM ""MovieMetadata"" LEFT OUTER JOIN ""Movies"" ON ""Movies"".""MovieMetadataId"" = ""MovieMetadata"".""Id""
LEFT OUTER JOIN ""Movies"" ON ""Movies"".""MovieMetadataId"" = ""MovieMetadata"".""Id"" LEFT OUTER JOIN ""Collections"" ON ""Collections"".""TmdbId"" = ""MovieMetadata"".""CollectionTmdbId""
LEFT OUTER JOIN ""Collections"" ON ""Collections"".""TmdbId"" = ""MovieMetadata"".""CollectionTmdbId"" LEFT OUTER JOIN ""ImportListMovies"" ON ""ImportListMovies"".""MovieMetadataId"" = ""MovieMetadata"".""Id""
LEFT OUTER JOIN ""ImportListMovies"" ON ""ImportListMovies"".""MovieMetadataId"" = ""MovieMetadata"".""Id"" WHERE ""Movies"".""Id"" IS NULL AND ""ImportListMovies"".""Id"" IS NULL AND ""Collections"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL AND ""ImportListMovies"".""Id"" IS NULL AND ""Collections"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,16 +14,14 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"UPDATE ""Movies""
mapper.Execute(@"UPDATE ""Movies"" SET ""MovieFileId"" = 0
SET ""MovieFileId"" = 0 WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""Movies"".""Id"" FROM ""Movies""
SELECT ""Movies"".""Id"" FROM ""Movies"" LEFT OUTER JOIN ""MovieFiles""
LEFT OUTER JOIN ""MovieFiles"" ON ""Movies"".""MovieFileId"" = ""MovieFiles"".""Id""
ON ""Movies"".""MovieFileId"" = ""MovieFiles"".""Id"" WHERE ""MovieFiles"".""Id"" IS NULL)");
WHERE ""MovieFiles"".""Id"" IS NULL)");
}
} }
} }
} }

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

@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""PendingReleases""
mapper.Execute(@"DELETE FROM ""PendingReleases"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""PendingReleases"".""Id"" FROM ""PendingReleases""
SELECT ""PendingReleases"".""Id"" FROM ""PendingReleases"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""PendingReleases"".""MovieId"" = ""Movies"".""Id""
ON ""PendingReleases"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
} }
} }

@ -20,29 +20,25 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedByMovie() private void DeleteOrphanedByMovie()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""SubtitleFiles""
mapper.Execute(@"DELETE FROM ""SubtitleFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""SubtitleFiles"".""Id"" FROM ""SubtitleFiles""
SELECT ""SubtitleFiles"".""Id"" FROM ""SubtitleFiles"" LEFT OUTER JOIN ""Movies""
LEFT OUTER JOIN ""Movies"" ON ""SubtitleFiles"".""MovieId"" = ""Movies"".""Id""
ON ""SubtitleFiles"".""MovieId"" = ""Movies"".""Id"" WHERE ""Movies"".""Id"" IS NULL)");
WHERE ""Movies"".""Id"" IS NULL)");
}
} }
private void DeleteOrphanedByMovieFile() private void DeleteOrphanedByMovieFile()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"DELETE FROM ""SubtitleFiles""
mapper.Execute(@"DELETE FROM ""SubtitleFiles"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""SubtitleFiles"".""Id"" FROM ""SubtitleFiles""
SELECT ""SubtitleFiles"".""Id"" FROM ""SubtitleFiles"" LEFT OUTER JOIN ""MovieFiles""
LEFT OUTER JOIN ""MovieFiles"" ON ""SubtitleFiles"".""MovieFileId"" = ""MovieFiles"".""Id""
ON ""SubtitleFiles"".""MovieFileId"" = ""MovieFiles"".""Id"" WHERE ""SubtitleFiles"".""MovieFileId"" > 0
WHERE ""SubtitleFiles"".""MovieFileId"" > 0 AND ""MovieFiles"".""Id"" IS NULL)");
AND ""MovieFiles"".""Id"" IS NULL)");
}
} }
} }
} }

@ -18,31 +18,29 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions", "ImportLists", "Indexers" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToList();
if (usedTags.Any())
{ {
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions", "ImportLists", "Indexers" } var usedTagsList = usedTags.Select(d => d.ToString()).Join(",");
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToList();
if (usedTags.Any()) if (_database.DatabaseType == DatabaseType.PostgreSQL)
{ {
var usedTagsList = usedTags.Select(d => d.ToString()).Join(","); mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])");
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])");
}
else
{
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" IN ({usedTagsList})");
}
} }
else else
{ {
mapper.Execute("DELETE FROM \"Tags\""); mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" IN ({usedTagsList})");
} }
} }
else
{
mapper.Execute("DELETE FROM \"Tags\"");
}
} }
private int[] GetUsedTags(string table, IDbConnection mapper) private int[] GetUsedTags(string table, IDbConnection mapper)

@ -24,13 +24,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
_logger.Debug("Not running scheduled task last execution cleanup during debug"); _logger.Debug("Not running scheduled task last execution cleanup during debug");
} }
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ mapper.Execute(@"UPDATE ""ScheduledTasks""
mapper.Execute(@"UPDATE ""ScheduledTasks"" SET ""LastExecution"" = @time
SET ""LastExecution"" = @time WHERE ""LastExecution"" > @time",
WHERE ""LastExecution"" > @time", new { time = DateTime.UtcNow });
new { time = DateTime.UtcNow });
}
} }
} }
} }

Loading…
Cancel
Save