Skip to content

Commit

Permalink
Actually support query logging (#225)
Browse files Browse the repository at this point in the history
* Support query log levels. Also fix some Sendable stuff.

* Use logging metadata for logging the transaction control queries
  • Loading branch information
gwynne authored May 29, 2024
1 parent 4f07d7f commit 93e7254
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.8.0"),
.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.9.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.9.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.4"),
.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.8.0"),
.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.9.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentMySQLDriver/Docs.docc/theme-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"logo-shape": { "dark": "#000", "light": "#fff" },
"fill": { "dark": "#000", "light": "#fff" }
},
"icons": { "technology": "/mysqlkit/images/vapor-fluentmysqldriver-logo.svg" }
"icons": { "technology": "/fluentmysqldriver/images/vapor-fluentmysqldriver-logo.svg" }
},
"features": {
"quickNavigation": { "enable": true },
Expand Down
39 changes: 27 additions & 12 deletions Sources/FluentMySQLDriver/FluentMySQLConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AsyncKit
import struct NIO.TimeAmount
import FluentKit
import MySQLKit
import Logging

extension DatabaseConfigurationFactory {
/// Create a database configuration factory for connecting to a server through a UNIX domain socket.
Expand All @@ -24,7 +25,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
sqlLogLevel: Logger.Level? = .debug
) throws -> Self {
let configuration = MySQLConfiguration(
unixDomainSocketPath: unixDomainSocketPath,
Expand All @@ -37,7 +39,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
encoder: encoder,
decoder: decoder
decoder: decoder,
sqlLogLevel: sqlLogLevel
)
}

Expand All @@ -56,17 +59,19 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
sqlLogLevel: Logger.Level? = .debug
) throws -> Self {
guard let url = URL(string: urlString) else {
throw FluentMySQLError.invalidURL(urlString)
}
return try self.mysql(
return try .mysql(
url: url,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
encoder: encoder,
decoder: decoder
decoder: decoder,
sqlLogLevel: sqlLogLevel
)
}

Expand All @@ -85,7 +90,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
sqlLogLevel: Logger.Level? = .debug
) throws -> Self {
guard let configuration = MySQLConfiguration(url: url) else {
throw FluentMySQLError.invalidURL(url.absoluteString)
Expand All @@ -95,7 +101,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
encoder: encoder,
decoder: decoder
decoder: decoder,
sqlLogLevel: sqlLogLevel
)
}

Expand Down Expand Up @@ -123,7 +130,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
sqlLogLevel: Logger.Level? = .debug
) -> Self {
.mysql(
configuration: .init(
Expand All @@ -137,7 +145,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
encoder: encoder,
decoder: decoder
decoder: decoder,
sqlLogLevel: sqlLogLevel
)
}

Expand All @@ -155,7 +164,8 @@ extension DatabaseConfigurationFactory {
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
encoder: MySQLDataEncoder = .init(),
decoder: MySQLDataDecoder = .init()
decoder: MySQLDataDecoder = .init(),
sqlLogLevel: Logger.Level? = .debug
) -> Self {
Self {
FluentMySQLConfiguration(
Expand All @@ -164,6 +174,7 @@ extension DatabaseConfigurationFactory {
connectionPoolTimeout: connectionPoolTimeout,
encoder: encoder,
decoder: decoder,
sqlLogLevel: sqlLogLevel,
middleware: []
)
}
Expand All @@ -187,6 +198,9 @@ struct FluentMySQLConfiguration: DatabaseConfiguration {
/// A `MySQLDataDecoder` used to translate `MySQLData` values into output values in `SQLRow`s.
let decoder: MySQLDataDecoder

/// A logging level used for logging queries.
let sqlLogLevel: Logger.Level?

// See `DatabaseConfiguration.middleware`.
var middleware: [any AnyModelMiddleware]

Expand All @@ -201,10 +215,11 @@ struct FluentMySQLConfiguration: DatabaseConfiguration {
requestTimeout: self.connectionPoolTimeout,
on: databases.eventLoopGroup
)
return _FluentMySQLDriver(
return FluentMySQLDriver(
pool: pool,
encoder: self.encoder,
decoder: self.decoder
decoder: self.decoder,
sqlLogLevel: self.sqlLogLevel
)
}
}
Loading

0 comments on commit 93e7254

Please sign in to comment.