Skip to content

Commit 2a6df80

Browse files
committed
transactions passing
1 parent c1ad2fc commit 2a6df80

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Async
2+
3+
extension PostgreSQLDatabase: TransactionSupporting {
4+
/// See `TransactionSupporting.execute(transaction:on:)`
5+
public static func execute(transaction: DatabaseTransaction<PostgreSQLDatabase>, on connection: PostgreSQLConnection) -> Future<Void> {
6+
return connection.simpleQuery("BEGIN TRANSACTION").flatMap(to: Void.self) { results in
7+
return transaction.run(on: connection).flatMap(to: Void.self) { void in
8+
return connection.simpleQuery("END TRANSACTION").transform(to: ())
9+
}.flatMapError { error in
10+
return connection.simpleQuery("ROLLBACK").map(to: Void.self) { results in
11+
throw error
12+
}
13+
}
14+
}
15+
}
16+
}
17+
18+
19+
extension Future {
20+
func mapError(_ callback: @escaping (Error) -> (Expectation)) -> Future<Expectation> {
21+
let promise = Promise(Expectation.self)
22+
addAwaiter { result in
23+
switch result {
24+
case .error(let error): promise.complete(callback(error))
25+
case .expectation(let e): promise.complete(e)
26+
}
27+
}
28+
return promise.future
29+
}
30+
31+
32+
func flatMapError(_ callback: @escaping (Error) -> (Future<Expectation>)) -> Future<Expectation> {
33+
let promise = Promise(Expectation.self)
34+
addAwaiter { result in
35+
switch result {
36+
case .error(let error): callback(error).chain(to: promise)
37+
case .expectation(let e): promise.complete(e)
38+
}
39+
}
40+
return promise.future
41+
}
42+
}

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class FluentPostgreSQLTests: XCTestCase {
2929
try benchmarker.benchmarkTimestampable_withSchema()
3030
}
3131

32-
// func testTransactions() throws {
33-
// try benchmarker.benchmarkTransactions_withSchema().blockingAwait(timeout: .seconds(60))
34-
// }
32+
func testTransactions() throws {
33+
try benchmarker.benchmarkTransactions_withSchema()
34+
}
3535

3636
func testChunking() throws {
3737
try benchmarker.benchmarkChunking_withSchema()
@@ -46,7 +46,7 @@ class FluentPostgreSQLTests: XCTestCase {
4646
("testModels", testModels),
4747
("testRelations", testRelations),
4848
("testTimestampable", testTimestampable),
49-
// ("testTransactions", testTransactions),
49+
("testTransactions", testTransactions),
5050
("testChunking", testChunking),
5151
("testAutoincrement", testAutoincrement),
5252
]

0 commit comments

Comments
 (0)