Which Capistrano hooks to use for events to happen on both "cap deploy" and "cap deploy:migrations"
When deploying an application with "
cap deploy" by default  you only deploy your code but do not run migrations. To avoid an application to be running with code which requires database changes that did not happen yet you should use
Let's say that you have something like that in your
config/deploy.rb to create a database dump every time you deploy:
before ‘deploy’, ‘db:dump’
This will not be called for
cap deploy:migrations. The same applies to other things that are hooked similarly, like an
after 'deploy', 'craken:install'.
How to avoid it
When looking at the default deploy recipe mind those three tasks:
| Task name | Called tasks | Note |
symlink | Clones a new release, symlinks it to
current but does not restart Passenger |
update (see above), then
restart | This one is used when running
cap deploy |
restart | Prefer (see first paragraph) |
As we can see, the
migrations task never calls
deploy and thus would not run its
after hooks. But, just like the
default task, it calls
update_code at the beginning,
symlink to link the "
current" path and
restart once all done. – Those are the hooks to go for , don't just use the
deploy hook for important things.
So instead of… before ‘deploy’, ‘db:dump’ after ‘deploy’, ‘craken:install’ after ‘deploy’, ‘db:show_dump_usage’
…you say… before ‘deploy:update_code’, ‘db:dump’ after ‘deploy:symlink’, ‘craken:install’ after ‘deploy:restart’, ‘db:show_dump_usage’
…and are able to run both
cap deploy and
Note that we prefer to hook onto
symlink for things that should happen once the application is successfully deployed. If you have tasks that inform you about something but not stop deployment (e.g. the above "
show_dump_usage") you should consider hooking them after
restart so your application starts up earlier.
 You may want to consider overwriting the
default task to run
migrations (instead of
restart). This way you can say
cap deploy to get the latest code on the server, migrate the database and restart as a default – if that is what you want.
 You could hook your dump task before
deploy:migrate but when using only
:update) you can end up having new code online without a recent database dump. Almost always you would first update the code and then migrate – making the
deploy:update_code hook the better choice.
Author of this card:
- About this deck:
- We are makandra and do test-driven, agile Ruby on Rails software development.
- License for source code
License for source code
All source code included in the card Which Capistrano hooks to use for events to happen on both "cap deploy" and "cap deploy:migrations" is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source.
The MIT License (MIT) Copyright (c) 2011-2012 makandra GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.