Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Sources/SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,27 @@ public final class Connection {
let name = name.quote("'")
let savepoint = "SAVEPOINT \(name)"

try transaction(savepoint, block, "RELEASE \(savepoint)", or: "ROLLBACK TO \(savepoint)")
try transaction(
savepoint,
block,
"RELEASE \(savepoint)",
or: "ROLLBACK TO \(savepoint)",
followedBy: "RELEASE \(savepoint)"
)
}

fileprivate func transaction(_ begin: String, _ block: () throws -> Void, _ commit: String, or rollback: String) throws {
fileprivate func transaction(_ begin: String, _ block: () throws -> Void, _ commit: String,
or rollback: String, followedBy cleanup: String? = nil) throws {
return try sync {
try self.run(begin)
do {
try block()
try self.run(commit)
} catch {
try self.run(rollback)
if let cleanup {
try self.run(cleanup)
}
throw error
}
}
Expand Down
23 changes: 21 additions & 2 deletions Tests/SQLiteTests/Core/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,27 @@ class ConnectionTests: SQLiteTestCase {
assertSQL("INSERT INTO users (email) VALUES ('alice@example.com')", 2)
assertSQL("ROLLBACK TO SAVEPOINT '2'")
assertSQL("ROLLBACK TO SAVEPOINT '1'")
assertSQL("RELEASE SAVEPOINT '2'", 0)
assertSQL("RELEASE SAVEPOINT '1'", 0)
assertSQL("RELEASE SAVEPOINT '2'")
assertSQL("RELEASE SAVEPOINT '1'")
}

func test_savepoint_releasesAfterRollback() throws {
let rollbackError = NSError(domain: "com.stephencelis.SQLiteTests", code: 1, userInfo: nil)

XCTAssertThrowsError(try db.savepoint("1") {
try db.run("INSERT INTO users (email) VALUES (?)", "alice@example.com")
throw rollbackError
}) { error in
let error = error as NSError
XCTAssertEqual(rollbackError.domain, error.domain)
XCTAssertEqual(rollbackError.code, error.code)
}

XCTAssertEqual(0, try db.scalar(users.count))
try db.transaction {
try db.run("INSERT INTO users (email) VALUES (?)", "alice@example.com")
}
XCTAssertEqual(1, try db.scalar(users.count))
}

func test_updateHook_setsUpdateHook_withInsert() throws {
Expand Down