|
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 |
5 | 7 | end |
6 | | - end |
7 | 8 |
|
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. |
11 | 12 |
|
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. |
15 | 16 |
|
16 | | - Example: |
| 17 | + Example: |
17 | 18 |
|
18 | | - defmodule MyApp.Repo do |
19 | | - use Phoenix.Sync.Sandbox.Postgres |
| 19 | + defmodule MyApp.Repo do |
| 20 | + use Phoenix.Sync.Sandbox.Postgres |
20 | 21 |
|
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 |
25 | 26 |
|
26 | | - ### Custom environments |
| 27 | + ### Custom environments |
27 | 28 |
|
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: |
30 | 31 |
|
31 | | - defmodule MyApp.Repo do |
32 | | - use Phoenix.Sync.Sandbox.Postgres |
| 32 | + defmodule MyApp.Repo do |
| 33 | + use Phoenix.Sync.Sandbox.Postgres |
33 | 34 |
|
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()) |
38 | 48 |
|
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 |
51 | 53 | end |
52 | 54 | end |
0 commit comments