Although migrations are a very cool feature of Ruby on Rails, they are not covered in any of the basic books on RoR i have encountered so far (Agile Web Development with Rails, Ruby for Rails Programmers).
Update: Check out my recent post: Ruby on Rails Migrations: Reloaded for an update.
Both these books are using an ‘in medias res’ style approach – they guide the reader through the essential features of Rails by building a web app from scratch. The models in the examples are creaed in SQL rather than with migrations. Let’s examine the difference on a simple example, taken from AWDwR. (Further I am assuming that you have generated a rails application, a development database for the application and the DB connection settings (database.yaml) are correct.)
The classic way: SQL DDL
Create the sql file, create.sql:
drop table if exists products; create table products ( id int not null auto_increment, title varchar(100) not null, description text not null, image_url varchar(200) not null, price decimal(10,2) not null, primary key (id) );
After this, you can create the table with:
mysql name_of_your_DB < create.sql
You are now ready to generate your model.
Doing the same with migrations
In your rails app directory, issue the following command:
ruby script/generate migration ProductMigration
then open the file db/migrate/001_product_migration.rb and edit it. To achieve the same result as in the SQL example, the file should look like this:
class ProductMigration < ActiveRecord::Migration
def self.up create_table :products do |table|
table.column :title, :string, :limit => 100, :null => false table.column :description, :text, :null => false table.column :image_url, :string, :limit => 200, :null => false table.column :price, :float, :null => false end end def self.down drop_table :products end end
Run the migration wit the following command:
rake migrate [VERSION=version_number]
And you achieved the same result as with the first method!
That's very nice, but...
Well, if the only purpose of migrations would be solely the possibility to write Ruby code instead of SQL, even this would be enough for me to go for them. However, i have to admit that this alone would be a rather feeble argument. The good news is that it is not! There is much more to migrations than writing Ruby code:
- Migrations are DB agnostic - The 'write once, use everywhere' principle really works here!
- You don't have to think about obscure SQL specific things anymore - let Rails handle them for you! (OK there are some really complicated things, but fortunately they are adressed by some great books like Rails Recipes, code snippets like Migrate Plus, and I believe that by the Rails team, too.)
- You can change the database as much as you want, and the data you have already there is not affected.
- You get very effective versioning: track changes, concurrent versions, upgrade/downgrade your schemas easily!
- You can generate DB schemas from migrations.
- And possibly much much more... I am a newbie too!
In my oppinion, judging based on the Rails mailing list discussions, migrations are accepted more and more as the definitve way of creating, maintaining, versioning your DB models - so everybody considering serious Rails development should give them a look!