可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I recently upgraded to the newest version of Rails, and I don't understand how to deploy applications to Heroku.
Here is my database.yml file
default: &default adapter: postgresql pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3
I have never seen this syntax before in database.yml. Does anyone know how to configure this?
It looks a lot different than what I'm used to
development: adapter: mysql2 encoding: utf8 database: my_app_development pool: 5 username: root password: test: adapter: mysql2 encoding: utf8 database: my_app_test pool: 5 username: root password: production: adapter: mysql2 encoding: utf8 database: ymca_gym_production pool: 5 username: root password:
Thanks
回答1:
For Heroku you will have to use postgresql as it doesn't support mysql2. Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql
Essentially, treat "heroku's databases" and local databases that you define in this file completely different. It will be easier for you to use sqlite for local and test environments and for production you should change your yaml code to this :
development: adapter: mysql2 encoding: utf8 database: my_app_development pool: 5 username: root password: test: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 production: adapter: postgresql database: my_database_production pool: 5 timeout: 5000
Above code is not enough to get it working on heroku yet, you will also need to edit the gemfile content like below :
gem 'pg', :group => :production gem 'mysql2' , :group => :development gem 'sqlite3', :group => :test
I have made the gemfile code according to the database.yaml code that i wrote. You can use mysql2 for both development and test environment. If you are doing so you can change the gemfile contents like below:
gem 'pg', :group => :production gem 'mysql2' , :group => [:development, :test]
Hope this helps.. :)
回答2:
in yaml
<<: *default
this means that you are appending default to the group, so this
default: &default adapter: postgresql pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3
is equal to this
development: adapter: postgresql pool: 5 timeout: 5000 database: db/development.sqlite3
回答3:
I don't know if this is exactly what you want, but I just spent a while figuring this out myself, so I thought I'd post it here.
ActiveRecord supports getting database connection information both from the environmental variable DATABASE_URL and from the database.yml file. Exactly which one gets used in any situation can be a little tricky. Fortunately, this has already mostly been taken care of - see this pull request for most of the details.
The short version is that if there isn't a url key in your database.yml, then the information in DATABASE_URL automatically overrides database.yml for everything that it can set that is present in the URL, including username, password, server, port, adapter, and database name. If there is a url key in database.yml, then that overrides the DATABASE_URL env var and everything else that it conflicts with in the database.yml entry.
Since Heroku sets a full DATABASE_URL automatically, basically everything except for the pool and encoding are taken from that, and entries for the other things in the production section of the database.yml are ignored. So you can leave them blank, or set anything that is more convenient for you on your dev machine.
Since it is possible to put ERB in yml files, some sources recommend that you explicitly put
production: url: <%= ENV['DATABASE_URL'] %>
in your database.yml to explicitly set the url as the env var. This makes it more clear that whatever is in the env var is meant to override everything in the yml.