简体   繁体   English

Rails 5 - ENV DATABASE_URL - 指定编码和排序规则

[英]Rails 5 - ENV DATABASE_URL - specify encoding and collation

Our database.yml is added to .gitignore so devs can customize local environments and we plan to use ENV['DATABASE_URL'] for production servers.我们的 database.yml 被添加到.gitignore以便开发人员可以自定义本地环境,我们计划将ENV['DATABASE_URL']用于生产服务器。 For default setup, this works.对于默认设置,这有效。 However, we need to configure encoding and collation to utf8mb4 .但是,我们需要将encodingcollation配置为utf8mb4

  • encoding: utf8mb4编码:utf8mb4
  • collation: utf8mb4_unicode_ci整理:utf8mb4_unicode_ci

I tried padding it to query parameters, like the ?pool=5 example in the docs, but it doesn't seem to work.我尝试填充它以查询参数,例如文档中的?pool=5示例,但它似乎不起作用。

DATABASE_URL=mysql2://user:passwd@host:port/dbname?encoding=utf8mb4&collation=utf8mb4_unicode_ci

The tables created are still using the default encoding/collation so I assume the parameters doesn't work.创建的表仍然使用默认编码/整理,所以我假设参数不起作用。 Is there any way I could configure this by any other methods?有什么办法可以通过任何其他方法配置它吗? Encoding and collation is the same for all environments.所有环境的编码和整理都是相同的。

Requirement is that dev environment can have a file with a config whereas prod should have no special file added, it should only use ENV variables.要求是 dev 环境可以有一个带有 config 的文件,而 prod 应该没有添加特殊文件,它应该只使用 ENV 变量。 Maybe add this to one of the files inside config dir like application.rb or other files?也许将此添加到配置目录中的文件之一,如 application.rb 或其他文件?

Thanks in advance.提前致谢。

PS: I'm new to Rails and Ruby (1 week) since I'm coming from PHP/Python. PS:我是 Rails 和 Ruby 的新手(1 周),因为我来自 PHP/Python。

You shouldn't specify any of the configuration options in the ENV variable.您不应在 ENV 变量中指定任何配置选项。 You should keep it clean and specify these options in database.yml file:您应该保持干净并在 database.yml 文件中指定这些选项:

production:
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci
  pool: 5

The way it works is that rails takes what it can from the DATABASE_URL, and then applies options configured in database.yml for specified environment.它的工作方式是 rails 从 DATABASE_URL 中获取它可以获取的内容,然后将 database.yml 中配置的选项应用于指定的环境。 If you want to learn more, follow this link: https://devcenter.heroku.com/articles/rails-database-connection-behavior#rails-4-1-and-beyond如果您想了解更多信息,请点击此链接: https : //devcenter.heroku.com/articles/rails-database-connection-behavior#rails-4-1-and-beyond

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM