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
Rails - Build Test Environment DB From Migrations
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
event security bath event security birmingham event security bradford event security brighton
Ahsan Gill replied on Wed, 2013/04/10 - 3:50am
http://www.armstrongsecurity.co.uk/event-security-greenhithe.html http://www.armstrongsecurity.co.uk/event-security-erith.html http://www.armstrongsecurity.co.uk/event-security-welling.html http://www.armstrongsecurity.co.uk/event-security-bexleyheath.html http://www.armstrongsecurity.co.uk/event-security-bexley.html http://www.armstrongsecurity.co.uk/event-security-longfield.html
Ahsan Gill replied on Sun, 2013/03/24 - 2:36pm
http://www.armstrongsecurity.co.uk/event-security-aberdeen.html http://www.armstrongsecurity.co.uk/event-security-milltimber.html http://www.armstrongsecurity.co.uk/event-security-peterculter.html http://www.armstrongsecurity.co.uk/event-security-laurencekirk.html http://www.armstrongsecurity.co.uk/event-security-banchory.html http://www.armstrongsecurity.co.uk/event-security-alford.html http://www.armstrongsecurity.co.uk/event-security-birmingham.html
Adrian Chaffee replied on Thu, 2013/01/03 - 5:19am
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 londonAhsan Gill replied on Sat, 2012/12/01 - 6:19am
i am data base Administrator i m so happy to see this code it very easily to understand and very its performance is very effect in any database..Thanks for sharing its really help
http://www.armstrongsecurity.co.uk/blog/event-security-services-in-london.html
http://www.armstrongsecurity.co.uk/blog/event-security-guard-in-london.html
http://www.armstrongsecurity.co.uk/blog/event-security-firms-in-london.html
http://www.armstrongsecurity.co.uk/blog/event-security-agencies-in-london.html
http://www.armstrongsecurity.co.uk/blog/hire-event-security-in-london.html
http://www.armstrongsecurity.co.uk/blog/event-security-companies-in-london.html
Snippets Manager replied on Sun, 2012/05/06 - 2:28pm
Snippets Manager replied on Sun, 2012/05/06 - 2:28pm
Snippets Manager replied on Sat, 2012/05/05 - 12:58pm
Snippets Manager replied on Sat, 2009/03/14 - 2:19pm
Snippets Manager replied on Thu, 2008/03/13 - 2:21pm
Albert Chou replied on Mon, 2007/08/20 - 8:22pm
Snippets Manager replied on Tue, 2007/06/19 - 4:09pm
Snippets Manager replied on Wed, 2006/10/04 - 8:54pm
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