Skip to content

Commit 2186121

Browse files
authored
Update configuration handling to work with Electric 1.0 (#16)
1 parent de26a68 commit 2186121

File tree

4 files changed

+144
-47
lines changed

4 files changed

+144
-47
lines changed

lib/phoenix/sync/electric.ex

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ defmodule Phoenix.Sync.Electric do
320320
defp start_embedded(env, mode, db_config_fun, message) do
321321
db_config =
322322
db_config_fun.()
323-
|> Keyword.update!(:connection_opts, &Electric.Utils.obfuscate_password/1)
323+
|> obfuscate_credentials()
324324

325325
electric_config = core_configuration(env, db_config)
326326

@@ -367,6 +367,24 @@ defmodule Phoenix.Sync.Electric do
367367
opts
368368
end
369369
end
370+
371+
defp obfuscate_credentials(opts) do
372+
opts
373+
|> Keyword.update!(:replication_connection_opts, &Electric.Utils.obfuscate_password/1)
374+
|> then(fn opts ->
375+
case Keyword.fetch(opts, :query_connection_opts) do
376+
:error ->
377+
opts
378+
379+
{:ok, query_connection_opts} ->
380+
Keyword.put(
381+
opts,
382+
:query_connection_opts,
383+
Electric.Utils.obfuscate_password(query_connection_opts)
384+
)
385+
end
386+
end)
387+
end
370388
else
371389
defp start_embedded(_env, _mode, _db_config_fun, _message) do
372390
{:error,
@@ -428,13 +446,20 @@ defmodule Phoenix.Sync.Electric do
428446
defp validate_database_config(_env, mode, opts) do
429447
case Keyword.pop(opts, :repo, nil) do
430448
{nil, opts} ->
431-
case Keyword.fetch(opts, :connection_opts) do
432-
{:ok, connection_opts} when is_list(connection_opts) ->
433-
# TODO: validate reasonable connection opts?
434-
{:start, fn -> opts end,
435-
"Starting Electric replication stream from postgresql://#{connection_opts[:host]}:#{connection_opts[:port] || 5432}/#{connection_opts[:database]}"}
449+
case {Keyword.get(opts, :connection_opts),
450+
Keyword.get(opts, :replication_connection_opts)} do
451+
{[_ | _] = connection_opts, _} ->
452+
opts =
453+
opts
454+
|> Keyword.delete(:connection_opts)
455+
|> Keyword.put(:replication_connection_opts, connection_opts)
436456

437-
:error ->
457+
{:start, fn -> opts end, start_message(connection_opts)}
458+
459+
{nil, [_ | _] = connection_opts} ->
460+
{:start, fn -> opts end, start_message(connection_opts)}
461+
462+
{nil, nil} ->
438463
case mode do
439464
:embedded ->
440465
{:error,
@@ -450,14 +475,21 @@ defmodule Phoenix.Sync.Electric do
450475
repo_config = apply(repo, :config, [])
451476

452477
{:start,
453-
fn -> Keyword.put(opts, :connection_opts, convert_repo_config(repo_config)) end,
454-
"Starting Electric replication stream using #{repo} configuration"}
478+
fn ->
479+
connection_opts = convert_repo_config(repo_config)
480+
481+
Keyword.put(opts, :replication_connection_opts, connection_opts)
482+
end, "Starting Electric replication stream using #{repo} configuration"}
455483
else
456484
{:error, "#{inspect(repo)} is not a valid Ecto.Repo module"}
457485
end
458486
end
459487
end
460488

489+
defp start_message(connection_opts) do
490+
"Starting Electric replication stream from postgresql://#{connection_opts[:host]}:#{connection_opts[:port] || 5432}/#{connection_opts[:database]}"
491+
end
492+
461493
if @electric_available? do
462494
defp convert_repo_config(repo_config) do
463495
expected_keys = Electric.connection_opts_schema() |> Keyword.keys()

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ defmodule Phoenix.Sync.MixProject do
3838
{:ecto_sql, "~> 3.10", optional: true},
3939
# require an exact version because electric moves very quickly atm
4040
# and a more generous specification would inevitably break.
41-
{:electric, "== 1.0.0-beta.20", optional: true},
42-
{:electric_client, ">= 0.3.0-beta.4"}
41+
{:electric, "== 1.0.1", optional: true},
42+
{:electric_client, "== 0.3.0"}
4343
] ++ deps_for_env(Mix.env())
4444
end
4545

0 commit comments

Comments
 (0)