Never been to DZone Snippets before?

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

Rails - Build Test Environment DB from Migrations (See related posts)

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.

   1  
   2  module Rake
   3    module TaskManager
   4      def redefine_task(task_class, args, &block)
   5        task_name, deps = resolve_args(args)
   6        task_name = task_class.scope_name(@scope, task_name)
   7        deps = [deps] unless deps.respond_to?(:to_ary)
   8        deps = deps.collect {|d| d.to_s }
   9        task = @tasks[task_name.to_s] = task_class.new(task_name, self)
  10        task.application = self
  11        task.add_comment(@last_comment)
  12        @last_comment = nil
  13        task.enhance(deps, &block)
  14        task
  15      end
  16    end
  17    class Task
  18      class << self
  19        def redefine_task(args, &block)
  20          Rake.application.redefine_task(self, args, &block)
  21        end
  22      end
  23    end
  24  end
  25  
  26  def redefine_task(args, &block)
  27    Rake::Task.redefine_task(args, &block)
  28  end
  29  
  30  namespace :db do
  31    namespace :test do
  32  
  33      desc 'Prepare the test database and migrate schema'
  34      redefine_task :prepare => :environment do
  35        Rake::Task['db:test:migrate_schema'].invoke
  36      end
  37  
  38      desc 'Use the migrations to create the test database'
  39      task :migrate_schema => 'db:test:purge' do
  40        ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  41        ActiveRecord::Migrator.migrate("db/migrate/")
  42      end
  43    
  44    end
  45  end

Comments on this post

majglow posts on Feb 03, 2007 at 22:09
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)

   1  
   2  namespace :db do
   3    namespace :test do
   4  
   5      desc 'Prepare the test database and migrate schema'
   6      redefine_task :prepare => :environment do
   7        if defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
   8          Rake::Task[{ :sql  => "db:test:clone_structure", :ruby => "db:test:clone", :migration => "db:test:migrate" }[ActiveRecord::Base.schema_format]].invoke
   9        end
  10      end
  11  
  12      desc 'Use the migrations to create the test database'
  13      task :migrate => 'db:test:purge' do
  14        begin
  15          ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['development'])
  16          schema_version = ActiveRecord::Base.count_by_sql("SELECT version AS count_all FROM schema_info LIMIT 1;")
  17          ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
  18          ActiveRecord::Migrator.migrate("db/migrate/", schema_version)
  19        rescue
  20          puts "The development database doesn't exist or no migrations have been run on it."
  21        end
  22      end
  23  
  24    end
  25  end
webmail716 posts on Jun 19, 2007 at 18:31
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 ?
HotFusionMan posts on Aug 20, 2007 at 20:38
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!)
jeznet posts on Mar 13, 2008 at 16:30
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

You need to create an account or log in to post comments to this site.


Click here to browse all 5562 code snippets

Related Posts