How to use ecto reset when using multiple repos

Hi,

I have two repos in my phoenix app and one repo targets the database from another app where I cannot have access to it’s migration files.

so now when I hit mix ecto.reset -r MyApp.MyRepo I get:

** (Mix) Could not find migrations directory “/…/my_app/priv/remote_app_repo/migrations” for repo MyApp.RemoteRepo.

I used the -r option to avoid resetting the remote app.

So you can help me in two ways, by telling me:

  • how to reset only the first repo
  • how to reset both repos at once

Thanks

I use for now this command:

mix ecto.drop -r MyApp.MyRepo && mix ecto.create -r MyApp.MyRepo && mix ecto.migrate -r MyApp.MyRepo && mix run priv/repo/seeds.exs -r MyApp.MyRepo

1 Like

Ecto does not have an ecto.reset command. Maybe you are referring to an alias (that I think the phoenix generators add by default)? In which case if it is a simple runner alias then it cannot take arguments, however it can be enhanced so that it does though. Can you check you mix.exs file and see if you have an “ecto.reset” alias in there?

You are right, there is

  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      "test": ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end

My solution:

defmodule Mix.Tasks.Reset do
  use Mix.Task

  @shortdoc "Simply runs the < mix reset > command."
  def run([] = params) do
      repo = String.to_existing_atom("Elixir.MyApp.Repo")
      Mix.Tasks.Ecto.Drop.run(["-r", repo])
      Mix.Tasks.Ecto.Create.run(["-r", repo])
      Mix.Tasks.Ecto.Migrate.run(["-r", repo])
      Mix.Tasks.Run.run(["priv/repo/seeds.exs"])
  end
end
3 Likes

Yep that works, though you can always pass in the repo name in the params list too, thus making it generic. :slight_smile:

I was thinking of that but if I use “-r RemoteRepo” then I don’t know the path to the remote seeds file. I don’t know if even would work. I would have to solve the migration file path also. Do you have a solution?

Add it to the configuration or make a json file to read in or just use some specially named paths or so? :slight_smile:

You lost me :slight_smile:
Can you give something specific, more detailed, choose one :grin:

The config.exs file you could setup the paths as a map from the repo names for the seeds, that would probably be the easiest.