Quick question about rake migrate

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

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

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.

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

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

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