From 2dfbe8e5f2d6164c193f9da04598ceef58a3f1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 31 Mar 2018 15:56:42 +0200 Subject: [PATCH] Prevent snapshot creation from within a transaction --- GRDB/Core/DatabasePool.swift | 38 ++++++++++++++++++++- Tests/GRDBTests/DatabaseSnapshotTests.swift | 15 -------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index f21e3e8fe..ade0ad076 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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 diff --git a/Tests/GRDBTests/DatabaseSnapshotTests.swift b/Tests/GRDBTests/DatabaseSnapshotTests.swift index aa5799a9e..e988626f9 100644 --- a/Tests/GRDBTests/DatabaseSnapshotTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotTests.swift @@ -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