【问题标题】:Specifying different database addresses in project.clj在 project.clj 中指定不同的数据库地址
【发布时间】:2015-03-08 06:53:33
【问题描述】:

我有一个 Leiningen Clojure 项目,它在 project.clj 中指定一个 Postgres 数据库,如下所示:

:profiles {:test {:env {:db-address "//non-standard/location" ...} ...} ...}

现在我试图在数据库位于标准位置的机器上运行我的测试,但我不知道如何以尊重该位置的方式运行我的测试。

我添加了一个额外的个人资料条目:

:other {:env {:db-address "//standard/location"}}}

...然后运行lein with-profile other test,认为只有:db-address 条目会覆盖:test 中的条目,但我仍然得到Connection refused。

我在:other 中完全复制了:test 条目,只是我只更改了:db-address 条目,但这没有任何效果,无论我运行lein with-profile other test 还是lein with-profile +other test。

如果我随后完全删除 :test 配置文件,leiningen 将完全失败,即使文档说 lein with-profile other test 应该忽略其他配置文件。

我非常难过。如果我在运行lein 之前用project.clj 中的标准位置文本替换非标准位置,我可以运行我的测试,但我希望有一些更官方和更少hacky 的东西。任何帮助将不胜感激。

编辑添加:

我尝试以两种方式使用^:replace 标签。

:other {:env {:db-address ^:replace "//standard/location" } }

这会导致错误Metadata can only be applied to IMetas。

:other {:env ^:replace { ... map copied from :test profile ... } }

这会导致错误java.io.FileNotFoundException: Could not locate ring/mock/request__init.class or ring/mock/request.clj on classpath: , compiling [...]。所以显然我的 CLASSPATH 在这种情况下被核了?莫名其妙。

编辑进一步添加:

上面的“空类路径”错误显然是由于:test 配置文件中的:dependencies 参数我没有转移到:other 配置文件。

【问题讨论】:

  • 这应该可以。我能想到一些它没有的原因。您是否尝试过在您的:other 配置文件中使用^replace,例如:db-address ^:replace "//some-location",或者在整个:env 键上使用更好。您是否在启动时打印调试过:db-address 的值?您是否有一个环境变量 DB_ADDRESS 可以覆盖 project.clj 中的那个?
  • 应该可以。我唯一能想到的是两个配置文件都被加载并且test 覆盖other。试试 Kungi 的建议,或者这样:lein with-profile -test,+other test
  • 啊,是的,我尝试使用:^replace 标签,只是忘了提及。有关详细信息,请参见上文。
  • Kungi:您的评论基本上让我解决了这个问题。如果您将其发布为官方答案,我会接受。
  • 我会为标准位置创建一个额外的配置文件,然后简单地运行 lein with-profile +standard-test test 用于标准数据库位置的测试,lein with-profile +other-test test 用于另一个。此外,您可以为这两种情况创建别名,例如 {:aliases {"test" ["with-profile" "+standard-test" "test"] "test-other" ["with-profile" "+other-test" "test"] } }。

标签: clojure leiningen


【解决方案1】:

我发现最好不要将这些信息直接保存在我的 project.clj 文件中。相反,我使用environ,这将允许您使用shell 环境变量或环境配置文件——您可以为每个环境使用不同的环境变量。

这样做的好处是您不必担心可能会将敏感的数据库详细信息推送到版本控制中,并且您可以获得更简单的 project.clj (IMO) 设置。我没有将我的环境配置文件放入版本控制中,并且我发现使用环境配置文件和普通 shell 环境的组合来管理部署很容易(可能不熟悉 clojure 的系统管理员非常熟悉)

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多