Trouble on migrating in production mode

2019-06-10 09:10发布

问题:

I am using Ruby on Rails 3.1.0 and the Capistrano gem and I created a migration file in order to change database table column names. When in my local machine I run the rake db:migrate command (in development mode) it works, but I would like to make effective these changes on the remote server in production mode. My problem is: if I run the Capistrano command cap deploy:migrate I get the following error message in the production log file on the remote server :

* executing "cd /<my_application_path>/releases/20111025205951 && rake RAILS_ENV=production  db:migrate"
  servers: ["<my_application_IP>"]
  [<my_application_IP>] executing command
 ** [out :: <my_remote_server_IP>] (in /<my_application_path>/releases/20111025205951)
*** [err :: <my_remote_server_IP>] rake aborted!
*** [err :: <my_remote_server_IP>] uninitialized constant Rake::DSL
*** /usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
*** ...

How can I solve the problem? Is it related to using Ruby version 1.9.1 as outputted in the above error message?


Note: If I run the ruby -v command

# on the local machine (MacOS running Snow Leopard 10.6.7) I get
>> ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.7.0]
# on the remote machine (Linux running Ubuntu 10.04 LTS) I get
>> ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

UPDATE for @Simone Carletti

After I added the require 'bundler/capistrano' at the top of the deploy.rb file and after I run the cap deploy:migrations command I get the following error:

...
* executing "cd /<my_application_path>/releases/20111026132212 && bundle install --gemfile /<my_application_path>/releases/20111026132212/Gemfile --path /<my_application_path>/shared/bundle --deployment --quiet --without development test"
  servers: ["<my_remote_server_IP>"]
  [<my_remote_server_IP>] executing command
** [out :: <my_remote_server_IP>] Some gems seem to be missing from your vendor/cache directory.
** [out :: <my_remote_server_IP>] Could not find libv8-3.3.10.2 in any of the sources
   command finished in 2554ms
failed: "sh -c 'cd /<my_application_path>/releases/20111026132212 && bundle install --gemfile /<my_application_path>/releases/20111026132212/Gemfile --path /<my_application_path>/shared/bundle --deployment --quiet --without development test'" on <my_remote_server_IP>

What does this mean? It may be related to the last part (that is, the N.B. part) of the following text? If so, what I should do?

When I run the cap -e bundle:install command (from the official documentation) I get:

------------------------------------------------------------
cap bundle:install
------------------------------------------------------------
Install the current Bundler environment. By default, gems will be installed to
the shared/bundle path. Gems in the development and test group will not be
installed. The install command is executed with the --deployment and --quiet
flags. If the bundle cmd cannot be found then you can override the bundle_cmd
variable to specifiy which one it should use.

You can override any of these defaults by setting the variables shown below.

N.B. bundle_roles must be defined before you require 'bundler/capistrano' in
your deploy.rb file.

  set :bundle_gemfile,  "Gemfile"
  set :bundle_dir,      File.join(fetch(:shared_path), 'bundle')
  set :bundle_flags,    "--deployment --quiet"
  set :bundle_without,  [:development, :test]
  set :bundle_cmd,      "bundle" # e.g. "/opt/ruby/bin/bundle"
  set :bundle_roles,    {:except => {:no_release => true}} # e.g. [:app, :batch]

Note: The cap deploy:migrations command created an empty /<my_application_path>/shared/bundle/ruby/1.9.1 directory with a 755 pemission.

回答1:

This is the guilty.

rake RAILS_ENV=production  db:migrate

You should run

bundle exec rake RAILS_ENV=production  db:migrate

Make sure you are including the bundler/capistrano recipes in your deploy.rb file.

require 'bundler/capistrano'

These recipes will change the command for you. See Automatic deployment with Capistrano.