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

Return ArgumentError when missing :database key in config #671

Merged
merged 3 commits into from
Apr 16, 2024
Merged
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
26 changes: 17 additions & 9 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,12 @@ defmodule Postgrex.Protocol do
%{opts: opts, types_mod: types_mod} = status,
previous_errors
) do
types_key = if types_mod, do: {host, port, Keyword.fetch!(opts, :database)}
opts = Config.Reader.merge(opts, extra_opts)

status = %{status | types_key: types_key, opts: opts}

case connect_and_handshake(host, port, sock_opts, timeout, s, status) do
{:ok, _} = ret ->
ret

with {:ok, database} <- fetch_database(opts),
opts = Config.Reader.merge(opts, extra_opts),
status = %{status | types_key: if(types_mod, do: {host, port, database}), opts: opts},
{:ok, ret} <- connect_and_handshake(host, port, sock_opts, timeout, s, status) do
{:ok, ret}
else
{:error, err} ->
connect_endpoints(
remaining_endpoints,
Expand All @@ -205,6 +202,17 @@ defmodule Postgrex.Protocol do
{:error, %Postgrex.Error{message: message}}
end

defp fetch_database(opts) do
case Keyword.fetch(opts, :database) do
{:ok, value} ->
{:ok, value}

:error ->
message = "missing the :database key in options for #{inspect(opts[:repo])}"
{:error, %ArgumentError{message: message}}
end
end

defp connect_and_handshake(host, port, sock_opts, timeout, s, status) do
case connect(host, port, sock_opts, timeout, s) do
{:ok, s} ->
Expand Down
Loading