Repository: elixir-ecto/ecto

Issue #2782: DBConnection.OwnershipError bug in tests
Created: 2018-10-31T00:58:09Z
I've been on a roll today.
Environment

Elixir version (elixir -v): 1.7.4
Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): PostgreSQL 10
Ecto version (mix deps): 3.0.0
Database adapter and version (mix deps): Postgrex 0.14.0
Operating system: MacOS Mojave

Current behavior
Include code samples, errors and stacktraces if appropriate.
I'm running tests that rely upon Ecto.Migrator to run migrations during tests. After upgrading Ecto to version 3.0 some of my tests now are failing with the following error message:
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.700.0>.

 When using ownership, you must manage connections in one
 of the four ways:

 * By explicitly checking out a connection
 * By explicitly allowing a spawned process
 * By running the pool in shared mode
 * By using :caller option with allowed process

 The first two options require every new process to explicitly
 check a connection out or be allowed by calling checkout or
 allow respectively.

 The third option requires a {:shared, pid} mode to be set.
 If using shared mode in tests, make sure your tests are not
 async.

 The fourth option requires [caller: pid] to be used when
 checking out a connection from the pool. The caller process
 should already be allowed on a connection.

 If you are reading this error, it means you have not done one
 of the steps above or that the owner process has crashed.

 stacktrace:
   (db_connection) lib/db_connection.ex:766: DBConnection.transaction/3
   (ecto_sql) lib/ecto/migrator.ex:185: Ecto.Migrator.do_run_maybe_in_transaction/3
   (elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
   (elixir) lib/task/supervised.ex:38: Task.Supervised.reply/5
   (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Expected behavior
The tests should not error out since my application is running the pool in shared mode. Here is the following setup macro for my application
setup tags do
  :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)

  unless tags[:async] do
    Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})
  end

  {:ok, conn: Phoenix.ConnTest.build_conn()}
end

I've confirmed that the async tag is set to true when running the failing test. It looks like the do_run_maybe_in_transaction function includes a call to Task where it previously did not.

Duplicate issue #2784: Running migration while testing failed in Ecto 3.0
Created: 2018-11-01T12:38:23Z
Test code is here:
defmodule MyAppTest do
  use ExUnit.Case

  defmodule Test do
    use Ecto.Schema

    schema "myapptest_test" do
      timestamps()
    end
  end

  defmodule TestMigration do
    use Ecto.Migration

    def change() do
      create table(:myapptest_test) do
        add(:inserted_at, :utc_datetime, null: false)
        add(:updated_at, :utc_datetime, null: false)
      end
    end
  end

  test "test" do
    Ecto.Adapters.SQL.Sandbox.unboxed_run(MyApp.Repo, fn ->
      Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})
      Ecto.Migrator.up(MyApp.Repo, 100, TestMigration)
    end)
  end
end
And run mix ecto.drop; mix ecto.create && mix test. Result is here:
The database for MyApp.Repo has been dropped
The database for MyApp.Repo has been created
                           
21:14:22.282 [debug] QUERY OK source="schema_migrations" db=0.3ms
SELECT s0."version"::bigint FROM "schema_migrations" AS s0 FOR UPDATE []                 
                      
                      
  1) test test (MyAppTest)                  
     test/my_app_test.exs:23                
     ** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 978ms
     stacktrace:                                                        
       (db_connection) lib/db_connection.ex:766: DBConnection.transaction/3
       (ecto_sql) lib/ecto/migrator.ex:184: Ecto.Migrator.do_run_maybe_in_transaction/3
       (elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
       (elixir) lib/task/supervised.ex:38: Task.Supervised.reply/5
       (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

The test is passed in Ecto 2.2.11. So I think it is problem in Ecto 3.0.
I tested on Elixir 1.7.4 and macOS High Sierra and PostgreSQL 10.5.
Full project is here: https://github.com/melpon/ecto3-migration-issue

Closing comment (2018-11-01T12:44:14Z): This is a duplicate of #2782. Unfortunately our migrations now need two database connections and the sandbox only ever uses one. For running migration in tests, I would recommend setting up a separate repository with the explicit purpose of testing those migrations.