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!

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?

1 Like

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.

1 Like

@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:

4 Likes

I think it’s just a database transaction? relevant source: https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/lib/active_record/railties/console_sandbox.rb

From rails docs:

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

sounds “fun” :wink:

4 Likes