elixir-ecto / ecto_sql

SQL-based adapters for Ecto and database migrations
https://hexdocs.pm/ecto_sql
Apache License 2.0
572 stars 312 forks source link

ArgumentError in Ecto.Repo.Registry.lookup #119

Closed dmorneau closed 5 years ago

dmorneau commented 5 years ago

Environment

Current behavior

In an older application that has a test_helper.exs file like this one (Phoenix 1.1.x generator):

ExUnit.start
Mix.Task.run "ecto.create", ["--quiet"]
Mix.Task.run "ecto.migrate", ["--quiet"]
Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :manual)

The first time tests are run after a new migration is added, the run fails with this stacktrace:

$ mix test
Generated myapp app
** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Ecto.Repo.Registry, #PID<0.1011.0>, 3)
    (ecto) lib/ecto/repo/registry.ex:18: Ecto.Repo.Registry.lookup/1
    (ecto) lib/ecto/adapter.ex:127: Ecto.Adapter.lookup_meta/1
    (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:486: Ecto.Adapters.SQL.Sandbox.lookup_meta!/1
    (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:387: Ecto.Adapters.SQL.Sandbox.mode/2

Expected behavior

Ideally the repo should still be up and usable after the Ecto migration runs, or there could be a more helpful error message. Since the migration is silent, this showed up as flaky tests that were quite hard to track down.

josevalim commented 5 years ago

Hi, can you please provide a sample application that reproduces the error? Thank you.

dmorneau commented 5 years ago

This should do it:

git clone git@github.com:dmorneau/repro-ecto-119.git
cd repro-ecto-119
mix deps.get
mix test
MIX_ENV=test mix ecto.rollback; mix test

The last command almost always reproduces it for me. The diff versus a new Phoenix app is this commit https://github.com/dmorneau/repro-ecto-119/commit/5074ed5cf6d13ab1218f8ccb3616193b5d5d9e7b.

michalmuskala commented 5 years ago

This error is most likely caused by the Ecto OTP application not starting. Do you have it correctly configured in mix.exs? Are you running the tests with the --no-start flag?