Hello, Ecto/Elixir enthusiasts!
My name is Bruno, and I'm currently building stuff with Elixir for the company I work in.
So, we're developing an website + api for a client, and we stumbled on the "security" word, so we decided to separate our API from our website.
As dividing stuff in Elixir is pretty easy with umbrella apps, I've went there and made 3 umbrella apps: core, core_web and core_api.
The core project is where all, well, the
core lives in. As it is a basic app (so far), it only contains one Ecto schema.
The other two apps do what they are told: browser and API related stuff.
The problem started when I decided to move migrations back to the
core application. Since schemas will live there, it makes sense to have migrations living there as well.
I proceeded to install
Ecto and Postgrex as a dependency, start them and configure it like this:
config :core, Core.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "core__12354",
database: "core_repository_dev",
hostname: "localhost",
pool_size: 10
After that, I decided to make a Core.Repo module:
defmodule Core.Repo do
use Ecto.Repo, otp_app: Core
end
And after that, make a real otp_app out of the Core module:
defmodule Core do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
supervisor(Core.Repo, []),
]
opts = [strategy: :one_for_one, name: Core.Supervisor]
Supervisor.start_link(children, opts)
end
end
OK, so far so good. All I have to do now is to run the migration!
$ mix ecto.setup # I copied the alias from a Phoenix config file
Oh no!
== Compilation error on file lib/repo/repo.ex ==
** (ArgumentError) missing :adapter configuration in config Core, Core.Repo
lib/ecto/repo/supervisor.ex:37: Ecto.Repo.Supervisor.parse_config/2
lib/repo/repo.ex:2: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
Wait, what? But I can see that I
did added a
adapter: Ecto.Adapters.Postgres up there in the config file! Look:
config :core, Core.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "core__12354",
database: "core_repository_dev",
hostname: "localhost",
pool_size: 10
I suspect there is something to do about the umbrella app. But I need to make this work.
And then, another thought popped in my head:
Wait, so we're making two repos, one for core_web and another for core_api. Ok. So we have... 20 connections to the database machine, sitting idle? Wouldn't it be nice to have only one Repo, with 10 connections?So, is it possible to share a Repo between applications? Both the web and API clients will probably be in the same machine, albeit different ports (API won't be accessible from the outside, only to a handful of machines).
So, it makes sense that both of them shared a Repo, which is just an OTP app that is shared across them.
Any help, please?
I know there is a thread with someone asking the same thing, but I couldn't draw a solution from there.
Thanks!