【发布时间】:2014-11-30 04:51:29
【问题描述】:
我无法让 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 会自动运行任何新的迁移,这是真的吗?
我的应用配置可以在这里找到: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}
非常感谢任何帮助!
【问题讨论】:
-
要么您的迁移未推送到存储库,因此在
releases/timestamp/db/migrate目录或schema_migrations表中不可用,迁移时间戳不正确。在生产环境中检查select * from schema_migrations并查看它匹配的迁移时间戳。 -
迁移是在存储库中(见https://github.com/Abundnce10/testapp/tree/master/db/migrate),我在服务器上看到所有4个(~/testapp/releases/20141130081711/db/migrate) .
schema migrations表包含前 3 个迁移(带有正确的时间戳),但错过了最新的迁移——因为cap production deploy:migrate不会运行新的迁移。我可以手动运行RAILS_ENV=production bundle exec rake db:migrate,但我想依靠 Capistrano 来回滚。 -
我的
/config/deploy/production.rb文件 -server 'XXX.XXX.XXX.xx', user: 'deploy', roles: %w{web app}中似乎没有包含db。添加db允许数据库迁移在我部署时自动运行。
标签: ruby-on-rails ruby-on-rails-4 deployment capistrano