在多个服务器上运行数据迁移Capistrano


Typically the sites I work on have a traditional single Staging server, and a single Production server.
一般我都是发布到单台服务器上。staging和productin环境各自部署。

Recently I’ve been working on a client project where we needed the same database schema (and subsequent migrations) deployed to multiple target servers.
最近,有个项目需要部署多台,包括数据库也需要在多台上部署。

No problem I thought – I’ll just add the additional servers to the classic Capistrano :app,:web and :db roles :
我想应该没什么问题。我作了如下角色的配置:

role :app, %w{deploy@srv1 deploy@srv2 deploy@srv3}
role :web, %w{deploy@srv1 deploy@srv2 deploy@srv3}
role :db,  %w{deploy@srv1 deploy@srv2 deploy@srv3}

…and things will work without a hitch.
运行的ok,没有故障。 without a hitch .... hitch 有搭便车的意思。  我做某一件事的时候,有东西搭便车上来了。成了意外。

Guess what? There was a hitch.
但是,这里还是有个故障, hitch.

I was surprised to learn that Capistrano only runs database migrations on the first of the servers in the :db group, i.e. deploy@srv1 and any subsequent ones are ignored.
我感到很吃惊, Capistrano 只是在db 角色的 第一台服务器上运行 migrations.

From what I can gather this has been the case since Capistrano v3.4.0.


To get the migrations to run on all three servers the following lines needed to be added to config/deploy/production.rb :

set :migration_role, :db
set :migration_servers, -> { release_roles(fetch(:migration_role)) }
After this all servers got the database updates, and I could go back to concentrating on the code and not pesky deployment issues 🙂

阅读量: 296
发布于:
修改于: