From 82919a8ed17b0da70514c9339f5efd8197783a17 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 16 Jun 2023 02:45:13 +0300 Subject: [PATCH] Convert to 'using' declaration in Housekeeping Tasks --- .../CleanupAbsolutePathMetadataFiles.cs | 24 ++++----- .../CleanupAdditionalNamingSpecs.cs | 12 ++--- .../Housekeepers/CleanupAdditionalUsers.cs | 12 ++--- .../CleanupDuplicateMetadataFiles.cs | 54 +++++++++---------- .../Housekeepers/CleanupOrphanedBlocklist.cs | 16 +++--- .../CleanupOrphanedEpisodeFiles.cs | 16 +++--- .../Housekeepers/CleanupOrphanedEpisodes.cs | 16 +++--- .../CleanupOrphanedHistoryItems.cs | 32 +++++------ .../CleanupOrphanedImportListStatus.cs | 16 +++--- .../CleanupOrphanedIndexerStatus.cs | 16 +++--- .../CleanupOrphanedMetadataFiles.cs | 48 ++++++++--------- .../CleanupOrphanedPendingReleases.cs | 16 +++--- .../Housekeepers/CleanupUnusedTags.cs | 16 +++--- .../FixFutureRunScheduledTasks.cs | 12 ++--- 14 files changed, 134 insertions(+), 172 deletions(-) diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs index 1293d4f07..8acce195f 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupAbsolutePathMetadataFiles.cs @@ -14,19 +14,17 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT Id FROM MetadataFiles - WHERE RelativePath - LIKE '_:\%' - OR RelativePath - LIKE '\%' - OR RelativePath - LIKE '/%' - )"); - } + using var mapper = _database.OpenConnection(); + 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 a1d9c56b7..d0803292a 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 4460ac83c..356105d01 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/CleanupDuplicateMetadataFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupDuplicateMetadataFiles.cs index 5aafdce15..1ac1504b0 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 DeleteDuplicateSeriesMetadata() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT Id FROM MetadataFiles - WHERE Type = 1 - GROUP BY SeriesId, Consumer - HAVING COUNT(SeriesId) > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT Id FROM MetadataFiles + WHERE Type = 1 + GROUP BY SeriesId, Consumer + HAVING COUNT(SeriesId) > 1 + )"); } private void DeleteDuplicateEpisodeMetadata() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT Id FROM MetadataFiles - WHERE Type = 2 - GROUP BY EpisodeFileId, Consumer - HAVING COUNT(EpisodeFileId) > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT Id FROM MetadataFiles + WHERE Type = 2 + GROUP BY EpisodeFileId, Consumer + HAVING COUNT(EpisodeFileId) > 1 + )"); } private void DeleteDuplicateEpisodeImages() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT Id FROM MetadataFiles - WHERE Type = 5 - GROUP BY EpisodeFileId, Consumer - HAVING COUNT(EpisodeFileId) > 1 - )"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT Id FROM MetadataFiles + WHERE Type = 5 + GROUP BY EpisodeFileId, Consumer + HAVING COUNT(EpisodeFileId) > 1 + )"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedBlocklist.cs index 6dd21295a..fa9b9767d 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 Series - ON Blocklist.SeriesId = Series.Id - WHERE Series.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM Blocklist + WHERE Id IN ( + SELECT Blocklist.Id FROM Blocklist + LEFT OUTER JOIN Series + ON Blocklist.SeriesId = Series.Id + WHERE Series.Id IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodeFiles.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodeFiles.cs index c7d9be03a..d90d3e491 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodeFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodeFiles.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM EpisodeFiles - WHERE Id IN ( - SELECT EpisodeFiles.Id FROM EpisodeFiles - LEFT OUTER JOIN Episodes - ON EpisodeFiles.Id = Episodes.EpisodeFileId - WHERE Episodes.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM EpisodeFiles + WHERE Id IN ( + SELECT EpisodeFiles.Id FROM EpisodeFiles + LEFT OUTER JOIN Episodes + ON EpisodeFiles.Id = Episodes.EpisodeFileId + WHERE Episodes.Id IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs index d96bb4e25..4cf39c4ab 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs @@ -14,15 +14,13 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"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)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"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)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs index ce8559943..c4b4dd786 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 CleanupOrphanedBySeries() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM History - WHERE Id IN ( - SELECT History.Id FROM History - LEFT OUTER JOIN Series - ON History.SeriesId = Series.Id - WHERE Series.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM History + WHERE Id IN ( + SELECT History.Id FROM History + LEFT OUTER JOIN Series + ON History.SeriesId = Series.Id + WHERE Series.Id IS NULL)"); } private void CleanupOrphanedByEpisode() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM History - WHERE Id IN ( - SELECT History.Id FROM History - LEFT OUTER JOIN Episodes - ON History.EpisodeId = Episodes.Id - WHERE Episodes.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM History + WHERE Id IN ( + SELECT History.Id FROM History + LEFT OUTER JOIN Episodes + ON History.EpisodeId = Episodes.Id + WHERE Episodes.Id IS NULL)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedImportListStatus.cs index f40ec672f..606e87390 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 7347792d3..d70fcd360 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 90a99cb24..d1db9bbac 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedMetadataFiles.cs @@ -21,41 +21,35 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers private void DeleteOrphanedBySeries() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT MetadataFiles.Id FROM MetadataFiles - LEFT OUTER JOIN Series - ON MetadataFiles.SeriesId = Series.Id - WHERE Series.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT MetadataFiles.Id FROM MetadataFiles + LEFT OUTER JOIN Series + ON MetadataFiles.SeriesId = Series.Id + WHERE Series.Id IS NULL)"); } private void DeleteOrphanedByEpisodeFile() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT MetadataFiles.Id FROM MetadataFiles - LEFT OUTER JOIN EpisodeFiles - ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id - WHERE MetadataFiles.EpisodeFileId > 0 - AND EpisodeFiles.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT MetadataFiles.Id FROM MetadataFiles + LEFT OUTER JOIN EpisodeFiles + ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id + WHERE MetadataFiles.EpisodeFileId > 0 + AND EpisodeFiles.Id IS NULL)"); } private void DeleteWhereEpisodeFileIsZero() { - using (var mapper = _database.OpenConnection()) - { - mapper.Execute(@"DELETE FROM MetadataFiles - WHERE Id IN ( - SELECT Id FROM MetadataFiles - WHERE Type IN (2, 5) - AND EpisodeFileId = 0)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM MetadataFiles + WHERE Id IN ( + SELECT Id FROM MetadataFiles + WHERE Type IN (2, 5) + AND EpisodeFileId = 0)"); } } } diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedPendingReleases.cs index 812eae52f..de1c171da 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 Series - ON PendingReleases.SeriesId = Series.Id - WHERE Series.Id IS NULL)"); - } + using var mapper = _database.OpenConnection(); + mapper.Execute(@"DELETE FROM PendingReleases + WHERE Id IN ( + SELECT PendingReleases.Id FROM PendingReleases + LEFT OUTER JOIN Series + ON PendingReleases.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 1f5aac28a..5a548c786 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupUnusedTags.cs @@ -17,17 +17,15 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers public void Clean() { - using (var mapper = _database.OpenConnection()) - { - var usedTags = new[] { "Series", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging" } - .SelectMany(v => GetUsedTags(v, mapper)) - .Distinct() - .ToArray(); + using var mapper = _database.OpenConnection(); + var usedTags = new[] { "Series", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging" } + .SelectMany(v => GetUsedTags(v, mapper)) + .Distinct() + .ToArray(); - var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray()); + var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray()); - mapper.Execute($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})"); - } + mapper.Execute($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})"); } 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 82a7af7fa..d4047cfc3 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 }); } } }