Elixir: Sandboxing an `iex` session
We can change the default .iex.exs
file for another to be pre-loaded by iex
using --dot-iex
:
MIX_ENV=test iex --dot-iex .iex-sandbox.exs -S mix
This way we could create a rudimentary way to "protect" our existing database of changes on the iex
session. Check this out:
# .iex-sandbox.exs
:ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo, ownership_timeout: 300_000)
This approach works for existing databases, so if the app we are working has a very difficult data setup then maybe this is the way to go. Although, making the db to hold data changes for as long as iex sessions are running it can be a bit too much in terms of db memory. Row & Table locks can definately be a problem as well.
Another important note is that we may have to call Sandbox.allow/3 if we need to call a GenServer or another process that touches the db.
The way to go
If we have a simpler data, with a nice seeds file, we'd be much better this way:
(export DB_NAME=my_app_sandbox && mix ecto.reset && iex -S mix)
Don't forget to change our ecto config to use that ENV var.
Tweet