Skip to content

Commit c5ec19d

Browse files
0xTimgwynne
andauthored
Make SQL Log Level Configurable (#182)
* Add SQL Log Level as a configurable option * Use new log level * Just some more CI updates. Could do this in my sleep. In fact, I think I am right now. Co-authored-by: Gwynne Raskind <[email protected]>
1 parent afe159c commit c5ec19d

File tree

4 files changed

+77
-40
lines changed

4 files changed

+77
-40
lines changed

.github/workflows/test.yml

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,42 @@ on:
33
pull_request:
44
push:
55
branches:
6-
- master
6+
- main
77
jobs:
88
linux:
99
runs-on: ubuntu-latest
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
image:
14-
# 5.2 Stable
13+
runner:
1514
- swift:5.2-xenial
1615
- swift:5.2-bionic
17-
# 5.2 Unstable
18-
- swiftlang/swift:nightly-5.2-xenial
19-
- swiftlang/swift:nightly-5.2-bionic
20-
# 5.3 Unstable
21-
- swiftlang/swift:nightly-5.3-xenial
22-
- swiftlang/swift:nightly-5.3-bionic
23-
# Master Unsable
24-
- swiftlang/swift:nightly-master-xenial
25-
- swiftlang/swift:nightly-master-bionic
26-
- swiftlang/swift:nightly-master-focal
27-
- swiftlang/swift:nightly-master-centos8
28-
- swiftlang/swift:nightly-master-amazonlinux2
16+
- swift:5.2-focal
17+
- swift:5.2-centos7
18+
- swift:5.2-centos8
19+
- swift:5.2-amazonlinux2
20+
- swift:5.3-xenial
21+
- swift:5.3-bionic
22+
- swift:5.3-focal
23+
- swift:5.3-centos7
24+
- swift:5.3-centos8
25+
- swift:5.3-amazonlinux2
26+
- swift:5.4-bionic
27+
- swift:5.4-focal
28+
- swift:5.4-centos7
29+
- swift:5.4-centos8
30+
- swift:5.4-amazonlinux2
31+
- swiftlang/swift:nightly-5.5-focal
32+
- swiftlang/swift:nightly-5.5-centos8
33+
- swiftlang/swift:nightly-5.5-amazonlinux2
34+
- swiftlang/swift:nightly-main-focal
35+
- swiftlang/swift:nightly-main-centos8
36+
- swiftlang/swift:nightly-main-amazonlinux2
2937
dbimage:
3038
- postgres:11
3139
- postgres:12
3240
- postgres:13
33-
container: ${{ matrix.image }}
41+
container: ${{ matrix.runner }}
3442
services:
3543
postgres-a:
3644
image: ${{ matrix.dbimage }}
@@ -49,6 +57,12 @@ jobs:
4957
POSTGRES_HOSTNAME_B: postgres-b
5058
LOG_LEVEL: info
5159
steps:
60+
- name: Workaround SPM incompatibility with old Git on CentOS 7
61+
if: ${{ contains(matrix.runner, 'centos7') }}
62+
run: |
63+
yum install -y make libcurl-devel
64+
git clone https://github.com/git/git -bv2.28.0 --depth 1 && cd git
65+
make prefix=/usr -j all install NO_OPENSSL=1 NO_EXPAT=1 NO_TCLTK=1 NO_GETTEXT=1 NO_PERL=1
5266
- name: Checkout code
5367
uses: actions/checkout@v2
5468
- name: Run tests with Thread Sanitizer
@@ -62,15 +76,18 @@ jobs:
6276
datadir: postgresql@11
6377
- formula: postgresql@12
6478
datadir: postgres
79+
xcode:
80+
- latest
81+
- latest-stable
6582
env:
6683
POSTGRES_DATABASE_A: vapor_database_a
6784
POSTGRES_DATABASE_B: vapor_database_b
6885
runs-on: macos-latest
6986
steps:
7087
- name: Select latest available Xcode
71-
uses: maxim-lobanov/setup-xcode@1.0
72-
with:
73-
xcode-version: latest
88+
uses: maxim-lobanov/setup-xcode@v1
89+
with:
90+
xcode-version: ${{ matrix.xcode }}
7491
- name: Replace Postgres install and start server
7592
run: |
7693
brew uninstall --force postgresql php && rm -rf /usr/local/{etc,var}/{postgres,pg}*

Sources/FluentPostgresDriver/FluentPostgresConfiguration.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import Logging
2+
13
extension DatabaseConfigurationFactory {
24
public static func postgres(
35
url urlString: String,
46
maxConnectionsPerEventLoop: Int = 1,
57
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
68
encoder: PostgresDataEncoder = .init(),
7-
decoder: PostgresDataDecoder = .init()
9+
decoder: PostgresDataDecoder = .init(),
10+
sqlLogLevel: Logger.Level = .debug
811
) throws -> DatabaseConfigurationFactory {
912
guard let url = URL(string: urlString) else {
1013
throw FluentPostgresError.invalidURL(urlString)
@@ -14,7 +17,8 @@ extension DatabaseConfigurationFactory {
1417
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
1518
connectionPoolTimeout: connectionPoolTimeout,
1619
encoder: encoder,
17-
decoder: decoder
20+
decoder: decoder,
21+
sqlLogLevel: sqlLogLevel
1822
)
1923
}
2024

@@ -23,15 +27,17 @@ extension DatabaseConfigurationFactory {
2327
maxConnectionsPerEventLoop: Int = 1,
2428
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
2529
encoder: PostgresDataEncoder = .init(),
26-
decoder: PostgresDataDecoder = .init()
30+
decoder: PostgresDataDecoder = .init(),
31+
sqlLogLevel: Logger.Level = .debug
2732
) throws -> DatabaseConfigurationFactory {
2833
guard let configuration = PostgresConfiguration(url: url) else {
2934
throw FluentPostgresError.invalidURL(url.absoluteString)
3035
}
3136
return .postgres(
3237
configuration: configuration,
3338
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
34-
connectionPoolTimeout: connectionPoolTimeout
39+
connectionPoolTimeout: connectionPoolTimeout,
40+
sqlLogLevel: sqlLogLevel
3541
)
3642
}
3743

@@ -45,7 +51,8 @@ extension DatabaseConfigurationFactory {
4551
maxConnectionsPerEventLoop: Int = 1,
4652
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
4753
encoder: PostgresDataEncoder = .init(),
48-
decoder: PostgresDataDecoder = .init()
54+
decoder: PostgresDataDecoder = .init(),
55+
sqlLogLevel: Logger.Level = .debug
4956
) -> DatabaseConfigurationFactory {
5057
return .postgres(
5158
configuration: .init(
@@ -57,7 +64,8 @@ extension DatabaseConfigurationFactory {
5764
tlsConfiguration: tlsConfiguration
5865
),
5966
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
60-
connectionPoolTimeout: connectionPoolTimeout
67+
connectionPoolTimeout: connectionPoolTimeout,
68+
sqlLogLevel: sqlLogLevel
6169
)
6270
}
6371

@@ -66,7 +74,8 @@ extension DatabaseConfigurationFactory {
6674
maxConnectionsPerEventLoop: Int = 1,
6775
connectionPoolTimeout: NIO.TimeAmount = .seconds(10),
6876
encoder: PostgresDataEncoder = .init(),
69-
decoder: PostgresDataDecoder = .init()
77+
decoder: PostgresDataDecoder = .init(),
78+
sqlLogLevel: Logger.Level = .debug
7079
) -> DatabaseConfigurationFactory {
7180
return DatabaseConfigurationFactory {
7281
FluentPostgresConfiguration(
@@ -75,7 +84,8 @@ extension DatabaseConfigurationFactory {
7584
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
7685
connectionPoolTimeout: connectionPoolTimeout,
7786
encoder: encoder,
78-
decoder: decoder
87+
decoder: decoder,
88+
sqlLogLevel: sqlLogLevel
7989
)
8090
}
8191
}
@@ -90,6 +100,7 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
90100
let connectionPoolTimeout: NIO.TimeAmount
91101
let encoder: PostgresDataEncoder
92102
let decoder: PostgresDataDecoder
103+
let sqlLogLevel: Logger.Level
93104

94105
func makeDriver(for databases: Databases) -> DatabaseDriver {
95106
let db = PostgresConnectionSource(
@@ -103,8 +114,9 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
103114
)
104115
return _FluentPostgresDriver(
105116
pool: pool,
106-
encoder: encoder,
107-
decoder: decoder
117+
encoder: self.encoder,
118+
decoder: self.decoder,
119+
sqlLogLevel: self.sqlLogLevel
108120
)
109121
}
110122
}

Sources/FluentPostgresDriver/FluentPostgresDatabase.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import FluentSQL
2+
import Logging
23

34
struct _FluentPostgresDatabase {
45
let database: PostgresDatabase
56
let context: DatabaseContext
67
let encoder: PostgresDataEncoder
78
let decoder: PostgresDataDecoder
89
let inTransaction: Bool
10+
let sqlLogLevel: Logger.Level
911
}
1012

1113
extension _FluentPostgresDatabase: Database {
@@ -24,7 +26,7 @@ extension _FluentPostgresDatabase: Database {
2426
default: break
2527
}
2628
let (sql, binds) = self.serialize(expression)
27-
self.logger.debug("\(sql) \(binds)")
29+
self.logger.log(level: self.sqlLogLevel, "\(sql) \(binds)")
2830
do {
2931
return try self.query(sql, binds.map { try self.encoder.encode($0) }) {
3032
onOutput($0.databaseOutput(using: self.decoder))
@@ -38,7 +40,7 @@ extension _FluentPostgresDatabase: Database {
3840
let expression = SQLSchemaConverter(delegate: PostgresConverterDelegate())
3941
.convert(schema)
4042
let (sql, binds) = self.serialize(expression)
41-
self.logger.debug("\(sql) \(binds)")
43+
self.logger.log(level: self.sqlLogLevel, "\(sql) \(binds)")
4244
do {
4345
return try self.query(sql, binds.map { try self.encoder.encode($0) }) {
4446
fatalError("unexpected row: \($0)")
@@ -55,7 +57,7 @@ extension _FluentPostgresDatabase: Database {
5557
for c in e.createCases {
5658
_ = builder.value(c)
5759
}
58-
self.logger.debug("\(builder.query)")
60+
self.logger.log(level: self.sqlLogLevel, "\(builder.query)")
5961
return builder.run()
6062
case .update:
6163
if !e.deleteCases.isEmpty {
@@ -68,11 +70,11 @@ extension _FluentPostgresDatabase: Database {
6870
for create in e.createCases {
6971
_ = builder.add(value: create)
7072
}
71-
self.logger.debug("\(builder.query)")
73+
self.logger.log(level: self.sqlLogLevel, "\(builder.query)")
7274
return builder.run()
7375
case .delete:
7476
let builder = self.sql().drop(enum: e.name)
75-
self.logger.debug("\(builder.query)")
77+
self.logger.log(level: self.sqlLogLevel, "\(builder.query)")
7678
return builder.run()
7779
}
7880
}
@@ -82,22 +84,23 @@ extension _FluentPostgresDatabase: Database {
8284
return closure(self)
8385
}
8486
return self.database.withConnection { conn in
85-
self.logger.debug("BEGIN")
87+
self.logger.log(level: self.sqlLogLevel, "BEGIN")
8688
return conn.simpleQuery("BEGIN").flatMap { _ in
8789
let db = _FluentPostgresDatabase(
8890
database: conn,
8991
context: self.context,
9092
encoder: self.encoder,
9193
decoder: self.decoder,
92-
inTransaction: true
94+
inTransaction: true,
95+
sqlLogLevel: self.sqlLogLevel
9396
)
9497
return closure(db).flatMap { result in
95-
self.logger.debug("COMMIT")
98+
self.logger.log(level: self.sqlLogLevel, "COMMIT")
9699
return conn.simpleQuery("COMMIT").map { _ in
97100
result
98101
}
99102
}.flatMapError { error in
100-
self.logger.debug("ROLLBACK")
103+
self.logger.log(level: self.sqlLogLevel, "ROLLBACK")
101104
return conn.simpleQuery("ROLLBACK").flatMapThrowing { _ in
102105
throw error
103106
}
@@ -113,7 +116,8 @@ extension _FluentPostgresDatabase: Database {
113116
context: self.context,
114117
encoder: self.encoder,
115118
decoder: self.decoder,
116-
inTransaction: self.inTransaction
119+
inTransaction: self.inTransaction,
120+
sqlLogLevel: self.sqlLogLevel
117121
))
118122
}
119123
}

Sources/FluentPostgresDriver/FluentPostgresDriver.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Logging
2+
13
enum FluentPostgresError: Error {
24
case invalidURL(String)
35
}
@@ -6,6 +8,7 @@ struct _FluentPostgresDriver: DatabaseDriver {
68
let pool: EventLoopGroupConnectionPool<PostgresConnectionSource>
79
let encoder: PostgresDataEncoder
810
let decoder: PostgresDataDecoder
11+
let sqlLogLevel: Logger.Level
912

1013
var eventLoopGroup: EventLoopGroup {
1114
self.pool.eventLoopGroup
@@ -17,7 +20,8 @@ struct _FluentPostgresDriver: DatabaseDriver {
1720
context: context,
1821
encoder: self.encoder,
1922
decoder: self.decoder,
20-
inTransaction: false
23+
inTransaction: false,
24+
sqlLogLevel: self.sqlLogLevel
2125
)
2226
}
2327

0 commit comments

Comments
 (0)