Sharing a Repo between different applications

584 views
Skip to first unread message

Annard Brouwer

unread,
Jan 12, 2016, 9:08:50 AM1/12/16
to elixir-ecto
Hello fellow elixir+ecto enthusiasts,

A question to the people familiar with Ecto and its intended use for a Repo intended to be shared between different applications.
In our case we have a series of applications (that are not part of an umbrella project) that we intend to run on different machines. However, two of them are using a Repo that we would like to make available to both. So here is our situation:

App Core (defines a Model.Repo with all the necessary logic)

App Prez (wants to use Model.Repo from App Core as well)

For App Prez we've added App Core to the applications that need to run in mix.exs. However, this doesn't compile because the code in App Core that is defined within the deps folder can't find its configuration for the Repo within that deps folder:

== Compilation error on file lib/model/repo.ex ==
** (ArgumentError) missing :adapter configuration in config :core, Model.Repo
   lib/ecto/repo/supervisor.ex:37: Ecto.Repo.Supervisor.parse_config/2
   lib/model/repo.ex:6: (module)
   (stdlib) erl_eval.erl:669: :erl_eval.do_apply/6

By copying that configuration data inside the config.exs of App Prez as follows (prez/config/config.exs) we get it to compile:

config :core, :app_repo, Model.Repo
config :core, Model.Repo,
  adapter: Ecto.Adapters.Postgres,
config :prez,
  xxx: yyy

Now we have two copies of this configuration data:
  1. in the core project
  2. in the prez project
This seems clumsy to us. Is there a better way?

How would people share the functionality of a database with its Ecto model between different applications using only one copy of the source code and its configuration?

Thank you for you insights!
Annard

José Valim

unread,
Jan 12, 2016, 12:50:36 PM1/12/16
to elixi...@googlegroups.com
Are you using umbrella applications? If so, you can share configs.

There are a couple other alternatives. For example, you can include the config from ../deps/core/config/config.exs. I have not tried this and it may be a bad idea but worth exploring.

If not, you can configure the core application in itsr mix.exs instead of config/config.exs, this means it will be exported alongside your app:

def application do
  [env: [{MyRepo, username: ...}]]
end

You could also try something like this:

def application do
  [env: Application.get_all_env(:core)]
end

Although I have never tried this last one. It may possibly be a bad idea too.


José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/d24b2ed7-7212-4ddd-bf13-d8bd07561f12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary Rennie

unread,
Jan 13, 2016, 3:42:20 AM1/13/16
to elixir-ecto
 > There are a couple other alternatives. For example, you can include the config from ../deps/core/config/config.exs. I have not tried this and it may be a bad idea but worth exploring.

I have used this, can't say I have noticed any difficulties so far. One this I made sure to do is include the core config before the application config so that it could be overwritten if necessary.

Annard Brouwer

unread,
Jan 13, 2016, 4:56:37 AM1/13/16
to elixi...@googlegroups.com
Thanks guys, we will give this a try. It seems the easiest option.

olivier...@intellicore.net

unread,
Jan 19, 2016, 4:12:45 AM1/19/16
to elixir-ecto

 > There are a couple other alternatives. For example, you can include the config from ../deps/core/config/config.exs. I have not tried this and it may be a bad idea but worth exploring.

I have used this, can't say I have noticed any difficulties so far. One this I made sure to do is include the core config before the application config so that it could be overwritten if necessary.


I tried this approach and I have the following issue when executing mix deps.get could not load config deps/core/config/config.exs

So it looks like the configuration file are read before retrieving the dependencies which sounds a bit odd.
Is there any reason why the configuration files are read before?

Thank you.

José Valim

unread,
Jan 19, 2016, 4:28:51 AM1/19/16
to elixi...@googlegroups.com
Ah yes, good catch. This approach won't work then unless check if the file exists before... which I am not sure makes the solution still worth.

The config files are loaded earlier because they may configure Mix itself or a dependency that is used when compiling another dependency and so on.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages