Skip to content

Commit 412b555

Browse files
authored
Prioritize explicit endpoint options over PGHOST-derived defaults (#742)
- Updated `default_opts/1` to apply PGHOST-derived host only when no endpoint-related options (:socket, :socket_dir, :hostname, :endpoints) are explicitly provided. - Added test coverage for new behavior, ensuring explicit endpoint keys override PGHOST-derived values.
1 parent 257daa7 commit 412b555

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/postgrex/utils.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,22 @@ defmodule Postgrex.Utils do
8484

8585
@doc """
8686
Fills in the given `opts` with default options.
87+
Only adds keys extracted via PGHOST if no endpoint-related keys are explicitly provided.
8788
"""
8889
@spec default_opts(Keyword.t()) :: Keyword.t()
8990
def default_opts(opts) do
9091
{field, value} = extract_host(System.get_env("PGHOST"))
9192

93+
endpoint_keys = [:socket, :socket_dir, :hostname, :endpoints]
94+
has_endpoint? = Enum.any?(endpoint_keys, &Keyword.has_key?(opts, &1))
95+
9296
opts
9397
|> Keyword.put_new(:username, System.get_env("PGUSER") || System.get_env("USER"))
9498
|> Keyword.put_new(:password, System.get_env("PGPASSWORD"))
9599
|> Keyword.put_new(:database, System.get_env("PGDATABASE"))
96-
|> Keyword.put_new(field, value)
100+
|> then(fn opts ->
101+
if has_endpoint?, do: opts, else: Keyword.put(opts, field, value)
102+
end)
97103
|> Keyword.put_new(:port, System.get_env("PGPORT"))
98104
|> Keyword.update!(:port, &normalize_port/1)
99105
|> Keyword.put_new(:types, Postgrex.DefaultTypes)

test/utils/envs_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,37 @@ defmodule Utils.EnvsTest do
7272
assert ctx.opts[:socket] == <<0, "foo">>
7373
end
7474
end
75+
76+
describe "PGHOST with manual overrides" do
77+
@env PGHOST: "/test/socket"
78+
test "respects explicit hostname even if PGHOST is set" do
79+
opts = Postgrex.Utils.default_opts(hostname: "localhost")
80+
81+
assert Keyword.get(opts, :hostname) == "localhost"
82+
refute Keyword.has_key?(opts, :socket_dir)
83+
end
84+
85+
@env PGHOST: "/test/socket"
86+
test "respects explicit endpoints even if PGHOST is set" do
87+
opts = Postgrex.Utils.default_opts(endpoints: [{"localhost", 5432}])
88+
89+
assert Keyword.get(opts, :endpoints) == [{"localhost", 5432}]
90+
refute Keyword.has_key?(opts, :socket_dir)
91+
end
92+
93+
@env PGHOST: "/test/socket"
94+
test "respects explicit socket even if PGHOST is set" do
95+
opts = Postgrex.Utils.default_opts(socket: "/var/run/postgresql")
96+
97+
assert Keyword.get(opts, :socket) == "/var/run/postgresql"
98+
refute Keyword.has_key?(opts, :socket_dir)
99+
end
100+
101+
@env PGHOST: "/test/socket"
102+
test "respects explicit socket_dir even if PGHOST is set" do
103+
opts = Postgrex.Utils.default_opts(socket_dir: "/another/test/socket")
104+
105+
assert Keyword.get(opts, :socket_dir) == "/another/test/socket"
106+
end
107+
end
75108
end

0 commit comments

Comments
 (0)