如何在config.exs中定义多个ecto Repo

时间:2015-04-05 13:04:33

标签: config elixir ecto

我需要在config.exs中定义多个ecto Repo,但我不想逐个定义它们:

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"

...

所以我定义了一个repo列表并尝试在循环中定义它们:

__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)

我在调用Application.get_all_env(CC)时看不到任何repo配置,但是:repo_all和:repo_count的配置值都是可见的。

我该怎么做才能让它发挥作用?

提前致谢!

1 个答案:

答案 0 :(得分:2)

这是一个Elixir错误。你能打开一份报告吗?现在,你必须手动完成它,虽然这应该有所帮助:

shared = [adapter: Ecto.Adapters.Postgres]

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

...