diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index a6e0c8ea8..f62751351 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -202,8 +202,8 @@ 56894F752606576600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F742606576600268F4D /* FoundationDecimalTests.swift */; }; 56894FB72606589700268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F94260657D600268F4D /* Decimal.swift */; }; 568D131F2207213E00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; }; - 568EB71929211E0800E59445 /* WALSnapshotToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71829211E0700E59445 /* WALSnapshotToken.swift */; }; - 568EB71E2921234800E59445 /* WALSnapshotTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71D2921234800E59445 /* WALSnapshotTokenTests.swift */; }; + 568EB71929211E0800E59445 /* DatabaseSnapshotPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71829211E0700E59445 /* DatabaseSnapshotPool.swift */; }; + 568EB71E2921234800E59445 /* DatabaseSnapshotPoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71D2921234800E59445 /* DatabaseSnapshotPoolTests.swift */; }; 568ECA8A25D7013000B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECA8925D7013000B71526 /* SQLSelection.swift */; }; 568ECA9F25D7E53E00B71526 /* SQLOrdering.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECA9E25D7E53E00B71526 /* SQLOrdering.swift */; }; 568ECB1825D9161600B71526 /* SQLSubquery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB1725D9161500B71526 /* SQLSubquery.swift */; }; @@ -618,8 +618,8 @@ 56894F742606576600268F4D /* FoundationDecimalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDecimalTests.swift; sourceTree = ""; }; 56894F94260657D600268F4D /* Decimal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = ""; }; 568D13182207213E00674B58 /* SQLQueryGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLQueryGenerator.swift; sourceTree = ""; }; - 568EB71829211E0700E59445 /* WALSnapshotToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WALSnapshotToken.swift; sourceTree = ""; }; - 568EB71D2921234800E59445 /* WALSnapshotTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WALSnapshotTokenTests.swift; sourceTree = ""; }; + 568EB71829211E0700E59445 /* DatabaseSnapshotPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseSnapshotPool.swift; sourceTree = ""; }; + 568EB71D2921234800E59445 /* DatabaseSnapshotPoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseSnapshotPoolTests.swift; sourceTree = ""; }; 568ECA8925D7013000B71526 /* SQLSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSelection.swift; sourceTree = ""; }; 568ECA9E25D7E53E00B71526 /* SQLOrdering.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLOrdering.swift; sourceTree = ""; }; 568ECB1725D9161500B71526 /* SQLSubquery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSubquery.swift; sourceTree = ""; }; @@ -1338,6 +1338,7 @@ 56EA86931C91DFE7002BB4DF /* DatabaseReaderTests.swift */, 564CE5BD21B8FFA300652B19 /* DatabaseRegionObservationTests.swift */, 56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */, + 568EB71D2921234800E59445 /* DatabaseSnapshotPoolTests.swift */, 566A8424204120B700E50BFD /* DatabaseSnapshotTests.swift */, 5682D71A239582AA004B58C4 /* DatabaseSuspensionTests.swift */, 56A238131B9C74A90082EB20 /* DatabaseTests.swift */, @@ -1360,7 +1361,6 @@ 56A238201B9C74A90082EB20 /* Statement */, 56E8CE0F1BB4FE5B00828BEC /* StatementColumnConvertibleFetchTests.swift */, 5607EFD11BB8253300605DE3 /* TransactionObserver */, - 568EB71D2921234800E59445 /* WALSnapshotTokenTests.swift */, ); name = Core; sourceTree = ""; @@ -1443,6 +1443,7 @@ 564CE5AB21B8FAB400652B19 /* DatabaseRegionObservation.swift */, 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */, 566A84192041146100E50BFD /* DatabaseSnapshot.swift */, + 568EB71829211E0700E59445 /* DatabaseSnapshotPool.swift */, 56A238751B9C75030082EB20 /* DatabaseValue.swift */, 560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */, 563363C31C942C37000BE133 /* DatabaseWriter.swift */, @@ -1460,7 +1461,6 @@ 560D923F1C672C3E00F4F92B /* StatementColumnConvertible.swift */, 566B91321FA4D3810012D5B0 /* TransactionObserver.swift */, 56B7EE822863781300C0525F /* WALSnapshot.swift */, - 568EB71829211E0700E59445 /* WALSnapshotToken.swift */, 5605F1471C672E4000235C62 /* Support */, ); path = Core; @@ -1791,7 +1791,7 @@ 56D496B01D813385008276D7 /* DatabaseErrorTests.swift in Sources */, 56176C5C1EACCCC7000F3F2B /* FTS5TableBuilderTests.swift in Sources */, 562206061E420EA4005860AC /* DatabasePoolBackupTests.swift in Sources */, - 568EB71E2921234800E59445 /* WALSnapshotTokenTests.swift in Sources */, + 568EB71E2921234800E59445 /* DatabaseSnapshotPoolTests.swift in Sources */, 56D496B51D813413008276D7 /* DatabaseCollationTests.swift in Sources */, 563DE4F3231A91E2005081B7 /* DatabaseConfigurationTests.swift in Sources */, 56BF2282241781C5003D86EB /* UtilsTests.swift in Sources */, @@ -2042,7 +2042,7 @@ 566B91131FA4C3F50012D5B0 /* DatabaseCollation.swift in Sources */, 5605F1591C672E4000235C62 /* CGFloat.swift in Sources */, 5674A7031F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift in Sources */, - 568EB71929211E0800E59445 /* WALSnapshotToken.swift in Sources */, + 568EB71929211E0800E59445 /* DatabaseSnapshotPool.swift in Sources */, 5605F1631C672E4000235C62 /* Date.swift in Sources */, 5605F1711C672E4000235C62 /* DatabaseValueConvertible+RawRepresentable.swift in Sources */, 56A2387B1B9C75030082EB20 /* Configuration.swift in Sources */, diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 7f9378746..be8503d6c 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -54,15 +54,10 @@ import UIKit /// - ``asyncConcurrentRead(_:)`` /// - ``writeInTransaction(_:_:)`` /// -/// ### Accessing WAL Snapshots +/// ### Creating Database Snapshots /// /// - ``makeSnapshot()`` -/// - ``currentSnapshotToken()`` -/// - ``read(from:_:)-7wtr2`` -/// - ``read(from:_:)-8bwfe`` -/// - ``readPublisher(from:receiveOn:value:)`` -/// - ``asyncRead(from:_:)`` -/// - ``WALSnapshotToken`` +/// - ``makeSnapshotPool()`` /// /// ### Managing SQLite Connections /// @@ -483,7 +478,6 @@ extension DatabasePool: DatabaseReader { releaseReader() } do { - // The block isolation comes from the DEFERRED transaction. try db.clearSchemaCacheIfNeeded() value(.success(db)) } catch { @@ -836,7 +830,7 @@ extension DatabasePool { // MARK: - Snapshots - /// Creates a database snapshot. + /// Creates an independent database snapshot. /// /// The returned snapshot sees an unchanging database content, as it existed /// at the moment it was created. diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index ae0f3663a..f59fd93ba 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -224,8 +224,8 @@ public protocol DatabaseReader: AnyObject, Sendable { /// access method. Doing so raises a "Database methods are not reentrant" /// fatal error at runtime. /// - /// - warning: Database operations are not wrapped in a transaction. They - /// can see changes performed by concurrent writes or writes performed by + /// - warning: Database operations may not be wrapped in a transaction. They + /// may see changes performed by concurrent writes or writes performed by /// other processes: two identical requests performed by the `value` /// closure may not return the same value. /// - warning: Attempts to write in the database may succeed. @@ -256,8 +256,8 @@ public protocol DatabaseReader: AnyObject, Sendable { /// } /// ``` /// - /// - warning: Database operations are not wrapped in a transaction. They - /// can see changes performed by concurrent writes or writes performed by + /// - warning: Database operations may not be wrapped in a transaction. They + /// may see changes performed by concurrent writes or writes performed by /// other processes: two identical requests performed by the `value` /// closure may not return the same value. /// - warning: Attempts to write in the database may succeed. @@ -293,8 +293,8 @@ public protocol DatabaseReader: AnyObject, Sendable { /// of the closure. Do not store or return the database connection for /// later use. /// - /// - warning: Database operations are not wrapped in a transaction. They - /// can see changes performed by concurrent writes or writes performed by + /// - warning: Database operations may not be wrapped in a transaction. They + /// may see changes performed by concurrent writes or writes performed by /// other processes: two identical requests performed by the `value` /// closure may not return the same value. /// - warning: Attempts to write in the database may succeed. @@ -467,8 +467,8 @@ extension DatabaseReader { /// of the closure. Do not store or return the database connection for /// later use. /// - /// - warning: Database operations are not wrapped in a transaction. They - /// can see changes performed by concurrent writes or writes performed by + /// - warning: Database operations may not be wrapped in a transaction. They + /// may see changes performed by concurrent writes or writes performed by /// other processes: two identical requests performed by the `value` /// closure may not return the same value. /// - warning: Attempts to write in the database may succeed. diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 9dfb2250c..997bb10e5 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -1,23 +1,25 @@ import Dispatch -/// A database connection that sees an unchanging database content, as it -/// existed at the moment it was created. +/// A database connection that serializes accesses to an unchanging +/// database content, as it existed at the moment the snapshot was created. /// /// ## Overview /// -/// A `DatabaseSnapshot` creates one single SQLite connection. All database +/// A `DatabaseSnapshot` never sees any database modification during all its +/// lifetime. All database accesses performed from a snapshot always see the +/// same identical database content. +/// +/// A snapshot creates one single SQLite connection. All database /// accesses are executed in a serial **reader dispatch queue**. The SQLite /// connection is closed when the `DatabaseSnapshot` is deallocated. /// -/// A snapshot never sees any database modification during all its lifetime. All -/// database accesses performed from a snapshot always see the same identical -/// database content. +/// A snapshot created on a [WAL](https://sqlite.org/wal.html) database doesn't +/// prevent database modifications performed by other connections (but it won't +/// see them). Refer to [Isolation In SQLite](https://sqlite.org/isolation.html) +/// for more information. /// -/// In the [WAL mode](https://sqlite.org/wal.html), a snapshot doesn't prevent -/// database modifications performed by other connections (but it won't see -/// them). Refer to [Isolation In SQLite](https://sqlite.org/isolation.html) for -/// more information. Otherwise, a snapshot prevents all writes to the database -/// as long as it exists, due to the +/// On non-WAL databases, a snapshot prevents all database modifications as long +/// as it exists, because of the /// [SHARED lock](https://www.sqlite.org/lockingv3.html) it holds. /// /// ## Usage @@ -67,7 +69,7 @@ import Dispatch /// ``DatabaseReader`` protocols. /// /// `DatabaseSnapshot` serializes database accesses and can't perform concurrent -/// reads. For concurrent reads, see ``WALSnapshotToken``. +/// reads. For concurrent reads, see ``DatabaseSnapshotPool``. public final class DatabaseSnapshot { private let reader: SerializedDatabase diff --git a/GRDB/Core/DatabaseSnapshotPool.swift b/GRDB/Core/DatabaseSnapshotPool.swift new file mode 100644 index 000000000..bf389a2bc --- /dev/null +++ b/GRDB/Core/DatabaseSnapshotPool.swift @@ -0,0 +1,253 @@ +// swiftlint:disable:next line_length +#if (compiler(<5.7.1) && (os(macOS) || targetEnvironment(macCatalyst))) || GRDBCIPHER || (GRDBCUSTOMSQLITE && !SQLITE_ENABLE_SNAPSHOT) +#else + +#if canImport(Combine) +import Combine +import Dispatch +#endif + +/// A database connection that allows concurrent accesses to an unchanging +/// database content, as it existed at the moment the snapshot was created. +/// +/// ## Overview +/// +/// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) +/// +/// A `DatabaseSnapshotPool` never sees any database modification during all its +/// lifetime. All database accesses performed from a snapshot always see the +/// same identical database content. +/// +/// A `DatabaseSnapshotPool` instance shares the same pool of reader SQLite +/// connections as a ``DatabasePool``. It requires an SQLite database in the +/// [WAL mode](https://www.sqlite.org/wal.html). +/// +/// ## Usage +/// +/// You create a `DatabaseSnapshotPool` from a ``DatabasePool``, +/// with ``DatabasePool/makeSnapshotPool()``: +/// +/// ```swift +/// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") +/// let snapshot = try dbPool.makeSnapshotPool() +/// let playerCount = try snapshot.read { db in +/// try Player.fetchCount(db) +/// } +/// ``` +/// +/// When you want to control the database state seen by a snapshot, create the +/// snapshot from within a write access, outside of any transaction. +/// +/// For example, compare the two snapshots below. The first one is guaranteed to +/// see an empty table of players, because is is created after all players have +/// been deleted, and from the serialized writer dispatch queue which prevents +/// any concurrent write. The second is created without this concurrency +/// protection, which means that some other threads may already have created +/// some players: +/// +/// ```swift +/// let snapshot1 = try dbPool.writeWithoutTransaction { db -> DatabaseSnapshot in +/// try db.inTransaction { +/// try Player.deleteAll() +/// return .commit +/// } +/// +/// return dbPool.makeSnapshotPool() +/// } +/// +/// // <- Other threads may have created some players here +/// let snapshot2 = try dbPool.makeSnapshotPool() +/// +/// // Guaranteed to be zero +/// let count1 = try snapshot1.read(Player.fetchCount) +/// +/// // Could be anything +/// let count2 = try snapshot2.read(Player.fetchCount) +/// ``` +/// +/// `DatabaseSnapshotPool` inherits its database access methods from the +/// ``DatabaseReader`` protocols. +/// +/// Related SQLite documentation: +/// +/// - +/// - +public final class DatabaseSnapshotPool { + /// The DatabasePool that provides reader connections. + let dbPool: DatabasePool + + /// The connection that holds a transaction and prevents checkpointing in + /// order to keep `walSnapshot` valid. + let dbSnapshot: DatabaseSnapshot + + /// The WAL snapshot + let walSnapshot: WALSnapshot + + /// The version of the schema cache. + let schemaVersion: Int32 + + // Protected by a lock because the cache can be used by multiple + // database connections. + @LockedBox var schemaCache: Database.SchemaCache + + init( + dbPool: DatabasePool, + dbSnapshot: DatabaseSnapshot, + walSnapshot: WALSnapshot, + schemaVersion: Int32, + schemaCache: Database.SchemaCache) + { + self.dbPool = dbPool + self.dbSnapshot = dbSnapshot + self.walSnapshot = walSnapshot + self.schemaVersion = schemaVersion + self.schemaCache = schemaCache + } +} + +extension DatabaseSnapshotPool: @unchecked Sendable { } + +extension DatabaseSnapshotPool: DatabaseReader { + public var configuration: Configuration { + DatabasePool.readerConfiguration(dbPool.configuration) + } + + /// Throws a ``DatabaseError`` of code `SQLITE_MISUSE`. Close the source + /// ``DatabasePool`` instead. + public func close() throws { + throw DatabaseError(resultCode: .SQLITE_MISUSE, message: "") + } + + public func interrupt() { + dbPool.interrupt() + } + + public func read(_ value: (Database) throws -> T) throws -> T { + try dbPool.read { db in + try db.read(from: self) { + try value(db) + } + } + } + + public func unsafeRead(_ value: (Database) throws -> T) throws -> T { + /// There is no unsafe access to a snapshot. + try read(value) + } + + public func asyncRead(_ value: @escaping (Result) -> Void) { + dbPool.asyncRead { dbResult in + do { + let db = try dbResult.get() + try db.read(from: self) { + value(.success(db)) + } + } catch { + value(.failure(error)) + } + } + } + + public func asyncUnsafeRead(_ value: @escaping (Result) -> Void) { + /// There is no unsafe access to a snapshot. + dbPool.asyncRead(value) + } + + public func unsafeReentrantRead(_ value: (Database) throws -> T) throws -> T { + try dbPool.unsafeReentrantRead { db in + try db.isolated { + try db.read(from: self) { + try value(db) + } + } + } + } + + public func _add( + observation: ValueObservation, + scheduling scheduler: ValueObservationScheduler, + onChange: @escaping (Reducer.Value) -> Void) + -> AnyDatabaseCancellable where Reducer: ValueReducer + { + _addReadOnly(observation: observation, scheduling: scheduler, onChange: onChange) + } +} + +extension DatabasePool { + /// Creates a database snapshot that uses the reader connections of + /// the pool. + /// + /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) + /// + /// The returned snapshot sees an unchanging database content, as it existed + /// at the moment it was created. It shares the same reader SQLite + /// connections as the `DatabasePool`. + /// + /// An error is thrown unless the SQLite database is in the + /// [WAL mode](https://www.sqlite.org/wal.html). + /// + /// It is a programmer error to create a snapshot from the writer dispatch + /// queue when a transaction is opened: + /// + /// ```swift + /// try dbPool.write { db in + /// try Player.deleteAll() + /// + /// // fatal error: makeSnapshot() must not be called from inside a transaction + /// let snapshot = try dbPool.makeSnapshot() + /// } + /// ``` + /// + /// To avoid this fatal error, create the snapshot *before* or *after* + /// the transaction: + /// + /// ```swift + /// let snapshot = try dbPool.makeSnapshot() // OK + /// + /// try dbPool.writeWithoutTransaction { db in + /// let snapshot = try dbPool.makeSnapshot() // OK + /// + /// try db.inTransaction { + /// try Player.deleteAll() + /// return .commit + /// } + /// + /// // OK + /// let snapshot = try dbPool.makeSnapshot() // OK + /// } + /// + /// let snapshot = try dbPool.makeSnapshot() // OK + /// ``` + public func makeSnapshotPool() throws -> DatabaseSnapshotPool { + let dbSnapshot = try makeSnapshot() + return try dbSnapshot.read { db in + try DatabaseSnapshotPool( + dbPool: self, + dbSnapshot: dbSnapshot, + walSnapshot: WALSnapshot(db), + schemaVersion: db.schemaVersion(), + schemaCache: db.schemaCache) + } + } +} + +extension Database { + fileprivate func open(_ snapshot: DatabaseSnapshotPool) throws { + let code = sqlite3_snapshot_open(sqliteConnection, "main", snapshot.walSnapshot.sqliteSnapshot) + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code) + } + lastSchemaVersion = snapshot.schemaVersion + schemaCache = snapshot.schemaCache + } + + fileprivate func read(from snapshot: DatabaseSnapshotPool, _ value: () throws -> T) throws -> T { + try open(snapshot) + defer { + snapshot.$schemaCache.update { $0.formUnion(schemaCache) } + } + + return try value() + } +} +#endif diff --git a/GRDB/Core/WALSnapshot.swift b/GRDB/Core/WALSnapshot.swift index c3a7b1dcc..6ec2d7e48 100644 --- a/GRDB/Core/WALSnapshot.swift +++ b/GRDB/Core/WALSnapshot.swift @@ -19,7 +19,7 @@ /// Yes, this is an awfully complex logic. /// /// See . -final class WALSnapshot { +final class WALSnapshot: Sendable { // Xcode 14 ships with a macOS SDK that misses snapshot support. // Xcode 14.1 ships with a macOS SDK that has snapshot support. // This is the meaning of (compiler(<5.7.1) && (os(macOS) || targetEnvironment(macCatalyst))) diff --git a/GRDB/Core/WALSnapshotToken.swift b/GRDB/Core/WALSnapshotToken.swift deleted file mode 100644 index a171b2cc1..000000000 --- a/GRDB/Core/WALSnapshotToken.swift +++ /dev/null @@ -1,322 +0,0 @@ -// swiftlint:disable:next line_length -#if (compiler(<5.7.1) && (os(macOS) || targetEnvironment(macCatalyst))) || GRDBCIPHER || (GRDBCUSTOMSQLITE && !SQLITE_ENABLE_SNAPSHOT) -#else - -#if canImport(Combine) -import Combine -import Dispatch -#endif - -/// A token that indicates which WAL snapshot is being accessed. -/// -/// ## Overview -/// -/// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) -/// -/// All database accesses performed from a `WALSnapshotToken` see the database -/// content as it existed at the moment the token was created. -/// -/// ## Usage -/// -/// You create a `WALSnapshotToken` from a ``DatabasePool``, -/// with ``DatabasePool/currentSnapshotToken()``: -/// -/// ```swift -/// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") -/// let token = try dbPool.currentSnapshotToken() -/// let playerCount = try dbPool.read(from: token) { db in -/// try Player.fetchCount(db) -/// } -/// ``` -/// -/// When you want to control the database state seen by a token, create the -/// token from within a write access, outside of any transaction. -/// -/// For example, compare the two tokens below. The first one is guaranteed to -/// see an empty table of players, because is is created after all players have -/// been deleted, and from the serialized writer dispatch queue which prevents -/// any concurrent write. The second is created without this concurrency -/// protection, which means that some other threads may already have created -/// some players: -/// -/// ```swift -/// let token1 = try dbPool.writeWithoutTransaction { db -> WALSnapshotToken in -/// try db.inTransaction { -/// try Player.deleteAll() -/// return .commit -/// } -/// -/// return dbPool.currentSnapshotToken() -/// } -/// -/// // <- Other threads may have created some players here -/// let token2 = try dbPool.currentSnapshotToken() -/// -/// // Guaranteed to be zero -/// let count1 = try dbPool.read(from: token1, Player.fetchCount) -/// -/// // Could be anything -/// let count2 = try dbPool.read(from: token2, Player.fetchCount) -/// ``` -/// -/// Related SQLite documentation: -/// -/// - -/// - -public struct WALSnapshotToken { - let walSnapshot: WALSnapshot - - /// The connection that holds a transaction and prevents checkpointing in - /// order to keep `walSnapshot` valid. - let snapshot: DatabaseSnapshot - - let schemaVersion: Int32 - - // Protected by a lock because a token can be used in multiple - // database connections. - @LockedBox var schemaCache: Database.SchemaCache -} - -extension DatabasePool { - /// Returns a token associated with the WAL snapshot of the current state of - /// the database. - /// - /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) - /// - /// The returned token makes it possible to see the database content as it - /// existed at the moment the token was created. - /// - /// It is a programmer error to get a WAL snapshot token from the writer - /// dispatch queue when a transaction is opened: - /// - /// ```swift - /// try dbPool.write { db in - /// try Player.deleteAll() - /// - /// // fatal error: currentSnapshotToken() must not be called from inside a transaction - /// let token = try dbPool.currentSnapshotToken() - /// } - /// ``` - /// - /// To avoid this fatal error, create the token *before* or *after* - /// the transaction: - /// - /// ```swift - /// let token = try dbPool.currentSnapshotToken() // OK - /// - /// try dbPool.writeWithoutTransaction { db in - /// let token = try dbPool.currentSnapshotToken() // OK - /// - /// try db.inTransaction { - /// try Player.deleteAll() - /// return .commit - /// } - /// - /// let token = try dbPool.currentSnapshotToken() // OK - /// } - /// - /// let token = try dbPool.currentSnapshotToken() // OK - /// ``` - public func currentSnapshotToken() throws -> WALSnapshotToken { - let snapshot = try makeSnapshot() - return try snapshot.read { db in - try WALSnapshotToken( - walSnapshot: WALSnapshot(db), - snapshot: snapshot, - schemaVersion: db.schemaVersion(), - schemaCache: db.schemaCache) - } - } - - /// Executes read-only database operations from the given WAL snapshot, and - /// returns their result after they have finished executing. - /// - /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) - /// - /// For example: - /// - /// ```swift - /// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") - /// let token = try dbPool.currentSnapshotToken() - /// let count = try reader.read(from: token) { db in - /// try Player.fetchCount(db) - /// } - /// ``` - /// - /// Database operations see the database content as it existed at the moment - /// the token was created. - /// - /// The database connection is read-only: attempts to write throw a - /// ``DatabaseError`` with resultCode `SQLITE_READONLY`. - /// - /// The ``Database`` argument to `value` is valid only during the execution - /// of the closure. Do not store or return the database connection for - /// later use. - /// - /// It is a programmer error to call this method from another database - /// access method. Doing so raises a "Database methods are not reentrant" - /// fatal error at runtime. - /// - /// - parameter value: A closure which accesses the database. - /// - throws: The error thrown by `value`, or any ``DatabaseError`` that - /// would happen while establishing the database access. - @_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails - public func read(from token: WALSnapshotToken, _ value: (Database) throws -> T) throws -> T { - try read { db in - try db.read(from: token) { - try value(db) - } - } - } - - /// Executes read-only database operations from the given WAL snapshot, and - /// returns their result after they have finished executing. - /// - /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) - /// - /// For example: - /// - /// ```swift - /// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") - /// let token = try dbPool.currentSnapshotToken() - /// let count = try await reader.read(from: token) { db in - /// try Player.fetchCount(db) - /// } - /// ``` - /// - /// Database operations see the database content as it existed at the moment - /// the token was created. - /// - /// The database connection is read-only: attempts to write throw a - /// ``DatabaseError`` with resultCode `SQLITE_READONLY`. - /// - /// The ``Database`` argument to `value` is valid only during the execution - /// of the closure. Do not store or return the database connection for - /// later use. - /// - /// - parameter value: A closure which accesses the database. - /// - throws: The error thrown by `value`, or any ``DatabaseError`` that - /// would happen while establishing the database access. - @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) - public func read( - from token: WALSnapshotToken, - _ value: @Sendable @escaping (Database) throws -> T) - async throws -> T - { - try await read { db in - try db.read(from: token) { - try value(db) - } - } - } - - /// Schedules read-only database operations for execution, from the given - /// WAL snapshot, and returns immediately. - /// - /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) - /// - /// For example: - /// - /// ```swift - /// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") - /// let token = try dbPool.currentSnapshotToken() - /// try reader.asyncRead(from: token) { dbResult in - /// do { - /// let db = try dbResult.get() - /// let count = try Player.fetchCount(db) - /// } catch { - /// // Handle error - /// } - /// } - /// ``` - /// - /// Database operations see the database content as it existed at the moment - /// the token was created. - /// - /// The database connection is read-only: attempts to write throw a - /// ``DatabaseError`` with resultCode `SQLITE_READONLY`. - /// - /// - parameter value: A closure which accesses the database. Its argument - /// is a `Result` that provides the database connection, or the failure - /// that would prevent establishing the read access to the database. - public func asyncRead(from token: WALSnapshotToken, _ value: @escaping (Result) -> Void) { - asyncRead { dbResult in - do { - let db = try dbResult.get() - try db.read(from: token) { - value(.success(db)) - } - } catch { - value(.failure(error)) - } - } - } - -#if canImport(Combine) - /// Returns a publisher that publishes one value fetched from the given WAL - /// snapshot, and completes. - /// - /// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features) - /// - /// The database is not accessed until subscription. Value and completion - /// are published on `scheduler` (the main dispatch queue by default). - /// - /// For example: - /// - /// ```swift - /// let dbPool = try DatabasePool(path: "/path/to/database.sqlite") - /// let token = try dbPool.currentSnapshotToken() - /// let countPublisher = reader.readPublisher(from: token) { db in - /// try Player.fetchCount(db) - /// } - /// ``` - /// - /// Database operations are isolated in a transaction: they do not see - /// changes performed by eventual concurrent writes (even writes performed - /// by other processes). - /// - /// The database connection is read-only: attempts to write throw a - /// ``DatabaseError`` with resultCode `SQLITE_READONLY`. - /// - /// The ``Database`` argument to `value` is valid only during the execution - /// of the closure. Do not store or return the database connection for - /// later use. - /// - /// - parameter scheduler: A Combine Scheduler. - /// - parameter value: A closure which accesses the database. - @available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *) - public func readPublisher( - from token: WALSnapshotToken, - receiveOn scheduler: some Combine.Scheduler = DispatchQueue.main, - value: @escaping (Database) throws -> Output) - -> DatabasePublishers.Read - { - readPublisher(receiveOn: scheduler) { db in - try db.read(from: token) { - try value(db) - } - } - } -#endif -} - -extension Database { - fileprivate func open(_ token: WALSnapshotToken) throws { - let code = sqlite3_snapshot_open(sqliteConnection, "main", token.walSnapshot.sqliteSnapshot) - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code) - } - lastSchemaVersion = token.schemaVersion - schemaCache = token.schemaCache - } - - fileprivate func read(from token: WALSnapshotToken, _ value: () throws -> T) throws -> T { - try open(token) - defer { - token.$schemaCache.update { $0.formUnion(schemaCache) } - } - - return try value() - } -} -#endif diff --git a/GRDB/Documentation.docc/DatabaseConnections.md b/GRDB/Documentation.docc/DatabaseConnections.md index 6e5f61819..0c959f199 100644 --- a/GRDB/Documentation.docc/DatabaseConnections.md +++ b/GRDB/Documentation.docc/DatabaseConnections.md @@ -33,6 +33,7 @@ The differences are: - ``DatabaseQueue`` - ``DatabasePool`` - ``DatabaseSnapshot`` +- ``DatabaseSnapshotPool`` ### Using Database Connections diff --git a/GRDB/Utils/LockedBox.swift b/GRDB/Utils/LockedBox.swift index f9c72785a..79388ae12 100644 --- a/GRDB/Utils/LockedBox.swift +++ b/GRDB/Utils/LockedBox.swift @@ -66,3 +66,5 @@ extension LockedBox where T: Numeric { } } } + +extension LockedBox: @unchecked Sendable where T: Sendable { } diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index b1f4dd214..7c3bb40ed 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -199,8 +199,8 @@ 56894F332604FC1E00268F4D /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F322604FC1E00268F4D /* Table.swift */; }; 56894FE3260658A400268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FA0260657F600268F4D /* Decimal.swift */; }; 56894FF2260658E600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */; }; - 568EB71C2921232200E59445 /* WALSnapshotToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71A2921232200E59445 /* WALSnapshotToken.swift */; }; - 568EB7202921235E00E59445 /* WALSnapshotTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71F2921235E00E59445 /* WALSnapshotTokenTests.swift */; }; + 568EB71C2921232200E59445 /* DatabaseSnapshotPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71A2921232200E59445 /* DatabaseSnapshotPool.swift */; }; + 568EB7202921235E00E59445 /* DatabaseSnapshotPoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568EB71F2921235E00E59445 /* DatabaseSnapshotPoolTests.swift */; }; 568ECB0D25D904CA00B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB0C25D904CA00B71526 /* SQLSelection.swift */; }; 568ECB3525D91CEF00B71526 /* SQLSubquery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB3425D91CEE00B71526 /* SQLSubquery.swift */; }; 5690C3291D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; }; @@ -641,8 +641,8 @@ 56894F322604FC1E00268F4D /* Table.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Table.swift; sourceTree = ""; }; 56894FA0260657F600268F4D /* Decimal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = ""; }; 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDecimalTests.swift; sourceTree = ""; }; - 568EB71A2921232200E59445 /* WALSnapshotToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WALSnapshotToken.swift; sourceTree = ""; }; - 568EB71F2921235E00E59445 /* WALSnapshotTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WALSnapshotTokenTests.swift; sourceTree = ""; }; + 568EB71A2921232200E59445 /* DatabaseSnapshotPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseSnapshotPool.swift; sourceTree = ""; }; + 568EB71F2921235E00E59445 /* DatabaseSnapshotPoolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseSnapshotPoolTests.swift; sourceTree = ""; }; 568ECB0C25D904CA00B71526 /* SQLSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSelection.swift; sourceTree = ""; }; 568ECB3425D91CEE00B71526 /* SQLSubquery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSubquery.swift; sourceTree = ""; }; 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = ""; }; @@ -1334,6 +1334,7 @@ 56EA86931C91DFE7002BB4DF /* DatabaseReaderTests.swift */, 564CE5C521B8FFE500652B19 /* DatabaseRegionObservationTests.swift */, 56C3F7521CF9F12400F6A361 /* DatabaseSavepointTests.swift */, + 568EB71F2921235E00E59445 /* DatabaseSnapshotPoolTests.swift */, 566A843420413DE400E50BFD /* DatabaseSnapshotTests.swift */, 564B3D70239BDBD6007BF308 /* DatabaseSuspensionTests.swift */, 56A238131B9C74A90082EB20 /* DatabaseTests.swift */, @@ -1356,7 +1357,6 @@ 56A238201B9C74A90082EB20 /* Statement */, 56E8CE0F1BB4FE5B00828BEC /* StatementColumnConvertibleFetchTests.swift */, 5607EFD11BB8253300605DE3 /* TransactionObserver */, - 568EB71F2921235E00E59445 /* WALSnapshotTokenTests.swift */, ); name = Core; sourceTree = ""; @@ -1439,6 +1439,7 @@ 564CE5B521B8FBEA00652B19 /* DatabaseRegionObservation.swift */, 5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */, 566A842B20413D9A00E50BFD /* DatabaseSnapshot.swift */, + 568EB71A2921232200E59445 /* DatabaseSnapshotPool.swift */, 56A238751B9C75030082EB20 /* DatabaseValue.swift */, 560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */, 563363C31C942C37000BE133 /* DatabaseWriter.swift */, @@ -1456,7 +1457,6 @@ 560D923F1C672C3E00F4F92B /* StatementColumnConvertible.swift */, 566B91321FA4D3810012D5B0 /* TransactionObserver.swift */, 56564F3828637C9900A19E9F /* WALSnapshot.swift */, - 568EB71A2921232200E59445 /* WALSnapshotToken.swift */, 5605F1471C672E4000235C62 /* Support */, ); path = Core; @@ -1867,7 +1867,7 @@ F3BA80901CFB2E7A003DC1BA /* Record.swift in Sources */, 56C0539322ACEECD0029D27D /* Fetch.swift in Sources */, 56E9FAC42210468500C703A8 /* SQLInterpolation.swift in Sources */, - 568EB71C2921232200E59445 /* WALSnapshotToken.swift in Sources */, + 568EB71C2921232200E59445 /* DatabaseSnapshotPool.swift in Sources */, 563EF420215F8A76007DAACD /* OrderedDictionary.swift in Sources */, F3BA80721CFB2E55003DC1BA /* Row.swift in Sources */, 569EF0E6200D37FD00A9FA45 /* DatabaseRegion.swift in Sources */, @@ -2087,7 +2087,7 @@ F3BA81391CFB3064003DC1BA /* RecordWithColumnNameManglingTests.swift in Sources */, 5691D96F27256C490021D540 /* AvailableElements.swift in Sources */, 56419C7F24A51D6E004967E1 /* DatabaseReaderReadPublisherTests.swift in Sources */, - 568EB7202921235E00E59445 /* WALSnapshotTokenTests.swift in Sources */, + 568EB7202921235E00E59445 /* DatabaseSnapshotPoolTests.swift in Sources */, 56FF45431D2C23BA00F21EF9 /* TableRecordDeleteTests.swift in Sources */, F3BA80F01CFB3017003DC1BA /* FetchableRecordTests.swift in Sources */, 5657AB491D108BA9006283EF /* FoundationNSNullTests.swift in Sources */, diff --git a/Tests/GRDBTests/WALSnapshotTokenTests.swift b/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift similarity index 67% rename from Tests/GRDBTests/WALSnapshotTokenTests.swift rename to Tests/GRDBTests/DatabaseSnapshotPoolTests.swift index 0fd0dcbd4..7cea62e75 100644 --- a/Tests/GRDBTests/WALSnapshotTokenTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotPoolTests.swift @@ -4,7 +4,7 @@ import XCTest import GRDB // test create from non-wal (read-only) snapshot -final class WALSnapshotTokenTests: GRDBTestCase { +final class DatabaseSnapshotPoolTests: GRDBTestCase { /// A helper type private struct Counter { init(dbPool: DatabasePool) throws { @@ -22,11 +22,11 @@ final class WALSnapshotTokenTests: GRDBTestCase { } } - func testWALSnapshotTokenCreationFromNewDatabase() throws { - _ = try makeDatabasePool().currentSnapshotToken() + func testSnapshotPoolCreationFromNewDatabase() throws { + _ = try makeDatabasePool().makeSnapshotPool() } - func testWALSnapshotTokenCreationFromNonWALDatabase() throws { + func testSnapshotPoolCreationFromNonWALDatabase() throws { let dbQueue = try makeDatabaseQueue() var config = Configuration() @@ -34,70 +34,70 @@ final class WALSnapshotTokenTests: GRDBTestCase { let dbPool = try DatabasePool(path: dbQueue.path, configuration: config) do { - _ = try dbPool.currentSnapshotToken() + _ = try dbPool.makeSnapshotPool() XCTFail("Expected error") } catch DatabaseError.SQLITE_ERROR { } } - func testWALSnapshotToken() throws { + func testSnapshotPool() throws { let dbPool = try makeDatabasePool() let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try dbPool.write(counter.increment) try XCTAssertEqual(dbPool.read(counter.value), 2) - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) // Reuse the last connection try XCTAssertEqual(dbPool.read(counter.value), 2) } - func testPassiveCheckpointDoesNotInvalidateWALSnapshotToken() throws { + func testPassiveCheckpointDoesNotInvalidateSnapshotPool() throws { let dbPool = try makeDatabasePool() let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.passive) } // ignore if error or not, that's not the point - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) } - func testFullCheckpointDoesNotInvalidateWALSnapshotToken() throws { + func testFullCheckpointDoesNotInvalidateSnapshotPool() throws { let dbPool = try makeDatabasePool() let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.full) } // ignore if error or not, that's not the point - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) } - func testRestartCheckpointDoesNotInvalidateWALSnapshotToken() throws { + func testRestartCheckpointDoesNotInvalidateSnapshotPool() throws { let dbPool = try makeDatabasePool() let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.restart) } // ignore if error or not, that's not the point - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) } - func testTruncateCheckpointDoesNotInvalidateWALSnapshotToken() throws { + func testTruncateCheckpointDoesNotInvalidateSnapshotPool() throws { let dbPool = try makeDatabasePool() let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.truncate) } // ignore if error or not, that's not the point - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) - try XCTAssertEqual(dbPool.read(from: token, counter.value), 1) + try XCTAssertEqual(snapshot.read(counter.value), 1) } - func testWALSnapshotTokenAndSchemaCache() throws { + func testSnapshotPoolAndSchemaCache() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in try db.execute(sql: """ @@ -105,7 +105,7 @@ final class WALSnapshotTokenTests: GRDBTestCase { """) } - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try dbPool.write { db in try db.execute(sql: """ @@ -119,7 +119,7 @@ final class WALSnapshotTokenTests: GRDBTestCase { } do { - let exists = try dbPool.read(from: token) { try $0.tableExists("team") } + let exists = try snapshot.read { try $0.tableExists("team") } XCTAssertFalse(exists) } @@ -129,7 +129,7 @@ final class WALSnapshotTokenTests: GRDBTestCase { } } - func testWALSnapshotTokenAndStatementCache() throws { + func testSnapshotPoolAndStatementCache() throws { let dbPool = try makeDatabasePool() let request = SQLRequest(sql: "SELECT COUNT(*) FROM player", cached: true) try dbPool.write { db in @@ -138,14 +138,14 @@ final class WALSnapshotTokenTests: GRDBTestCase { """) } - let token = try dbPool.currentSnapshotToken() + let snapshot = try dbPool.makeSnapshotPool() try dbPool.write { db in try db.execute(sql: "DROP TABLE player") } do { - let count = try dbPool.read(from: token) { try request.fetchOne($0) } + let count = try snapshot.read { try request.fetchOne($0) } XCTAssertEqual(count, 0) } @@ -155,7 +155,7 @@ final class WALSnapshotTokenTests: GRDBTestCase { } catch DatabaseError.SQLITE_ERROR { } do { - let count = try dbPool.read(from: token) { try request.fetchOne($0) } + let count = try snapshot.read { try request.fetchOne($0) } XCTAssertEqual(count, 0) } }