Skip to content

Commit

Permalink
Log transaction start and end queries (#95)
Browse files Browse the repository at this point in the history
Route transaction control queries through the SQLKit interface so they get logged properly.
  • Loading branch information
gwynne authored May 29, 2024
1 parent 2362c28 commit 40303a2
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Sources/FluentSQLiteDriver/FluentSQLiteDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,23 @@ struct FluentSQLiteDatabase: Database, SQLDatabase, SQLiteDatabase {
}

return try await self.withConnection { conn in
_ = try await conn.query("BEGIN TRANSACTION")
let db = FluentSQLiteDatabase(
database: conn,
context: self.context,
dataEncoder: self.dataEncoder,
dataDecoder: self.dataDecoder,
queryLogLevel: self.queryLogLevel,
inTransaction: true
)

try await db.raw("BEGIN TRANSACTION").run()
do {
let result = try await closure(FluentSQLiteDatabase(
database: conn,
context: self.context,
dataEncoder: self.dataEncoder,
dataDecoder: self.dataDecoder,
queryLogLevel: self.queryLogLevel,
inTransaction: true
))
let result = try await closure(db)

_ = try await conn.query("COMMIT TRANSACTION")
try await db.raw("COMMIT TRANSACTION").run()
return result
} catch {
_ = try? await conn.query("ROLLBACK TRANSACTION")
try? await db.raw("ROLLBACK TRANSACTION").run()
throw error
}
}
Expand Down

0 comments on commit 40303a2

Please sign in to comment.