Catfish
Catfish

Reputation: 19294

rails capistrano having problems with rake db:migrate

I just ran rake db:migrate directly on my server with no problems:

$ pwd
/var/www/vhosts/example.com/current
$ rake db:migrate

Then i tried to run it via a capistrano task and i get an error. The task and output are below. Why does rake db:migrate not work via my capistrano task?

task

namespace :deploy do
  # run the db migrations
  task :run_migrations, :roles => :db do
    puts "RUNNING DB MIGRATIONS"
    run "cd #{current_path}; rake db:migrate RAILS_ENV=#{rails_env}"
  end
end

cap deploy:run_migrations

$ cap deploy:run_migrations 
  * executing `deploy:run_migrations'
RUNNING DB MIGRATIONS
  * executing "cd /var/www/vhosts/example.com/current; rake db:migrate"
    servers: ["example.com"]
    [example.com] executing command
 ** [out :: example.com] (in /var/www/vhosts/example.com/releases/20121122011144)
 ** [out :: example.com] rake aborted!
 ** [out :: example.com] no such file to load -- rubygems
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121122011144/Rakefile:5:in `require'
 ** [out :: example.com] (See full trace by running task with --trace)
    command finished in 390ms
failed: "sh -c 'cd /var/www/vhosts/example.com/current; rake db:migrate'" on example.com

EDIT

$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
$ which ruby
/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby
$ which rails
/usr/local/rvm/gems/ruby-1.9.3-p125/bin/rails

EDIT When i added --trace to the db migrations, it looks like it's using /usr/lib/ruby/site_ruby/1.8/rake.rb, but from the looks of this: $ which rake /usr/local/rvm/gems/ruby-1.9.3-p125/bin/rake i would have expected it to use rake at /usr/local/rvm/gems/ruby-1.9.3-p125/bin/rake rather than /usr/lib/ruby/site_ruby/1.8/rake.rb.

cap deploy's output from the deploy:run_migrations task

* executing `deploy:run_migrations'
RUNNING DB MIGRATIONS
  * executing "cd /var/www/vhosts/example.com/current; rake db:migrate RAILS_ENV=production --trace"
    servers: ["example.com"]
    [example.com] executing command
 ** [out :: example.com] (in /var/www/vhosts/example.com/releases/20121123184358)
 ** [out :: example.com] rake aborted!
 ** [out :: example.com] no such file to load -- rubygems
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/boot.rb:1:in `require'
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/boot.rb:1
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/application.rb:1:in `require'
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/application.rb:1
 ** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/Rakefile:5:in `require'
 ** [out :: example.com] /var/www/vhosts/famnfo.com/releases/20121123184358/Rakefile:5
 ** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1828:in `load'
 ** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1828:in `load_rakefile'
 ** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1900:in `run'
 ** [out :: example.com] /usr/bin/rake:8
    command finished in 422ms
failed: "sh -c 'cd /var/www/vhosts/example.com/current; rake db:migrate RAILS_ENV=production --trace'" on example.com

EDIT output after adding @Super Engineers code

* executing `bundle:install'
  * executing "ls -x /var/www/vhosts/example.com/releases"
    servers: ["example.com"]
    [example.com] executing command
    command finished in 572ms
  * executing "cd /var/www/vhosts/example.com/releases/20121124160218 && bundle install --gemfile /var/www/vhosts/example.com/releases/20121124160218/Gemfile --path /var/www/vhosts/example.com/shared/bundle --deployment --quiet --without development test"
    servers: ["example.com"]
    [example.com] executing command
 ** [out :: example.com] You are trying to install in deployment mode after changing
 ** [out :: example.com] your Gemfile. Run `bundle install` elsewhere and add the
 ** [out :: example.com] updated Gemfile.lock to version control.
 ** [out :: example.com] 
 ** [out :: example.com] You have added to the Gemfile:
 ** [out :: example.com] * mysql2
 ** [out :: example.com] * therubyracer
 ** [out :: example.com] * rvm-capistrano
 ** [out :: example.com] * passenger
 ** [out :: example.com] 
 ** [out :: example.com] You have deleted from the Gemfile:
 ** [out :: example.com] * mysql
    command finished in 999ms
*** [deploy:update_code] rolling back
  * executing "rm -rf /var/www/vhosts/example.com/releases/20121124160218; true"
    servers: ["example.com"]
    [example.com] executing command
    command finished in 826ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.3-p125' -c 'cd /var/www/vhosts/example.com/releases/20121124160218 && bundle install --gemfile /var/www/vhosts/example.com/releases/20121124160218/Gemfile --path /var/www/vhosts/example.com/shared/bundle --deployment --quiet --without development test'" on example.com

EDIT Entire deploy.rb file

require 'bundler/capistrano'
require 'rvm/capistrano'

# set environment
set :rails_env, "production"

# set the ruby version
set :rvm_ruby_string, 'ruby-1.9.3-p125'
set :rvm_type, 'webadmin'

# server username and password
set :user, 'super'
set :password, 'secret'

# subversion repo username and password
set :scm, :subversion
set :scm_username, "super" 
set :scm_password, 'secret'
set :svnserver, "myreposerver" 
set :repository, "myrepo" 

# project info
set :server, ''
set :application, "FamNFo"
set :applicationdir, '/Volumes/Macintosh HD/Users/myname/Sites/example' 

role :web, "example.com"
role :app, "example.com"
role :db,  "example.com", :primary => true
set :use_sudo, true

# database config
#set :migrate_env, "#{rails_env}" 

# specify the rvm type. We just want to use the system wide one since we're not currently specifying gemsets for each project
set :rvm_type, :system

# where to put the files
set :deploy_to, "/var/www/vhosts/example.com" 

# fixes the "sorry, you must have a tty to run sudo" issue
default_run_options[:pty] = true

# precompiles the assets
load 'deploy/assets'

# If you are using Passenger mod_rails uncomment this:
 namespace :deploy do

    # restart the server
    task :restart, :roles => :app do
        run "/etc/init.d/http restart graceful"
    end

    # chmod the files
    task :after_update_code, :roles => [:web, :db, :app] do
        run "chmod 755 #{release_path}/public -R" 
    end

    # install new gems
    desc "run bundle install and ensure all gem requirements are met"
    task :install do
        run "cd #{current_path} && bundle install  --without=test --no-update-sources --trace"
    end

    # run the db migrations
    task :run_migrations, :roles => :db do
        puts "RUNNING DB MIGRATIONS"
        run "cd #{current_path}; rake db:migrate RAILS_ENV=#{rails_env} --trace"
    end

    # precompile assets
    task :precompile_assets do
        run "cd #{release_path}; bundle exec rake assets:precompile RAILS_ENV=#{rails_env}"
    end

    task :start do ; end
    task :stop do ; end

    # restart the server
    task :restart, :roles => :app, :except => { :no_release => true } do
        run "#{sudo} /etc/init.d/httpd restart graceful"
    end
 end

 # hook to run db migrations after code update
after("deploy:update", "deploy:run_migrations")
#after("deploy:update", "deploy:precompile_assets")
after "deploy:update", "deploy:install"
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup" # leave the last 5 releases only

Upvotes: 0

Views: 3124

Answers (3)

Super Engineer
Super Engineer

Reputation: 1976

Its because of ruby conflict. I've faced same error. You will need to use rvm/capistrano gem. I'll paste some code from my cap file which will give you some idea about how to set the ruby version while deploying.

install the gem

gem install rvm-capistrano

in you deploy.rb file add these lines

require 'bundler/capistrano'
require 'rvm/capistrano'

set :rvm_ruby_string, "ruby-1.9.3-p125"
set :rvm_type, 'webadmin'

For more information you can visit this link rvm/capistrano

Upvotes: 4

Máté
Máté

Reputation: 2345

Are you using rvm/capistrano? With that you can specify the ruby version that you want to use on the server in your capistrano recipes. I suspect that when you run capistrano it's not using your rvm setup, when you login and run the rake task, your user is setup to use the rvm ruby version.

Upvotes: 0

Prakash Murthy
Prakash Murthy

Reputation: 13067

The relevant error here seems to be no such file to load -- rubygems.

Quite likely it is because of multiple ruby installations on the production server.

Check out this previous solution for a similar problem : https://stackoverflow.com/a/2896596/429758

Also, this thread on the capistrano mailing list is helpful: https://groups.google.com/forum/?fromgroups=#!topic/capistrano/JzVPRbQclY4

Upvotes: 1

Related Questions