March 11, 2008

Migration-Related Rake Commands

Filed under: migration, rails, rake — oomoo @ 1:28 pm

Here are some of the Rake commands that are used when doing migrations (and some tips)…

rake db:migrate   Migrate the database through scripts in db/migrate. Target specific version with VERSION=x  

rake db:schema:dump  Dump the database structure into the “master migration script” db/schema.rb

rake db:structure:dump  Dump the database structure to a SQL file  

rake db:schema:load  Load a schema.rb file into the database

rake db:fixtures:load  Load fixtures into the current environment’s database. Load specific fixtures using FIXTURES=x,y  

rake db:sessions:clear  Clear the sessions table

rake db:sessions:create Creates a sessions table for use with CGI::Session::ActiveRecordStore  

rake db:test:clone   Recreate the test database from the current environment’s database schema

rake db:test:clone_structure  Recreate the test databases from the development structure

rake db:test:prepare   Prepare the test database and load the schema

rake db:test:purge   Empty the test database  

Next “Migration Number” that needs to be applied, is stored in table “schema_info” field named “version” 

Test the Migration Scrips…   “rake db:migrate –dry-run –trace” 

Apply the Migration Scripts…  “rake db:migrate”

    Did an error occur?  See the “troubleshooting” section below. 

Roll back a script…  “rake db:migrate VERSION=004”    (substitute the desired “version #” for the number “4”) (Use the number “0” to drop the tables) 

    If you manually “dropped” the table yourself, you need to add the table back (just add one field), so the rollback will work.
    OR, you could manually change the number stored in “schema_info.version” to a number prior to when the table existed. 
    It is a good idea to “roll back” EVERY script, just to make sure your “self.down” works properly (before the next \db\migrate\ script gets created). 

Apply the Migration Script…Again  “rake db:migrate”   (After it rolls back successfully, just “rake db:migrate” again to put your migration back into effect.) 

Regenerate the “schema.rb” file to include your new model…  “rake db:schema:dump” 


  1. […] Migration Related Rake Commands (Rake Cheatsheet) […]

    Pingback by How To Use Fixtures to Populate Your Database in Rails | Biodegradable Geek — July 11, 2008 @ 12:59 am

  2. […] Migration-Related Rake Commands « Oomoo Posted: Sunday, March 22nd, 2009 @ 4:00 pm Tags:analytics, apple, apps, cheatsheet, database, delicious, design, documentation, framework, git, icons, iphone, javascript, mac, monitoring, osx, rails, reference, resources, RubyOnRails, social, software, tools, utilities, versioncontrol […]

    Pingback by Delicious Bookmarks (2009-03-08 - 2009-03-22) | Josh Babetski : Quixotic Bravado — March 22, 2009 @ 4:05 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: