1

我需要在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 的配置值都是可见的。

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

提前致谢!

4

1 回答 1

2

这是一个 Elixir 错误。你能打开一份报告吗?现在,您将不得不手动执行此操作,尽管这应该会有所帮助:

shared = [adapter: Ecto.Adapters.Postgres]

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

...
于 2015-04-05T17:43:41.807 回答