Search code examples
ruby-on-railscapistranostaging

Rails & Capistrano 3 - staging server trying to user production database


I've setup a server for my staging environment using NGINX & Passenger. I've also setup a staging.rb file which is a duplicate of my production.rb file under environments. In my database.yml file I've put in a staging configuration:

  staging:
  adapter: mysql2
  database: myapp_staging
  username: root
  password: xxxxxxxxxxxxx
  port: 3306
  pool: 15
  timeout: 5000

Environments/staging.rb:

role :app, %w{deploy@111.111.111.111}
role :web, %w{deploy@111.111.111.111}
role :db,  %w{deploy@111.111.111.111}


# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.

server '111.111.111.111', user: 'deploy', roles: %w{web app db}, port: 0001

I deploy with cap staging deploy however the app won't start and in the logs it says: Unknown database 'myapp_production'

How can I force it to use the staging database?

EDIT

Deploy.rb:

set :application, 'dispatch'
set :repo_url, 'myapp'

set :deploy_to, '/home/deploy/myapp'

set :scm, :git
set :branch, 'master'
set :keep_releases, 5
set :format, :pretty
set :log_level, :debug
set :pty, true
set :passenger_restart_with_sudo, true

set :stages, ["staging", "production"]
set :default_stage, "staging"

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('rvmsudo')

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

Solution

  • With my cap 3 setup, i have a config/deploy/production.rb file where the environment gets set. Are you doing the same for both staging & production?

    set :stage, :staging
    server 'example.com', user: 'aaron', roles: %w{web app db}
    set :rails_env, :staging
    

    Need to be sure it sets the proper environment, so the db tasks will know what database to connect to.