Skip to content

Commit

Permalink
Use logging metadata for logging the transaction control queries
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed May 28, 2024
1 parent 6aa7924 commit 40f6710
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Sources/FluentMySQLDriver/FluentMySQLDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,18 @@ struct FluentMySQLDatabase: Database, SQLDatabase, MySQLDatabase {
inTransaction: true
)

if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "START TRANSACTION []") }
// N.B.: We cannot route the transaction start/finish queries through the SQLKit interface due to
// the limitations of MySQLNIO, so we have to use the MySQLNIO interface and log the queries manually.
if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": "START TRANSACTION", "binds": []]) }
_ = try await conn.simpleQuery("START TRANSACTION").get()
do {
let result = try await closure(db)

if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "COMMIT []") }
if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": "COMMIT", "binds": []]) }
_ = try await conn.simpleQuery("COMMIT").get()
return result
} catch {
if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "ROLLBACK []") }
if let queryLogLevel = db.queryLogLevel { db.logger.log(level: queryLogLevel, "Executing query", metadata: ["sql": "ROLLBACK", "binds": []]) }
_ = try? await conn.simpleQuery("ROLLBACK").get()
throw error
}
Expand Down

0 comments on commit 40f6710

Please sign in to comment.