Convert to 'using' declaration in Housekeeping Tasks

This commit is contained in:
Bogdan 2023-06-16 02:45:13 +03:00 committed by Mark McDowall
parent 9af6d1b9d9
commit 82919a8ed1
14 changed files with 134 additions and 172 deletions

View File

@ -14,19 +14,17 @@ 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 MetadataFiles
mapper.Execute(@"DELETE FROM MetadataFiles WHERE Id IN (
WHERE Id IN ( SELECT Id FROM MetadataFiles
SELECT Id FROM MetadataFiles WHERE RelativePath
WHERE RelativePath LIKE '_:\%'
LIKE '_:\%' OR RelativePath
OR RelativePath LIKE '\%'
LIKE '\%' OR RelativePath
OR RelativePath LIKE '/%'
LIKE '/%' )");
)");
}
} }
} }
} }

View File

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

View File

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

View File

@ -21,44 +21,38 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteDuplicateSeriesMetadata() private void DeleteDuplicateSeriesMetadata()
{ {
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 = 1
WHERE Type = 1 GROUP BY SeriesId, Consumer
GROUP BY SeriesId, Consumer HAVING COUNT(SeriesId) > 1
HAVING COUNT(SeriesId) > 1 )");
)");
}
} }
private void DeleteDuplicateEpisodeMetadata() private void DeleteDuplicateEpisodeMetadata()
{ {
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 = 2
WHERE Type = 2 GROUP BY EpisodeFileId, Consumer
GROUP BY EpisodeFileId, Consumer HAVING COUNT(EpisodeFileId) > 1
HAVING COUNT(EpisodeFileId) > 1 )");
)");
}
} }
private void DeleteDuplicateEpisodeImages() private void DeleteDuplicateEpisodeImages()
{ {
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 = 5
WHERE Type = 5 GROUP BY EpisodeFileId, Consumer
GROUP BY EpisodeFileId, Consumer HAVING COUNT(EpisodeFileId) > 1
HAVING COUNT(EpisodeFileId) > 1 )");
)");
}
} }
} }
} }

View File

@ -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 Series
LEFT OUTER JOIN Series ON Blocklist.SeriesId = Series.Id
ON Blocklist.SeriesId = Series.Id WHERE Series.Id IS NULL)");
WHERE Series.Id IS NULL)");
}
} }
} }
} }

View File

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

View File

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

View File

@ -20,28 +20,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void CleanupOrphanedBySeries() private void CleanupOrphanedBySeries()
{ {
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 Series
LEFT OUTER JOIN Series ON History.SeriesId = Series.Id
ON History.SeriesId = Series.Id WHERE Series.Id IS NULL)");
WHERE Series.Id IS NULL)");
}
} }
private void CleanupOrphanedByEpisode() private void CleanupOrphanedByEpisode()
{ {
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 Episodes
LEFT OUTER JOIN Episodes ON History.EpisodeId = Episodes.Id
ON History.EpisodeId = Episodes.Id WHERE Episodes.Id IS NULL)");
WHERE Episodes.Id IS NULL)");
}
} }
} }
} }

View File

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

View File

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

View File

@ -21,41 +21,35 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private void DeleteOrphanedBySeries() private void DeleteOrphanedBySeries()
{ {
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 Series
LEFT OUTER JOIN Series ON MetadataFiles.SeriesId = Series.Id
ON MetadataFiles.SeriesId = Series.Id WHERE Series.Id IS NULL)");
WHERE Series.Id IS NULL)");
}
} }
private void DeleteOrphanedByEpisodeFile() private void DeleteOrphanedByEpisodeFile()
{ {
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 EpisodeFiles
LEFT OUTER JOIN EpisodeFiles ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id
ON MetadataFiles.EpisodeFileId = EpisodeFiles.Id WHERE MetadataFiles.EpisodeFileId > 0
WHERE MetadataFiles.EpisodeFileId > 0 AND EpisodeFiles.Id IS NULL)");
AND EpisodeFiles.Id IS NULL)");
}
} }
private void DeleteWhereEpisodeFileIsZero() private void DeleteWhereEpisodeFileIsZero()
{ {
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, 5)
WHERE Type IN (2, 5) AND EpisodeFileId = 0)");
AND EpisodeFileId = 0)");
}
} }
} }
} }

View File

@ -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 Series
LEFT OUTER JOIN Series ON PendingReleases.SeriesId = Series.Id
ON PendingReleases.SeriesId = Series.Id WHERE Series.Id IS NULL)");
WHERE Series.Id IS NULL)");
}
} }
} }
} }

View File

@ -17,17 +17,15 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public void Clean() public void Clean()
{ {
using (var mapper = _database.OpenConnection()) using var mapper = _database.OpenConnection();
{ var usedTags = new[] { "Series", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging" }
var usedTags = new[] { "Series", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging" } .SelectMany(v => GetUsedTags(v, mapper))
.SelectMany(v => GetUsedTags(v, mapper)) .Distinct()
.Distinct() .ToArray();
.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) private int[] GetUsedTags(string table, IDbConnection mapper)

View File

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