Ecto Testing Sandbox Module Not Available

Hi all! Trying to follow the docs to get a testing sandbox setup with a project that already has a functioning Ecto database.

Upon mix test I’m getting -

**[info]  Migrations already up
 (UndefinedFunctionError) function PhotoGrid.Repo.get_dynamic_repo/0 is undefined (module PhotoGrid.Repo is not available)
    PhotoGrid.Repo.get_dynamic_repo()

I believe I have the naming convention wrong for this test repo, but I don’t know what it’s supposed to be. Following the docs, the convention seems to be MyApp.Repo, my app’s name is PhotoGrid. I understand the naming convention for the existing Ecto database is different. For example-

config/config.exs

import Config

config :photo_grid, Posts.Repo,
  database: "photo_grid_repo",
  username: "postgres",
  password: "pass",
  hostname: "localhost"


config :photo_grid, ecto_repos: [Posts.Repo]

import_config "#{Mix.env()}.exs"

vs

config/test.exs

use Mix.Config

config :photo_grid, PhotoGrid.Repo,
  username: "postgres",
  password: "postgres",
  database: "photo_grid_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

As well-

photo_grid_test.exs

defmodule PhotoGridTest do
  use ExUnit.Case

  setup do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(PhotoGrid.Repo)
  end
....
end

Thanks for any help! I’m sure I’m missing something simple here.

So, uhm, does the PhotoGrid.Repo module exist? And why a different repo module for the test environment?

No, I didn’t explicitly make a PhotoGrid.Repo. That’s part of my confusion with the docs- I didn’t quite understand if in this setup Ecto was doing any work behind the scenes to essentially create a sandbox that’s PhotoGrid.Repo , separate from my real data existing in Posts.Repo. Perhaps I misunderstood the whole abstraction :upside_down_face:

I suppose it should just be Posts.Repo for everything like I have in config/config.exs, correct?

Thanks for your helpful questions and sanity check !

Your basic criteria for this is the following:

Find all occurences of use Ecto.Repo in your code. Any modules that match that are a candidate for usage in DB operations. If a module does not have use Ecto.Repo inside then you can’t use it for DB operations.

2 Likes

That makes sense. Thanks Dimitar!

Did you fix your problem?

Yes, thanks Dimitar!

Glad to hear it. I recommend, for future readers’ sake, to post how did you solve it and how does your corrected code look like – if you don’t mind.

It was just by prepending the appropriate calls of .Repo with Posts. This was confirmed by your reminder of what’s available Repo wise with what already exists with use Ecto.Repo .

But that doesn’t seem to be the problem? If you do NOT do alias Photos.Repo then it’s pretty normal to use the fully qualified name.

I would think the fix is replacing PhotoGrid.Repo – which doesn’t seem to exist – with Posts.Repo.

I’m sorry, it seems I didn’t communicate this clearly above.

That’s what I did, replaced PhotoGrid.Repo, with Posts.Repo :slight_smile:

Thanks for clarifying!

1 Like