1
votes

I've set up my Phoenix app so that it connects to two databases. I'm having a little trouble managing my migrations and the various Ecto mix tasks - specifically, creating new migrations and rolling back migrations.

First off, here's the repo configuration from config/dev.exs

config :phoenix_backend, PhoenixBackend.Repo,
    adapter: Ecto.Adapters.Postgres,
    username: "postgres",
    password: "postgres",
    database: "phoenix_backend_dev",
    hostname: "localhost",
    pool_size: 10

config :phoenix_backend, PhoenixBackend.SearchRepo,
   adapter: Ecto.Adapters.Postgres,
   username: "postgres",
   password: "postgres",
   database: "phoenix_backend_search_dev",
   hostname: "localhost",
   pool_size: 10

Initially, my app just had one repo, the default Repo, I added SearchRepo later on.

When I first set up SearchRepo, I ran $ mix gen.model to create a new model in that database. The migration went into priv/repo/migrations, and then I adjusted the auto-generated module definition to point at my new database - I changed

defmodule PhoenixBackend.Repo.Migrations.CreateUserRole do

to

defmodule PhoenixBackend.SearchRepo.Migrations.CreateSearch do

When I ran $ mix ecto.migrate, I got the error

Could not find migrations directory "priv/search_repo/migrations" for repo PhoenixBackend.SearchRepo.

So I created that directory, moved the migration into it, and ran the migration again - perfect it worked!

So this brings me to the problem(s)...

I now have two migration directories in priv - priv/repo/migrations and priv/search_repo/migrations.

  1. If I run $ mix ecto.migrate new_migration, this will create a migration file for new_migration in both directories.

  2. If I run $ mix ecto.rollback, this only rolls back migrations from priv/repo/migrations.

  3. If I run $ mix ecto.migrate, this runs the migrations from both directories.

So what I'd like to know is, how can I standardize this behavior? Is the problem with the commands that I'm running, or is there something in the configuration of the app that I need to change to point at these different directories?

I would love to be able to create migrations and point them to the specific directories, and I'd love to be able to specify which databases migrate and rollback point to. How can I customize this behavior?

1

1 Answers

3
votes

The option you are looking for is -r CustomRepo

$ mix ecto.gen.migration add_something -r MyApp.SearchRepo
$ mix ecto.migrate -r MyApp.SearchRepo

Its all in the mix help. To get that

$ mix help ecto.gen.migration
$ mix help ecto.migrate