Skip to content

Fixing Invalid DATABASE_URL Reference on CircleCI

Tags: devops, ruby • Categories: Software

Table of Contents

I’ve become a huge fan of CircleCI—it’s the best CI tool for small-medium projects that I’ve found. Recently I ran into an issue setting up a new rails project with Postgres as the database backend with circle:

export RAILS_ENV="test"
export RACK_ENV="test"
bundle exec rake db:create db:schema:load --trace
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
rake aborted!
NameError: Cannot load `Rails.application.database_configuration`:
uninitialized constant DATABASE_URL

However, when I looked at my database.yml the constant DATABASE_URL was clearly not being used. DATABASE_URL was properly being used as the lookup string for an ENV reference:

# config/database.yml
test:
  url: <%= ENV['TEST_DATABASE_URL'] %>

I was stumped for a while until I ssh’d into circle and noticed that somehow the single quotes were stripped from DATABASE_URL:

# cat config/database.yml
test:
  url: <%= ENV[DATABASE_URL] %>
  adapter: postgresql
  database: circle_ruby_test
  username: ubuntu
  host: localhost

Circle’s build inference process involves generating a database.yml file which uses the original database.yml file to some extent. Some part of the process involves stripping single quotes, which is what was causing the build breakage. Make sure that you use double quotes instead of single quotes in database.yml when using CircleCI.

Automating Staging Deploys to Staging on CI Build Success

Here’s how to automatically push new builds (and run database migrations) to a heroku-hosted app using circle CI:

deployment:
  staging:
    branch: master
    commands:
      - heroku maintenance:on --app the-app-name
      - git push git@heroku.com:the-app-name.git $CIRCLE_SHA1:master
      - heroku run rake db:migrate --app the-app-name
      - heroku maintenance:off --app the-app-name

Keep in Touch

Subscribe to my email list to keep in touch. I’ll send you new blog posts and other thoughts.