Access test database from third party client in Phoenix

I’m having this problem: In my test, I have a HTTP client which would send a request to my server to create a record (in test database). If I start the server normally like this: mix phx.server it would create a record in the development database. So I’ve tried MIX_ENV=test iex -S mix phx.server to start the server in the test environment, then I run the test with this command MIX_ENV=test iex -S mix test . But I keep getting the error message:

** (Mix) The database for MyApp.Repo couldn’t be dropped: ERROR 55006 (object_in_use): database “my_app_test” is being accessed by other users

There are 10 other sessions using the database.

I’ve tried to search but seems like there are no similar problems, any help would be very appreciated. Thanks, guys.

Do you have any aliases setup in your mix.exs file? It sounds like when you run mix test an alias may be setup that drops and re-creates the database.

1 Like

IIRC phoenix does create such an alias per default in projects created with mix and without --no-ecto.

Are you starting the server with iex -S mix phx.server AND trying to run mix test while the first one is still running in another terminal window? If so, the 10 connections are probably the Ecto pool from the mix phx.server command that you started in test env and now when mix test runs it to drop and recreate the db to ensure clean slate, it can’t drop because you still have the server running in another terminal window with open connections.

If you just want to run the tests, do not start the server first because mix test will start your application for you as part of the script (as well as resetting the test db before tests run).

I am assuming that the HTTP client you are talking about is part of the test itself, not some external client. If part of an external client, it should be controlled by the test (or mocked out as part of the test) such that it sends the request only once mix test has set up the test instance of your application.