简体   繁体   中英

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

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.

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.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM