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