Thomas McGoey-Smith

Rails Migration. Up and down vs change.

So I’m starting to plan out my migrations. I’ve already but a database schema for Has It Shipped Yet, so I want to port it over to rails.

Since I’m working with Shopify, I thought it was best to include their ShopifyApp gem that allows you to focus on building your app and not worry about things like getting OAuth setup.

I noticed though, in their migration they used self.up and self.down when making the shop migration.

It’s a bit weird (for me at least) because I’ve only ever seen the change method.

With a bit of searching, I was able to find this Stack Overflow post.

This gist was that up/down methods were used in Rails 3. They were used originally because Rails didn’t know how to rollback the migration. For example if you accidentally added the wrong field, you wanted to rollback but it wasn’t sure how to proceed.

With Rails 4 (I’m using the current stable release), you can usually just consider using the change method.

The Rails Guide talks about it nicely here.

I think for the fields I’ll be adding. Change should work just fine.

@tamcgoey on Jan 11, 2016

Enjoyed the article? Subscribe to my newsletter for more.

© Thomas McGoey-Smith (2014-2018). RSS.