Current section

Files

Jump to
retort lib retort sandbox.ex
Raw

lib/retort/sandbox.ex

defmodule Retort.Sandbox do
@moduledoc """
Generates tokens and redeems those tokens for access to Ecto sandbox, such as
[`Ecto.Adapters.SQL.Sandbox`](https://hexdocs.pm/ecto/Ecto.Adapters.SQL.Sandbox.html)
"""
@doc """
Uses decoded beam metadata to allow the current proceess access to the `repo` of `owner`.
"""
@spec allow_access(%{required(:owner) => pid, required(:repo) => module}) :: :ok
@spec allow_access(module, %{required(:owner) => pid, required(:repo) => module}) :: :ok
# See https://github.com/phoenixframework/phoenix_ecto/blob/90ba79feef55e31573047f789b3561f4ab7f30f6/lib/phoenix_ecto/
# sql/sandbox.ex#L58-L61
def allow_access(sandbox \\ Ecto.Adapters.SQL.Sandbox, token)
def allow_access(sandbox, %{owner: owner, repo: repo}) do
repo
|> List.wrap
|> Enum.each(&sandbox.allow(&1, owner, self()))
end
@doc """
`repo` and owner process in a versioned format
"""
@spec version1_token([module] | module) :: map
def version1_token(repo) do
%{owner: self(), repo: repo}
end
end