Translate

Wednesday, March 23, 2022

Configuring Capistrano to run migrations on multiple servers

 



A few months ago I found myself in a situation where I had to run the migrations on a second server. After some research, I learned that Capistrano only runs migrations on the first server in the :db group, so other servers with :db will be ignored.


I was very surprised!!!

I kept looking into this and finally found the option I was looking for:


This is an example of the config/deploy/production.rb

 server 'sample-web-server1', user: 'deploy', roles: %w{app web db}
 server 'sample-web-server2', user: 'deploy', roles: %w{app web}
server 'sample-web-server3', user: 'deploy', roles: %w{app web db}

Now in your config/deploy.rb file add this option:

 set :migration_servers, -> { release_roles(fetch(:migration_role)) }

Now, you just have to deploy.


As a side note:

According to the capistrano changelogs, this option was introduced from version 1.1.7, so if you have capistrano 1.1.7 onwards, you can safely use it. 

You can also check the capistrano documentation here


No comments:

Post a Comment