Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ defmodule MySystem.Repo do
adapter: Ecto.Adapters.Postgres

@impl Ecto.Repo
def init(_type, config) do
{:ok,
Keyword.merge(
config,
hostname: MySystem.Config.db_host(),
database_name: MySystem.Config.db_name(),
pool_size: MySystem.Config.db_pool_size(),
# ...
)
}
def init(context, config) do
Provider.ecto_config(MySystem.Config, context, fn ->
{:ok,
Keyword.merge(
config,
hostname: MySystem.Config.db_host(),
database_name: MySystem.Config.db_name(),
pool_size: MySystem.Config.db_pool_size(),
# ...
)
}
end)
end
end
```
Expand Down
14 changes: 14 additions & 0 deletions lib/provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ defmodule Provider do
@type type :: :string | :integer | :float | :boolean
@type value :: String.t() | number | boolean | nil
@type data :: %{param_name => value}
@type ecto_context :: :supervisor | :runtime

# ------------------------------------------------------------------------
# API
Expand Down Expand Up @@ -129,6 +130,19 @@ defmodule Provider do
end
end

@spec ecto_config(module(), ecto_context(), (-> {:ok, Keyword.t()})) :: {:ok, Keyword.t()}
def ecto_config(mod, context, fun) do
mod.start_link(nil)

ret = fun.()

if context == :runtime do
GenServer.stop(mod)
end

ret
end

# ------------------------------------------------------------------------
# Private
# ------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Provider.MixProject do
use Mix.Project

@version "0.2.0"
@version "0.2.1"

def project do
[
Expand Down
42 changes: 42 additions & 0 deletions test/provider_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ defmodule ProviderTest do
end
end

describe "ecto_config" do
test "starts and stops the server when the context is :runtime" do
System.put_env("OPT_1", "some data")
System.put_env("OPT_2", "42")
System.put_env("OPT_6", "false")
System.put_env("OPT_7", "3.14")

pid = Process.whereis(TestModule)
assert is_nil(pid)

config =
Provider.ecto_config(TestModule, :runtime, fn ->
{:ok, [hostname: TestModule.opt_1()]}
end)

assert {:ok, [hostname: "some data"]} = config

pid = Process.whereis(TestModule)
assert is_nil(pid)
end

test "starts but not stops the server when the context is :supervisor" do
System.put_env("OPT_1", "some data")
System.put_env("OPT_2", "42")
System.put_env("OPT_6", "false")
System.put_env("OPT_7", "3.14")

pid = Process.whereis(TestModule)
assert is_nil(pid)

config =
Provider.ecto_config(TestModule, :supervisor, fn ->
{:ok, [hostname: TestModule.opt_1()]}
end)

assert {:ok, [hostname: "some data"]} = config

pid = Process.whereis(TestModule)
assert not is_nil(pid)
end
end

describe "generated module" do
setup do
Enum.each(1..7, &System.delete_env("OPT_#{&1}"))
Expand Down
Loading