Phoenix equivalent of rails console sandbox

What’s elixir/phoenix equivalent of below?

rails console —sandbox

Which rollbacks all DB changes done from console upon exit.

Thanks!

1 Like

You should explain what…

rails console —sandbox

…does because not everybody knows Rails.

Probably You can setup an environment using Ecto Sandbox.

2 Likes

My question updated now.

Hi @meraj_enigma I don’t think there is anything you can run that will do exactly that. However, Ecto does have a sandbox adapter that you can run. Someone with some experience with that could probably wire together something like your console command relatively easily. There are some caveats to this though, since the sandbox works by putting everything in a transaction which never commits. This means that you’re ultimately limited to the sandbox time limit your database allows, and things which rely on emitting at commit time (like pg_notify) will never fire.

2 Likes

Thanks for your reply @benwilson512. I find it pretty useful in Rails. I hope someday it will be available for IEx too.

Anyone know how rails implements this?

2 Likes

At $work we just drop our dev DBs and recreate whenever it seems appropriate – it takes very little time to drop->create->migrate->seed and get back to work :grinning: Alternatively you could set up a separate sandbox environment and do the same there if you need/want to preserve the data in dev.

2 Likes

@benwilson512 I would like to know too how rails does it and why in elixir/phoenix we still don’t have such a useful thing!

I would imagine it having something to do with this being the first request for the feature, and no PRs contributing it :wink:

5 Likes

I think it’s just a database transaction? relevant source: rails/activerecord/lib/active_record/railties/console_sandbox.rb at b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c · rails/rails · GitHub

From rails docs:

a long running session of sandbox console could lead a database server to run out of memory

sounds “fun” :wink:

5 Likes

Run the following command from within the root of your Phoenix project:
iex -S mix

1 Like