Skip to content

Commit

Permalink
Hooked poolboy up...
Browse files Browse the repository at this point in the history
  • Loading branch information
robconery committed Feb 25, 2017
1 parent e8469c0 commit d3fe799
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Moebius is *not* an ORM. There are no mappings, no schemas, no migrations; only

- Fixed a number of issues surrounding dates etc
- Moved to a more Elixiry way of returning results, using `{:ok, result}` and `{:error, error}`. We were always doing the latter, but decided to move to the former to keep in step with other libraries.
- Moved to Ecto 1.4, along with Dates etc.
- Removed multiple dependencies, including Timex and Poolboy (which is built into the driver, Postgrex)


## Documentation
Expand All @@ -24,7 +26,6 @@ API documentation is available at http://hexdocs.pm/moebius
$ MIX_ENV=dev mix docs
```


## Installation

Installing Moebius involves a few small steps:
Expand All @@ -33,7 +34,7 @@ Installing Moebius involves a few small steps:

```ex
def deps do
[{:moebius, "~> 2.0.0"}]
[{:moebius, "~> 3.0.0"}]
end
```

Expand All @@ -56,8 +57,7 @@ config :moebius, connection: [
hostname: "localhost",
username: "username",
password: "password",
database: "my_db",
pool_mod: DBConnection.Poolboy
database: "my_db"
],
scripts: "test/db"
```
Expand All @@ -71,6 +71,8 @@ config :moebius, connection: [
scripts: "test/db"
```
If you want to use environment variables, just set `HOSTNAME`, `USERNAME`, `PASSWORD` or `DATABASE` and they will be read in.
Under the hood, Moebius uses [the Postgrex driver](https://github.com/ericmj/postgrex) to manage connections and connection pooling. Connections are supervised, so if there's an error any transaction pending will be rolled back effectively (more on that later). The settings you provide in `:connection` will be passed directly to Postgrex (aside from `:url`, which we parse).

You might be wondering what the `scripts` entry is? Moebius can execute SQL files directly for you - we'll get to that in a bit.
Expand Down
2 changes: 2 additions & 0 deletions lib/moebius.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ defmodule Moebius do
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]
cond do
Keyword.has_key?(opts, :url) -> Keyword.merge(opts, parse_connection(opts[:url]))
true -> opts
end
opts ++ pool_opts
end

#thanks to the Ecto team for this code!
Expand Down
8 changes: 5 additions & 3 deletions lib/moebius/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Moebius.Database do
defmacro __using__(_opts) do
quote location: :keep do
@name __MODULE__

alias __MODULE__
def start_link(opts) do

Expand Down Expand Up @@ -101,7 +102,7 @@ defmodule Moebius.Database do

def transaction(fun) do
try do
{:ok, conn} = Postgrex.transaction(@name, fun)
{:ok, conn} = Postgrex.transaction(@name, fun, Moebius.get_connection)
conn
catch
e, %{message: message} -> {:error, message}
Expand Down Expand Up @@ -229,7 +230,8 @@ defmodule Moebius.Database do


def execute(cmd) do
case Postgrex.query(cmd.conn, cmd.sql, cmd.params) do

case Postgrex.query(cmd.conn, cmd.sql, cmd.params, Moebius.get_connection) do
{:ok, result} -> {:ok, result}
{:error, err} -> {:error, err.postgres.message}
end
Expand All @@ -241,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) do
case Postgrex.query(conn, cmd.sql, cmd.params, Moebius.get_connection) do
{:ok, result} -> {:ok, result}
{:error, err} -> Postgrex.query(conn, "ROLLBACK", []) && raise err.postgres.message
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ defmodule Moebius.Mixfile do
defp deps do
[{:postgrex, "~> 0.13.1"},
{:inflex, "~> 1.5.0"},
{:poison, "~> 3.0.0", override: true},
{:poolboy, ">= 0.0.0"},
{:poison, "~> 3.0.0"},
{:ex_doc, "~> 0.11.2", only: [:dev, :docs]},
{:earmark, "~> 0.2.0", only: [:dev, :docs]},
{:credo, "~> 0.2.5", only: [:dev, :test]}]
Expand Down
5 changes: 3 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ExUnit.start()
defmodule TestDb, do: use Moebius.Database

defmodule TestDb do
use Moebius.Database
end

worker = Supervisor.Spec.worker(TestDb, [Moebius.get_connection])
Supervisor.start_link [worker], strategy: :one_for_one
Expand Down

0 comments on commit d3fe799

Please sign in to comment.