# Quick question about rake migrate

**URL:** <https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061>\
**Category:** Rails\
**Created:** [August 23, 2006, 3:24am UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061 "2006-08-23T03:24:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 3:24am UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/1 "2006-08-23T03:24:07Z")

</div>

when creating a table, i use script/generate migration tablename…and  
when i am done with adding fields, i type rake migrate. now what do i do  
if i want to add additional fields to the table? do i just make the  
modifications to the file that was created in the db directory and then  
rake migrate again? if so, i did, but my changes did not appear in the  
table. or do i need to generate another migration table?

thanks for the help

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 3:54am UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/2 "2006-08-23T03:54:21Z")

</div>

Migrate applies changes, moving the database from one state to the next,  
until it is at the latest.

Migrations capture a set of changes in numbered files. When you do ‘rake  
migrate’ those changes are applied to your database in sequential order  
and  
the final number is recorded in a table called ‘schema\_version’. If you  
run  
‘rake migrate’ again, the schema version is compared to the highest  
numbered  
migration. If the numbers match then the database is considered to be at  
the  
latest schema version and no migrations are performed.

If you have previously run a migration and you want to add columns you  
have  
2 choices: 1) you can reverse the previous migration or 2) you can  
create a  
new migration.

I tend to do #1 if I am working on some changes and I simply forgot  
something and I have not yet released the new version. #2 is the primary  
mode of operation for migrations.

Reversing a migration assumes you have implemented the ‘down’ method. If  
you  
have done this then you simple say ‘rake migrate VERSION=[some version  
number]’. For example, let’s say your latest migration is  
005\_add\_project\_table and that you have already done a ‘rake migrate’.  
To  
revert to 4 you would do this:

rake migrate VERSION=4

Good luck.

-Kelly

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/3 "2006-08-23T14:06:54Z")

</div>

ok, so i think was incorrectly using rake migrate. for every new table i  
created, i created a new migration and rake migrated it. nuts.

hi Hammed,

if i have a table called profiles and the file name is  
create\_profiles\_version1,

class Profiles\< ActiveRecord::Migration

def self.up  
create\_table :Profiles do |table|  
table.column :name, :string  
end  
end

def self.down  
drop\_table :Profiles  
end  
end

and now id like to create a new profiles table, do i script/generate  
migrations profiles? and then in self.up do add\_column somecolumnname ?

afterwards type rake migrate?

thanks all for the response.

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/4 "2006-08-23T19:56:12Z")

</div>

koloa wrote:

> ok, so i think was incorrectly using rake migrate. for every new table i  
> created, i created a new migration and rake migrated it. nuts.

Shrug, that’s pretty much what I do.

> and now id like to create a new profiles table, do i script/generate  
> migrations profiles? and then in self.up do add\_column somecolumnname ?
> 
> afterwards type rake migrate?

Pretty much, apart from the fact that you don’t actually create a “new”  
table, merely change the existing one. And I’d probably name the  
migration akin to AddStuffToProfiles, but each to his own.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 3:56am UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/5 "2006-08-23T03:56:22Z")

</div>

Koloa,

The whole idea behind migrations is that each change you make goes into  
a  
new version. To add a new field, create a new migration file:

script/generate migration adding\_new\_field

and then add edit that file and add

add\_column :table\_name, :new\_field\_name

for each of the new fields that you have to the self.up method. Remember  
to  
add

remove\_column :table\_name, :new\_field\_name

to the self.down method for each of the new field.

Hammed

---

<div class="post-metadata">

**Author:** ![koloa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@koloa](https://www.ruby-forum.com/u/koloa)\
**Post date:** [August 23, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/quick-question-about-rake-migrate/66061/6 "2006-08-23T23:20:37Z")

</div>

> and now id like to create a new profiles table, do i script/generate

I’m assuming you meant to say you’d like to create a new FIELD in the  
existing profiles table (you can’t have two tables named the same). If  
so,  
you’re right, create a new migration

script/generate migration add\_new\_field\_to\_profile

This will create a new migration file called something like  
009\_add\_new\_field\_to\_profile. Edit this migration file:

class AddNewFieldToProfile\< ActiveRecord::Migration

def self.up  
add\_column "profiles,“new\_column”  
end

def self.down  
remove\_column “profiles”, “new\_column”  
end  
end

when you run rake migrate next, the new column will be added to your  
table.  
If you’d like to roll back this change and remove the column, try

rake migrate VERSION=8

Hammed
