1

We are moving our table data into a different database for archival. So, when users add migrations in Main DB i want to display a default message reminding them to perform the same migration for the table in archived DB. How can i do this without having to add message manually in migration ?

  • Why not make the migration happen on both databases at once by customizing `rake db:migrate` definition? See http://stackoverflow.com/a/6379712/429758 for hints on how to do it. – Prakash Murthy Mar 18 '13 at 08:30
  • We are moving tables phase wise. So, not all tables are present in both the DBs. –  Mar 18 '13 at 18:24

1 Answers1

1

Prakash proposes an elegant solution. But your original question was about overriding default tasks to add a message I believe.

Maybe try something like this. Courtesy: http://metaskills.net/2010/05/26/the-alias_method_chain-of-rake-override-rake-task/

Rake::TaskManager.class_eval do
  def alias_task(fq_name)
    new_name = "#{fq_name}:original"
    @tasks[new_name] = @tasks.delete(fq_name)
  end
end

def alias_task(fq_name)
  Rake.application.alias_task(fq_name)
end

def override_task(*args, &block)
  name, params, deps = Rake.application.resolve_args(args.dup)
  fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
  alias_task(fq_name)
  Rake::Task.define_task(*args, &block)
end

Now you can override rake db:migrate like this.

 namespace :db do
    override_task :migrate => :environment do
      # Your message here
      # To invoke the original task add ":original" to its name
      Rake::Task["db:migrate:original"].execute
      ...
    end
 end
Rahul
  • 412
  • 4
  • 13
  • Actually, I was thinking of overriding ActiveRecord::Migration class, but was not sure if it was the right way. –  Mar 18 '13 at 18:23