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
| 26204 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

Constration Security replied on Thu, 2014/02/13 - 2:49am

Hello everyone, This blog is very helpful and have great information , thanks for sharingRegards, Constration Security Londonfollow us : construction site security in london 

Engr Ubaid Masood replied on Wed, 2014/02/12 - 11:54pm


Hello World,

Really appreciate the blogs and sensible comments , keep sharing

regards,


bodyguard services,London
follow us : http://www.ibodyguardlondon.com

Hire security in london

Ievent Security
follow us : http://www.ieventsecurity.co.uk

Royal Armor Group

construction site security in london

follow us on: http://www.royalarmorgroup.com

Oliverz

hire domestic workers in london hire domestic workers in london

http://www.oliverz.co.uk

Ahsan Gill replied on Tue, 2014/02/04 - 2:57pm

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need bodyguar services in rio de jeneiro bodyguard services in sao paulo bodyguard services in sochi bodyguard services in russia

Engr Ubaid Masood replied on Mon, 2014/02/03 - 2:09am

Hello World,

Really appriciate the blogs and sensible comments , keep sharing

regards,


bodyguard services,London 
follow us : www.ibodyguardlondon.com

Hire security in london 

Ievent Security
follow us : www.ieventsecurity.co.uk

 

Royal Armor Group

construction site security in london 

follow us on: www.royalarmorgroup.com

Oliverz

hire domestic workers in london hire domestic workers in london

www.oliverz.co.uk

Eric Foxy replied on Fri, 2014/01/31 - 5:17pm

It is a nice to seeing these things after a very long time.it just make so easy for understanding.Thanks for sharing. Visit us on:
<a title="Construction Site Security in London" href="www.royalarmorgroup.com">Construction Site Security in London</a>

Ahsan Gill replied on Tue, 2013/11/19 - 9:32am

That's a great attitude, I hope the incoming comments and suggestions are equally positive. I know I've said it in person, but for anyone reading the comments, its awesome!construction security companies in london security companies in london security guards in london hire security guards in london event security london event security companies london event security guards london event security services london bodyguard companies london bodyguard services london bodyguards in london chauffeur driven cars in london chauffeur hire in london construction security companies in london construction security london construction security companies london event security london event security companies london event security services london event security london event security birmingham event security leeds event security glasgow event security sheffield event security bradford event security edinburgh event security liverpool event security manchester event security bristol event security wakefield event security cardiff event security coventry event security nottingham event security leicester event security sunderland event security belfast event security newcastle upon tyne event security brighton event security hull event security plymouth event security stoke-on-trent event security wolverhampton event security derby event security swansea event security southampton event security salford event security aberdeen event security westminster event security portsmouth event security york event security peterborough event security dundee event security lancaster event security oxford event security newport event security preston event security st albans event security norwich event security chester event security cambridge event security salisbury event security exeter event security gloucester event security lisburn event security chichester event security winchester event security londonderry event security carlisle event security worcester event security bath event security durham event security lincoln event security hereford event security armagh event security inverness event security stirling event security canterbury event security lichfield event security newry event security ripon event security bangor event security truro event security ely event security wells event security st davids security companies in london security companies in birmingham security companies in leeds security companies in glasgow security companies in sheffield security companies in bradford security companies in edinburgh security companies in liverpool security companies in manchester security companies in bristol security companies in wakefield security companies in cardiff security companies in coventry security companies in nottingham security companies in leicester security companies in sunderland security companies in belfast security companies in newcastle upon tyne security companies in brighton security companies in hull security companies in plymouth security companies in stoke-on-trent security companies in wolverhampton security companies in derby security companies in swansea security companies in southampton security companies in salford security companies in aberdeen security companies in westminster security companies in portsmouth security companies in york security companies in peterborough security companies in dundee security companies in lancaster security companies in oxford security companies in newport security companies in preston security companies in st albans security companies in norwich security companies in chester security companies in cambridge security companies in salisbury security companies in exeter security companies in gloucester security companies in lisburn security companies in chichester security companies in winchester security companies in londonderry security companies in carlisle security companies in worcester security companies in bath security companies in durham security companies in lincoln security companies in hereford security companies in armagh security companies in inverness security companies in stirling security companies in canterbury security companies in lichfield security companies in newry security companies in ripon security companies in bangor security companies in truro security companies in ely security companies in wells security companies in st davids hire security guards in london hire security guards in birmingham hire security guards in leeds hire security guards in glasgow hire security guards in sheffield hire security guards in bradford hire security guards in edinburgh hire security guards in liverpool hire security guards in manchester hire security guards in bristol hire security guards in wakefield hire security guards in cardiff hire security guards in coventry hire security guards in nottingham hire security guards in leicester hire security guards in sunderland hire security guards in belfast hire security guards in newcastle upon tyne hire security guards in brighton hire security guards in hull hire security guards in plymouth hire security guards in stoke-on-trent hire security guards in wolverhampton hire security guards in derby hire security guards in swansea hire security guards in southampton hire security guards in salford hire security guards in aberdeen hire security guards in westminster hire security guards in portsmouth hire security guards in york hire security guards in peterborough hire security guards in dundee hire security guards in lancaster hire security guards in oxford hire security guards in newport hire security guards in preston hire security guards in st albans hire security guards in norwich hire security guards in chester hire security guards in cambridge hire security guards in salisbury hire security guards in exeter hire security guards in gloucester hire security guards in lisburn hire security guards in chichester hire security guards in winchester hire security guards in londonderry hire security guards in carlisle hire security guards in worcester hire security guards in bath hire security guards in durham hire security guards in lincoln hire security guards in hereford hire security guards in armagh hire security guards in inverness hire security guards in stirling hire security guards in canterbury hire security guards in lichfield hire security guards in newry hire security guards in ripon hire security guards in bangor hire security guards in truro hire security guards in ely hire security guards in wells hire security guards in st davids construction security companies in london construction security companies in birmingham construction security companies in leeds construction security companies in glasgow construction security companies in sheffield construction security companies in bradford construction security companies in edinburgh construction security companies in liverpool construction security companies in manchester construction security companies in bristol construction security companies in wakefield construction security companies in cardiff construction security companies in coventry construction security companies in nottingham construction security companies in leicester construction security companies in sunderland construction security companies in belfast construction security companies in newcastle upon tyne construction security companies in brighton construction security companies in hull construction security companies in plymouth construction security companies in stoke-on-trent construction security companies in wolverhampton construction security companies in derby construction security companies in swansea construction security companies in southampton construction security companies in salford construction security companies in aberdeen construction security companies in westminster construction security companies in portsmouth construction security companies in york construction security companies in peterborough construction security companies in dundee construction security companies in lancaster construction security companies in oxford construction security companies in newport construction security companies in preston construction security companies in st albans construction security companies in norwich construction security companies in chester construction security companies in cambridge construction security companies in salisbury construction security companies in exeter construction security companies in gloucester construction security companies in lisburn construction security companies in chichester construction security companies in winchester construction security companies in londonderry construction security companies in carlisle construction security companies in worcester construction security companies in bath construction security companies in durham construction security companies in lincoln construction security companies in hereford construction security companies in armagh construction security companies in inverness construction security companies in stirling construction security companies in canterbury construction security companies in lichfield construction security companies in newry construction security companies in ripon construction security companies in bangor construction security companies in truro construction security companies in ely construction security companies in wells construction security companies in st davids event security companies london event security companies birmingham event security companies leeds event security companies glasgow event security companies sheffield event security companies bradford event security companies edinburgh event security companies liverpool event security companies manchester event security companies bristol event security companies wakefield event security companies cardiff event security companies coventry event security companies nottingham event security companies leicester event security companies sunderland event security companies belfast event security companies newcastle upon tyne event security companies brighton event security companies hull event security companies plymouth event security companies stoke-on-trent event security companies wolverhampton event security companies derby event security companies swansea event security companies southampton event security companies salford event security companies aberdeen event security companies westminster event security companies portsmouth event security companies york event security companies peterborough event security companies dundee event security companies lancaster event security companies oxford event security companies newport event security companies preston event security companies st albans event security companies norwich event security companies chester event security companies cambridge event security companies salisbury event security companies exeter event security companies gloucester event security companies lisburn event security companies chichester event security companies winchester event security companies londonderry event security companies carlisle event security companies worcester event security companies bath event security companies durham event security companies lincoln event security companies hereford event security companies armagh event security companies inverness event security companies stirling event security companies canterbury event security companies lichfield event security companies newry event security companies ripon event security companies bangor event security companies truro event security companies ely event security companies wells event security companies st davids security companies london security companies birmingham security companies leeds security companies glasgow security companies sheffield security companies bradford security companies edinburgh security companies liverpool security companies manchester security companies bristol security companies wakefield security companies cardiff security companies coventry security companies nottingham security companies leicester security companies sunderland security companies belfast security companies newcastle upon tyne security companies brighton security companies hull security companies plymouth security companies stoke-on-trent security companies wolverhampton security companies derby security companies swansea security companies southampton security companies salford security companies aberdeen security companies westminster security companies portsmouth security companies york security companies peterborough security companies dundee security companies lancaster security companies oxford security companies newport security companies preston security companies st albans security companies norwich security companies chester security companies cambridge security companies salisbury security companies exeter security companies gloucester security companies lisburn security companies chichester security companies winchester security companies londonderry security companies carlisle security companies worcester security companies bath security companies durham security companies lincoln security companies hereford security companies armagh security companies inverness security companies stirling security companies canterbury security companies lichfield security companies newry security companies ripon security companies bangor security companies truro security companies ely security companies wells security companies st davids hire security guards london hire security guards birmingham hire security guards leeds hire security guards glasgow hire security guards sheffield hire security guards bradford hire security guards edinburgh hire security guards liverpool hire security guards manchester hire security guards bristol hire security guards wakefield hire security guards cardiff hire security guards coventry hire security guards nottingham hire security guards leicester hire security guards sunderland hire security guards belfast hire security guards newcastle upon tyne hire security guards brighton hire security guards hull hire security guards plymouth hire security guards stoke-on-trent hire security guards wolverhampton hire security guards derby hire security guards swansea hire security guards southampton hire security guards salford hire security guards aberdeen hire security guards westminster hire security guards portsmouth hire security guards york hire security guards peterborough hire security guards dundee hire security guards lancaster hire security guards oxford hire security guards newport hire security guards preston hire security guards st albans hire security guards norwich hire security guards chester hire security guards cambridge hire security guards salisbury hire security guards exeter hire security guards gloucester hire security guards lisburn hire security guards chichester hire security guards winchester hire security guards londonderry hire security guards carlisle hire security guards worcester hire security guards bath hire security guards durham hire security guards lincoln hire security guards hereford hire security guards armagh hire security guards inverness hire security guards stirling hire security guards canterbury hire security guards lichfield hire security guards newry hire security guards ripon hire security guards bangor hire security guards truro hire security guards ely hire security guards wells hire security guards st davids construction security london construction security birmingham construction security leeds construction security glasgow construction security sheffield construction security bradford construction security edinburgh construction security liverpool construction security manchester construction security bristol construction security wakefield construction security cardiff construction security coventry construction security nottingham construction security leicester construction security sunderland construction security belfast construction security newcastle upon tyne construction security brighton construction security hull construction security plymouth construction security stoke-on-trent construction security wolverhampton construction security derby construction security swansea construction security southampton construction security salford construction security aberdeen construction security westminster construction security portsmouth construction security york construction security peterborough construction security dundee construction security lancaster construction security oxford construction security newport construction security preston construction security st albans construction security norwich construction security chester construction security cambridge construction security salisbury construction security exeter construction security gloucester construction security lisburn construction security chichester construction security winchester construction security londonderry construction security carlisle construction security worcester construction security bath construction security durham construction security lincoln construction security hereford construction security armagh construction security inverness construction security stirling construction security canterbury construction security lichfield construction security newry construction security ripon construction security bangor construction security truro construction security ely construction security wells construction security st davids

Ahsan Gill replied on Wed, 2013/09/11 - 1:59pm

You should be able to stick this in a file called ".rake" and put it in your "tasks" directory. thats really works. thanks for sharing
http://www.armstrongsecurity.co.uk/hire-security-guards-in-aberdeen.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-milltimber.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-peterculter.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-laurencekirk.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-banchory.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-westhill.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-alford.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-aboyne.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-ballater.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-strathdon.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-ballindalloch.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-aberlour.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-stonehaven.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-ellon.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-peterhead.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-fraserburgh.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-macduff.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-banff.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-inverurie.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-insch.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-turriff.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-huntly.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-keith.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-buckie.html http://www.armstrongsecurity.co.uk/hire-security-guards-in-st-albans.html

Jamshaid Ayaz replied on Thu, 2013/06/27 - 9:58am

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!http://www.armstrongsecurity.co.uk/construction-security-aberdeen.html http://www.armstrongsecurity.co.uk/construction-security-milltimber.html http://www.armstrongsecurity.co.uk/construction-security-peterculter.html http://www.armstrongsecurity.co.uk/construction-security-laurencekirk.html http://www.armstrongsecurity.co.uk/construction-security-banchory.html http://www.armstrongsecurity.co.uk/construction-security-westhill.html http://www.armstrongsecurity.co.uk/construction-security-alford.html http://www.armstrongsecurity.co.uk/construction-security-aboyne.html http://www.armstrongsecurity.co.uk/construction-security-ballater.html http://www.armstrongsecurity.co.uk/construction-security-strathdon.html http://www.armstrongsecurity.co.uk/construction-security-ballindalloch.html http://www.armstrongsecurity.co.uk/construction-security-aberlour.html http://www.armstrongsecurity.co.uk/construction-security-stonehaven.html http://www.armstrongsecurity.co.uk/construction-security-ellon.html http://www.armstrongsecurity.co.uk/construction-security-peterhead.html http://www.armstrongsecurity.co.uk/construction-security-fraserburgh.html http://www.armstrongsecurity.co.uk/construction-security-macduff.html http://www.armstrongsecurity.co.uk/construction-security-banff.html http://www.armstrongsecurity.co.uk/construction-security-inverurie.html http://www.armstrongsecurity.co.uk/construction-security-insch.html http://www.armstrongsecurity.co.uk/construction-security-turriff.html http://www.armstrongsecurity.co.uk/construction-security-huntly.html http://www.armstrongsecurity.co.uk/construction-security-keith.html http://www.armstrongsecurity.co.uk/construction-security-buckie.html http://www.armstrongsecurity.co.uk/construction-security-st-albans.html http://www.armstrongsecurity.co.uk/construction-security-harpenden.html http://www.armstrongsecurity.co.uk/construction-security-welwyn.html http://www.armstrongsecurity.co.uk/construction-security-welwyn-garden-city.html http://www.armstrongsecurity.co.uk/construction-security-hatfield.html http://www.armstrongsecurity.co.uk/construction-security-birmingham.html http://www.armstrongsecurity.co.uk/construction-security-alcester.html http://www.armstrongsecurity.co.uk/construction-security-bromsgrove.html http://www.armstrongsecurity.co.uk/construction-security-halesowen.html http://www.armstrongsecurity.co.uk/construction-security-cradley-heath.html http://www.armstrongsecurity.co.uk/construction-security-rowley-regis.html http://www.armstrongsecurity.co.uk/construction-security-smethwick.html http://www.armstrongsecurity.co.uk/construction-security-oldbury.html http://www.armstrongsecurity.co.uk/construction-security-west-bromwich.html http://www.armstrongsecurity.co.uk/construction-security-sutton-coldfield.html http://www.armstrongsecurity.co.uk/construction-security-tamworth.html http://www.armstrongsecurity.co.uk/construction-security-studley.html http://www.armstrongsecurity.co.uk/construction-security-solihull.html http://www.armstrongsecurity.co.uk/construction-security-henley-in-arden.html http://www.armstrongsecurity.co.uk/construction-security-redditch.html http://www.armstrongsecurity.co.uk/construction-security-bath.html http://www.armstrongsecurity.co.uk/construction-security-radstock.html

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