Skip to content

Commit 6efb7dc

Browse files
authored
sandbox: Test for both EctoSQL and Electric for Sandbox compatibility (#86)
1 parent 85501d9 commit 6efb7dc

File tree

13 files changed

+71
-57
lines changed

13 files changed

+71
-57
lines changed

lib/phoenix/sync.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,9 @@ defmodule Phoenix.Sync do
338338
def shape!(shape, shape_opts \\ []) do
339339
PredefinedShape.new!(shape, shape_opts)
340340
end
341+
342+
@doc false
343+
def sandbox_enabled? do
344+
Code.ensure_loaded?(Electric) && Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox)
345+
end
341346
end

lib/phoenix/sync/electric.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ defmodule Phoenix.Sync.Electric do
150150
{:ok, []}
151151

152152
:sandbox ->
153-
{:ok, [Phoenix.Sync.Sandbox]}
153+
if Phoenix.Sync.sandbox_enabled?() do
154+
{:ok, [Phoenix.Sync.Sandbox]}
155+
else
156+
{:error,
157+
"Sandbox mode is only available if `:ecto_sql` and `:electric` are listed as dependencies"}
158+
end
154159

155160
mode when mode in @valid_modes ->
156161
embedded_children(env, mode, electric_opts)
@@ -289,7 +294,7 @@ defmodule Phoenix.Sync.Electric do
289294
|> Keyword.fetch!(:api)
290295
end
291296

292-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
297+
if Phoenix.Sync.sandbox_enabled?() do
293298
defp plug_opts(_env, :sandbox, _electric_opts) do
294299
%Phoenix.Sync.Sandbox.APIAdapter{}
295300
end
@@ -537,7 +542,7 @@ defmodule Phoenix.Sync.Electric do
537542
# to a real instance -- I think that's reasonable. The overhead of a real
538543
# electric consuming a real replication stream is way too high for a simple
539544
# consumer of streams
540-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
545+
if Phoenix.Sync.sandbox_enabled?() do
541546
defp configure_client(_opts, :sandbox) do
542547
Phoenix.Sync.Sandbox.client()
543548
end

lib/phoenix/sync/sandbox.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox do
33
@moduledoc """
44
Integration between `Ecto.Adapters.SQL.Sandbox` and `Electric` that produces

lib/phoenix/sync/sandbox/api_adapter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.APIAdapter do
33
@moduledoc false
44

lib/phoenix/sync/sandbox/fetch.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.Fetch do
33
@moduledoc false
44

lib/phoenix/sync/sandbox/inspector.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.Inspector do
33
@moduledoc false
44

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
1-
defmodule Phoenix.Sync.Sandbox.Postgres do
2-
defmacro __using__(_opts) do
3-
quote do
4-
require Phoenix.Sync.Sandbox.Postgres
1+
if Phoenix.Sync.sandbox_enabled?() do
2+
defmodule Phoenix.Sync.Sandbox.Postgres do
3+
defmacro __using__(_opts) do
4+
quote do
5+
require Phoenix.Sync.Sandbox.Postgres
6+
end
57
end
6-
end
78

8-
@doc """
9-
Replace hard-coded references to `Ecto.Adapters.Postgres` with this macro to
10-
enable the `Phoenix.Sync.Sandbox` in test mode.
9+
@doc """
10+
Replace hard-coded references to `Ecto.Adapters.Postgres` with this macro to
11+
enable the `Phoenix.Sync.Sandbox` in test mode.
1112
12-
In development or production environments the repo will use the standard
13-
Postgres adapter directly, only if `Mix.env() == :test` (see below) will it
14-
use the sandbox adapter shim.
13+
In development or production environments the repo will use the standard
14+
Postgres adapter directly, only if `Mix.env() == :test` (see below) will it
15+
use the sandbox adapter shim.
1516
16-
Example:
17+
Example:
1718
18-
defmodule MyApp.Repo do
19-
use Phoenix.Sync.Sandbox.Postgres
19+
defmodule MyApp.Repo do
20+
use Phoenix.Sync.Sandbox.Postgres
2021
21-
use Ecto.Repo,
22-
otp_app: :my_app,
23-
adapter: Phoenix.Sync.Sandbox.Postgres.adapter()
24-
end
22+
use Ecto.Repo,
23+
otp_app: :my_app,
24+
adapter: Phoenix.Sync.Sandbox.Postgres.adapter()
25+
end
2526
26-
### Custom environments
27+
### Custom environments
2728
28-
If you want to enable the sandbox adapter in different environments, you can
29-
use your own evaluation logic:
29+
If you want to enable the sandbox adapter in different environments, you can
30+
use your own evaluation logic:
3031
31-
defmodule MyApp.Repo do
32-
use Phoenix.Sync.Sandbox.Postgres
32+
defmodule MyApp.Repo do
33+
use Phoenix.Sync.Sandbox.Postgres
3334
34-
use Ecto.Repo,
35-
otp_app: :my_app,
36-
adapter: Phoenix.Sync.Sandbox.Postgres.adapter(Mix.env() in [:test, :special])
37-
end
35+
use Ecto.Repo,
36+
otp_app: :my_app,
37+
adapter: Phoenix.Sync.Sandbox.Postgres.adapter(Mix.env() in [:test, :special])
38+
end
39+
40+
> #### Warning {: .warning}
41+
>
42+
> The expression passed to `adapter/1` will be evaluated at **compile time**,
43+
> not run-time so only use expressions that can be evaluated at then (this
44+
> includes the various `Mix` functions).
45+
"""
46+
defmacro adapter(expr \\ Mix.env() == :test) do
47+
{enable_sandbox?, _binding} = Code.eval_quoted(expr, binding())
3848

39-
> #### Warning {: .warning}
40-
>
41-
> The expression passed to `adapter/1` will be evaluated at **compile time**,
42-
> not run-time so only use expressions that can be evaluated at then (this
43-
> includes the various `Mix` functions).
44-
"""
45-
defmacro adapter(expr \\ Mix.env() == :test) do
46-
{enable_sandbox?, _binding} = Code.eval_quoted(expr, binding())
47-
48-
if enable_sandbox?,
49-
do: Phoenix.Sync.Sandbox.Postgres.Adapter,
50-
else: Ecto.Adapters.Postgres
49+
if enable_sandbox?,
50+
do: Phoenix.Sync.Sandbox.Postgres.Adapter,
51+
else: Ecto.Adapters.Postgres
52+
end
5153
end
5254
end

lib/phoenix/sync/sandbox/postgres/adapter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.Postgres) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.Postgres.Adapter do
33
@moduledoc false
44

lib/phoenix/sync/sandbox/producer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.Producer do
33
@moduledoc false
44

lib/phoenix/sync/sandbox/publication_manager.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do
1+
if Phoenix.Sync.sandbox_enabled?() do
22
defmodule Phoenix.Sync.Sandbox.PublicationManager do
33
@moduledoc false
44

0 commit comments

Comments
 (0)