Skip to content

Commit 41726bb

Browse files
committed
Add default port if none specified
Fixes #1
1 parent f19480a commit 41726bb

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

config/test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ config :phoenix_sync, Support.ConfigTestRepo,
2424
password: "password",
2525
hostname: "localhost",
2626
database: "electric",
27-
port: 54321,
27+
# phoenix_sync should fill in default port
28+
# port: 54321,
2829
stacktrace: true,
2930
show_sensitive_data_on_connection_error: true,
3031
ssl: true,

lib/phoenix/sync/electric.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ defmodule Phoenix.Sync.Electric do
408408
|> Keyword.take(expected_keys)
409409
|> Keyword.merge(ssl_opts)
410410
|> Keyword.merge(tcp_opts)
411+
|> Keyword.put_new(:port, 5432)
411412
end
412413

413414
defp http_mode_plug_opts(electric_config) do

test/phoenix/sync/application_test.exs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ defmodule Phoenix.Sync.ApplicationTest do
55

66
Code.ensure_loaded!(Support.ConfigTestRepo)
77

8-
defp validate_repo_connection_opts!(opts) do
8+
defp validate_repo_connection_opts!(opts, overrides \\ []) do
99
assert {pass_fun, connection_opts} = Keyword.pop!(opts[:connection_opts], :password)
1010

1111
assert pass_fun.() == "password"
1212

13-
assert connection_opts == [
14-
username: "postgres",
15-
hostname: "localhost",
16-
database: "electric",
17-
port: 54321,
18-
sslmode: :require,
19-
ipv6: true
20-
]
13+
base_opts = [
14+
username: "postgres",
15+
hostname: "localhost",
16+
database: "electric",
17+
port: 5432,
18+
sslmode: :require,
19+
ipv6: true
20+
]
21+
22+
expected_opts = Keyword.merge(base_opts, overrides)
23+
24+
assert Enum.sort(connection_opts) == Enum.sort(expected_opts)
2125
end
2226

2327
describe "children/1" do
@@ -75,6 +79,35 @@ defmodule Phoenix.Sync.ApplicationTest do
7579
} = Map.new(opts)
7680
end
7781

82+
test "with defined port" do
83+
original_config = Application.get_env(:phoenix_sync, Support.ConfigTestRepo, [])
84+
85+
override_port = 6543
86+
87+
config = [
88+
env: :dev,
89+
repo: Support.ConfigTestRepo
90+
]
91+
92+
try do
93+
Application.put_env(
94+
:phoenix_sync,
95+
Support.ConfigTestRepo,
96+
Keyword.put(original_config, :port, override_port)
97+
)
98+
99+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
100+
101+
validate_repo_connection_opts!(opts, port: override_port)
102+
after
103+
Application.put_env(
104+
:phoenix_sync,
105+
Support.ConfigTestRepo,
106+
original_config
107+
)
108+
end
109+
end
110+
78111
test "only repo config given and electric installed defaults to embedded" do
79112
config = [
80113
env: :dev,

0 commit comments

Comments
 (0)