foggy

foggy

Create mix tasks that invoke existing modules which use Ecto

I have a simple module that, when invoked, lists all records in a table.

defmodule App.Adapter do
  alias App.Repo

  def list_all(model) do
    Repo.all(model)
  end
end

I want to have a simple mix task such that I can invoke this function.

defmodule Mix.Tasks.ListAllThings do
  use Mix.Task

  def run(_) do
    App.Adapter.list_all(App.Thing)
  end
end

If I try to invoke this task (and the task does exist), the error is (RuntimeError) could not lookup App.Repo because it was not started or it does not exist. I have a supervisor and am able to run these queries from iex -S mix in the console. Everything I have looked up about this so far has suggested using import Mix.Ecto but none of the helper method seem to behave to me (#ensure_started doesn’t make sense to me).

This list_all task is meant as a quick proof of concept for a small app that essentially just acts as a bundle of scripts to interface with a DB on the fly (no API involved and the app does not persist on a machine for any period of time beyond running the script). Am I just tangling myself in an anti-pattern? If I want to just write elixir scripts to interface with a DB should I be approaching this with something other than tasks?

First Post!

foggy

foggy

I’m still curious about this but having hacked at this a bit it seems like using mix run to run scripts is a more felicitous approach for my use case.

Most Liked

al2o3cr

al2o3cr

The Phoenix docs suggest using:

Mix.Task.run("app.start")

in your task’s run/1 function to boot the application and its dependencies.

Alternatively, if you just need a repo, there’s Ecto.Migrator.with_repo/2: ecto_sql/lib/ecto/migrator.ex at master · elixir-ecto/ecto_sql · GitHub

axelson

axelson

Scenic Core Team

WARNING: In Ecto 2.X you should not reference Mix.Ecto because it is a private API: Preload fails to checkout connection from poolboy · Issue #1586 · elixir-ecto/ecto · GitHub

And in Ecto 3.X, while Mix.Ecto is now considered public (i.e. no longer has @moduledoc false), the ensure_started function has been removed. Off the top of my head I’m not sure what you should use instead (but maybe it is Mix.Task.run).

Last Post!

onomated

onomated

Thanks @al2o3cr, Ecto.Migrator.with_repo/2 was just what the doctor ordered. Works! Wish I could select as answered.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement