Ecto/Phoenix/Mix with multiple databases

Hi all

I have a Phoenix application that has it’s own Postgres database and also needs to communicate to a read only database on a different machine to get catalog type of data. This works fine and I’ve setup my application with two Repo’s and can retrieve data.

The problem I’m running into is with running
mix test

this is attempting to use a test database that doesn’t exist (and can’t really as the read only database is a huge legacy system of record for multiple applications). I know with ecto.migrate I can pass a -r flag to say which Repo to use but when running tests that flag doesn’t seem to exist.

Can anyone point me to some documentation on this?

BTW even the -r flag is a bit wonky in my opinion. I would think having a flag in the configuration to say the database is read only so don’t try and use it for generators, migrations or tests would be a much cleaner integration and potentially avoid risk of accidentally running mix ecto.reset and having it cascade inadvertently to a database that wasn’t meant for dropping (I realize you could have a different db user that’s read only but for development it’s not always easy to get an operations team member to do things like add users to a database).

Thanks for any help.

1 Like

Create a dummy database for that read-only one.

Unless your tests are directly or indirectly relying on data in that database it will work if you leave it empty I think. If not you need to fill in some stub data.


Thanks Nobbz that works but is kind of wonky for my needs. I discovered though that I can modify the aliases in mix.exs to do what I need for this scenario which is migrations, reset, create, drop ONLY apply to my database and not my read replica that I use for ancillary data and is not part of this applications CRUD operations. So I did this and it works great

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

There is a configuration in your config files named ecto_repos. If you don’t pass the --repo/-r flags, only the repositories listed there will be set up, migrated, etc. You should be able to set the config to an empty list, change it per environment, etc.


Ah thanks Jose - much better!!

Sorry for joining late. Follow this post: