@@ -7,12 +7,13 @@ defmodule Ecto.Adapters.SQL.Sandbox do
77 repository will automatically check connections out as with any
88 other pool.
99
10- The `mode/2` function can be used to change the pool mode to
11- manual or shared. In both modes, the connection must be explicitly
12- checked out before use. When explicit checkouts are made, the
13- sandbox will wrap the connection in a transaction by default and
14- control who has access to it. This means developers have a safe
15- mechanism for running concurrent tests against the database.
10+ The `mode/2` function can be used to change the pool mode from
11+ automatic to either manual or shared. In the later two modes,
12+ the connection must be explicitly checked out before use.
13+ When explicit checkouts are made, the sandbox will wrap the
14+ connection in a transaction by default and control who has
15+ access to it. This means developers have a safe mechanism for
16+ running concurrent tests against the database.
1617
1718 ## Database support
1819
@@ -423,15 +424,33 @@ defmodule Ecto.Adapters.SQL.Sandbox do
423424 @ doc """
424425 Sets the mode for the `repo` pool.
425426
426- The mode can be `:auto`, `:manual` or `{:shared, <pid>}`.
427-
428- Warning: you should only call this function in the setup block for a test and
429- not within a test, because if the mode is changed during the test it will cause
430- other database connections to be checked in (causing errors).
427+ The modes can be:
428+
429+ * `:auto` - this is the default mode. When trying to use the repository,
430+ processes can automatically checkout a connection without calling
431+ `checkout/2` or `start_owner/2` before. This is the mode you will run
432+ on before your test suite starts
433+
434+ * `:manual` - in this mode, the connection always has to be explicitly
435+ checked before used. Other processes are allowed to use the same
436+ connection if they are explicitly allowed via `allow/4`. You usually
437+ set the mode to manual at the end of your `test/test_helper.exs` file.
438+ This is also the mode you will run your async tests in
439+
440+ * `{:shared, pid}` - after checking out a connection in manual mode,
441+ you can change the mode to `{:shared, pid}`, where pid is the process
442+ that owns the connection, most often `{:shared, self()}`. This makes it
443+ so all processes can use the same connection as the one owner by the
444+ current process. This is the mode you will your sync tests in
445+
446+ Whenever you change the mode to `:manual` or `:auto`, all existing
447+ connections are checked in. Therefore, it is recommend to set those
448+ modes before your test suite starts, as otherwise you will check in
449+ connections being used in any other test running concurrently.
431450 """
432451 def mode ( repo , mode )
433- when is_atom ( repo ) or ( is_pid ( repo ) and mode in [ :auto , :manual ] )
434- when is_atom ( repo ) or ( is_pid ( repo ) and elem ( mode , 0 ) == :shared and is_pid ( elem ( mode , 1 ) ) ) do
452+ when ( is_atom ( repo ) or is_pid ( repo ) ) and mode in [ :auto , :manual ]
453+ when ( is_atom ( repo ) or is_pid ( repo ) ) and elem ( mode , 0 ) == :shared and is_pid ( elem ( mode , 1 ) ) do
435454 % { pid: pool , opts: opts } = lookup_meta! ( repo )
436455 DBConnection.Ownership . ownership_mode ( pool , mode , opts )
437456 end
0 commit comments