mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +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
|
// 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
|
/// A boolean value indicating whether it is valid to leave a transaction
|
||||||
/// opened at the end of a database access method.
|
/// opened at the end of a database access method.
|
||||||
///
|
///
|
||||||
|
|||||||
+15
-16
@@ -1282,14 +1282,10 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
/// Use ``inSavepoint(_:)`` instead.
|
/// Use ``inSavepoint(_:)`` instead.
|
||||||
///
|
///
|
||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - kind: The transaction type (default nil).
|
/// - kind: The transaction type.
|
||||||
///
|
///
|
||||||
/// If nil, and the database connection is read-only, the transaction
|
/// If nil, the transaction kind is DEFERRED when the current
|
||||||
/// kind is ``TransactionKind/deferred``.
|
/// database access is read-only, and IMMEDIATE otherwise.
|
||||||
///
|
|
||||||
/// If nil, and the database connection is not read-only, the
|
|
||||||
/// transaction kind is the ``Configuration/defaultTransactionKind``
|
|
||||||
/// of the ``configuration``.
|
|
||||||
/// - operations: A function that executes SQL statements and returns
|
/// - operations: A function that executes SQL statements and returns
|
||||||
/// either ``TransactionCompletion/commit`` or ``TransactionCompletion/rollback``.
|
/// either ``TransactionCompletion/commit`` or ``TransactionCompletion/rollback``.
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or the
|
/// - 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
|
// By default, top level SQLite savepoints open a
|
||||||
// deferred transaction.
|
// deferred transaction.
|
||||||
//
|
//
|
||||||
// But GRDB database configuration mandates a default transaction
|
// But GRDB prefers immediate transactions for writes.
|
||||||
// kind that we have to honor.
|
|
||||||
//
|
//
|
||||||
// Besides, starting some (?) SQLCipher/SQLite version, SQLite has a
|
// Besides, starting some (?) SQLCipher/SQLite version, SQLite has a
|
||||||
// bug. Returning 1 from `sqlite3_commit_hook` does not leave the
|
// bug. Returning 1 from `sqlite3_commit_hook` does not leave the
|
||||||
@@ -1504,16 +1499,20 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - kind: The transaction type (default nil).
|
/// - kind: The transaction type (default nil).
|
||||||
///
|
///
|
||||||
/// If nil, and the database connection is read-only, the transaction
|
/// If nil, the transaction kind is DEFERRED when the current
|
||||||
/// kind is ``TransactionKind/deferred``.
|
/// database access is read-only, and IMMEDIATE otherwise.
|
||||||
///
|
|
||||||
/// If nil, and the database connection is not read-only, the
|
|
||||||
/// transaction kind is the ``Configuration/defaultTransactionKind``
|
|
||||||
/// of the ``configuration``.
|
|
||||||
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
|
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs.
|
||||||
public func beginTransaction(_ kind: TransactionKind? = nil) throws {
|
public func beginTransaction(_ kind: TransactionKind? = nil) throws {
|
||||||
// SQLite throws an error for non-deferred transactions when read-only.
|
// 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")
|
try execute(sql: "BEGIN \(kind.rawValue) TRANSACTION")
|
||||||
assert(sqlite3_get_autocommit(sqliteConnection) == 0)
|
assert(sqlite3_get_autocommit(sqliteConnection) == 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,10 +118,6 @@ public final class DatabasePool {
|
|||||||
|
|
||||||
configuration.readonly = true
|
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>
|
// <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
|
// > But there are some obscure cases where a query against a WAL-mode
|
||||||
// > database can return SQLITE_BUSY, so applications should be prepared
|
// > database can return SQLITE_BUSY, so applications should be prepared
|
||||||
@@ -787,9 +783,10 @@ extension DatabasePool: DatabaseWriter {
|
|||||||
///
|
///
|
||||||
/// - precondition: This method is not reentrant.
|
/// - precondition: This method is not reentrant.
|
||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
/// - kind: The transaction type (default nil).
|
||||||
/// type is the ``Configuration/defaultTransactionKind`` of the
|
///
|
||||||
/// the ``configuration``.
|
/// If nil, the transaction kind is DEFERRED when the database
|
||||||
|
/// connection is read-only, and IMMEDIATE otherwise.
|
||||||
/// - updates: A function that updates the database.
|
/// - updates: A function that updates the database.
|
||||||
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
||||||
public func writeInTransaction(
|
public func writeInTransaction(
|
||||||
|
|||||||
@@ -352,9 +352,10 @@ extension DatabaseQueue: DatabaseWriter {
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - kind: The transaction type (default nil). If nil, the transaction
|
/// - kind: The transaction type (default nil).
|
||||||
/// type is the ``Configuration/defaultTransactionKind`` of the
|
///
|
||||||
/// the ``configuration``.
|
/// If nil, the transaction kind is DEFERRED when the database
|
||||||
|
/// connection is read-only, and IMMEDIATE otherwise.
|
||||||
/// - updates: A function that updates the database.
|
/// - updates: A function that updates the database.
|
||||||
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
/// - throws: The error thrown by `updates`, or by the wrapping transaction.
|
||||||
public func inTransaction(
|
public func inTransaction(
|
||||||
|
|||||||
@@ -126,10 +126,6 @@ public final class DatabaseSnapshot {
|
|||||||
// DatabaseSnapshot is read-only.
|
// DatabaseSnapshot is read-only.
|
||||||
configuration.readonly = true
|
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.
|
// DatabaseSnapshot keeps a long-lived transaction.
|
||||||
configuration.allowsUnsafeTransactions = true
|
configuration.allowsUnsafeTransactions = true
|
||||||
|
|
||||||
|
|||||||
@@ -242,10 +242,6 @@ public final class DatabaseSnapshotPool {
|
|||||||
// DatabaseSnapshotPool is read-only.
|
// DatabaseSnapshotPool is read-only.
|
||||||
configuration.readonly = true
|
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.
|
// DatabaseSnapshotPool keeps a long-lived transaction.
|
||||||
configuration.allowsUnsafeTransactions = true
|
configuration.allowsUnsafeTransactions = true
|
||||||
|
|
||||||
|
|||||||
@@ -152,12 +152,11 @@ If several processes want to write in the database, configure the database pool
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
var configuration = Configuration()
|
var configuration = Configuration()
|
||||||
configuration.defaultTransactionKind = .immediate
|
|
||||||
configuration.busyMode = .timeout(/* a TimeInterval */)
|
configuration.busyMode = .timeout(/* a TimeInterval */)
|
||||||
let dbPool = try DatabasePool(path: ..., configuration: configuration)
|
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:
|
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
|
### Configuring GRDB Connections
|
||||||
|
|
||||||
- ``allowsUnsafeTransactions``
|
- ``allowsUnsafeTransactions``
|
||||||
- ``defaultTransactionKind``
|
|
||||||
- ``label``
|
- ``label``
|
||||||
- ``maximumReaderCount``
|
- ``maximumReaderCount``
|
||||||
- ``observesSuspensionNotifications``
|
- ``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.
|
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:
|
The transaction kind can be chosen for individual transaction:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
@@ -222,20 +224,3 @@ let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")
|
|||||||
// BEGIN EXCLUSIVE TRANSACTION ...
|
// BEGIN EXCLUSIVE TRANSACTION ...
|
||||||
try dbQueue.inTransaction(.exclusive) { db in ... }
|
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.
|
- [ ] 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
|
- [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: 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
|
- [ ] GRDB7: Sendable conformances
|
||||||
- [ ] anydatabasecancellable
|
- [ ] anydatabasecancellable
|
||||||
- [ ] anydatabaseregionconvertible
|
- [ ] anydatabaseregionconvertible
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class BackupTestCase: GRDBTestCase {
|
|||||||
let sourceDbPageCount = try setupBackupSource(source)
|
let sourceDbPageCount = try setupBackupSource(source)
|
||||||
try setupBackupDestination(destination)
|
try setupBackupDestination(destination)
|
||||||
|
|
||||||
try source.write { sourceDb in
|
try source.read { sourceDb in
|
||||||
try destination.barrierWriteWithoutTransaction { destDb in
|
try destination.barrierWriteWithoutTransaction { destDb in
|
||||||
XCTAssertThrowsError(
|
XCTAssertThrowsError(
|
||||||
try sourceDb.backup(to: destDb, pagesPerStep: 1) { progress in
|
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)
|
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
|
try destination.barrierWriteWithoutTransaction { dbDest in
|
||||||
var progressCount: Int = 1
|
var progressCount: Int = 1
|
||||||
var isCompleted: Bool = false
|
var isCompleted: Bool = false
|
||||||
|
|||||||
@@ -369,8 +369,6 @@ class DatabaseQueueTests: GRDBTestCase {
|
|||||||
func test_busy_timeout_and_IMMEDIATE_transactions_do_prevent_SQLITE_BUSY() throws {
|
func test_busy_timeout_and_IMMEDIATE_transactions_do_prevent_SQLITE_BUSY() throws {
|
||||||
var configuration = dbConfiguration!
|
var configuration = dbConfiguration!
|
||||||
// Test fails when this line is commented
|
// Test fails when this line is commented
|
||||||
configuration.defaultTransactionKind = .immediate
|
|
||||||
// Test fails when this line is commented
|
|
||||||
configuration.busyMode = .timeout(10)
|
configuration.busyMode = .timeout(10)
|
||||||
|
|
||||||
let dbQueue = try makeDatabaseQueue(filename: "test")
|
let dbQueue = try makeDatabaseQueue(filename: "test")
|
||||||
|
|||||||
@@ -96,226 +96,8 @@ class DatabaseSavepointTests: GRDBTestCase {
|
|||||||
XCTAssertThrowsError(try db.execute(sql: "COMMIT"))
|
XCTAssertThrowsError(try db.execute(sql: "COMMIT"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testReleaseTopLevelSavepointFromDatabaseWithDefaultDeferredTransactions() throws {
|
func testReleaseTopLevelSavepoint() 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
|
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
let observer = Observer()
|
let observer = Observer()
|
||||||
dbQueue.add(transactionObserver: observer)
|
dbQueue.add(transactionObserver: observer)
|
||||||
@@ -344,8 +126,7 @@ class DatabaseSavepointTests: GRDBTestCase {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRollbackTopLevelSavepointFromDatabaseWithDefaultImmediateTransactions() throws {
|
func testRollbackTopLevelSavepoint() throws {
|
||||||
dbConfiguration.defaultTransactionKind = .immediate
|
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
let observer = Observer()
|
let observer = Observer()
|
||||||
dbQueue.add(transactionObserver: observer)
|
dbQueue.add(transactionObserver: observer)
|
||||||
@@ -374,8 +155,7 @@ class DatabaseSavepointTests: GRDBTestCase {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func testNestedSavepointFromDatabaseWithDefaultImmediateTransactions() throws {
|
func testNestedSavepoint() throws {
|
||||||
dbConfiguration.defaultTransactionKind = .immediate
|
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
let observer = Observer()
|
let observer = Observer()
|
||||||
dbQueue.add(transactionObserver: observer)
|
dbQueue.add(transactionObserver: observer)
|
||||||
|
|||||||
@@ -42,7 +42,14 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
|
|||||||
let dbPool = try makeDatabasePool()
|
let dbPool = try makeDatabasePool()
|
||||||
let counter = try Counter(dbPool: dbPool) // 0
|
let counter = try Counter(dbPool: dbPool) // 0
|
||||||
try dbPool.write(counter.increment) // 1
|
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 dbPool.write(counter.increment) // 2
|
||||||
|
|
||||||
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
||||||
@@ -55,7 +62,9 @@ final class DatabaseSnapshotPoolTests: GRDBTestCase {
|
|||||||
let dbPool = try makeDatabasePool()
|
let dbPool = try makeDatabasePool()
|
||||||
let counter = try Counter(dbPool: dbPool) // 0
|
let counter = try Counter(dbPool: dbPool) // 0
|
||||||
try dbPool.write(counter.increment) // 1
|
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 dbPool.write(counter.increment) // 2
|
||||||
|
|
||||||
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
try XCTAssertEqual(dbPool.read(counter.value), 2)
|
||||||
|
|||||||
@@ -507,7 +507,7 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
|||||||
try db.execute(sql: "SELECT * FROM sqlite_master")
|
try db.execute(sql: "SELECT * FROM sqlite_master")
|
||||||
XCTAssertEqual(db.journalModeCache, "wal")
|
XCTAssertEqual(db.journalModeCache, "wal")
|
||||||
}
|
}
|
||||||
try dbPool.write { db in
|
dbPool.writeWithoutTransaction { db in
|
||||||
XCTAssertEqual(db.journalModeCache, "wal")
|
XCTAssertEqual(db.journalModeCache, "wal")
|
||||||
}
|
}
|
||||||
try dbPool.read { db in
|
try dbPool.read { db in
|
||||||
|
|||||||
@@ -505,16 +505,54 @@ class DatabaseTests : GRDBTestCase {
|
|||||||
try dbQueue.inTransaction { db in .commit }
|
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 {
|
func testExplicitTransactionManagement() throws {
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
|
|
||||||
try dbQueue.writeWithoutTransaction { db in
|
try dbQueue.writeWithoutTransaction { db in
|
||||||
try db.beginTransaction()
|
try db.beginTransaction(.deferred)
|
||||||
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
XCTAssertEqual(lastSQLQuery, "BEGIN DEFERRED TRANSACTION")
|
||||||
try db.rollback()
|
try db.commit()
|
||||||
XCTAssertEqual(lastSQLQuery, "ROLLBACK TRANSACTION")
|
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||||
try db.beginTransaction(.immediate)
|
try db.beginTransaction(.exclusive)
|
||||||
XCTAssertEqual(lastSQLQuery, "BEGIN IMMEDIATE TRANSACTION")
|
XCTAssertEqual(lastSQLQuery, "BEGIN EXCLUSIVE TRANSACTION")
|
||||||
try db.commit()
|
try db.commit()
|
||||||
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||||
}
|
}
|
||||||
@@ -561,7 +599,6 @@ class DatabaseTests : GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testReadOnlyTransaction() throws {
|
func testReadOnlyTransaction() throws {
|
||||||
dbConfiguration.defaultTransactionKind = .immediate
|
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
do {
|
do {
|
||||||
|
|||||||
Reference in new issue
Block a user