Capistrano: creating a database dump if migrating

Posted 5 months ago. Visible to the public.

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.

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
    
Dominik Schöler
Last edit
3 months ago
Michael Leimstädtner
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2023-12-11 12:32)