Friday, August 21, 2009

15-second Guide to Rails Migrations

To create a migration:
script/generate migration a_migration_name
Then go to db/migrate and add stuff to self.up (thing that you want to change as part of the migration) and self.down (things that you would do to revert in case you need to rollback the migration).

When ready to run the migration:

rake db:migrate

If you need to undo one or more migrations (where the_version is the number preceding the name in the filename of the migration in db/migrate directory):

rake db:migrate VERSION=the_version

ActiveRecord objects have accessors to the fields defined in the table corresponding to them in the DB, so as soon as the DB table is changed (via migration or otherwise), it effects what could be persisted via the corresponding model.

For more info see the Rails API

No comments: