I want to display data from my DB through Ecto in a custom mix task. How can I get the Ecto repo in my task (or start it)?
I tried something like this but it didn't work:
defmodule Mix.Tasks.Users.List do
use Mix.Task
use Mix.Config
use Ecto.Repo, otp_app: :app
@shortdoc "List active users"
@moduledoc """
List active users
"""
def run(_) do
import Ecto.Query, only: [from: 1]
Mix.shell.info "=== Active users ==="
query = from u in "users"
sync = all(query)
Enum.each(users, fn(s) -> IO.puts(u.name) end)
end
end
This will give me the following output when I launch mix users.list:
** (ArgumentError) repo Mix.Tasks.Users.List is not started, please ensure it is part of your supervision tree
lib/ecto/query/planner.ex:64: Ecto.Query.Planner.query_lookup/5
lib/ecto/query/planner.ex:48: Ecto.Query.Planner.query_with_cache/6
lib/ecto/repo/queryable.ex:119: Ecto.Repo.Queryable.execute/5
Any idea or other way to solve this problem?