Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable libpq-compatible "postgresql" URL schemes #251

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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