Using Ecto Repo in Custom Mix Task
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…
The Fix
For Ecto 2.x
The important function for this scenario is Mix.Ecto.ensure_started/2
Updated for Ecto 3.x
The important function for this scenario is Mix.EctoSQL.ensure_started/2
.
José Valim encourages people to instead use:
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.
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
.