我有capistrano设置将rails应用程序部署到远程服务器.在部署代码时一切正常,但即使我安装了capistrano-rails gem,也没有迁移数据库.当我在服务器上手动迁移迁移时,迁移会起作用,但让它们自
为了弄清楚出了什么问题,我进入了capistrano-rails源代码,并将deploy:migrate方法直接复制到我的deploy.rb文件中.我添加了一堆打印行,发现当它进入方法时,它没有返回任何内容,因为行:在主要提取(:migration_role)上返回一个空数组.
capistrano documentation指的是主要定义的方式,如果没有指定,则第一个列出的角色是默认主要角色.
这是我插入到deploy.rb文件中的capistrano-rails源代码,我唯一的修改是两个放置行.
namespace :deploy do desc 'Runs rake db:migrate if migrations are set' task :migrate => [:set_rails_env] do puts "on primary fetch(:migration_role) ==> #{on primary fetch(:migration_role)}" on primary fetch(:migration_role) do puts "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" conditionally_migrate = fetch(:conditionally_migrate) info '[deploy:migrate] Checking changes in /db/migrate' if conditionally_migrate if conditionally_migrate && test("diff -q #{release_path}/db/migrate #{current_path}/db/migrate") info '[deploy:migrate] Skip `deploy:migrate` (nothing changed in db/migrate)' else info '[deploy:migrate] Run `rake db:migrate`' if conditionally_migrate within release_path do with rails_env: fetch(:rails_env) do execute :rake, "db:migrate" end end end end end after :updated, :migrate end
以下是我的staging.rb角色文件中可能相关的一些其他设置.我更改了我的特定服务器名称.
role :app, %w{deploy@example.com} role :web, %w{deploy@example.com} # Default deploy_to directory is /var/www/my_app set :deploy_to, '/path/to/my/staging_dir' # Define server(s) server 'example.com', user: 'deploy', roles: %w{web app} # Setup Options set :migration_role, 'migrator' set :conditionally_migrate, true set :assets_roles, [:web, :app]
如果您对此处可能出现的问题有任何疑问,请告诉我.谢谢!
将“db”添加到服务器的角色:server 'example.com', user: 'deploy', roles: %w{web app db}
另外,在你的Capfile中:
require 'capistrano/rails/migrations'
我相信你也会在你的Capfile中也想要这些require语句:
require 'capistrano/bundler' require 'capistrano/rails/assets'