Monday, October 15, 2007

Migration

Wow. I must say, I'm totally impressed with this feature in RoR. I'm so glad that this feature comes packaged with RoR, and that I don't have to do this manually. I remember writing SQL files to do this, and keeping them organized so I can run them later. It was a pain in the ass!

When I heard about this built-in feature, my draw dropped. Migration basically allows you to manipulate the database in any way. Each file has an 'up' and a 'down'. The 'up' makes changes to your database, whereas the 'down' rolls back your changes. Each file is also versioned, so you can quickly move your database to any previous version, or blow it away entirely and rebuild it. This is nice when you first start developing because there may be some kinks in the database design. However, when the database becomes stable and the application is ready to use, user data is stored, and this data should not be lost when adding upgrades or rollbacks. The migration files give a nice way to centralize and remember the changes and their rollbacks.

RoR has an online API documentation that is quite useful and can be found here:

http://api.rubyonrails.org/

Under ActiveRecord::Migration contains all of the possible things you can do with migration.

By running:

>rake db:migrate VERSION=0

RoR will rollback all changes in reverse version order. This will basically blow away your database. If your keen, you'll notice that VERSION is specified, and that the number refers to the version you want your database to be at. So if you're currently on version 10, you could have VERSION=9, which would rollback the changes when version 10 was made.

By running:

>rake db:migrate

RoR will apply all the versions after the current version of your database. Hence, if you have a fresh database, using this command will get you up to speed right away!

Migration... Check it out if you have time. I hope it'll blow you away as much as it did for me.

W

No comments: