Convert to 'using' declaration in Housekeeping Tasks

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

@ -14,8 +14,7 @@ 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"" mapper.Execute(@"DELETE FROM ""MetadataFiles""
@ -44,5 +43,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
} }
} }
} }
}
} }

@ -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,8 +21,7 @@ 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""
@ -31,12 +30,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -45,12 +42,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -60,5 +55,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
)"); )");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""Id"" IS NULL)"); WHERE ""Books"".""Id"" IS NULL AND ""Authors"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Authors"".""Id"" IS NULL)"); WHERE ""Authors"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,9 +14,9 @@ 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 (
@ -26,5 +26,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Editions"".""Id"" IS NULL)"); WHERE ""Editions"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Authors"".""Id"" IS NULL)"); WHERE ""Authors"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Books"".""Id"" IS NULL)"); WHERE ""Books"".""Id"" IS NULL)");
} }
} }
}
} }

@ -20,8 +20,7 @@ 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""
@ -29,12 +28,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -43,5 +40,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Books"".""Id"" IS NULL)"); WHERE ""Books"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""ImportLists"".""Id"" IS NULL)"); WHERE ""ImportLists"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Indexers"".""Id"" IS NULL)"); WHERE ""Indexers"".""Id"" IS NULL)");
} }
} }
}
} }

@ -23,8 +23,7 @@ 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""
@ -32,12 +31,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -46,12 +43,10 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -60,24 +55,20 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
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""
@ -85,5 +76,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
AND ""BookFileId"" = 0)"); AND ""BookFileId"" = 0)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -24,5 +23,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Authors"".""Id"" IS NULL)"); WHERE ""Authors"".""Id"" IS NULL)");
} }
} }
}
} }

@ -14,8 +14,7 @@ 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""
@ -31,5 +30,4 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE ""Series"".""Id"" IS NULL)"); WHERE ""Series"".""Id"" IS NULL)");
} }
} }
}
} }

@ -18,8 +18,7 @@ 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" } var usedTags = new[] { "Authors", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers" }
.SelectMany(v => GetUsedTags(v, mapper)) .SelectMany(v => GetUsedTags(v, mapper))
.Distinct() .Distinct()
@ -43,7 +42,6 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
mapper.Execute("DELETE FROM \"Tags\""); 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