diff --git a/config/config.exs b/config/config.exs index 634ac92..7191c07 100644 --- a/config/config.exs +++ b/config/config.exs @@ -8,4 +8,7 @@ config :moebius, connection: [ test_db: [ database: "meebuss" ], +chinook: [ + database: "chinook" +], scripts: "test/db" diff --git a/lib/moebius.ex b/lib/moebius.ex index 935610b..d355af6 100644 --- a/lib/moebius.ex +++ b/lib/moebius.ex @@ -35,35 +35,17 @@ defmodule Moebius do System.cmd "psql", args end - def system_envs(raw_opts) do - Enum.map(Enum.into(raw_opts, %{}), fn {k, v} -> - case v do - {:system, env_var} -> - case System.get_env(env_var) do - nil -> raise "No environment variable or default set for #{env_var}." - val -> {k, val} - end - {:system, env_var, default} -> - case System.get_env(env_var) do - nil -> {k, default} - val -> {k, val} - end - _ -> {k, v} - end - end) - end - - def get_connection(), do: get_connection(:connection) + def pool_opts do + [pool: DBConnection.Poolboy] + end def get_connection(key) when is_atom(key) do - #thanks to oskarth for this - had to pull in manually to fit to v3 - opts = system_envs(Application.get_env(:moebius, key)) - pool_opts = [pool: DBConnection.Poolboy] + opts = Application.get_env(:moebius, key) cond do Keyword.has_key?(opts, :url) -> Keyword.merge(opts, parse_connection(opts[:url])) true -> opts end - opts ++ pool_opts + opts ++ pool_opts() end #thanks to the Ecto team for this code! diff --git a/lib/moebius/database.ex b/lib/moebius/database.ex index 9020800..8a548a6 100644 --- a/lib/moebius/database.ex +++ b/lib/moebius/database.ex @@ -231,7 +231,7 @@ defmodule Moebius.Database do def execute(cmd) do - case Postgrex.query(cmd.conn, cmd.sql, cmd.params, Moebius.get_connection) do + case Postgrex.query(cmd.conn, cmd.sql, cmd.params, Moebius.pool_opts) do {:ok, result} -> {:ok, result} {:error, err} -> {:error, err.postgres.message} end @@ -243,7 +243,7 @@ defmodule Moebius.Database do it will be caught in `Query.transaction/1` and reported back using `{:error, err}`. """ def execute(cmd, %DBConnection{} = conn) do - case Postgrex.query(conn, cmd.sql, cmd.params, Moebius.get_connection) do + case Postgrex.query(conn, cmd.sql, cmd.params, Moebius.pool_opts) do {:ok, result} -> {:ok, result} {:error, err} -> Postgrex.query(conn, "ROLLBACK", []) && raise err.postgres.message end diff --git a/test/moebius/extension_test.exs b/test/moebius/extension_test.exs deleted file mode 100644 index 2b13ccb..0000000 --- a/test/moebius/extension_test.exs +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Moebius.ExtensionTest do - use ExUnit.Case - - test "certain types are ignored on read" do - res = """ - select to_tsvector('ha ha'), - now()::timestamptz, - '127.0.0.1'::inet, - '6d52474a-912a-4c3c-8ca2-56e55ae2f3f8'::uuid, - --12.00::money, - 12::bigint, - 'hello'::text, - now()::time, - now()::date - """ |> TestDb.run - assert res - end -end diff --git a/test/moebius/multi_connection_test.exs b/test/moebius/multi_connection_test.exs new file mode 100644 index 0000000..ecefeaf --- /dev/null +++ b/test/moebius/multi_connection_test.exs @@ -0,0 +1,23 @@ +# defmodule DB1, do: use Moebius.Database +# defmodule DB2, do: use Moebius.Database +# +# +# defmodule Moebius.BasicSelectTest do +# +# use ExUnit.Case +# import Moebius.Query +# +# setup_all do +# w1 = Supervisor.Spec.worker(DB1, [Moebius.get_connection(:test_db)]) +# w2 = Supervisor.Spec.worker(DB2, [Moebius.get_connection(:chinook)]) +# Supervisor.start_link [w1, w2], strategy: :one_for_one +# +# {:ok, [thing: true]} +# end +# +# test "they connect to different dbs" do +# {:ok, res} = db(:artist) |> limit(1) |> DB2.run +# IO.inspect res +# end +# +# end