0
votes

I am setting up a Ruby on Rails app on Heroku. Heroku apparently does not support SQLite3, which is Rails' native database, but instead prefers PostgreSQL. So I'm switching production to that database.

database.yml

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  url: <%= ENV['DATABASE_URL'] %>

Gemfile

(relevant portion)

group :production do
  gem 'pg'
end
group :development, :test do
  gem 'sqlite3'
end

And the error I'm getting in the browser is:

An unhandled lowlevel error occurred. The application logs may have details.

Digging into the application logs, it seems the error there is:

#<RuntimeError: Missingsecret_key_basefor 'production' environment, set this value inconfig/secrets.yml`>

I've confirmed that the correct DATABASE_URL environment variable is set in my Heroku settings. And I've got config/secrets.yml in .gitignore. I don't really want to track this file. I'm using Rails 5.0.1. What am I doing wrong here? The database credentials are included in the DATABASE_URL environment variable.

2
Did you look at the application logs? - Brian
Do you mean Activity Feed > Build Log? I am not seeing any errors there. - wogsland
You can actually remove the whole production section from database.yml. The default behaviour in Rails is to use ENV['DATABASE_URL'] if its present. You can get the rails logs by running $ heroku logs - max
I'm using the web interface. Is there a terminal somewhere I'm missing to run that command? - wogsland
Install the heroku CLI and follow the rails quickstart guide. You should also be using Postgres on your local machine. 12factor.net/dev-prod-parity - max

2 Answers

1
votes

The best answer I can give is SWITCH EVERYTHING! :-)

Seriously, you're going to encounter never-ending headaches maintaining two separate databases for two different environments. Save yourself a lot of heartache and switch your entire application to postgres.

Here's a small sampling of the issues you could encounter by having two separate databases:

  1. Differences in supported data types.
  2. Differences in SQL syntax.
  3. Differences in performance optimization.

This is a great tutorial for making the transition: http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

1
votes

Start by setting up Postgres on your local machine.

You can setup a new Rails application with postgres by using the --database=postgresql option.

rails new myapp --database=postgresql

To convert an existing app add the pg gem to your gemfile.

gem 'pg', '~> 0.19.0'

You want to use it in all environments so don't put it in a group.

This is all you need in your database.yml.

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development
test:
  <<: *default
  database: myapp_test

Then setup your local DB by running

$ bin/rails db:create
$ bin/rails db:migrate

Most guides add a ton of other useless cruft out of ignorance - Heroku will setup the prod database by using the ENV['DATABASE_URL'] env var so no production section is needed at all.

For development and test favor using ENV['DATABASE_URL'] to setup passwords and usernames rather than writing it into config/database.yml. This avoids stupid developer wars and separates the application from local configuration. The dotenv gem and direnv make this very simple to acheive.