mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
[BREAKING] Remove Configuration.defaultTransactionKind
GRDB know defaults read-only transactions to DEFERRED, and write transactions to IMMEDIATE. See https://github.com/groue/GRDB.swift/issues/1483 for some context.
This commit is contained in:
1 parent
2297c8f432
commit
2661ff4690
16 files changed
+88
-314
No files matched your search
@@ -238,28 +238,6 @@ public struct Configuration {
|
||||
|
||||
// MARK: - Transactions
|
||||
|
||||
/// The default kind of write transactions.
|
||||
///
|
||||
/// The default is ``Database/TransactionKind/deferred``.
|
||||
///
|
||||
/// You can change the default transaction kind. For example, you can force
|
||||
/// all write transactions to be `IMMEDIATE`:
|
||||
///
|
||||
/// ```swift
|
||||
/// var config = Configuration()
|
||||
/// config.defaultTransactionKind = .immediate
|
||||
/// let dbQueue = try DatabaseQueue(configuration: config)
|
||||
///
|
||||
/// // BEGIN IMMEDIATE TRANSACTION; ...; COMMIT TRANSACTION;
|
||||
/// try dbQueue.write { db in ... }
|
||||
/// ```
|
||||
///
|
||||
/// This property is ignored for read-only transactions. Those always open
|
||||
/// `DEFERRED` SQLite transactions.
|
||||
///
|
||||
/// Related SQLite documentation: <https://www.sqlite.org/lang_transaction.html>
|
||||
public var defaultTransactionKind: Database.TransactionKind = .deferred
|
||||
|
||||
/// A boolean value indicating whether it is valid to leave a transaction
|
||||
/// opened at the end of a database access method.
|
||||
///
|
||||
|
||||
+15
-16
@@ -1282,14 +1282,10 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
/// Use ``inSavepoint(_:)`` instead.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil).
|
||||
/// - kind: The transaction type.
|
||||
///
|
||||
/// If nil, and the database connection is read-only, the transaction
|
||||
/// kind is ``TransactionKind/deferred``.
|
||||
///
|
||||
/// If nil, and the database connection is not read-only, the
|
||||
/// transaction kind is the ``Configuration/defaultTransactionKind``
|
||||
/// of the ``configuration``.
|
||||
/// If nil, the transaction kind is DEFERRED when the current
|
||||
/// database access is read-only, and IMMEDIATE otherwise.
|
||||
/// - operations: A function that executes SQL statements and returns
|
||||
/// either ``TransactionCompletion/commit`` or ``TransactionCompletion/rollback``.
|
||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or the
|
||||
@@ -1413,8 +1409,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
// By default, top level SQLite savepoints open a
|
||||
// deferred transaction.
|
||||
//
|
||||
// But GRDB database configuration mandates a default transaction
|
||||
// kind that we have to honor.
|
||||
// But GRDB prefers immediate transactions for writes.
|
||||
//
|
||||
// Besides, starting some (?) SQLCipher/SQLite version, SQLite has a
|
||||
// bug. Returning 1 from `sqlite3_commit_hook` does not leave the
|
||||
@@ -1504,16 +1499,20 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil).
|
||||
///
|
||||
/// If nil, and the database connection is read-only, the transaction
|
||||
/// kind is ``TransactionKind/deferred``.
|
||||
///
|
||||
/// If nil, and the database connection is not read-only, the
|
||||
/// transaction kind is the ``Configuration/defaultTransactionKind``
|
||||
/// of the ``configuration``.
|
||||
/// If nil, the transaction kind is DEFERRED when the current
|
||||
/// database access is read-only, and IMMEDIATE otherwise.
|
||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
|
||||
public func beginTransaction(_ kind: TransactionKind? = nil) throws {
|
||||
// SQLite throws an error for non-deferred transactions when read-only.
|
||||
let kind = kind ?? (isReadOnly ? .deferred : configuration.defaultTransactionKind)
|
||||
// We prefer immediate transactions for writes, so that write
|
||||
// transactions can not overlap. This reduces the opportunity for
|
||||
// SQLITE_BUSY, which is immediately thrown whenever a transaction
|
||||
// is upgraded after an initial read and a concurrent processes
|
||||
// has acquired the write lock beforehand. This SQLITE_BUSY error
|
||||
// can not be avoided with a busy timeout.
|
||||
//
|
||||
// See <https://github.com/groue/GRDB.swift/issues/1483>.
|
||||
let kind = kind ?? (isReadOnly ? .deferred : .immediate)
|
||||
try execute(sql: "BEGIN \(kind.rawValue) TRANSACTION")
|
||||
assert(sqlite3_get_autocommit(sqliteConnection) == 0)
|
||||
}
|
||||
|
||||
@@ -118,10 +118,6 @@ public final class DatabasePool {
|
||||
|
||||
configuration.readonly = true
|
||||
|
||||
// Readers use deferred transactions by default.
|
||||
// Other transaction kinds are forbidden by SQLite in read-only connections.
|
||||
configuration.defaultTransactionKind = .deferred
|
||||
|
||||
// <https://www.sqlite.org/wal.html#sometimes_queries_return_sqlite_busy_in_wal_mode>
|
||||
// > But there are some obscure cases where a query against a WAL-mode
|
||||
// > database can return SQLITE_BUSY, so applications should be prepared
|
||||
@@ -787,9 +783,10 @@ extension DatabasePool: DatabaseWriter {
|
||||
///
|
||||
/// - precondition: This method is not reentrant.
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
||||
/// type is the ``Configuration/defaultTransactionKind`` of the
|
||||
/// the ``configuration``.
|
||||
/// - kind: The transaction type (default nil).
|
||||
///
|
||||
/// If nil, the transaction kind is DEFERRED when the database
|
||||
/// connection is read-only, and IMMEDIATE otherwise.
|
||||
/// - updates: A function that updates the database.
|
||||
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
||||
public func writeInTransaction(
|
||||
|
||||
@@ -352,9 +352,10 @@ extension DatabaseQueue: DatabaseWriter {
|
||||
/// ```
|
||||
///
|
||||
/// - parameters:
|
||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
||||
/// type is the ``Configuration/defaultTransactionKind`` of the
|
||||
/// the ``configuration``.
|
||||
/// - kind: The transaction type (default nil).
|
||||
///
|
||||
/// If nil, the transaction kind is DEFERRED when the database
|
||||
/// connection is read-only, and IMMEDIATE otherwise.
|
||||
/// - updates: A function that updates the database.
|
||||
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
||||
public func inTransaction(
|
||||
|
||||
@@ -126,10 +126,6 @@ public final class DatabaseSnapshot {
|
||||
// DatabaseSnapshot is read-only.
|
||||
configuration.readonly = true
|
||||
|
||||
// DatabaseSnapshot uses deferred transactions by default.
|
||||
// Other transaction kinds are forbidden by SQLite in read-only connections.
|
||||
configuration.defaultTransactionKind = .deferred
|
||||
|
||||
// DatabaseSnapshot keeps a long-lived transaction.
|
||||
configuration.allowsUnsafeTransactions = true
|
||||
|
||||
|
||||
@@ -242,10 +242,6 @@ public final class DatabaseSnapshotPool {
|
||||
// DatabaseSnapshotPool is read-only.
|
||||
configuration.readonly = true
|
||||
|
||||
// DatabaseSnapshotPool uses deferred transactions by default.
|
||||
// Other transaction kinds are forbidden by SQLite in read-only connections.
|
||||
configuration.defaultTransactionKind = .deferred
|
||||
|
||||
// DatabaseSnapshotPool keeps a long-lived transaction.
|
||||
configuration.allowsUnsafeTransactions = true
|
||||
|
||||
|
||||
@@ -152,12 +152,11 @@ If several processes want to write in the database, configure the database pool
|
||||
|
||||
```swift
|
||||
var configuration = Configuration()
|
||||
configuration.defaultTransactionKind = .immediate
|
||||
configuration.busyMode = .timeout(/* a TimeInterval */)
|
||||
let dbPool = try DatabasePool(path: ..., configuration: configuration)
|
||||
```
|
||||
|
||||
Both the `defaultTransactionKind` and `busyMode` are important for preventing `SQLITE_BUSY`. The `immediate` transaction kind prevents write transactions from overlapping, and the busy timeout has write transactions wait, instead of throwing `SQLITE_BUSY`, whenever another process is writing.
|
||||
The busy timeout has write transactions wait, instead of throwing `SQLITE_BUSY`, whenever another process is writing. GRDB automatically opens all write transactions with the IMMEDIATE kind, preventing write transactions from overlapping.
|
||||
|
||||
With such a setup, you will still get `SQLITE_BUSY` errors if the database remains locked by another process for longer than the specified timeout. You can catch those errors:
|
||||
|
||||
|
||||
@@ -90,7 +90,6 @@ do {
|
||||
### Configuring GRDB Connections
|
||||
|
||||
- ``allowsUnsafeTransactions``
|
||||
- ``defaultTransactionKind``
|
||||
- ``label``
|
||||
- ``maximumReaderCount``
|
||||
- ``observesSuspensionNotifications``
|
||||
|
||||
@@ -214,6 +214,8 @@ SQLite savepoints are more than nested transactions, though. For advanced uses,
|
||||
|
||||
SQLite supports [three kinds of transactions](https://www.sqlite.org/lang_transaction.html): deferred (the default), immediate, and exclusive.
|
||||
|
||||
By default, GRDB opens DEFERRED transaction for reads, and IMMEDIATE transactions for writes.
|
||||
|
||||
The transaction kind can be chosen for individual transaction:
|
||||
|
||||
```swift
|
||||
@@ -222,20 +224,3 @@ let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")
|
||||
// BEGIN EXCLUSIVE TRANSACTION ...
|
||||
try dbQueue.inTransaction(.exclusive) { db in ... }
|
||||
```
|
||||
|
||||
It is also possible to configure the ``Configuration/defaultTransactionKind``:
|
||||
|
||||
```swift
|
||||
var config = Configuration()
|
||||
config.defaultTransactionKind = .immediate
|
||||
|
||||
let dbQueue = try DatabaseQueue(
|
||||
path: "/path/to/database.sqlite",
|
||||
configuration: config)
|
||||
|
||||
// BEGIN IMMEDIATE TRANSACTION ...
|
||||
try dbQueue.write { db in ... }
|
||||
|
||||
// BEGIN IMMEDIATE TRANSACTION ...
|
||||
try dbQueue.inTransaction { db in ... }
|
||||
```
|
||||
@@ -94,7 +94,7 @@
|
||||
- [ ] GRDB7: Question Sendability of SQLExpressible et al. If we can copy Sendable representations in requests, it's not needed.
|
||||
- [X] GRDB7: Raise the minimum watchOS target https://github.com/groue/GRDB.swift/issues/1472#issuecomment-1960696836
|
||||
- [ ] GRDB7: Consider putting a Task cancellation handler on async methods.
|
||||
- [ ] GRDB7: All writes should be immediate, right? https://github.com/groue/GRDB.swift/pull/1485#issuecomment-1953793553
|
||||
- [X] GRDB7: All writes should be immediate, right? https://github.com/groue/GRDB.swift/pull/1485#issuecomment-1953793553
|
||||
- [ ] GRDB7: Sendable conformances
|
||||
- [ ] anydatabasecancellable
|
||||
- [ ] anydatabaseregionconvertible
|
||||
|
||||
@@ -84,7 +84,7 @@ class BackupTestCase: GRDBTestCase {
|
||||
let sourceDbPageCount = try setupBackupSource(source)
|
||||
try setupBackupDestination(destination)
|
||||
|
||||
try source.write { sourceDb in
|
||||
try source.read { sourceDb in
|
||||
try destination.barrierWriteWithoutTransaction { destDb in
|
||||
XCTAssertThrowsError(
|
||||
try sourceDb.backup(to: destDb, pagesPerStep: 1) { progress in
|
||||
@@ -102,7 +102,7 @@ class BackupTestCase: GRDBTestCase {
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT id FROM items")!, 1)
|
||||
}
|
||||
|
||||
try source.write { dbSource in
|
||||
try source.read { dbSource in
|
||||
try destination.barrierWriteWithoutTransaction { dbDest in
|
||||
var progressCount: Int = 1
|
||||
var isCompleted: Bool = false
|
||||
|
||||
@@ -369,8 +369,6 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
func test_busy_timeout_and_IMMEDIATE_transactions_do_prevent_SQLITE_BUSY() throws {
|
||||
var configuration = dbConfiguration!
|
||||
// Test fails when this line is commented
|
||||
configuration.defaultTransactionKind = .immediate
|
||||
// Test fails when this line is commented
|
||||
configuration.busyMode = .timeout(10)
|
||||
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test")
|
||||
|
||||
@@ -96,226 +96,8 @@ class DatabaseSavepointTests: GRDBTestCase {
|
||||
XCTAssertThrowsError(try db.execute(sql: "COMMIT"))
|
||||
}
|
||||
}
|
||||
|
||||
func testReleaseTopLevelSavepointFromDatabaseWithDefaultDeferredTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .deferred
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
}
|
||||
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"COMMIT TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item3')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item2", "item3"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 3)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 3)
|
||||
#endif
|
||||
}
|
||||
|
||||
func testRollbackTopLevelSavepointFromDatabaseWithDefaultDeferredTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .deferred
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
}
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"ROLLBACK TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item3')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item3"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 3)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 3)
|
||||
#endif
|
||||
}
|
||||
|
||||
func testNestedSavepointFromDatabaseWithDefaultDeferredTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .deferred
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item4")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item5")
|
||||
}
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item3')",
|
||||
"RELEASE SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item4')",
|
||||
"COMMIT TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item5')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item2", "item3", "item4", "item5"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 5)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 5)
|
||||
#endif
|
||||
try dbQueue.inDatabase { db in try db.execute(sql: "DELETE FROM items") }
|
||||
observer.reset()
|
||||
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item4")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item5")
|
||||
}
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item3')",
|
||||
"RELEASE SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item4')",
|
||||
"ROLLBACK TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item5')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item5"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 5)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 5)
|
||||
#endif
|
||||
try dbQueue.inDatabase { db in try db.execute(sql: "DELETE FROM items") }
|
||||
observer.reset()
|
||||
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item4")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item5")
|
||||
}
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item3')",
|
||||
"ROLLBACK TRANSACTION TO SAVEPOINT grdb",
|
||||
"RELEASE SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item4')",
|
||||
"COMMIT TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item5')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item2", "item4", "item5"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 4)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 4)
|
||||
#endif
|
||||
try dbQueue.inDatabase { db in try db.execute(sql: "DELETE FROM items") }
|
||||
observer.reset()
|
||||
|
||||
sqlQueries.removeAll()
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try insertItem(db, name: "item1")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item2")
|
||||
try db.inSavepoint {
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item3")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item4")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
try insertItem(db, name: "item5")
|
||||
}
|
||||
XCTAssertEqual(sqlQueries, [
|
||||
"INSERT INTO items (name) VALUES ('item1')",
|
||||
"BEGIN DEFERRED TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item2')",
|
||||
"SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item3')",
|
||||
"ROLLBACK TRANSACTION TO SAVEPOINT grdb",
|
||||
"RELEASE SAVEPOINT grdb",
|
||||
"INSERT INTO items (name) VALUES ('item4')",
|
||||
"ROLLBACK TRANSACTION",
|
||||
"INSERT INTO items (name) VALUES ('item5')"
|
||||
])
|
||||
XCTAssertEqual(try fetchAllItemNames(dbQueue), ["item1", "item5"])
|
||||
XCTAssertEqual(observer.allRecordedEvents.count, 4)
|
||||
#if SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
XCTAssertEqual(observer.allRecordedPreUpdateEvents.count, 4)
|
||||
#endif
|
||||
try dbQueue.inDatabase { db in try db.execute(sql: "DELETE FROM items") }
|
||||
observer.reset()
|
||||
}
|
||||
|
||||
func testReleaseTopLevelSavepointFromDatabaseWithDefaultImmediateTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .immediate
|
||||
|
||||
func testReleaseTopLevelSavepoint() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
@@ -344,8 +126,7 @@ class DatabaseSavepointTests: GRDBTestCase {
|
||||
#endif
|
||||
}
|
||||
|
||||
func testRollbackTopLevelSavepointFromDatabaseWithDefaultImmediateTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .immediate
|
||||
func testRollbackTopLevelSavepoint() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
@@ -374,8 +155,7 @@ class DatabaseSavepointTests: GRDBTestCase {
|
||||
#endif
|
||||
}
|
||||
|
||||
func testNestedSavepointFromDatabaseWithDefaultImmediateTransactions() throws {
|
||||
dbConfiguration.defaultTransactionKind = .immediate
|
||||
func testNestedSavepoint() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let observer = Observer()
|
||||
dbQueue.add(transactionObserver: observer)
|
||||
|
||||
@@ -42,7 +42,14 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
|
||||
let dbPool = try makeDatabasePool()
|
||||
let counter = try Counter(dbPool: dbPool) // 0
|
||||
try dbPool.write(counter.increment) // 1
|
||||
let snapshot = try dbPool.write { db in try DatabaseSnapshotPool(db) } // locked at 1
|
||||
// We can't open a DatabaseSnapshotPool from an IMMEDIATE
|
||||
// transaction (as documented by sqlite3_snapshot_get). So we
|
||||
// force a DEFERRED transaction:
|
||||
var snapshot: DatabaseSnapshotPool!
|
||||
try dbPool.writeInTransaction(.deferred) { db in
|
||||
snapshot = try DatabaseSnapshotPool(db) // locked at 1
|
||||
return .commit
|
||||
}
|
||||
try dbPool.write(counter.increment) // 2
|
||||
|
||||
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
||||
@@ -55,7 +62,9 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
|
||||
let dbPool = try makeDatabasePool()
|
||||
let counter = try Counter(dbPool: dbPool) // 0
|
||||
try dbPool.write(counter.increment) // 1
|
||||
let snapshot = try dbPool.writeWithoutTransaction { db in try DatabaseSnapshotPool(db) } // locked at 1
|
||||
let snapshot = try dbPool.writeWithoutTransaction { db in
|
||||
try DatabaseSnapshotPool(db) // locked at 1
|
||||
}
|
||||
try dbPool.write(counter.increment) // 2
|
||||
|
||||
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
||||
|
||||
@@ -507,7 +507,7 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
try db.execute(sql: "SELECT * FROM sqlite_master")
|
||||
XCTAssertEqual(db.journalModeCache, "wal")
|
||||
}
|
||||
try dbPool.write { db in
|
||||
dbPool.writeWithoutTransaction { db in
|
||||
XCTAssertEqual(db.journalModeCache, "wal")
|
||||
}
|
||||
try dbPool.read { db in
|
||||
|
||||
@@ -505,16 +505,54 @@ class DatabaseTests : GRDBTestCase {
|
||||
try dbQueue.inTransaction { db in .commit }
|
||||
}
|
||||
|
||||
func testImplicitTransactionManagement() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
try dbQueue.read { db in
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
||||
}
|
||||
|
||||
try dbQueue.write { db in
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN IMMEDIATE TRANSACTION")
|
||||
}
|
||||
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try db.beginTransaction()
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN IMMEDIATE TRANSACTION")
|
||||
try db.rollback()
|
||||
XCTAssertEqual(lastSQLQuery, "ROLLBACK TRANSACTION")
|
||||
|
||||
try db.inSavepoint {
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN IMMEDIATE TRANSACTION")
|
||||
return .commit
|
||||
}
|
||||
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||
|
||||
try db.readOnly {
|
||||
try db.beginTransaction()
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
||||
try db.rollback()
|
||||
XCTAssertEqual(lastSQLQuery, "ROLLBACK TRANSACTION")
|
||||
|
||||
try db.inSavepoint {
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
||||
return .rollback
|
||||
}
|
||||
XCTAssertEqual(lastSQLQuery, "ROLLBACK TRANSACTION")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExplicitTransactionManagement() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
try dbQueue.writeWithoutTransaction { db in
|
||||
try db.beginTransaction()
|
||||
try db.beginTransaction(.deferred)
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
||||
try db.rollback()
|
||||
XCTAssertEqual(lastSQLQuery, "ROLLBACK TRANSACTION")
|
||||
try db.beginTransaction(.immediate)
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN IMMEDIATE TRANSACTION")
|
||||
try db.commit()
|
||||
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||
try db.beginTransaction(.exclusive)
|
||||
XCTAssertEqual(lastSQLQuery, "BEGIN EXCLUSIVE TRANSACTION")
|
||||
try db.commit()
|
||||
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||
}
|
||||
@@ -561,7 +599,6 @@ class DatabaseTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
func testReadOnlyTransaction() throws {
|
||||
dbConfiguration.defaultTransactionKind = .immediate
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
do {
|
||||
|
||||
Reference in new issue
Block a user