Capistrano的'cap生产部署:迁移`将无效

时间:2014-11-30 04:51:30

标签: ruby-on-rails ruby-on-rails-4 deployment capistrano

我无法让Capistrano运行数据库迁移。

我正在使用DigitalOcean提供的VPS来托管我的Rails应用程序。以前我会使用git push heroku master在Heroku上主持我的项目,但现在我想要更便宜的东西。我正在尝试使用Capistrano将我的代码部署到服务器(使用this教程启动并运行)。我可以成功地将我的新提交到服务器上,但是,我无法让Capistrano运行我的数据库迁移。

为了展示我的问题,我创建了一个新模型,提交了更改,推送到Github仓库,然后运行cap production deploy。我查看了服务器,我可以看到新的迁移文件。 Here是该命令的输出,以防它在调试问题时有用。

当我运行cap production deploy:migrate时,数据库没有任何反应:

~/Projects/rails/testapp $ cap production deploy:migrate
DEBUG[aec67347] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.3 ] on 104.236.181.65
DEBUG[aec67347] Command: [ -d ~/.rbenv/versions/2.1.3 ]
DEBUG[aec67347] Finished in 1.107 seconds with exit status 0 (successful).

这是我在生产服务器上的PostgeSQL数据库(没有改变):

testapp_production=> \d
               List of relations
 Schema |       Name        |   Type   | Owner  
--------+-------------------+----------+--------
 public | cars              | table    | deploy
 public | cars_id_seq       | sequence | deploy
 public | schema_migrations | table    | deploy
 public | users             | table    | deploy
 public | users_id_seq      | sequence | deploy
(5 rows)

现在,当我在服务器上运行RAILS_ENV=production bundle exec rake db:migrate时,它成功运行了迁移,由新的manufacturers表证明:

testapp_production=> \d
                 List of relations
 Schema |         Name         |   Type   | Owner  
--------+----------------------+----------+--------
 public | cars                 | table    | deploy
 public | cars_id_seq          | sequence | deploy
 public | manufacturers        | table    | deploy
 public | manufacturers_id_seq | sequence | deploy
 public | schema_migrations    | table    | deploy
 public | users                | table    | deploy
 public | users_id_seq         | sequence | deploy
(7 rows)

为什么cap production deploy:migrate无法运行数据库迁移?另外,我认为cap production deploy会自动运行任何新的迁移,这是真的吗?< / p>

我的应用配置可在此处找到:https://github.com/Abundnce10/testapp

这是我的Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rbenv'

set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.3'

这是我的/config/deploy.rb文件:

lock '3.1.0'
set :application, 'testapp'
set :repo_url, 'https://github.com/Abundnce10/testapp'
set :deploy_to, '/home/deploy/testapp'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

namespace :deploy do

  before :publishing, 'deploy:migrate'

  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

这是我的/config/deploy/production.rb文件:

set :stage, :production
server '104.236.181.65', user: 'deploy', roles: %w{web app}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:9)

我没有在db文件中加入/config/deploy/production.rb。它现在看起来像:

set :stage, :production
server '104.236.181.65', user: 'deploy', roles: %w{web app db}

db添加到:roles允许我运行cap production deploy时自动运行数据库迁移。