From d6a4d03cdb2dfd4d3cae61df180f876927edd17d Mon Sep 17 00:00:00 2001 From: Matt Darnall Date: Mon, 27 Jul 2020 12:26:00 -0700 Subject: [PATCH] Add initializer that takes database Connection Because: - It will allow consumers to further configure the Connection in their application and pass it in - See: https://github.com/apollographql/apollo-ios/pull/1162/files#r410367735 This commit: - Adds a new initializer to SQLNormalizedCache --- Sources/ApolloSQLite/SQLiteNormalizedCache.swift | 12 ++++++++++++ Tests/ApolloSQLiteTests/CachePersistenceTests.swift | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index 1633f04aa2..6d5a8a70b9 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -31,6 +31,18 @@ public final class SQLiteNormalizedCache { try self.createTableIfNeeded() } + /// + /// Initializer that takes the Connection to use + /// - Parameters: + /// - db: The database Connection to use + /// - shouldVacuumOnClear: If the database should also be `VACCUM`ed on clear to remove all traces of info. Defaults to `false` since this involves a performance hit, but this should be used if you are storing any Personally Identifiable Information in the cache. + /// - Throws: Any errors attempting to access the database + public init(db: Connection, shouldVacuumOnClear: Bool = false) throws { + self.shouldVacuumOnClear = shouldVacuumOnClear + self.db = db + try self.createTableIfNeeded() + } + private func recordCacheKey(forFieldCacheKey fieldCacheKey: CacheKey) -> CacheKey { let components = fieldCacheKey.components(separatedBy: ".") var updatedComponents = [String]() diff --git a/Tests/ApolloSQLiteTests/CachePersistenceTests.swift b/Tests/ApolloSQLiteTests/CachePersistenceTests.swift index 12ae394435..bdf5422b76 100644 --- a/Tests/ApolloSQLiteTests/CachePersistenceTests.swift +++ b/Tests/ApolloSQLiteTests/CachePersistenceTests.swift @@ -4,6 +4,7 @@ import XCTest import ApolloTestSupport import ApolloSQLiteTestSupport import StarWarsAPI +import SQLite class CachePersistenceTests: XCTestCase { @@ -59,6 +60,10 @@ class CachePersistenceTests: XCTestCase { } } + func testPassInConnectionDoesNotThrow() { + XCTAssertNoThrow(try SQLiteNormalizedCache(db: Connection())) + } + func testClearCache() { let query = HeroNameQuery() let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()