Skip to content

Commit

Permalink
Enable use of libpq-compatible postgresql URL schemes. Fixes #250
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed Aug 14, 2023
1 parent 81d1106 commit dc22329
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/PostgresKit/SQLPostgresConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public struct SQLPostgresConfiguration {
/// The aliases always have the same semantics as the "canonical" modes, despite any differences
/// suggested by their names.
///
/// Also for compatibility, the URL scheme may also be `postgresql` or `postgresql+uds`.
///
/// > Note: It is possible to emulate `libpq`'s definitions for `prefer` (TLS if available with
/// > no certificate verification), `require` (TLS enforced, but also without certificate
/// > verification) and `verify-ca` (TLS enforced with no hostname verification) by manually
Expand Down Expand Up @@ -94,7 +96,7 @@ public struct SQLPostgresConfiguration {
}

switch comp.scheme {
case "postgres", "postgres+tcp":
case "postgres", "postgres+tcp", "postgresql", "postgresql+tcp":
guard let hostname = comp.host, !hostname.isEmpty else {
throw URLError(.badURL, userInfo: [NSURLErrorFailingURLErrorKey: url, NSURLErrorFailingURLStringErrorKey: url.absoluteString])
}
Expand All @@ -104,7 +106,7 @@ public struct SQLPostgresConfiguration {
database: url.lastPathComponent.isEmpty ? nil : url.lastPathComponent,
tls: try decideTLSConfig(from: comp.queryItems ?? [], defaultMode: "prefer")
)
case "postgres+uds":
case "postgres+uds", "postgresql+uds":
guard (comp.host?.isEmpty ?? true || comp.host == "localhost"), comp.port == nil, !comp.path.isEmpty, comp.path != "/" else {
throw URLError(.badURL, userInfo: [NSURLErrorFailingURLErrorKey: url, NSURLErrorFailingURLStringErrorKey: url.absoluteString])
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/PostgresKitTests/SQLPostgresConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ final class SQLPostgresConfigurationTests: XCTestCase {
XCTAssertTrue(config.coreConfiguration.tls.isEnforced)
}

XCTAssertNoThrow(try SQLPostgresConfiguration(url:"postgresql://test_username@test_hostname"))
XCTAssertNoThrow(try SQLPostgresConfiguration(url:"postgresql+tcp://test_username@test_hostname"))
XCTAssertNoThrow(try SQLPostgresConfiguration(url:"postgresql+uds://test_username@/tmp/postgres.sock"))

XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+tcp://test_hostname"), "should fail when username missing")
XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+tcp://test_username@test_hostname?tlsmode=absurd"), "should fail when TLS mode invalid")
XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+uds://localhost/tmp/postgres.sock?tlsmode=require"), "should fail when username missing")
Expand Down

0 comments on commit dc22329

Please sign in to comment.