Skip to content

Commit

Permalink
Fixes to the guides
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Jul 18, 2019
1 parent 6a47977 commit 11b7c2d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions guides/howtos/Replicas and dynamic repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ And now you are ready to work with primary and replicas, no hacks or complex dep

## Testing replicas

While all of the work we have done os far should fully work in development and production, it may not be enough for tests. Most developers testing Ecto applications are using a sandbox, such as the [Ecto SQL Sandbox](https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html).
While all of the work we have done so far should fully work in development and production, it may not be enough for tests. Most developers testing Ecto applications are using a sandbox, such as the [Ecto SQL Sandbox](https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html).

When using a sandbox, each of your tests run in an isolated and independent transaction. Once the test is done, the transaction is rolled back. Which means we can trivially revert all of the changes done in a test in a very performant way.

Expand All @@ -126,7 +126,7 @@ There are two options to tackle this problem: one is to change replicas and the
One simple solution to the problem above is to use a custom `replica` implementation during tests that always return the primary repository, like this:

```elixir
if Mix.env == :test do
if Mix.env() == :test do
def replica, do: __MODULE__
else
def replica, do: Enum.random(@replicas)
Expand Down Expand Up @@ -193,14 +193,14 @@ end
There is even a better way! We can pass a `:default_dynamic_repo` option when we define the repository. In this case, we want to set the `:default_dynamic_repo` to `MyApp.Repo` only during the test environment. In your `lib/my_app/repo.ex`, do this:

```elixir
dynamic_default_repo =
if Mix.env() == :test do
MyApp.Repo
else
__MODULE__
end

for repo <- @replicas do
dynamic_default_repo =
if Mix.env() == :test do
MyApp.Repo
else
repo
end

defmodule repo do
use Ecto.Repo,
otp_app: :my_app,
Expand Down

0 comments on commit 11b7c2d

Please sign in to comment.