diff --git a/main/DB/DBCommon.swift b/main/DB/DBCommon.swift index 8e7d6bc..047eded 100644 --- a/main/DB/DBCommon.swift +++ b/main/DB/DBCommon.swift @@ -48,11 +48,16 @@ extension SQLiteDatabase { /// - Returns: `true` if at least one row was deleted. @discardableResult func dnsLogsDeleteOlderThan(days: Int) throws -> Bool { guard days > 0 else { return false } - return try self.run(sql: "DELETE FROM cache WHERE ts < strftime('%s', 'now', ?);", - bind: [BindText("-\(days) days")]) { - try ifStep($0, SQLITE_DONE) - return numberOfChanges > 0 + func delFrom(_ table: String) throws -> Bool { + return try self.run(sql: "DELETE FROM \(table) WHERE ts < strftime('%s', 'now', ?);", + bind: [BindText("-\(days) days")]) { + try ifStep($0, SQLITE_DONE) + return numberOfChanges > 0 + } } + let didDelHeap = try delFrom("heap") + let didDelCache = try delFrom("cache") + return didDelHeap || didDelCache } } diff --git a/main/DB/TheGreatDestroyer.swift b/main/DB/TheGreatDestroyer.swift index 156fcd1..86434d6 100644 --- a/main/DB/TheGreatDestroyer.swift +++ b/main/DB/TheGreatDestroyer.swift @@ -34,10 +34,13 @@ struct TheGreatDestroyer { DispatchQueue.global().async { defer { sync.continue() } QLog.Info("Auto-delete logs") - guard let success = try? AppDB?.dnsLogsDeleteOlderThan(days: days), success else { - return // nothing changed + do { + if try AppDB!.dnsLogsDeleteOlderThan(days: days) { + sync.needsReloadDB() + } + } catch { + QLog.Warning("Couldn't auto-delete logs, \(error)") } - sync.needsReloadDB() } } }