Scenario

You have a custom mix task for your project. In that task, you want to interact with your project’s Ecto Repo.

When you do, you get the following error…

$ mix my_custom_task
** (ArgumentError) repo MyProject.Repo is not started, please ensure it is part of your supervision tree

The Fix

For Ecto 2.x

The important function for this scenario is Mix.Ecto.ensure_started/2

Mix.Ecto.ensure_started(MyProject.Repo, [])

Updated for Ecto 3.x

The important function for this scenario is Mix.EctoSQL.ensure_started/2.

José Valim encourages people to instead use:

Mix.Task.run "app.start"

The downside of using “app.start” is it will start your entire application. This may include scheduled background workers or jobs. Because of this, it may have undesirable side-effects. For me, I often only want to start the Repo. Possibly another application too, but if so, I will do that explicitly.

The danger of using ensure_started going forward is that it is considered a “private” API and may break in the future without notice.

Ultimately, it’s your decision.

Example Task

Here’s an example task that puts it to use.

defmodule Mix.Tasks.MyCustomTask do
  use Mix.Task

  @shortdoc "Description of my_custom_task"

  def run(_) do
    # start the Repo for interacting with data
    Mix.EctoSQL.ensure_started(MyProject.Repo, [])

    # code that uses MyProject.Repo
  end

end

Want More Examples?

If you’d like to see more examples of working with an Ecto Repo in your mix tasks, check out the ecto-sql project’s mix tasks or ecto’s mix tasks used for creating the existing tasks like mix ecto.create.