Skip to content

Commit 62fc763

Browse files
committed
fix bug associated w/ database names when parsing postgres url
1 parent 390c22c commit 62fc763

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public struct PostgreSQLDatabaseConfig {
4949
self.hostname = hostname
5050
self.port = port
5151
self.username = username
52-
self.database = database
52+
if database.hasPrefix("/") {
53+
self.database = database.dropFirst().description
54+
} else {
55+
self.database = database
56+
}
5357
self.password = urL.password
5458
}
5559
}

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

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

345+
func testURLParsing() throws {
346+
let databaseURL = "postgres://username:password@hostname.com:5432/database"
347+
let config = try PostgreSQLDatabaseConfig(url: databaseURL)
348+
XCTAssertEqual(config.hostname, "hostname.com")
349+
XCTAssertEqual(config.port, 5432)
350+
XCTAssertEqual(config.username, "username")
351+
XCTAssertEqual(config.password, "password")
352+
XCTAssertEqual(config.database, "database")
353+
}
354+
345355
static var allTests = [
346356
("testVersion", testVersion),
347357
("testSelectTypes", testSelectTypes),
@@ -351,6 +361,7 @@ class PostgreSQLConnectionTests: XCTestCase {
351361
("testStruct", testStruct),
352362
("testNull", testNull),
353363
("testGH24", testGH24),
364+
("testURLParsing", testURLParsing),
354365
]
355366
}
356367

0 commit comments

Comments
 (0)