Skip to content

Commit

Permalink
Ensured multi db connections works
Browse files Browse the repository at this point in the history
  • Loading branch information
robconery committed Feb 26, 2017
1 parent d3fe799 commit df635a9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 43 deletions.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ config :moebius, connection: [
test_db: [
database: "meebuss"
],
chinook: [
database: "chinook"
],
scripts: "test/db"
28 changes: 5 additions & 23 deletions lib/moebius.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/moebius/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions test/moebius/extension_test.exs

This file was deleted.

23 changes: 23 additions & 0 deletions test/moebius/multi_connection_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit df635a9

Please sign in to comment.