Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a3d6587

Browse files
clayellistanner0101
andauthored
Fix transaction rollback with top-level throw (#90)
* Fix GH89 by adding catchFlatMap to catch transaction closure throws. Add test. * Add tests to allTests * Update the contribute script to use docker-compose instead of docker-machine. Co-authored-by: Tanner <me@tanner.xyz>
1 parent c00c474 commit a3d6587

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extension PostgreSQLDatabase: TransactionSupporting {
22
/// See `TransactionSupporting`.
33
public static func transactionExecute<T>(_ transaction: @escaping (PostgreSQLConnection) throws -> Future<T>, on connection: PostgreSQLConnection) -> Future<T> {
4+
func rollback(error: Error) -> Future<T> {
5+
return connection.simpleQuery("ROLLBACK").map { throw error }
6+
}
7+
48
return connection.simpleQuery("BEGIN TRANSACTION").flatMap { results in
59
return try transaction(connection).flatMap { res in
610
return connection.simpleQuery("END TRANSACTION").transform(to: res)
7-
}.catchFlatMap { error in
8-
return connection.simpleQuery("ROLLBACK").map { results in
9-
throw error
10-
}
11-
}
12-
}
11+
}.catchFlatMap(rollback)
12+
}.catchFlatMap(rollback)
1313
}
1414
}

β€ŽTests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,45 @@ class FluentPostgreSQLTests: XCTestCase {
458458
try C.prepare(on: conn).wait()
459459
defer { try? C.revert(on: conn).wait() }
460460
}
461+
462+
// https://github.com/vapor/fluent-postgresql/issues/89
463+
func testGH89() throws {
464+
let conn = try benchmarker.pool.requestConnection().wait()
465+
conn.logger = DatabaseLogger(database: .psql, handler: PrintLogHandler())
466+
defer { benchmarker.pool.releaseConnection(conn) }
467+
468+
try Planet.prepare(on: conn).wait()
469+
defer { try? Planet.revert(on: conn).wait() }
470+
471+
enum SomeError: Error {
472+
case error
473+
}
474+
475+
func alwaysThrows() throws {
476+
throw SomeError.error
477+
}
478+
479+
var a = Planet(name: "Pluto")
480+
a = try a.save(on: conn).wait()
481+
482+
do {
483+
_ = try conn.transaction(on: .psql) { transaction -> Future<Planet> in
484+
a.name = "No Longer A Planet"
485+
let save = a.save(on: transaction)
486+
try alwaysThrows()
487+
return save
488+
}.wait()
489+
} catch {
490+
// No-op
491+
}
492+
493+
a = try Planet.query(on: conn)
494+
.filter(\.id == a.requireID())
495+
.first()
496+
.wait()!
497+
498+
XCTAssertEqual(a.name, "Pluto")
499+
}
461500

462501
// https://github.com/vapor/fluent-postgresql/issues/85
463502
func testGH85() throws {
@@ -555,8 +594,10 @@ class FluentPostgreSQLTests: XCTestCase {
555594
("testCustomFilter", testCustomFilter),
556595
("testCreateOrUpdate", testCreateOrUpdate),
557596
("testEnumArray", testEnumArray),
597+
("testAlterDrop", testAlterDrop),
598+
("testGH89", testGH89),
558599
("testGH85", testGH85),
559-
("testGH35", testGH35),
600+
("testGH35", testGH35)
560601
]
561602
}
562603

β€Žcontribute_boostrap.sh

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
echo "πŸ’§ starting docker..."
2-
docker-machine start default
3-
4-
echo "πŸ’§ exporting docker machine environment..."
5-
eval $(docker-machine env default)
6-
7-
echo "πŸ’§ cleaning previous vapor-psql dev db..."
8-
docker stop vapor-psql
9-
docker rm vapor-psql
10-
11-
echo "πŸ’§ creating vapor-psql dev db..."
12-
docker run --name vapor-psql -e POSTGRES_USER=vapor_username -e POSTGRES_DB=vapor_database -p 5432:5432 -d postgres:latest
13-
141
echo "πŸ’§ generating xcode proj..."
152
swift package generate-xcodeproj
163

17-
echo "πŸ’§ add the following env variable to Xcode test scheme:"
18-
echo ""
19-
echo " PSQL_HOSTNAME: `docker-machine ip`"
20-
echo ""
21-
224
echo "πŸ’§ opening xcode..."
23-
open *.xcodeproj
5+
open *.xcodeproj
6+
7+
echo "πŸ’§ starting docker..."
8+
docker-compose up psql-10

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /