From 8727a7400b8e5b0996334c99e7d1663dd0b88243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 27 Feb 2022 11:34:00 +0100 Subject: [PATCH] Consider dynamic repo in Ecto.Adapters.SQL operations Closes #386. --- integration_test/sql/sql.exs | 8 ++++++-- lib/ecto/adapters/sql.ex | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/integration_test/sql/sql.exs b/integration_test/sql/sql.exs index b84d755b..76dbada2 100644 --- a/integration_test/sql/sql.exs +++ b/integration_test/sql/sql.exs @@ -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 diff --git a/lib/ecto/adapters/sql.ex b/lib/ecto/adapters/sql.ex index 674d6c6f..fdceace4 100644 --- a/lib/ecto/adapters/sql.ex +++ b/lib/ecto/adapters/sql.ex @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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