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.

Similar posts

Comments

jpzwarte commented 8 months ago

2ac671a851f2e80837d479022c9866cf?size=52

s/logger.info/say/g

jpzwarte commented 8 months ago

2ac671a851f2e80837d479022c9866cf?size=52

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

E302c3320cd14b02cbe237b479d7f884?size=52

@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.prefix in #rails app config and symlink in capistrano). I think I will manage it soon and upgrade your recipe.

stympy commented 8 months ago

2ddece96c2f2046f723ee37485e86be5?size=52

When you add this to deploy.rb, the asset symlink is done for you, even when skipping precompilation:

load 'deploy/assets'

No change to the rails app is required.

makaroni4 commented 8 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

@stympy thanks a lot! I think there is no more questions about how to skip assets precompilation.

:+1:

jpzwarte
makaroni4
releu
stympy