Read more

Capistrano: creating a database dump if migrating

Dominik Schöler
December 11, 2023Software engineer at makandra GmbH

In Capistrano 3, your Capfile requires 'capistrano/rails/migrations', which brings two Capistrano tasks: deploy:migrate and deploy:migrating. The former checks whether migrations should be performed. If so, the latter is invoked, which performs the actual migrations.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Knowing this, it is easy to dump the db only if migrations will run. First, enable conditional migrations:

# config/deploy.rb
set :conditionally_migrate, true # Only attempt migration if db/migrate changed

Then hook up the dump task to deploy:migrating:

# Capfile

# deploy:migrate checks whether migrations need to be run, deploy:migrating runs them
before 'deploy:migrating', 'db:dump'

Background

In case you haven't employed dump-creation on deploy before, you can do it like this:

  1. Install geordi Show archive.org snapshot on your servers. It brings the dumple script.
  2. Store the attached db.rake to lib/capistrano/tasks/.
  3. Require it by adding this to your Capfile:
    Dir.glob('lib/capistrano/tasks/*.rake').each do |r|
      # `import r` calls Rake.application.add_import(r), which imports the file only
      # *after* this file has been processed, so the imported tasks would not be
      # available to the hooks below.
      Rake.load_rakefile r
    end
    
Posted by Dominik Schöler to makandra dev (2023-12-11 13:32)