Skip to content

Commit 94c7bf3

Browse files
committed
Warn if no env set in config
We can't infer the env of the parent app, so without this explict config value electric will always launch in prod mode with persistent shapes.
1 parent 87888ea commit 94c7bf3

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Config
22

3-
config :logger, level: :critical
3+
config :logger, level: :warning
44

55
config :phoenix_sync, mode: :disabled
66

lib/phoenix/sync/application.ex

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ defmodule Phoenix.Sync.Application do
99

1010
@impl true
1111
def start(_type, _args) do
12-
case children() do
13-
{:ok, children} ->
14-
Supervisor.start_link(children, strategy: :one_for_one, name: Phoenix.Sync.Supervisor)
12+
children =
13+
case children() do
14+
{:ok, children} ->
15+
children
1516

16-
{:error, reason} ->
17-
Logger.warning(reason)
18-
Supervisor.start_link([], strategy: :one_for_one, name: Phoenix.Sync.Supervisor)
19-
end
17+
{:error, reason} ->
18+
Logger.warning(reason)
19+
[]
20+
end
21+
22+
Supervisor.start_link(children, strategy: :one_for_one, name: Phoenix.Sync.Supervisor)
2023
end
2124

2225
@doc false
@@ -41,6 +44,8 @@ defmodule Phoenix.Sync.Application do
4144

4245
@doc false
4346
def children(opts) when is_list(opts) do
47+
warn_missing_env(opts)
48+
4449
{adapter, env} = adapter_env(opts)
4550

4651
apply(adapter, :children, [env, opts])
@@ -73,4 +78,23 @@ defmodule Phoenix.Sync.Application do
7378
:error -> {:error, "Missing required key #{inspect(key)}"}
7479
end
7580
end
81+
82+
defp warn_missing_env(config) do
83+
if config[:mode] != :disabled && !config[:env] do
84+
Logger.warning("""
85+
No `env` specified for :phoenix_sync: defaulting to `:prod`.
86+
87+
Add the following to your config:
88+
89+
config :phoenix_sync,
90+
env: config_env(),
91+
# the rest of your config
92+
93+
In `:prod` mode, shapes are persisted between server restarts
94+
which may cause problems in `:dev` or `:test` environments.
95+
""")
96+
end
97+
98+
config
99+
end
76100
end

test/phoenix/sync/application_test.exs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Phoenix.Sync.ApplicationTest do
33

44
alias Phoenix.Sync.Application, as: App
55

6+
import ExUnit.CaptureLog
7+
68
Code.ensure_loaded!(Support.ConfigTestRepo)
79

810
defp validate_repo_connection_opts!(opts, overrides \\ []) do
@@ -78,7 +80,20 @@ defmodule Phoenix.Sync.ApplicationTest do
7880
end
7981

8082
test "disabled mode" do
81-
assert {:ok, []} = App.children(mode: :disabled)
83+
refute capture_log(fn ->
84+
assert {:ok, []} = App.children(mode: :disabled)
85+
end) =~ ~r/No `env` specified for :phoenix_sync: defaulting to `:prod`/
86+
end
87+
88+
test "warns if env not set" do
89+
config = [
90+
mode: :embedded,
91+
repo: Support.ConfigTestRepo
92+
]
93+
94+
assert capture_log(fn ->
95+
assert {:ok, _} = App.children(config)
96+
end) =~ ~r/No `env` specified for :phoenix_sync: defaulting to `:prod`/
8297
end
8398

8499
test "embedded mode dev env" do

0 commit comments

Comments
 (0)