Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple databases in Ecto

Tags:

elixir

ecto

I have an application which targets multiple databases, it's like an application that watch applications which make reports, mediate notifications, run tasks and stuffs, in a way that is impossible to be controlled statically(in the configuration file), so i thought that it could be done starting a supervisor that supervises multiple Repos just changing its settings in the opts(i was thinking in a pool of repos, something like this, sounds crazy, i know), but it's impossible since Ecto.Repo has a macro which makes connection bound to the Repo's module, so, there's a way to rewrite the Ecto.Repo in a way that i can achieve my needs? I looked at ecto's sources, but i've found no solution digging alone.

I also looked at this post, but there's a possibility that the application will target multiple instances, with different users, different credentials, etc.

I've reached some solutions using Code.eval for create Repos on-the-fly, but i don't know how "good" is it, and if it's encouraged.

like image 747
Wesley Schleumer de Góes Avatar asked Aug 22 '16 02:08

Wesley Schleumer de Góes


1 Answers

In case you know all the repositories you need to access I suggest splitting the application in more umbrella apps. Each umbrella app can connect to a different ecto repository.

As a good practice I always extract Ecto and database related code to separate umbrella applications separated by domains. You can see one example in this demo application: https://github.com/silviurosu/elixir-umbrella-ddd.

like image 82
silviu.rosu Avatar answered Nov 03 '22 00:11

silviu.rosu