namespace :db do 
  namespace :migrate do 
    desc 'Run db:migrate and monitor ActiveRecord::ConcurrentMigrationError errors' 
    task monitor_concurrent: :environment do 
      loop do 
        puts 'Invoking Migrations' 
        Rake::Task['db:migrate'].reenable 
        Rake::Task['db:migrate'].invoke 
        puts 'Migrations successful' 
        break 
      rescue ActiveRecord::ConcurrentMigrationError 
        puts 'Migrations sleeping 5' 
        sleep(5)
      end 
    end 
  end 
end
