0

Our rails app (3.2.12) has two databases, a content database and an user database. How can I override deploy:migrate (rakefile here) so that during a "cap production deploy:cold" migrations are correspondingly run for their both the target databases.

essentially it should do this during deploy

 ** transaction: commit
  * 2013-10-16 02:26:15 executing `deploy:migrate'
  * executing "cd /home/deployer/apps/project/releases/20131015152439 && bundle exec rake RAILS_ENV=production  db:migrate"
  * executing "cd /home/deployer/apps/project/releases/20131015152439 && bundle exec rake RAILS_ENV=production  user:db:migrate"

Any help will be much appreciated.

Edit: I did replace the task with my own task in the deploy namespace

namespace :deploy do
  set :migration_role, fetch(:migration_role, :db)

  task :migrate do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "db:migrate"
          execute :rake, "user:db:migrate"
        end
      end
    end
  end
  after 'deploy:updated', 'deploy:migrate'
end

This throws an error "undefined method `primary'".

paddle42380
  • 6,921
  • 7
  • 32
  • 40
  • you could a) replace the task with your own b) add another task before or after that task – phoet Oct 16 '13 at 13:16

3 Answers3

0

Is it supposed to be on :primary ?

Or

task :migrate, :only => { :primary => true }  

You may also want to add the "after" line to outside the namespace declaration.

Electrawn
  • 2,254
  • 18
  • 24
0

Task deploy:migrate doesn't run automatically anyway. You can provide your own task after deploy:update_code in which you run the two db:migrate commands for your two different databases.

For example,

namespace :my_namespace
  task :migrate do
    your db:migrate statements go here...
  end
end

after 'deploy:update_code', 'my_namespace:migrate'
  • As of Capistrano 3, migrations *do* run automatically. See here: https://github.com/capistrano/rails/blob/b5f41b6482a0f481077f351d058307b9b29c9fac/lib/capistrano/tasks/migrations.rake#L23 – awendt Sep 02 '15 at 12:50
0

Capistrano Version: 3.6.1 (Rake Version: 11.3.0)

Add this just to help someone who encountered the same problem.

  1. First you need to clear migrate actions

config/deploy.rb

Rake::Task['deploy:migrate'].clear_actions
  1. Second write your own migration task

config/deploy.rb (I used sinatra)

namespace :deploy do
  desc 'migration'
  task :migrate do
    on roles(:app) do |host|
      with rails_env: fetch(:rails_env) do
        within current_path do
          execute :bundle, :exec, :rake, "db:migrate RACK_ENV=#{fetch(:rails_env)}"
        end
      end
    end
  end
end
  1. Call your migration task

As the deploy:migrate will be called automatically, so you do not need to do anything.

arthur bryant
  • 395
  • 5
  • 6