Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku what does 'Writing config/database.yml to read from DATABASE_URL' mean?

Tags:

heroku

When my app is built on Heroku it says

Writing config/database.yml to read from DATABASE_URL

What does that mean? Is this new to the Cedar stack?

I have a Rack app. My config.ru used to work on Barmy Bamboo stack:

#!/usr/bin/ruby
environment = ENV['DATABASE_URL'] ? 'production' : 'development'

require './pb.rb'

dbconfig = YAML.load(File.read('config/database.yml'))
Pb::Models::Base.establish_connection dbconfig[environment]
Pb.create if Pb.respond_to? :create

run Pb

On my own computer it would read my local config/database.yml (which isn't commited to source control)

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

And on Heroku it would read Heroku's database.yml. Inspecting it now on Cedar heroku run cat config/database.yml, it appears to be a Ruby file , and not yml. Is this change explained anywhere?

like image 439
Colonel Panic Avatar asked Sep 11 '12 23:09

Colonel Panic


1 Answers

When you push your app to Heroku, if you have a config directory, the Ruby build pack writes a new database.yml file, and produces the message you see when it does.

Rails runs this file through ERB when loading it, to allow various dynamic options. Heroku make use of this, producing an ERB file that read the DATABASE_URL environment variable and creates the approppriate .yml file pointing to that database.

I’m assuming that in the case of Bamboo the database.yml that was created was a plain Yaml file pointing to your database (I can’t find any reference).

To get your app working on Cedar, you need to run the file through ERB in the same way as Rails does. Change this:

dbconfig = YAML.load(File.read('config/database.yml'))

to

require 'erb'
dbconfig = YAML.load(ERB.new(File.read('config/database.yml')).result)

If your local database.yml is just plain yaml, running through ERB like this should be a no-op, and it will pick up the right values on Heroku.

like image 182
matt Avatar answered Sep 22 '22 04:09

matt