Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly setup a database.yml file in Rails 4

I'm curious how to correctly set up my database.yml file in a Rails 4 application.

It's not something I have really looked into in great detail as it all seems to just work when deploying to Heroku, but I want to understand it now and have noticed that the format has changed a little from Rails 4.0 to 4.1. For example

4.0.2

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:

4.1.0

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
    database: my_app_development


test:
  <<: *default
   database: my_app_test


 # On Heroku and other platform providers, you may have a full connection URL
 # available as an environment variable. For example:
 #
 #   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
 #
 # You can use this database configuration with:
 #
 #   production:
 #      url: <%= ENV['DATABASE_URL'] %>
 #

 production:
   <<: *default
     database: my_app_production
     username: my_app
     password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

My questions are

  1. Should I be setting usernames and passwords in ALL environments?
  2. I'm using ClearDB on Heroku as my database. Should I be using url: <%= ENV['DATABASE_URL'] %> for production as the comments suggest?
like image 811
Richlewis Avatar asked May 07 '14 18:05

Richlewis


People also ask

What is in database yml rails?

The database. yml is the file where you set up all the information to connect to the database. It differs depending on the kind of DB you use. You can find more information about this in the Rails Guide or any tutorial explaining how to setup a rails project.

How do I connect multiple databases in rails?

Rails 6.0 ships with all the rails tasks you need to use multiple databases in Rails. Running a command like bin/rails db:create will create both the primary and animals databases.


1 Answers

The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.

To answer your other questions:

1) Should I be setting usernames and passwords in ALL environments

you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.

2)If I'm using clear DB with Heroku as my database then should I be uncommenting

heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). 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.

like image 87
derekyau Avatar answered Sep 24 '22 13:09

derekyau