Skip to content

Commit

Permalink
Consider dynamic repo in Ecto.Adapters.SQL operations
Browse files Browse the repository at this point in the history
Closes #386.
  • Loading branch information
josevalim committed Feb 27, 2022
1 parent dd488c0 commit 8727a74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
8 changes: 6 additions & 2 deletions integration_test/sql/sql.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ defmodule Ecto.Integration.SQLTest do
end

test "query!/4 with dynamic repo" do
TestRepo.put_dynamic_repo(:unknown)
assert_raise RuntimeError, ~r/:unknown/, fn -> TestRepo.query!("SELECT 1") end
pid = spawn(fn -> :ok end)
TestRepo.put_dynamic_repo(pid)
assert_raise ArgumentError, fn -> TestRepo.query!("SELECT 1") end

TestRepo.put_dynamic_repo(pid)
assert_raise ArgumentError, fn -> Ecto.Adapters.SQL.query!(TestRepo, "SELECT 1") end
end

test "query!/4" do
Expand Down
22 changes: 16 additions & 6 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ defmodule Ecto.Adapters.SQL do
def explain(repo, operation, queryable, opts \\ [])

def explain(repo, operation, queryable, opts) when is_atom(repo) or is_pid(repo) do
explain(Ecto.Adapter.lookup_meta(repo), operation, queryable, opts)
explain(lookup_meta(repo), operation, queryable, opts)
end

def explain(%{repo: repo} = adapter_meta, operation, queryable, opts) do
Expand Down Expand Up @@ -378,7 +378,7 @@ defmodule Ecto.Adapters.SQL do
def disconnect_all(repo, interval, opts \\ [])

def disconnect_all(repo, interval, opts) when is_atom(repo) or is_pid(repo) do
disconnect_all(Ecto.Adapter.lookup_meta(repo), interval, opts)
disconnect_all(lookup_meta(repo), interval, opts)
end

def disconnect_all(%{pid: pid} = _adapter_meta, interval, opts) do
Expand Down Expand Up @@ -416,7 +416,7 @@ defmodule Ecto.Adapters.SQL do
@spec stream(Ecto.Repo.t, String.t, [term], Keyword.t) :: Enum.t
def stream(repo, sql, params \\ [], opts \\ []) do
repo
|> Ecto.Adapter.lookup_meta()
|> lookup_meta()
|> Ecto.Adapters.SQL.Stream.build(sql, params, opts)
end

Expand Down Expand Up @@ -470,7 +470,7 @@ defmodule Ecto.Adapters.SQL do
def query(repo, sql, params \\ [], opts \\ [])

def query(repo, sql, params, opts) when is_atom(repo) or is_pid(repo) do
query(Ecto.Adapter.lookup_meta(repo), sql, params, opts)
query(lookup_meta(repo), sql, params, opts)
end

def query(adapter_meta, sql, params, opts) do
Expand Down Expand Up @@ -527,7 +527,7 @@ defmodule Ecto.Adapters.SQL do
def query_many(repo, sql, params \\ [], opts \\ [])

def query_many(repo, sql, params, opts) when is_atom(repo) or is_pid(repo) do
query_many(Ecto.Adapter.lookup_meta(repo), sql, params, opts)
query_many(lookup_meta(repo), sql, params, opts)
end

def query_many(adapter_meta, sql, params, opts) do
Expand Down Expand Up @@ -559,7 +559,7 @@ defmodule Ecto.Adapters.SQL do
"""
@spec table_exists?(Ecto.Repo.t, table :: String.t) :: boolean
def table_exists?(repo, table) when is_atom(repo) do
%{sql: sql} = adapter_meta = Ecto.Adapter.lookup_meta(repo)
%{sql: sql} = adapter_meta = lookup_meta(repo)
{query, params} = sql.table_exists_query(table)
query!(adapter_meta, query, params, []).num_rows != 0
end
Expand Down Expand Up @@ -1009,6 +1009,16 @@ defmodule Ecto.Adapters.SQL do
end
end

## Meta

defp lookup_meta(repo) when is_atom(repo) do
Ecto.Adapter.lookup_meta(repo.get_dynamic_repo())
end

defp lookup_meta(pid) when is_pid(pid) do
Ecto.Adapter.lookup_meta(pid)
end

## Migrations

@doc false
Expand Down

0 comments on commit 8727a74

Please sign in to comment.