Mix tasks accessing the db with `Mix.Ecto`
ensure_started is no longer available. Try
Application.ensure_all_started(:tilex), where tilex is the module/atom that represents your application.
When running reports or one-off data operations it might be necessary to create
a mix task that can access the database. Ecto provides convenience functions in
Mix.Ecto module to help facilitate setting up and starting the Ecto
parse_repo(args) will process the arguments used when calling the mix task. It looks specifically for
-r MyApp.Repo but if you don't pass anything it will return all the repos from the configuration.
ensure_started(repo) takes the repo as an argument ensures that the Repo application has been started. Without calling this function the Repo will throw an error when used.
Put it all together:
defmodule Mix.Tasks.MyApp.SayHi do use Mix.Task import Mix.Ecto def run(args) do repo = parse_repo(args) |> hd ensure_started(repo) result = repo.query("select 'hi!';") result.rows |> hd |> hd |> IO.puts end end