Rake task to prepare test database from migrations

October 17, 2006 at 2:22 pm 3 comments

To use your migrations for preparing test database create a file testdb.rake
in the directory lib/tasks.
Now add the following code in the lib/tasks/testdb.rake file…

module Rake
  module TaskManager
    def redefine_task(task_class, args, &block)
      task_name, deps = resolve_args(args)
      task_name = task_class.scope_name(@scope, task_name)
      deps = [deps] unless deps.respond_to?(:to_ary)
      deps = deps.collect {|d| d.to_s }
      task = @tasks[task_name.to_s] = task_class.new(task_name, self)
      task.application = self
      task.add_comment(@last_comment)
      @last_comment = nil
      task.enhance(deps, &block)
      task
    end
  end
  class Task
    class << self
      def redefine_task(args, &block)
        Rake.application.redefine_task(self, args, &block)
      end
    end
  end
end

def redefine_task(args, &block)
  Rake::Task.redefine_task(args, &block)
end

namespace :db do
  namespace :test do

    desc 'Prepare the test database and migrate schema'
    redefine_task :prepare => :environment do
      Rake::Task['db:test:migrate_schema'].invoke
    end

    desc 'Use the migrations to create the test database'
    task :migrate_schema => 'db:test:purge' do
      ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
      ActiveRecord::Migrator.migrate("db/migrate/")
    end

  end
end

To prepare the test database from migrations run this command
$ rake db:test:migrate_schema

Advertisements

Entry filed under: ajax, migrations, rake, tests.

How to improve the image quality and generate random string image in the plugin validates_captcha Using Regular Expression in Ruby on Rails — Regexp for Password Validation

3 Comments Add your own

  • 1. Charles  |  December 1, 2006 at 2:29 pm

    Thanks for this! You saved me a heck of a lot of trouble!

    Reply
  • 2. paneer_tikka  |  January 12, 2007 at 2:25 am

    Awesome!! Thanks for sharing this. I would have wasted a lot of time writing this.

    Reply
  • 3. Sur Max  |  January 12, 2007 at 3:48 am

    although its not mine… i got it from code snippets.
    thanks.

    Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Contact

sur.max(at)gmail.com
October 2006
M T W T F S S
« Sep   Nov »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Power Shots

Tokyo

Moke Lake

Lower Kentucky Falls (Mapleton, OR)

Glorious Morning

Three of a kind

Peace, quiet and tranquility

Stag Beetle

In het gras/Into the grass

Detached.



More Photos

Subscribe


%d bloggers like this: