The ELC Community Blog
A knowledge exchange on Ruby on Rails and Agile Development
Migrate Test DB Rake Plugin
by jsiegel on July 12, 2007
MigrateTestDB plugin was created to allow migrations to be used to generate the test database in rake test commands. This is helpful for consistently testing migrations, but was absolutely necessary for the following use case we encountered:
Using VIEWS or STORED PROCEDURES. We were using a legacy database (a legacy MS SQL server for production, MySQL 5 for development and tests). Neither schema format (:ruby or :sql) plays nicely when exporting the schema for the database. They do not output the VIEWS nor STORED PROCEDURES so all of our tests requiring this functionality failed.
MigrateTestDB deletes Rails' default test:db:prepare task which only supports schema_format = :sql and schema_format = :ruby. The replacement task works identically (as of Rails 1.2.3) to Rails default task; however, specifying a schema_format = :migrate will cause a new preparation task to run within the plugin. The task runs migration scripts to ready the test database. The plugin leverages the existing rake tasks for purging the test database, and loading the migration scripts--so the plugin code is mainly a reglue of exisiting Rails default functionality.
This code was originally developed as a patch for railsties/lib/rake/databases.rake. The patch was developed by Andre Price under the direction of Jonathan Siegel at ELC Technologies. It was submitted for inclusion to Rails core and received the response:
http://dev.rubyonrails.org/ticket/4814#comment:1> "This is a great idea for a plugin. If you release it and most Rails developers begin using it then it might be a good candidate for core. Until then I'm going to close this ticket."
The patch was converted to a plugin by Jonathan Siegel.
Usage
1. Install the plugin:
./script/plugin install http://svn.elctech.com/svn/public/plugins/migrate_test_db
or with external SVN reference:
./script/plugin install -x http://svn.elctech.com/svn/public/plugins/migrate_test_db
2. Edit config/environment.rb:
config.active_record.schema_format = :migrate
3. That's it! The next time you run rake (or any rake task that depends on the test:db:prepare task) you'll see that your test database is being generated from your migrations. You can confirm that the plugin is active by running:
rake --tasks
You will see a modified db:test:prepare:
rake db:test:prepare # Overwritten standard prepare to include parsing of :migrate schema_format type.
Timeline
- Ruby on Rails Keynote at RailsConf
- Reconciling TextMate With redgreen Tests
- Rails with JRuby and Glassfish
- Scope Controller Model Plugin
- Congratulations to Buy.com, Shoperion and us!
- Migrate Test DB Rake Plugin
- FunnyOrDie.com in Time's 50 Best Websites of 2007
- TuneCore and Public Enemy in New York Times
- Conditional Action Caching with Cache_Fu
- SSH Configuring RightScale's RightImage FC6V2
- Preloading fixtures
Comments
extirpatory virescent aviatory diatropism untent cakey hyperterrestrial digonoporous Runescape Gold Cheap http://1t4.idezbagx.net/ Thrifty Car Rental Coupons Printable http://1ob.ljafryqzk.net/ Cape May Nj Shops http://29j.dvlknozx.net/
Thanks for the plugin! Very useful. I have a suggestion/patch: add
before the:
so that the migrations can decide whether to handle test DB creation specially. I’m rethinking and reimplementing my base data vs. test data setup (might use preloaded fixtures, might abandon fixtures for something better) but for now it’s necessary for me to know which database I’m creating, and as it is, Rake is creating the test DB via this plugin but the RAILS_ENV didn’t reflect that.
petroglyphy rebewail moldableness uncondition sloo waiwai embryoma transaccidentation AllPolitics The Thai Connection http://www.adventistdirectory.org/view_Entity.asp?EntityID=17898