Mix tasks in Phoenix using Ecto

Working with Phoenix version 0.16 I needed to import some records from a csv file.

My first problem was where to place the tasks, after some research I found that you place them under lib/tasks and name them some_name.ex (not exs)

The code for the task should look something like:

You can now run the task with the command

mix task_name

That was the first problem.

Next I needed to use Ecto, in the task, since I wanted to import some records.

When I wanted to insert records I got the error (my app is called ExCMS):

repo ExCMS.Repo is not started, please ensure it is part of your supervision tree

It was pretty obvious from this error message that I should start the Ecto repo, and I found that I needed to run the following code in order to start the Ecto repo:


And all was good.