# Migrate roll back

**URL:** <https://www.ruby-forum.com/t/migrate-roll-back/129140>\
**Category:** Rails\
**Created:** [February 14, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/migrate-roll-back/129140 "2008-02-14T19:59:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [February 14, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/migrate-roll-back/129140/1 "2008-02-14T19:59:54Z")

</div>

Say I table called products with 001\_create\_products.rb Then say I add  
a column called price to product using a the migration  
002\_add\_price.rb

002\_add\_price.rb  
class AddPrice \< ActiveRecord::Migration  
def self.up  
add\_column …  
end

def self.down  
remove\_column …  
end

Now if I decide to remove the price column, effectively, rolling BACK  
my migration to 001 - how does “self.down” in 002\_add\_price.rb get  
called? I’m puzzled 🙂

thanks, roupen n.

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [February 14, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/migrate-roll-back/129140/2 "2008-02-14T23:03:39Z")

</div>

On Thu, Feb 14, 2008 at 1:59 PM, nahabed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def self.down  
> remove\_column …  
> end
> 
> Now if I decide to remove the price column, effectively, rolling BACK  
> my migration to 001 - how does “self.down” in 002\_add\_price.rb get  
> called? I’m puzzled 🙂

Pretty much the same way the self.up got run going the other way.

Basically rake db:migrate uses the VERSION environment variable to  
determine which migration level is desired. It then looks at the  
schema\_version in the db to determine the current version. If  
schema\_version is less than VERSION, then it looks for migrations \>  
schema\_version and \<= VERSION loads each one in ascending sequence and  
executes the up method in each migration.

Similarly if schema\_version is \> than VERSION it runs the migrations  
in descending sequence and executes the down method in each one.

db:rollback works basically the same way.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [February 15, 2008, 1:12am UTC](https://www.ruby-forum.com/t/migrate-roll-back/129140/3 "2008-02-15T01:12:25Z")

</div>

Makes sense - thanks!
