Convert to 'using' declaration in Housekeeping Tasks

Closes #2612
pull/2615/head
Bogdan 2 years ago
parent c710b117ab
commit 60abb298b2

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

@ -21,24 +21,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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 }
}); });
} }
} }
} }

@ -21,44 +21,38 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteDuplicateAuthorMetadata() private void DeleteDuplicateAuthorMetadata()
{ {
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 ""AuthorId"", ""Consumer""
GROUP BY ""AuthorId"", ""Consumer"" HAVING COUNT(""AuthorId"") > 1
HAVING COUNT(""AuthorId"") > 1 )");
)");
}
} }
private void DeleteDuplicateBookMetadata() private void DeleteDuplicateBookMetadata()
{ {
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"" IN (2, 4)
WHERE ""Type"" IN (2, 4) GROUP BY ""BookId"", ""Consumer""
GROUP BY ""BookId"", ""Consumer"" HAVING COUNT(""BookId"") > 1
HAVING COUNT(""BookId"") > 1 )");
)");
}
} }
private void DeleteDuplicateBookFileMetadata() private void DeleteDuplicateBookFileMetadata()
{ {
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"" IN (2, 4)
WHERE ""Type"" IN (2, 4) GROUP BY ""BookFileId"", ""Consumer""
GROUP BY ""BookFileId"", ""Consumer"" HAVING COUNT(""BookFileId"") > 1
HAVING COUNT(""BookFileId"") > 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 ""AuthorMetadata""
mapper.Execute(@"DELETE FROM ""AuthorMetadata"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""AuthorMetadata"".""Id"" FROM ""AuthorMetadata""
SELECT ""AuthorMetadata"".""Id"" FROM ""AuthorMetadata"" LEFT OUTER JOIN ""Books"" ON ""Books"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id""
LEFT OUTER JOIN ""Books"" ON ""Books"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" LEFT OUTER JOIN ""Authors"" ON ""Authors"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id""
LEFT OUTER JOIN ""Authors"" ON ""Authors"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""Id"" IS NULL)");
WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""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 ""Authors""
LEFT OUTER JOIN ""Authors"" ON ""Blocklist"".""AuthorId"" = ""Authors"".""Id""
ON ""Blocklist"".""AuthorId"" = ""Authors"".""Id"" WHERE ""Authors"".""Id"" IS NULL)");
WHERE ""Authors"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,17 +14,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{
// Unlink where track no longer exists // Unlink where books no longer exists
mapper.Execute(@"UPDATE ""BookFiles"" mapper.Execute(@"UPDATE ""BookFiles""
SET ""EditionId"" = 0 SET ""EditionId"" = 0
WHERE ""Id"" IN ( WHERE ""Id"" IN (
SELECT ""BookFiles"".""Id"" FROM ""BookFiles"" SELECT ""BookFiles"".""Id"" FROM ""BookFiles""
LEFT OUTER JOIN ""Editions"" LEFT OUTER JOIN ""Editions""
ON ""BookFiles"".""EditionId"" = ""Editions"".""Id"" ON ""BookFiles"".""EditionId"" = ""Editions"".""Id""
WHERE ""Editions"".""Id"" IS NULL)"); WHERE ""Editions"".""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 ""Books""
mapper.Execute(@"DELETE FROM ""Books"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""Books"".""Id"" FROM ""Books""
SELECT ""Books"".""Id"" FROM ""Books"" LEFT OUTER JOIN ""Authors""
LEFT OUTER JOIN ""Authors"" ON ""Books"".""AuthorMetadataId"" = ""Authors"".""AuthorMetadataId""
ON ""Books"".""AuthorMetadataId"" = ""Authors"".""AuthorMetadataId"" WHERE ""Authors"".""Id"" IS NULL)");
WHERE ""Authors"".""Id"" IS NULL)");
}
} }
} }
} }

@ -17,11 +17,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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)");
} }
} }
} }

@ -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 ""Editions""
mapper.Execute(@"DELETE FROM ""Editions"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""Editions"".""Id"" FROM ""Editions""
SELECT ""Editions"".""Id"" FROM ""Editions"" LEFT OUTER JOIN ""Books""
LEFT OUTER JOIN ""Books"" ON ""Editions"".""BookId"" = ""Books"".""Id""
ON ""Editions"".""BookId"" = ""Books"".""Id"" WHERE ""Books"".""Id"" IS NULL)");
WHERE ""Books"".""Id"" IS NULL)");
}
} }
} }
} }

@ -20,28 +20,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void CleanupOrphanedByAuthor() private void CleanupOrphanedByAuthor()
{ {
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 ""Authors""
LEFT OUTER JOIN ""Authors"" ON ""History"".""AuthorId"" = ""Authors"".""Id""
ON ""History"".""AuthorId"" = ""Authors"".""Id"" WHERE ""Authors"".""Id"" IS NULL)");
WHERE ""Authors"".""Id"" IS NULL)");
}
} }
private void CleanupOrphanedByBook() private void CleanupOrphanedByBook()
{ {
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 ""Books""
LEFT OUTER JOIN ""Books"" ON ""History"".""BookId"" = ""Books"".""Id""
ON ""History"".""BookId"" = ""Books"".""Id"" WHERE ""Books"".""Id"" IS NULL)");
WHERE ""Books"".""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 ""ImportListStatus""
mapper.Execute(@"DELETE FROM ""ImportListStatus"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""ImportListStatus"".""Id"" FROM ""ImportListStatus""
SELECT ""ImportListStatus"".""Id"" FROM ""ImportListStatus"" LEFT OUTER JOIN ""ImportLists""
LEFT OUTER JOIN ""ImportLists"" ON ""ImportListStatus"".""ProviderId"" = ""ImportLists"".""Id""
ON ""ImportListStatus"".""ProviderId"" = ""ImportLists"".""Id"" WHERE ""ImportLists"".""Id"" IS NULL)");
WHERE ""ImportLists"".""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)");
}
} }
} }
} }

@ -23,67 +23,57 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedByAuthor() private void DeleteOrphanedByAuthor()
{ {
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 ""Authors""
LEFT OUTER JOIN ""Authors"" ON ""MetadataFiles"".""AuthorId"" = ""Authors"".""Id""
ON ""MetadataFiles"".""AuthorId"" = ""Authors"".""Id"" WHERE ""Authors"".""Id"" IS NULL)");
WHERE ""Authors"".""Id"" IS NULL)");
}
} }
private void DeleteOrphanedByBook() private void DeleteOrphanedByBook()
{ {
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 ""Books""
LEFT OUTER JOIN ""Books"" ON ""MetadataFiles"".""BookId"" = ""Books"".""Id""
ON ""MetadataFiles"".""BookId"" = ""Books"".""Id"" WHERE ""MetadataFiles"".""BookId"" > 0
WHERE ""MetadataFiles"".""BookId"" > 0 AND ""Books"".""Id"" IS NULL)");
AND ""Books"".""Id"" IS NULL)");
}
} }
private void DeleteOrphanedByTrackFile() private void DeleteOrphanedByTrackFile()
{ {
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 ""BookFiles""
LEFT OUTER JOIN ""BookFiles"" ON ""MetadataFiles"".""BookFileId"" = ""BookFiles"".""Id""
ON ""MetadataFiles"".""BookFileId"" = ""BookFiles"".""Id"" WHERE ""MetadataFiles"".""BookFileId"" > 0
WHERE ""MetadataFiles"".""BookFileId"" > 0 AND ""BookFiles"".""Id"" IS NULL)");
AND ""BookFiles"".""Id"" IS NULL)");
}
} }
private void DeleteWhereBookIdIsZero() private void DeleteWhereBookIdIsZero()
{ {
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 (2, 4)
WHERE ""Type"" IN (2, 4) AND ""BookId"" = 0)");
AND ""BookId"" = 0)");
}
} }
private void DeleteWhereTrackFileIsZero() private void DeleteWhereTrackFileIsZero()
{ {
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 (2, 4)
WHERE ""Type"" IN (2, 4) AND ""BookFileId"" = 0)");
AND ""BookFileId"" = 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 ""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 ""Authors""
LEFT OUTER JOIN ""Authors"" ON ""PendingReleases"".""AuthorId"" = ""Authors"".""Id""
ON ""PendingReleases"".""AuthorId"" = ""Authors"".""Id"" WHERE ""Authors"".""Id"" IS NULL)");
WHERE ""Authors"".""Id"" IS NULL)");
}
} }
} }
} }

@ -14,22 +14,20 @@ 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 ""SeriesBookLink""
mapper.Execute(@"DELETE FROM ""SeriesBookLink"" WHERE ""Id"" IN (
WHERE ""Id"" IN ( SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink""
SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" LEFT OUTER JOIN ""Books""
LEFT OUTER JOIN ""Books"" ON ""SeriesBookLink"".""BookId"" = ""Books"".""Id""
ON ""SeriesBookLink"".""BookId"" = ""Books"".""Id"" WHERE ""Books"".""Id"" IS NULL)");
WHERE ""Books"".""Id"" IS NULL)");
mapper.Execute(@"DELETE FROM ""SeriesBookLink"" mapper.Execute(@"DELETE FROM ""SeriesBookLink""
WHERE ""Id"" IN ( WHERE ""Id"" IN (
SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink""
LEFT OUTER JOIN ""Series"" LEFT OUTER JOIN ""Series""
ON ""SeriesBookLink"".""SeriesId"" = ""Series"".""Id"" ON ""SeriesBookLink"".""SeriesId"" = ""Series"".""Id""
WHERE ""Series"".""Id"" IS NULL)"); WHERE ""Series"".""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[] { "Authors", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
if (usedTags.Any())
{ {
var usedTags = new[] { "Authors", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers" } var usedTagsList = usedTags.Select(d => d.ToString()).Join(",");
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
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