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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
foggy
I’m still curious about this but having hacked at this a bit it seems like using
mix runto run scripts is a more felicitous approach for my use case.jackmarchant
You need to make sure to start Repo before using it in a Mix task:
axelson
WARNING: In Ecto 2.X you should not reference
Mix.Ectobecause it is a private API: Preload fails to checkout connection from poolboy · Issue #1586 · elixir-ecto/ecto · GitHubAnd in Ecto 3.X, while
Mix.Ectois now considered public (i.e. no longer has@moduledoc false), theensure_startedfunction has been removed. Off the top of my head I’m not sure what you should use instead (but maybe it isMix.Task.run).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:
for example to get a list of indexes. I encounter the same error:
Mix.Ecto.ensure_started...No longer exists. How else can we start the repo in a mix task?al2o3cr
The Phoenix docs suggest using:
in your task’s
run/1function 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 · GitHubonomated
Thanks @al2o3cr,
Ecto.Migrator.with_repo/2was just what the doctor ordered. Works! Wish I could select as answered.