advertisement

Print

Cookin' with Ruby on Rails - May
Pages: 1, 2, 3, 4, 5, 6

Paul:  (as CB finishes off the last bite of that piece)  Looks like it's working to me.  Now what?

CB:  Remember the SQL script we wrote before?  Pull it up to remind us where we're starting.  It's in a file called create.sql in the db directory under the application root directory.

Our original SQL script
Figure 4. (Click to enlarge.)


CB:  And let's take a look at the database itself while we're at it.  I want to show you how migrations work, and I've found that watching the changes as they're made is a pretty good way to get your arms around what's going on.  I'm still using a copy of MySQL-Front that I got before they "went out of business,"  but don't let that throw you.  All we're going to use it for is watching the database's contents.

Initial view of recipes table
Figure 5. (Click to enlarge.)

CB:  The other thing we'll want to watch is the filesystem.  Rails is going to generate some files for us that we need to understand.  So I'll open Explorer and move to the db directory.

Initial look at files in db directory
Figure 6.

CB:  So let me "frame" what we're about to do.  Migrations are a mechanism to manage the evolution of the database component of our Rails applications.  We've already got a database that we want to start with, so the first thing we're going to have to do is bring the Rails Migration mechanism "up to speed" on where we are.  That takes four easy steps.  The first step is to get Rails a "snapshot" of the database schema as it currently exists. To do that, we'll open a command window, change to the application directory, and enter:

rake db:schema:dump

Run initial schema dump
Figure 7. (Click to enlarge.)

Paul:  Doesn't look like it did anything.

CB:  I know.  But if we look at the filesystem we can see that it did do something.  

schema.rb file has been generated
Figure 8.

CB:  It generated a file named schema.rb that's key to what migrations do. We'll take a look at it in just a minute.  First, though, I want to get the second step out of the way.  That's to generate our first migration file.  To do that, I go back to the command window and enter:

ruby script\generate migration BaselineSchema

Generating our first migration file
Figure 9. (Click to enlarge.)

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow