Skip to content

Commit 6e4030b

Browse files
authored
integrate logging (vapor#156)
* integrate logging * withConnection updates * sqlkit updates * fix tests * fix leak test * bump
1 parent 2d0f308 commit 6e4030b

7 files changed

+129
-86
lines changed

Package.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ let package = Package(
77
.library(name: "PostgresKit", targets: ["PostgresKit"]),
88
],
99
dependencies: [
10-
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.0.0-alpha"),
11-
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.0.0-beta"),
12-
.package(url: "https://github.com/vapor/async-kit.git", from: "1.0.0-beta"),
10+
.package(url: "https://github.com/vapor/postgres-nio.git", .branch("master")),
11+
.package(url: "https://github.com/vapor/sql-kit.git", .branch("master")),
12+
.package(url: "https://github.com/vapor/async-kit.git", .branch("master")),
1313
],
1414
targets: [
1515
.target(name: "PostgresKit", dependencies: ["AsyncKit", "PostgresNIO", "SQLKit"]),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extension EventLoopConnectionPool where Source == PostgresConnectionSource {
2+
public func database(logger: Logger) -> PostgresDatabase {
3+
_ConnectionPoolPostgresDatabase(pool: self, logger: logger)
4+
}
5+
}
6+
7+
private struct _ConnectionPoolPostgresDatabase {
8+
let pool: EventLoopConnectionPool<PostgresConnectionSource>
9+
let logger: Logger
10+
}
11+
12+
extension _ConnectionPoolPostgresDatabase: PostgresDatabase {
13+
var eventLoop: EventLoop {
14+
self.pool.eventLoop
15+
}
16+
17+
func send(_ request: PostgresRequest, logger: Logger) -> EventLoopFuture<Void> {
18+
self.pool.withConnection(logger: logger) {
19+
$0.send(request, logger: logger)
20+
}
21+
}
22+
23+
func withConnection<T>(_ closure: @escaping (PostgresConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
24+
self.pool.withConnection(logger: self.logger, closure)
25+
}
26+
}

Sources/PostgresKit/Extensions.swift

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import SQLKit
2+
3+
extension PostgresDatabase {
4+
public func sql() -> SQLDatabase {
5+
_PostgresSQLDatabase(database: self)
6+
}
7+
}
8+
9+
private struct _PostgresSQLDatabase {
10+
let database: PostgresDatabase
11+
}
12+
13+
extension _PostgresSQLDatabase: SQLDatabase {
14+
var logger: Logger {
15+
self.database.logger
16+
}
17+
18+
var eventLoop: EventLoop {
19+
self.database.eventLoop
20+
}
21+
22+
var dialect: SQLDialect {
23+
PostgresDialect()
24+
}
25+
26+
func execute(sql query: SQLExpression, _ onRow: @escaping (SQLRow) -> ()) -> EventLoopFuture<Void> {
27+
let (sql, binds) = self.serialize(query)
28+
do {
29+
return try self.database.query(sql, binds.map { encodable in
30+
return try PostgresDataEncoder().encode(encodable)
31+
}) { row in
32+
onRow(row)
33+
}
34+
} catch {
35+
return self.eventLoop.makeFailedFuture(error)
36+
}
37+
}
38+
}

Sources/PostgresKit/PostgresConnectionSource.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
55
self.configuration = configuration
66
}
77

8-
public func makeConnection(on eventLoop: EventLoop) -> EventLoopFuture<PostgresConnection> {
8+
public func makeConnection(
9+
logger: Logger,
10+
on eventLoop: EventLoop
11+
) -> EventLoopFuture<PostgresConnection> {
912
let address: SocketAddress
1013
do {
1114
address = try self.configuration.address()
@@ -15,12 +18,14 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
1518
return PostgresConnection.connect(
1619
to: address,
1720
tlsConfiguration: self.configuration.tlsConfiguration,
21+
logger: .init(label: "codes.vapor.postgres"),
1822
on: eventLoop
1923
).flatMap { conn in
2024
return conn.authenticate(
2125
username: self.configuration.username,
2226
database: self.configuration.database,
23-
password: self.configuration.password
27+
password: self.configuration.password,
28+
logger: logger
2429
).flatMapErrorThrowing { error in
2530
_ = conn.close()
2631
throw error
+11-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
struct PostgresDialect: SQLDialect {
2-
private var bindOffset: Int
3-
4-
init() {
5-
self.bindOffset = 0
1+
public struct PostgresDialect: SQLDialect {
2+
public init() { }
3+
4+
public var name: String {
5+
"postgresql"
66
}
77

8-
var identifierQuote: SQLExpression {
8+
public var identifierQuote: SQLExpression {
99
return SQLRaw("\"")
1010
}
1111

12-
var literalStringQuote: SQLExpression {
12+
public var literalStringQuote: SQLExpression {
1313
return SQLRaw("'")
1414
}
1515

16-
mutating func nextBindPlaceholder() -> SQLExpression {
17-
self.bindOffset += 1
18-
return SQLRaw("$" + self.bindOffset.description)
16+
public func bindPlaceholder(at position: Int) -> SQLExpression {
17+
return SQLRaw("$" + position.description)
1918
}
2019

21-
func literalBoolean(_ value: Bool) -> SQLExpression {
20+
public func literalBoolean(_ value: Bool) -> SQLExpression {
2221
switch value {
2322
case false:
2423
return SQLRaw("false")
@@ -27,7 +26,7 @@ struct PostgresDialect: SQLDialect {
2726
}
2827
}
2928

30-
var autoIncrementClause: SQLExpression {
29+
public var autoIncrementClause: SQLExpression {
3130
return SQLRaw("GENERATED BY DEFAULT AS IDENTITY")
3231
}
3332
}

Tests/PostgresKitTests/PostgresKitTests.swift

+44-36
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,23 @@ import SQLKitBenchmark
33
import XCTest
44

55
class PostgresKitTests: XCTestCase {
6-
private var eventLoopGroup: EventLoopGroup!
7-
private var eventLoop: EventLoop {
8-
return self.eventLoopGroup.next()
9-
}
10-
11-
override func setUp() {
12-
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
13-
}
14-
15-
override func tearDown() {
16-
XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
17-
self.eventLoopGroup = nil
18-
}
19-
20-
216
func testSQLKitBenchmark() throws {
227
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
238
defer { try! conn.close().wait() }
24-
let benchmark = SQLBenchmarker(on: conn)
9+
conn.logger.logLevel = .trace
10+
let benchmark = SQLBenchmarker(on: conn.sql())
2511
try benchmark.run()
2612
}
2713

2814
func testPerformance() throws {
2915
let db = PostgresConnectionSource(
3016
configuration: .init(hostname: hostname, username: "vapor_username", password: "vapor_password", database: "vapor_database")
3117
)
32-
let pool = ConnectionPool(configuration: .init(maxConnections: 12), source: db, on: self.eventLoopGroup)
18+
let pool = EventLoopGroupConnectionPool(
19+
source: db,
20+
maxConnectionsPerEventLoop: 2,
21+
on: self.eventLoopGroup
22+
)
3323
defer { pool.shutdown() }
3424
self.measure {
3525
for _ in 1...100 {
@@ -43,35 +33,37 @@ class PostgresKitTests: XCTestCase {
4333
func testCreateEnumWithBuilder() throws {
4434
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
4535
defer { try! conn.close().wait() }
36+
let db = conn.sql()
4637

47-
try conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
48-
try conn.raw("DROP TYPE meal;").run().wait()
38+
try db.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
39+
try db.raw("DROP TYPE meal;").run().wait()
4940

50-
try conn.create(enum: SQLIdentifier("meal"), cases: "breakfast", "lunch", "dinner").run().wait()
51-
try conn.raw("DROP TYPE meal;").run().wait()
41+
try db.create(enum: SQLIdentifier("meal"), cases: "breakfast", "lunch", "dinner").run().wait()
42+
try db.raw("DROP TYPE meal;").run().wait()
5243
}
5344

5445
func testDropEnumWithBuilder() throws {
5546
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
5647
defer { try! conn.close().wait() }
48+
let db = conn.sql()
5749

5850
// these two should work even if the type does not exist
59-
try conn.drop(type: "meal").ifExists().run().wait()
60-
try conn.drop(type: "meal").ifExists().cascade().run().wait()
51+
try db.drop(type: "meal").ifExists().run().wait()
52+
try db.drop(type: "meal").ifExists().cascade().run().wait()
6153

62-
try conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
63-
try conn.drop(type: "meal").ifExists().cascade().run().wait()
54+
try db.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
55+
try db.drop(type: "meal").ifExists().cascade().run().wait()
6456

65-
try conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
66-
try conn.drop(type: "meal").run().wait()
57+
try db.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
58+
try db.drop(type: "meal").run().wait()
6759

68-
try conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
69-
try conn.drop(type: SQLIdentifier("meal")).run().wait()
60+
try db.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
61+
try db.drop(type: SQLIdentifier("meal")).run().wait()
7062

71-
try conn.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
72-
try conn.drop(type: "meal").cascade().run().wait()
63+
try db.create(enum: "meal", cases: "breakfast", "lunch", "dinner").run().wait()
64+
try db.drop(type: "meal").cascade().run().wait()
7365
}
74-
66+
7567
func testLeak() throws {
7668
struct Foo: Codable {
7769
var id: String
@@ -87,8 +79,10 @@ class PostgresKitTests: XCTestCase {
8779
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
8880
defer { try! conn.close().wait() }
8981

90-
try conn.raw("DROP TABLE IF EXISTS foos").run().wait()
91-
try conn.raw("""
82+
let db = conn.sql()
83+
84+
try db.raw("DROP TABLE IF EXISTS foos").run().wait()
85+
try db.raw("""
9286
CREATE TABLE foos (
9387
id TEXT PRIMARY KEY,
9488
description TEXT,
@@ -101,7 +95,7 @@ class PostgresKitTests: XCTestCase {
10195
)
10296
""").run().wait()
10397
defer {
104-
try? conn.raw("DROP TABLE IF EXISTS foos").run().wait()
98+
try? db.raw("DROP TABLE IF EXISTS foos").run().wait()
10599
}
106100

107101
for i in 0..<5_000 {
@@ -115,9 +109,23 @@ class PostgresKitTests: XCTestCase {
115109
modified_by: "test",
116110
modified_at: Date()
117111
)
118-
try conn.insert(into: "foos")
112+
try db.insert(into: "foos")
119113
.model(zipcode)
120114
.run().wait()
121115
}
122116
}
117+
118+
private var eventLoopGroup: EventLoopGroup!
119+
private var eventLoop: EventLoop {
120+
return self.eventLoopGroup.next()
121+
}
122+
123+
override func setUp() {
124+
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
125+
}
126+
127+
override func tearDown() {
128+
XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
129+
self.eventLoopGroup = nil
130+
}
123131
}

0 commit comments

Comments
 (0)