100

I'm just learning Phoenix and Elixir and I'm coming from Ruby/Rails where I work in the REPL using pry to inspect my database and application state.

I'm trying to figure out how to interact with my database and models in a Phoenix app. I'm aware of iex, but I don't know how to use it inspect my app's database from the repl. Do I need to connect to it with ecto each time from the repl? Is there a rails console equivalent. I've checked the Phoenix docs, Elixir Dose, and the Ecto repo, but can't find what I'm looking for. Am I missing something?

Edit: Based on the answer below I found this section of the ecto docs. Based on this I can do something like ArticlesApi.Repo.all ArticlesApi.Article

Chase
  • 2,748
  • 2
  • 23
  • 32
  • 2
    You can save some typing by aliasing the module names: `alias ArticlesApi.{Repo, Article}`. Also, keep in mind that you can very conveniently recompile individual modules from your console (`r Article`), or recompile the whole project using `recompile`. This allows you to leave an `iex` session open all the time, keeping aliases active. – hmans Mar 31 '17 at 12:27

4 Answers4

132

You can run iex -S mix to run iex with the dependencies in your current mix project included.. You can read about this at http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html

From there you can execute Ecto queries:

iex> MyApp.Repo.all(MyApp.User)

Running iex -S mix phx.server will also start the phoenix server.

Akash
  • 5,153
  • 3
  • 26
  • 42
Gazler
  • 83,029
  • 18
  • 279
  • 245
  • 10
    It's also worth noting the convenience of an `iex.exs` file in the root of your project to allow you to setup aliases (so you don't have to precede everything with `MyApp.`). An example would be: `alias MyApp.Repo alias MyApp.User alias MyApp.Car` – David Kuhta Oct 01 '16 at 22:12
  • 2
    Is there a way I can use this in production environment? – Chris.Zou Nov 04 '17 at 21:18
  • @Chris.Zou Yes, use `bin/my_app remote_console` in your remote shell. – Nic Nilov Aug 20 '18 at 17:46
  • 3
    This is actually `iex -S mix phx.server` since Phoenix 1.3. – t56k Dec 03 '18 at 20:07
29

For runtime debug, (like byebug or debugger or pry in rails), use

require IEx at the top of your model or controller or views file, then type

IEx.pry to wherever you need it to halt at runtime and continue debugging.

Type h for help inside the console

Most importantly, after all that, restart your server with:

iex -S mix phoenix.server

More info: here

Devaroop
  • 7,900
  • 2
  • 37
  • 34
6

If you're working in development, use iex -S mix phx.server.

If you need into the console of a deployed release, then go to your release directory and run bin/<name of your app> remote_console to open up a remote shell to your app's console.

Mark Wilbur
  • 2,809
  • 2
  • 23
  • 22
1

For me I wanted to run pry inside my Elixir tests. For that you need to prefix your mix test command with iex -S

In full this would be something like

iex -S mix test test/meta_api_web/pages/mutation/update/update_model_test.exs:270 
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39