DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rails - Build Test Environment DB From Migrations

05.15.2006
| 24897 views |
  • submit to reddit
        This custom rake task builds the test environment database from the migrations rather than the schema dump of the development database. This is especially handy if you have application data inserted via your migrations that you don't want to duplicate in fixtures.

You should be able to stick this in a file called "<whatever>.rake" and put it in your "tasks" directory.

To use as a plugin, create a directory in "vendor/plugins" and call it whatever you want. Inside, create a directory called "tasks". Place this code in a file there and call it whatever you want.

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
    

Comments

Ahsan Gill replied on Wed, 2013/05/08 - 9:41am

Adrian Chaffee replied on Thu, 2013/01/03 - 5:19am

This is my first time I visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work!

Loan

Principled Profit

Ahsan Gill replied on Fri, 2012/12/07 - 6:56am

I can see that you are putting lots of efforts into your blog. Keep posting the good work. Some really helpful information in there The good thing about your information is that it is explicit enough for students to grasp. Thanks for your efforts in spreading academic knowledge http://www.armstrongsecurity.co.ukhttp://www.armstrongsecurity.co.ukhttp://www.armstrongsecurity.co.ukhttp://www.armstrongsecurity.co.ukhttp://www.armstrongsecurity.co.ukhttp://www.armstrongsecurity.co.uk

 http://www.armstrongsecurity.co.uk

Ahsan Gill replied on Thu, 2012/12/06 - 8:37am

I just want to say you have a very nice blog, with helpful links and information as well. I admire the valuable information you offer in your articles. You wrote outstanding, this is completely to the point and it is giving the full explanation of subject.event security services London event security guard london event security firms london event security agencies london hire event security london event security companies london event security company London

Ahsan Gill replied on Wed, 2012/12/05 - 6:03am

Hello there, I should say it is a clever posting. I'll certainly be seeking in on this web site yet again soon.Thanks a lot for sharing such a wonderful post, it is a very nice site i really enjoyed to visit this site.Click Here  Click Here

Click Here

Click Here

Click Here

Click Here

Click Here

Ahsan Gill replied on Sun, 2012/12/02 - 9:07am

The next time I read a blog, I hope that it doesn’t disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you’d have something interesting to say. All I hear is a bunch of whining about something that you could fix if you weren’t too busy looking for attention. Useful Please visit for more information. event security services in london event security guard in london  event security firms in london  event security agencies in london

hire event security in london event security companies in london 

Snippets Manager replied on Sun, 2012/05/06 - 2:28pm

Informative and interesting which we share with you so i think so it is very useful and knowledgeable. I would like to thank you for the efforts. I am tiring the same best work from me in the future as well. Security Companies London EVENT SECURITY IN LONDON

Snippets Manager replied on Sun, 2012/05/06 - 2:28pm

I really impress with this code such easy and simple way to describe the contents its really help me Thanks for sharing. Security Companies London EVENT SECURITY IN LONDON

Snippets Manager replied on Sat, 2012/05/05 - 12:58pm

I had never had such a influencing written material. Really appreciate able.Little White Dresses

Snippets Manager replied on Sat, 2009/03/14 - 2:19pm

You should be able to stick this in a file called ".rake" and put it in your "tasks" directory. To use as a plugin, create a directory in "vendor/plugins" and call it whatever you want. Inside, create a directory cat product called "tasks". Place this code in a file there and cat product call it whatever you want.

Snippets Manager replied on Thu, 2008/03/13 - 2:21pm

To make this snippet work with rake > 0.8 you must use the code listed hire http://rubyforge.org/pipermail/rake-devel/2007-December.txt

Albert Chou replied on Mon, 2007/08/20 - 8:22pm

FYI, using an in-memory SQLite3 database for testing as described in http://nubyonrails.com/articles/2006/06/01/san-francisco-sqlite3-memory-tests-asteroids , I discovered that the Rake task described above isn't needed, because that plugin has the option (currently via a line of code that's commented out by default) to use migrations. (But when using a filesystem-based SQLite3 database, this task is definitely necessary!)

Snippets Manager replied on Tue, 2007/06/19 - 4:09pm

ok but what about any structural kind of data that gets added to the database via migrations ? what will happen to that data in between unit tests ? will it maintain its integrity ?

Snippets Manager replied on Wed, 2006/10/04 - 8:54pm

I modified the task slightly to achieve two things. 1) Only migrate up to the point that the development database is. This is because all the other test:prepare tasks do this. 2) Check the environment.rb file for the schema_format configuration setting and only run the migration version of db:test:prepare if it is set to :migration (just posting the relevant changes) namespace :db do namespace :test do desc 'Prepare the test database and migrate schema' redefine_task :prepare => :environment do if defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:clone", :migration => "db:test:migrate" }[ActiveRecord::Base.schema_format]].invoke end end desc 'Use the migrations to create the test database' task :migrate => 'db:test:purge' do begin ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['development']) schema_version = ActiveRecord::Base.count_by_sql("SELECT version AS count_all FROM schema_info LIMIT 1;") ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) ActiveRecord::Migrator.migrate("db/migrate/", schema_version) rescue puts "The development database doesn't exist or no migrations have been run on it." end end end end