maruko chibi
maruko chibi

Reputation: 13

How to define multiple ecto Repo in config.exs

I need to define multiple ecto Repo in config.exs, but I do not want to define them one by one:

config CC, CC.Repo.S0,
  adapter: Ecto.Adapters.Postgres,
  hostname: "192.168.0.100",
  database: "postgres",
  username: "postgres",
  password: "12345678"


config CC, CC.Repo.S1,
  adapter: Ecto.Adapters.Postgres,
  hostname: "192.168.0.101",
  database: "postgres",
  username: "postgres",
  password: "12345678"

...

So I defined a repo list and try to define them in a loop:

__repo_all__ =  [
  [ hostname: "192.168.0.100",
    database: "postgres",
    username: "postgres",
    password: "12345678" ],

  [ hostname: "192.168.0.101",
    database: "postgres",
    username: "postgres",
    password: "12345678" ]]

__repo_count__ = Enum.count(__repo_all__)

config CC, :repo_all, __repo_all__

config CC, :repo_count, __repo_count__

Enum.reduce(__repo_all__, 0, fn(opts, n) ->
  config CC, String.to_atom(Enum.join([CC.Repo, ".S", n])), 
        [{:adapter, Ecto.Adapters.Postgres} | opts]

  n + 1
end)

I cannot see any repo config while calling Application.get_all_env(CC), but the config value of :repo_all and :repo_count are all visible.

What shall I do to make it work?

Thanks in advance!

Upvotes: 1

Views: 949

Answers (1)

José Valim
José Valim

Reputation: 51359

This is an Elixir bug. Can you please open up a report? For now, you will have to do it manually although this should help:

shared = [adapter: Ecto.Adapters.Postgres]

config CC, CC.Repo.S1,
  [hostname: "192.168.0.101",
   database: "postgres",
   username: "postgres",
   password: "12345678"] ++ shared

...

Upvotes: 2

Related Questions