Packages
phoenix_ecto
4.4.2
4.7.0
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.3
4.4.2
4.4.1
4.4.0
4.3.0
4.2.1
4.2.0
4.1.0
4.0.0
3.6.0
3.5.0
retired
3.4.0
3.3.0
3.2.3
3.2.2
3.2.1
3.2.0
3.1.0
3.1.0-rc.0
3.0.1
3.0.0
3.0.0-rc.0
3.0.0-beta.2
3.0.0-beta.1
3.0.0-beta.0
2.0.3
2.0.2
2.0.1
2.0.0
1.2.0
1.1.0
1.0.0
0.9.0
0.8.1
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
Integration between Phoenix & Ecto
Current section
Files
Jump to
Current section
Files
lib/phoenix_ecto/sql/sandbox_session.ex
defmodule Phoenix.Ecto.SQL.SandboxSession do
@moduledoc false
use GenServer, restart: :temporary
@timeout 15_000
def start_link({repos, client, opts}) do
GenServer.start_link(__MODULE__, [repos, client, opts])
end
def init([repos, client, opts]) do
timeout = opts[:timeout] || @timeout
sandbox = opts[:sandbox] || Ecto.Adapters.SQL.Sandbox
:ok = checkout_connection(sandbox, repos, client)
Process.send_after(self(), :timeout, timeout)
{:ok, %{repos: repos, client: client, sandbox: sandbox}}
end
def handle_call(:checkin, _from, state) do
:ok = checkin_connection(state.sandbox, state.repos, state.client)
{:stop, :shutdown, :ok, state}
end
def handle_info(:timeout, state) do
:ok = checkin_connection(state.sandbox, state.repos, state.client)
{:stop, :shutdown, state}
end
def handle_info({:allowed, repo}, state) do
send(state.client, {:allowed, repo})
{:noreply, state}
end
defp checkin_connection(sandbox, repos, client) do
for repo <- repos do
:ok = sandbox.checkin(repo, client: client)
end
:ok
end
defp checkout_connection(sandbox, repos, client) do
for repo <- repos do
:ok = sandbox.checkout(repo, client: client)
end
:ok
end
end