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

4

7

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{[email protected]}
role :web, %w{[email protected]}
role :db,  %w{[email protected]}


# 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
Midvictorian answered 7/6, 2015 at 21:5 Comment(1)
Looks like there is a config/database.yml file in your repository that is pointing to myapp_production database. See the 2. Move secrets out of the repository. section in capistranorb.com/documentation/getting-started/… to learn how to deal with database.yml while deploying with capistrano. Essentially, you will need to keep a staging/production version of the file on the respective server, and copy it to the required location while deploying.Wildman
C
6

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.

Cotenant answered 7/6, 2015 at 21:39 Comment(3)
I added those lines like you said, but it's still looking for the production database, do I need to add a line in my deploy.rb file as well?Midvictorian
thanks for your help, I will include those changes you suggested, but my issue was setting the nginx.conf to staging for the server instead of production.Midvictorian
Oh interesting. Didn't know that would mess up the environment for running tasks actually. Good to know, and glad you figured it out :).Cotenant
D
3

You should set environment in staging.rb

set :stage, :staging
set :rails_env, :staging

Then you should also set environment using "PassengerAppEnv" variable in apache virtual host file

  <VirtualHost *:80>                                                           
    ServerName myserver.com                                                 
    # Tell Apache and Passenger where your app's 'public' directory is       
    DocumentRoot /var/www/your_app/current/public                         

    PassengerAppEnv staging
    <Directory /var/www/your_app/current/public>                          
      Allow from all                                                         
      Options -MultiViews                                                    
      Require all granted                                                    
    </Directory>                                                             
</VirtualHost> 

This fixed the same problem for me. I hope it helps everyone face it too.

Dint answered 19/8, 2016 at 23:52 Comment(0)
M
1

OK this was a pretty stupid to miss, but I did not set my NGINX config file to "Staging"

     server_name localhost;
     passenger_enabled on;
     rails_env    staging;
     root         /home/myapp;
Midvictorian answered 7/6, 2015 at 22:10 Comment(0)
C
0

I have met with a similar problem. In my case issue was related with ENV variables into .bashrc file in staging server. In this file was set variable:

RAILS_ENV=production

I changed to:

RAILS_ENV=staging

After you need reload variables:

source ~/.bashrc
Canaveral answered 25/7, 2018 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.