Skip to content

Commit c9b7afa

Browse files
committed
add table name cache test
1 parent 65c3e12 commit c9b7afa

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ extension PostgreSQLConnection {
5858
guard let row = currentRow else {
5959
throw PostgreSQLError(identifier: "query", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.", source: .capture())
6060
}
61+
print("before parse")
6162
let parsed = try row.parse(
6263
data: data,
6364
formatCodes: resultFormat.formatCodes,
6465
tableNameCache: self.tableNameCache
6566
)
67+
print("after parse")
6668
try onRow(parsed)
6769
case .close: break
6870
default: throw PostgreSQLError(identifier: "query", reason: "Unexpected message during PostgreSQLParseRequest: \(message)", source: .capture())

Sources/PostgreSQL/Database/PostgreSQLDatabase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public final class PostgreSQLDatabase: Database {
1212
internal var tableNameCache: PostgreSQLTableNameCache?
1313

1414
/// Creates a new `PostgreSQLDatabase`.
15-
public init(config: PostgreSQLDatabaseConfig) {
15+
public init(config: PostgreSQLDatabaseConfig, on worker: Worker) {
1616
self.config = config
17-
self.tableNameCache = PostgreSQLTableNameCache(connection: makeConnection(on: EmbeddedEventLoop()))
17+
self.tableNameCache = PostgreSQLTableNameCache(connection: makeConnection(on: worker))
1818
}
1919

2020
/// See `Database.makeConnection()`

Sources/PostgreSQL/PostgreSQLProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ extension PostgreSQLDatabaseConfig: ServiceType {
3333
extension PostgreSQLDatabase: ServiceType {
3434
/// See `ServiceType.makeService(for:)`
3535
public static func makeService(for worker: Container) throws -> PostgreSQLDatabase {
36-
return try .init(config: worker.make(for: PostgreSQLDatabase.self))
36+
return try .init(config: worker.make(for: PostgreSQLDatabase.self), on: worker)
3737
}
3838
}

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,34 @@ class PostgreSQLConnectionTests: XCTestCase {
342342
_ = try categories.wait()
343343
}
344344

345+
func testTableNameCache() throws {
346+
let hostname: String
347+
#if Xcode
348+
hostname = try Process.execute("docker-machine", "ip")
349+
#else
350+
hostname = "localhost"
351+
#endif
352+
let config = PostgreSQLDatabaseConfig.init(
353+
hostname: hostname,
354+
port: 5432,
355+
username: "vapor_username",
356+
database: "vapor_database",
357+
password: nil
358+
)
359+
let main = MultiThreadedEventLoopGroup(numThreads: 1)
360+
defer { try! main.syncShutdownGracefully() }
361+
let sub = MultiThreadedEventLoopGroup(numThreads: 1)
362+
defer { try! sub.syncShutdownGracefully() }
363+
364+
let database = PostgreSQLDatabase(config: config, on: main)
365+
let client = try database.makeConnection(on: sub).wait()
366+
let query = """
367+
select * from "pg_type" where "typlen" = $1 or "typlen" = $2
368+
"""
369+
let rows = try client.query(query, [1, 2]).wait()
370+
XCTAssertEqual(rows[0].keys.first?.table, "pg_type")
371+
}
372+
345373
static var allTests = [
346374
("testVersion", testVersion),
347375
("testSelectTypes", testSelectTypes),
@@ -351,14 +379,15 @@ class PostgreSQLConnectionTests: XCTestCase {
351379
("testStruct", testStruct),
352380
("testNull", testNull),
353381
("testGH24", testGH24),
382+
("testTableNameCache", testTableNameCache),
354383
]
355384
}
356385

357386
extension PostgreSQLConnection {
358387
/// Creates a test event loop and psql client.
359388
static func makeTest() throws -> PostgreSQLConnection {
360389
let hostname: String
361-
#if TEST_DOCKER_HOSTNAME
390+
#if Xcode
362391
hostname = try Process.execute("docker-machine", "ip")
363392
#else
364393
hostname = "localhost"

0 commit comments

Comments
 (0)