I have mulitple Repo’s.
# 1
use Mix.Config
config :my_app, MyApp.OneRepo,
adapter: Ecto.Adapters.MySQL,
database: "legacy_db",
username: "username",
password: "password",
hostname: "something.com"
config :my_app, MyApp.TwoRepo,
adapter: Ecto.Adapters.Postgres,
username: "username",
password: "password",
database: "some_db_two",
hostname: "example.com"
config :my_app, ecto_repos: [MyApp.OneRepo, MyApp.TwoRepo]
# 2
defmodule Databases do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
supervisor(MyApp.OneRepo, []), # <-- our addition
supervisor(MyApp.TwoRepo, []) # <-- our addition
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
# 3 [............]
In my routes I have a string corresponding to a Repo.
I can do this statically;
some_table= MyApp.OneRepo.get_by(SomeTable, %{username: "Alpha"})
other_table= MyApp.TwoRepo.all from ot in OtherTable,
where: ot.user_id == ^user.id
How can I do this dynamically, depending on a parameter in a URL? That is, if a URL is "/repos/repo1/articles"
def my_action(conn, params) do
rp_name = params["repo_name"] # repo1
repo = get_repo_from_url_param(rp_name) # ???? how to return MyApp.{RepoOne | RepoTwo | RepoN} ???
# ??? some_table = MyApp.?? repo ?? .get_by(SomeTable, %{username: "Alpha"})
end
Assume that all the Repo’s have the identical database schemas.
All the Repo’s of my app are declared in the config file - statically.