mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Prevent snapshot creation from within a transaction
This commit is contained in:
1 parent
e9d45b5435
commit
2dfbe8e5f2
2 files changed
+37
-16
No files matched your search
@@ -622,7 +622,7 @@ extension DatabasePool {
|
||||
// MARK: - Snapshots
|
||||
|
||||
/// Creates a database snapshot.
|
||||
//:
|
||||
///
|
||||
/// The snapshot sees an unchanging database content, as it existed at the
|
||||
/// moment it was created.
|
||||
///
|
||||
@@ -646,11 +646,47 @@ extension DatabasePool {
|
||||
/// try Player.fetchCount(db)
|
||||
/// }
|
||||
///
|
||||
/// It is forbidden to create a snapshot from the writer protected dispatch
|
||||
/// queue when a transaction is opened, though, because it is likely a
|
||||
/// programmer error:
|
||||
///
|
||||
/// try dbPool.write { db in
|
||||
/// try db.inTransaction {
|
||||
/// try Player.deleteAll()
|
||||
/// // fatal error: makeSnapshot() must not be called from inside a transaction
|
||||
/// let snapshot = dbPool.makeSnapshot()
|
||||
/// return .commit
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// To avoid this fatal error, create the snapshot *before* or *after* the
|
||||
/// transaction:
|
||||
///
|
||||
/// try dbPool.write { db in
|
||||
/// // OK
|
||||
/// let snapshot = dbPool.makeSnapshot()
|
||||
///
|
||||
/// try db.inTransaction {
|
||||
/// try Player.deleteAll()
|
||||
/// return .commit
|
||||
/// }
|
||||
///
|
||||
/// // OK
|
||||
/// let snapshot = dbPool.makeSnapshot()
|
||||
/// }
|
||||
///
|
||||
/// You can create as many snapshots as you need, regardless of the maximum
|
||||
/// number of reader connections in the pool.
|
||||
///
|
||||
/// For more information, read about "snapshot isolation" at https://sqlite.org/isolation.html
|
||||
public func makeSnapshot() throws -> DatabaseSnapshot {
|
||||
// Sanity check
|
||||
if writer.onValidQueue {
|
||||
writer.execute { db in
|
||||
GRDBPrecondition(!db.isInsideTransaction, "makeSnapshot() must not be called from inside a transaction.")
|
||||
}
|
||||
}
|
||||
|
||||
let snapshot = try DatabaseSnapshot(path: path, configuration: writer.configuration)
|
||||
snapshot.read { setupDatabase($0) }
|
||||
return snapshot
|
||||
|
||||
@@ -40,21 +40,6 @@ class DatabaseSnapshotTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSnapshotDoesNotSeeUncommittedTransaction() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
try db.create(table: "t") { $0.column("id", .integer).primaryKey() }
|
||||
try db.beginTransaction()
|
||||
try db.execute("INSERT INTO t DEFAULT VALUES")
|
||||
let snapshot = try dbPool.makeSnapshot()
|
||||
try db.commit()
|
||||
try XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM t")!, 1)
|
||||
try snapshot.read { db in
|
||||
try XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM t")!, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSnapshotCreatedOutsideOfWriterQueue() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
|
||||
Reference in new issue
Block a user