earth7316

earth7316

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.

Showing Posts 1 to 5

NobbZ

NobbZ

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.

earth7316

earth7316 OP

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"]
    ]
  end
josevalim

josevalim

Creator of Elixir

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.

earth7316

earth7316 OP

Ah thanks Jose - much better!!

vineetv2821993

vineetv2821993

Sorry for joining late. Follow this post: Using Multiple DBs in Phoenix · Blog

— 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
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
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews