Ask A Question

Notifications

You’re not receiving notifications from this thread.

Issue deploying Rails app with capistrano

Dylan asked in Rails

Hello all. This is my first time ever trying to deploy a Rails application. I followed the guide to do so on Ubuntu 20.04, but I run the command bundle exec cap production deploy and it gets to the point of migrating to the production database, it throws this error.

00:31 deploy:migrating
      01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
      01 rake aborted!
      01 NoMethodError: Cannot load database configuration:
      01 undefined method `[]' for nil:NilClass
      01 (erb):16:in `<main>'
      01 /home/dylan/deployments/pride_skills/shared/bundle/ruby/2.7.0/gems/rai…
      01 /home/dylan/deployments/pride_skills/shared/bundle/ruby/2.7.0/gems/act…
      01 /home/dylan/deployments/pride_skills/shared/bundle/ruby/2.7.0/gems/rak…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:i…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:i…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/exe/bundle:46:in `block…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/lib/bundler/friendly_er…
      01 /home/dylan/.gem/ruby/2.7.0/gems/bundler-2.1.4/exe/bundle:34:in `<top …
      01 /home/dylan/.rbenv/versions/2.7.0/bin/bundle:23:in `load'
      01 /home/dylan/.rbenv/versions/2.7.0/bin/bundle:23:in `<main>'

Here is my database.yml:

# MySQL. Versions 5.5.8 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= Rails.application.credentials.mysql[:username] %>
  password: <%= Rails.application.credentials.mysql[:password] %>
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
  database: pride_skills_development


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: pride_skills_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# 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
  url: <%= ENV['DATABASE_URL'] %>

I've tried to search online to no avail. Any help would be massively appreciated.

Reply

There are a few possible things going on here. Is your rails master key saved as an env varaiable?

ENV['RAILS_MASTER_KEY']

This is why the nil class error could be happening.

If you don't have the master key set then the following line won't work:

Rails.application.credentials.mysql[:username]

Try seeing if the master key ENV variable is set and just come back with any questions.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.