Skip to content

Commit 3c359f0

Browse files
committed
Add default port to pg config if none set (#8)
Fixes #1
1 parent f19480a commit 3c359f0

File tree

7 files changed

+94
-15
lines changed

7 files changed

+94
-15
lines changed

.github/workflows/elixir_tests.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@ jobs:
1818
runs-on: ubuntu-latest
1919
env:
2020
MIX_ENV: test
21+
22+
services:
23+
postgres:
24+
image: "postgres:17-alpine"
25+
env:
26+
POSTGRES_PASSWORD: password
27+
POSTGRES_DB: electric
28+
options: >-
29+
--health-cmd pg_isready
30+
--health-interval 10s
31+
--health-timeout 5s
32+
--health-retries 5
33+
ports:
34+
- 54321:5432
35+
2136
steps:
2237
- uses: actions/checkout@v4
2338

39+
- name: "Set PG settings"
40+
run: |
41+
docker exec ${{ job.services.postgres.id }} sh -c 'echo "wal_level=logical" >> /var/lib/postgresql/data/postgresql.conf'
42+
docker restart ${{ job.services.postgres.id }}
43+
2444
- uses: erlef/setup-beam@v1
2545
with:
2646
version-type: strict

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ The available options are:
367367

368368
- `table` (required). The Postgres table name. Be aware of casing and [Postgres's handling of unquoted upper-case names](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_upper_case_table_or_column_names).
369369
- `namespace` (optional). The Postgres namespace that the table belongs to. Defaults to `public`.
370-
- `where` (optional). Filter to apply to the synced data in SQL format, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')`.
370+
- `where` (optional). Filter to apply to the synced data in SQL format, e.g. `where: "amount < 1.23 AND colour in ('red', 'green')"`.
371371
- `columns` (optional). The columns to include in the synced data. By default Electric will include all columns in the table. The column list **must** include all primary keys. E.g. `columns: ["id", "title", "amount"]`.
372372
- `replica` (optional). By default Electric will only send primary keys + changed columns on updates. Set `replica: :full` to receive the full row, not just the changed columns.
373373

config/test.exs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +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,
30-
ssl: true,
31-
socket_options: [:inet6],
3231
pool_size: 10

lib/phoenix/sync.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Phoenix.Sync do
1010
@type shape_definition ::
1111
String.t()
1212
| Ecto.Queryable.t()
13-
| shape_definition()
13+
| shape_specification()
1414
@type param_override ::
1515
{:namespace, String.t()}
1616
| {:table, String.t()}
@@ -19,6 +19,7 @@ defmodule Phoenix.Sync do
1919
@type param_overrides :: [param_override()]
2020

2121
defdelegate plug_opts(), to: Phoenix.Sync.Application
22+
defdelegate plug_opts(config), to: Phoenix.Sync.Application
2223

2324
defdelegate client!(), to: Phoenix.Sync.Client, as: :new!
2425

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: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,42 @@ 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: :disable
19+
]
20+
21+
expected_opts = Keyword.merge(base_opts, overrides)
22+
23+
assert Enum.sort(connection_opts) == Enum.sort(expected_opts)
24+
end
25+
26+
defp with_modified_config(repo_config_overrides, fun) do
27+
original_config = Application.get_env(:phoenix_sync, Support.ConfigTestRepo, [])
28+
29+
try do
30+
Application.put_env(
31+
:phoenix_sync,
32+
Support.ConfigTestRepo,
33+
Keyword.merge(original_config, repo_config_overrides)
34+
)
35+
36+
fun.()
37+
after
38+
Application.put_env(
39+
:phoenix_sync,
40+
Support.ConfigTestRepo,
41+
original_config
42+
)
43+
end
2144
end
2245

2346
describe "children/1" do
@@ -75,6 +98,42 @@ defmodule Phoenix.Sync.ApplicationTest do
7598
} = Map.new(opts)
7699
end
77100

101+
test "passes repo pg port to electric" do
102+
config = [
103+
env: :dev,
104+
repo: Support.ConfigTestRepo
105+
]
106+
107+
repo_override = [port: 6543]
108+
109+
with_modified_config(repo_override, fn ->
110+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
111+
112+
validate_repo_connection_opts!(opts, repo_override)
113+
end)
114+
end
115+
116+
test "maps repo ssl and ipv6 settings to electric" do
117+
config = [
118+
env: :dev,
119+
repo: Support.ConfigTestRepo
120+
]
121+
122+
repo_override = [
123+
ssl: true,
124+
socket_options: [:inet6]
125+
]
126+
127+
with_modified_config(repo_override, fn ->
128+
assert {:ok, [{Electric.StackSupervisor, opts}]} = App.children(config)
129+
130+
validate_repo_connection_opts!(opts,
131+
sslmode: :require,
132+
ipv6: true
133+
)
134+
end)
135+
end
136+
78137
test "only repo config given and electric installed defaults to embedded" do
79138
config = [
80139
env: :dev,

test/support/repo.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule Support.Repo do
44
adapter: Ecto.Adapters.Postgres
55
end
66

7-
87
defmodule Support.ConfigTestRepo do
98
use Ecto.Repo,
109
otp_app: :phoenix_sync,

0 commit comments

Comments
 (0)