Search code examples
ruby-on-railsrubyruby-on-rails-4capistrano

Capistrano Migrate To Two Databases


I recently added a second database to my development Rails site, and made a custom rake task, 'SysConfig:db:migrate' which can be seen below:

namespace :SysConfig do

  task :set_custom_db_config_paths do
    ENV['SCHEMA'] = 'db_sysconfig/schema.rb'
    Rails.application.config.paths['db'] = ['db_sysconfig']
    Rails.application.config.paths['db/migrate'] = ['db_sysconfig/migrate']
    Rails.application.config.paths['db/seeds'] = ['db_sysconfig/seeds.rb']
    Rails.application.config.paths['config/database'] = ['config/database_sysconfig.yml']
  end

  namespace :db do
    task :migrate => :set_custom_db_config_paths do
      Rake::Task["db:migrate"].invoke
    end

    ...

  end
end

This takes all the migrations in the db_sysconfig/migrate folder and deploys them to the SysConfig database. However, I am struggling to work out how to set up this task in the deploy.rb file for Capistrano, for when I deploy to staging/production. Does anyone know how I can set the application config paths in capistrano?

Capistrano '2.15.4' Rails '4.0.2' Ruby '2.1.0'


Solution

  • I added a new task to the deploy namespace in the deploy.rb file:

    namespace :deploy do
      ...
      task :SysConfig, roles: :app do
        run "cd #{current_path}; RAILS_ENV=#{rails_env} rake SysConfig:db:migrate"
      end
    end
    
    after "deploy:migrate", "deploy:SysConfig"
    

    I then set it to run after the deploy:migrate task had been ran, which caused it to successfully migrate to both databases at the same time.