diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs index 8d50f05de..a69a13831 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs @@ -14,34 +14,32 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers 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 ( - SELECT ""Id"" FROM ""MetadataFiles"" - WHERE ""RelativePath"" - LIKE '_:\\%' - OR ""RelativePath"" - LIKE '\\%' - OR ""RelativePath"" - LIKE '/%' - )"); - } - else - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""Id"" FROM ""MetadataFiles"" - WHERE ""RelativePath"" - LIKE '_:\%' - OR ""RelativePath"" - LIKE '\%' - OR ""RelativePath"" - LIKE '/%' - )"); - } + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" = ANY ( + SELECT ""Id"" FROM ""MetadataFiles"" + WHERE ""RelativePath"" + LIKE '_:\\%' + OR ""RelativePath"" + LIKE '\\%' + OR ""RelativePath"" + LIKE '/%' + )"); + } + else + { + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""Id"" FROM ""MetadataFiles"" + WHERE ""RelativePath"" + LIKE '_:\%' + OR ""RelativePath"" + LIKE '\%' + OR ""RelativePath"" + LIKE '/%' + )"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs index d9c61e790..59a4b50e9 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalNamingSpecs.cs @@ -14,13 +14,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""NamingConfig"" - WHERE ""Id"" NOT IN ( - SELECT ""Id"" FROM ""NamingConfig"" - LIMIT 1)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""NamingConfig"" + WHERE ""Id"" NOT IN ( + SELECT ""Id"" FROM ""NamingConfig"" + LIMIT 1)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs index 3845110f4..d13adbca9 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAdditionalUsers.cs @@ -14,13 +14,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""Users"" - WHERE ""Id"" NOT IN ( - SELECT ""Id"" FROM ""Users"" - LIMIT 1)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""Users"" + WHERE ""Id"" NOT IN ( + SELECT ""Id"" FROM ""Users"" + LIMIT 1)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDownloadClientUnavailablePendingReleases.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDownloadClientUnavailablePendingReleases.cs index 30f581dad..1e3a7037c 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDownloadClientUnavailablePendingReleases.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDownloadClientUnavailablePendingReleases.cs @@ -21,24 +21,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers if (_database.DatabaseType == DatabaseType.PostgreSQL) { mapper.Execute(@"DELETE FROM ""PendingReleases"" - WHERE ""Added"" < @TwoWeeksAgo - AND ""Reason"" = ANY (@Reasons)", - new - { - TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), - Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } - }); + WHERE ""Added"" < @TwoWeeksAgo + AND ""Reason"" = ANY (@Reasons)", + new + { + TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), + Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } + }); } else { mapper.Execute(@"DELETE FROM ""PendingReleases"" - WHERE ""Added"" < @TwoWeeksAgo - AND ""REASON"" IN @Reasons", - new - { - TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), - Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } - }); + WHERE ""Added"" < @TwoWeeksAgo + AND ""REASON"" IN @Reasons", + new + { + TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), + Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } + }); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs index ca13e3c4a..0169a92a0 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs @@ -21,44 +21,38 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers private void DeleteDuplicateAuthorMetadata() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT MIN(""Id"") FROM ""MetadataFiles"" - WHERE ""Type"" = 1 - GROUP BY ""AuthorId"", ""Consumer"" - HAVING COUNT(""AuthorId"") > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT MIN(""Id"") FROM ""MetadataFiles"" + WHERE ""Type"" = 1 + GROUP BY ""AuthorId"", ""Consumer"" + HAVING COUNT(""AuthorId"") > 1 + )"); } private void DeleteDuplicateBookMetadata() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT MIN(""Id"") FROM ""MetadataFiles"" - WHERE ""Type"" IN (2, 4) - GROUP BY ""BookId"", ""Consumer"" - HAVING COUNT(""BookId"") > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT MIN(""Id"") FROM ""MetadataFiles"" + WHERE ""Type"" IN (2, 4) + GROUP BY ""BookId"", ""Consumer"" + HAVING COUNT(""BookId"") > 1 + )"); } private void DeleteDuplicateBookFileMetadata() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT MIN(""Id"") FROM ""MetadataFiles"" - WHERE ""Type"" IN (2, 4) - GROUP BY ""BookFileId"", ""Consumer"" - HAVING COUNT(""BookFileId"") > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT MIN(""Id"") FROM ""MetadataFiles"" + WHERE ""Type"" IN (2, 4) + GROUP BY ""BookFileId"", ""Consumer"" + HAVING COUNT(""BookFileId"") > 1 + )"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAuthorMetadata.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAuthorMetadata.cs index 0b64f930e..ced59b2ef 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAuthorMetadata.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedAuthorMetadata.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""AuthorMetadata"" - WHERE ""Id"" IN ( - SELECT ""AuthorMetadata"".""Id"" FROM ""AuthorMetadata"" - LEFT OUTER JOIN ""Books"" ON ""Books"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" - LEFT OUTER JOIN ""Authors"" ON ""Authors"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" - WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""AuthorMetadata"" + WHERE ""Id"" IN ( + SELECT ""AuthorMetadata"".""Id"" FROM ""AuthorMetadata"" + LEFT OUTER JOIN ""Books"" ON ""Books"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" + LEFT OUTER JOIN ""Authors"" ON ""Authors"".""AuthorMetadataId"" = ""AuthorMetadata"".""Id"" + WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs index a4f670c22..306f3ed83 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""Blocklist"" - WHERE ""Id"" IN ( - SELECT ""Blocklist"".""Id"" FROM ""Blocklist"" - LEFT OUTER JOIN ""Authors"" - ON ""Blocklist"".""AuthorId"" = ""Authors"".""Id"" - WHERE ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""Blocklist"" + WHERE ""Id"" IN ( + SELECT ""Blocklist"".""Id"" FROM ""Blocklist"" + LEFT OUTER JOIN ""Authors"" + ON ""Blocklist"".""AuthorId"" = ""Authors"".""Id"" + WHERE ""Authors"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBookFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBookFiles.cs index e7a396be4..500c39911 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBookFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBookFiles.cs @@ -14,17 +14,16 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - // Unlink where track no longer exists - mapper.Execute(@"UPDATE ""BookFiles"" - SET ""EditionId"" = 0 - WHERE ""Id"" IN ( - SELECT ""BookFiles"".""Id"" FROM ""BookFiles"" - LEFT OUTER JOIN ""Editions"" - ON ""BookFiles"".""EditionId"" = ""Editions"".""Id"" - WHERE ""Editions"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + + // Unlink where books no longer exists + mapper.Execute(@"UPDATE ""BookFiles"" + SET ""EditionId"" = 0 + WHERE ""Id"" IN ( + SELECT ""BookFiles"".""Id"" FROM ""BookFiles"" + LEFT OUTER JOIN ""Editions"" + ON ""BookFiles"".""EditionId"" = ""Editions"".""Id"" + WHERE ""Editions"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBooks.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBooks.cs index 16ddb2696..e80983c66 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBooks.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBooks.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""Books"" - WHERE ""Id"" IN ( - SELECT ""Books"".""Id"" FROM ""Books"" - LEFT OUTER JOIN ""Authors"" - ON ""Books"".""AuthorMetadataId"" = ""Authors"".""AuthorMetadataId"" - WHERE ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""Books"" + WHERE ""Id"" IN ( + SELECT ""Books"".""Id"" FROM ""Books"" + LEFT OUTER JOIN ""Authors"" + ON ""Books"".""AuthorMetadataId"" = ""Authors"".""AuthorMetadataId"" + WHERE ""Authors"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedDownloadClientStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedDownloadClientStatus.cs index 436299ca6..67ce3ceb9 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedDownloadClientStatus.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedDownloadClientStatus.cs @@ -17,11 +17,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers using var mapper = _database.OpenConnection(); mapper.Execute(@"DELETE FROM ""DownloadClientStatus"" - WHERE ""Id"" IN ( - SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus"" - LEFT OUTER JOIN ""DownloadClients"" - ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id"" - WHERE ""DownloadClients"".""Id"" IS NULL)"); + WHERE ""Id"" IN ( + SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus"" + LEFT OUTER JOIN ""DownloadClients"" + ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id"" + WHERE ""DownloadClients"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEditions.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEditions.cs index af2ff7108..d9673b430 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEditions.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEditions.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""Editions"" - WHERE ""Id"" IN ( - SELECT ""Editions"".""Id"" FROM ""Editions"" - LEFT OUTER JOIN ""Books"" - ON ""Editions"".""BookId"" = ""Books"".""Id"" - WHERE ""Books"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""Editions"" + WHERE ""Id"" IN ( + SELECT ""Editions"".""Id"" FROM ""Editions"" + LEFT OUTER JOIN ""Books"" + ON ""Editions"".""BookId"" = ""Books"".""Id"" + WHERE ""Books"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs index 5b5d37b78..5f8ecf5eb 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs @@ -20,28 +20,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers private void CleanupOrphanedByAuthor() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""History"" - WHERE ""Id"" IN ( - SELECT ""History"".""Id"" FROM ""History"" - LEFT OUTER JOIN ""Authors"" - ON ""History"".""AuthorId"" = ""Authors"".""Id"" - WHERE ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""History"" + WHERE ""Id"" IN ( + SELECT ""History"".""Id"" FROM ""History"" + LEFT OUTER JOIN ""Authors"" + ON ""History"".""AuthorId"" = ""Authors"".""Id"" + WHERE ""Authors"".""Id"" IS NULL)"); } private void CleanupOrphanedByBook() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""History"" - WHERE ""Id"" IN ( - SELECT ""History"".""Id"" FROM ""History"" - LEFT OUTER JOIN ""Books"" - ON ""History"".""BookId"" = ""Books"".""Id"" - WHERE ""Books"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""History"" + WHERE ""Id"" IN ( + SELECT ""History"".""Id"" FROM ""History"" + LEFT OUTER JOIN ""Books"" + ON ""History"".""BookId"" = ""Books"".""Id"" + WHERE ""Books"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs index 1ceb0f396..8b624ff52 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""ImportListStatus"" - WHERE ""Id"" IN ( - SELECT ""ImportListStatus"".""Id"" FROM ""ImportListStatus"" - LEFT OUTER JOIN ""ImportLists"" - ON ""ImportListStatus"".""ProviderId"" = ""ImportLists"".""Id"" - WHERE ""ImportLists"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""ImportListStatus"" + WHERE ""Id"" IN ( + SELECT ""ImportListStatus"".""Id"" FROM ""ImportListStatus"" + LEFT OUTER JOIN ""ImportLists"" + ON ""ImportListStatus"".""ProviderId"" = ""ImportLists"".""Id"" + WHERE ""ImportLists"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs index 059f059e4..9486641b5 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""IndexerStatus"" - WHERE ""Id"" IN ( - SELECT ""IndexerStatus"".""Id"" FROM ""IndexerStatus"" - LEFT OUTER JOIN ""Indexers"" - ON ""IndexerStatus"".""ProviderId"" = ""Indexers"".""Id"" - WHERE ""Indexers"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""IndexerStatus"" + WHERE ""Id"" IN ( + SELECT ""IndexerStatus"".""Id"" FROM ""IndexerStatus"" + LEFT OUTER JOIN ""Indexers"" + ON ""IndexerStatus"".""ProviderId"" = ""Indexers"".""Id"" + WHERE ""Indexers"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs index f04533489..fcf10b802 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs @@ -23,67 +23,57 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers private void DeleteOrphanedByAuthor() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" - LEFT OUTER JOIN ""Authors"" - ON ""MetadataFiles"".""AuthorId"" = ""Authors"".""Id"" - WHERE ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" + LEFT OUTER JOIN ""Authors"" + ON ""MetadataFiles"".""AuthorId"" = ""Authors"".""Id"" + WHERE ""Authors"".""Id"" IS NULL)"); } private void DeleteOrphanedByBook() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" - LEFT OUTER JOIN ""Books"" - ON ""MetadataFiles"".""BookId"" = ""Books"".""Id"" - WHERE ""MetadataFiles"".""BookId"" > 0 - AND ""Books"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" + LEFT OUTER JOIN ""Books"" + ON ""MetadataFiles"".""BookId"" = ""Books"".""Id"" + WHERE ""MetadataFiles"".""BookId"" > 0 + AND ""Books"".""Id"" IS NULL)"); } private void DeleteOrphanedByTrackFile() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" - LEFT OUTER JOIN ""BookFiles"" - ON ""MetadataFiles"".""BookFileId"" = ""BookFiles"".""Id"" - WHERE ""MetadataFiles"".""BookFileId"" > 0 - AND ""BookFiles"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""MetadataFiles"".""Id"" FROM ""MetadataFiles"" + LEFT OUTER JOIN ""BookFiles"" + ON ""MetadataFiles"".""BookFileId"" = ""BookFiles"".""Id"" + WHERE ""MetadataFiles"".""BookFileId"" > 0 + AND ""BookFiles"".""Id"" IS NULL)"); } private void DeleteWhereBookIdIsZero() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""Id"" FROM ""MetadataFiles"" - WHERE ""Type"" IN (2, 4) - AND ""BookId"" = 0)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""Id"" FROM ""MetadataFiles"" + WHERE ""Type"" IN (2, 4) + AND ""BookId"" = 0)"); } private void DeleteWhereTrackFileIsZero() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""MetadataFiles"" - WHERE ""Id"" IN ( - SELECT ""Id"" FROM ""MetadataFiles"" - WHERE ""Type"" IN (2, 4) - AND ""BookFileId"" = 0)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""MetadataFiles"" + WHERE ""Id"" IN ( + SELECT ""Id"" FROM ""MetadataFiles"" + WHERE ""Type"" IN (2, 4) + AND ""BookFileId"" = 0)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs index 2ae415117..7cdf91fe3 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""PendingReleases"" - WHERE ""Id"" IN ( - SELECT ""PendingReleases"".""Id"" FROM ""PendingReleases"" - LEFT OUTER JOIN ""Authors"" - ON ""PendingReleases"".""AuthorId"" = ""Authors"".""Id"" - WHERE ""Authors"".""Id"" IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""PendingReleases"" + WHERE ""Id"" IN ( + SELECT ""PendingReleases"".""Id"" FROM ""PendingReleases"" + LEFT OUTER JOIN ""Authors"" + ON ""PendingReleases"".""AuthorId"" = ""Authors"".""Id"" + WHERE ""Authors"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedSeriesBookLinks.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedSeriesBookLinks.cs index 54edfb570..3f47eac0f 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedSeriesBookLinks.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedSeriesBookLinks.cs @@ -14,22 +14,20 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM ""SeriesBookLink"" - WHERE ""Id"" IN ( - SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" - LEFT OUTER JOIN ""Books"" - ON ""SeriesBookLink"".""BookId"" = ""Books"".""Id"" - WHERE ""Books"".""Id"" IS NULL)"); + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM ""SeriesBookLink"" + WHERE ""Id"" IN ( + SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" + LEFT OUTER JOIN ""Books"" + ON ""SeriesBookLink"".""BookId"" = ""Books"".""Id"" + WHERE ""Books"".""Id"" IS NULL)"); - mapper.Execute(@"DELETE FROM ""SeriesBookLink"" - WHERE ""Id"" IN ( - SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" - LEFT OUTER JOIN ""Series"" - ON ""SeriesBookLink"".""SeriesId"" = ""Series"".""Id"" - WHERE ""Series"".""Id"" IS NULL)"); - } + mapper.Execute(@"DELETE FROM ""SeriesBookLink"" + WHERE ""Id"" IN ( + SELECT ""SeriesBookLink"".""Id"" FROM ""SeriesBookLink"" + LEFT OUTER JOIN ""Series"" + ON ""SeriesBookLink"".""SeriesId"" = ""Series"".""Id"" + WHERE ""Series"".""Id"" IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs index d45a6d139..932ddb569 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs @@ -18,31 +18,29 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers 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" } - .SelectMany(v => GetUsedTags(v, mapper)) - .Distinct() - .ToArray(); + var usedTagsList = usedTags.Select(d => d.ToString()).Join(","); - if (usedTags.Any()) + if (_database.DatabaseType == DatabaseType.PostgreSQL) { - var usedTagsList = usedTags.Select(d => d.ToString()).Join(","); - - 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})"); - } + mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])"); } 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) diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs index 47333e596..afb80baf2 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/FixFutureRunScheduledTasks.cs @@ -24,13 +24,11 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers _logger.Debug("Not running scheduled task last execution cleanup during debug"); } - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"UPDATE ""ScheduledTasks"" - SET ""LastExecution"" = @time - WHERE ""LastExecution"" > @time", - new { time = DateTime.UtcNow }); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"UPDATE ""ScheduledTasks"" + SET ""LastExecution"" = @time + WHERE ""LastExecution"" > @time", + new { time = DateTime.UtcNow }); } } }