foggy

foggy

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?

Showing Posts 1 to 6

foggy

foggy OP

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.

jackmarchant

jackmarchant

You need to make sure to start Repo before using it in a Mix task:

def run(_) do
 Mix.Ecto.ensure_started(App.Repo, [])
 App.Adapter.list_all(App.Thing)
end
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).

onomated

onomated

Running into this issue myself. Currently creating a mix task to generate a shell of my graphql schema based on my ecto schema. Need to get info about indexed columns/fields as well as nullability of fields, so attempting to execute:

Ecto.Adapters.SQL.query!(MyApp.Repo, "select * from pg_indexes where schemaname='public'")

for example to get a list of indexes. I encounter the same error:

** (RuntimeError) could not lookup MyApp.Repo because it was not started or it does not exist
    lib/ecto/repo/registry.ex:18: Ecto.Repo.Registry.lookup/1
    lib/ecto/adapter.ex:127: Ecto.Adapter.lookup_meta/1

Mix.Ecto.ensure_started... No longer exists. How else can we start the repo in a mix task?

"ecto": {:hex, :ecto, "3.1.7",},
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

onomated

onomated

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews