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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)