« LEarning Rails: Moving along to Migrations | Main | Learning Ruby: Ruby Cheat Sheets »

Learning Rails: Migrations Part 2

Summarizing what I've learned: 1) from application directory, run "rake db:schema:dump" to create a dump of the applications's data structure compatible with Migrations. It's called schema.rb and gets dumped in the \db directory. 2) Run "ruby script\generate migration BaselineSchema" to generate a Migrations file. 3)Edit the schema.rb to include version information. Change "ActiveRecord:: Schema.define() do" to "ActiveRecord::Schema.define(:version => 1) do". 4)Run "rake db:migrate" to actually to the work.

The script does the work, and in the process creates a table called schema_info in our database containing only one field, which is the version number of the database's current schema according to Migrations.

We have a baseline established, basically an empty migration. From now on, we can call the migrations by what they do and start building out our database!

Again, run "ruby script\generate migration MyMigrationName" Edit this file to define your changes and how to reverse them. Run this file using "rake db:migrate".

Note: In the tutorial I'm working through, the change defined in the first migration after the Baseline contains an error. The self.up method in the tutorial says add_column("Recipes", "contributor_name", "string" :default => "Unknown") while it ought to say add_column("Recipes", "contributor_name", :string, :default => "Unknown"). The tutorial's code will fail to run on my version of InstantRails.

Note to self: find out whether the long run time of "ruby script\generate migration Foo" is due to my system itself, my InstantRails, or if it's intrinsic to the process. I see that the Ruby.exe process is what's hitting the CPU pretty hard. Likewise with rake db:migrate. Perhaps this is due to loading the Ruby interpreter each time, like back when I was working with Java and experienced some harsh JVM startup times.

Now we can arbitrarily roll back to any previous version: rake db:migrate VERSION=2

Cool stuff! The next tutorial in this series is located here ( http://www.oreillynet.com/pub/a/ruby/2007/06/28/cookin-with-ruby-on-rails---june.html) and works with Testing.

Posted on September 14, 2007 | Permalink | Tag this post with del.icio.us | This Post Now Lives Here

Comments

The comments to this entry are closed.