Smarter migrations with capistrano in your Rails app by makaroni4
Quite often in #capistrano recipes I see running migrations and assets precompilation on each deploy which I think is not what you want because it takes time and if there is no new migrations or assets its time is spent in vain. In this post I will show you a script which is used in #gistflow to skip #migrations.
namespace :db_migrations do
task :migrate do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} db/migrate | wc -l").to_i > 0
run "cd #{current_release} && RAILS_ENV=#{rails_env} bundle exec rake db:migrate"
logger.info "New migrations added - running migrations."
else
logger.info "Skipping migrations - there are not any new."
end
end
end
The source could be found in gistflow's deploy.rb
Soon I will make the same thing for assets, make it DRY and opensource.
Comments
jpzwarte commented 8 months ago
s/logger.info/say/g
makaroni4 commented 8 months ago
@jpzwarte what do you mean?
jpzwarte commented 8 months ago
Sorry, never mind; i thought say was a built-in rake logging method, but it's actually part of ActiveRecord::Migration.
makaroni4 commented 8 months ago
@jpzwarte, OK, welcome to #gistflow
stympy commented 8 months ago
See my blog post on skipping asset compilation while deploying.
makaroni4 commented 8 months ago
@stympy, this is a great post!
But when you skip assets precompilation you need either copy public/assets folder to new release folder or symlink it (by setting
assets.prefixin #rails app config and symlink in capistrano). I think I will manage it soon and upgrade your recipe.stympy commented 8 months ago
When you add this to deploy.rb, the asset symlink is done for you, even when skipping precompilation:
No change to the rails app is required.
makaroni4 commented 8 months ago
@stympy thanks a lot! I think there is no more questions about how to skip assets precompilation.
releu commented 8 months ago
@makaroni4 Wrap it to gem please.