Migrations

the VERSION value at in rake db:migrate VERSION=[versionNumber] is the
migration number right? for instance lets say I have 3 models
generated in this respective order albums, users, and reviews. If I
do “rake db:migrate VERSION=0”, it will delete the data and existence
of all three tables…if I do VERSION=1, it will delete the existance/
data of users and reviews etc etc…

So my problem is I rolled back to VERSION=0, added a column to Review
in the migration file, and migrated again. For some reason rails
didn’t acknowledge the change and the table indecies are still the
same.

So my before I rolled back to version 0:

class CreateReviews < ActiveRecord::Migration
def self.up
create_table :reviews, :force => true do |t|
t.text :productReview
t.string :product
t.string :productCreator

  t.timestamps
end

end

def self.down
drop_table :reviews
end
end

I then put in a coloumn called album_id

class CreateReviews < ActiveRecord::Migration
def self.up
create_table :reviews, :force => true do |t|
t.text :productReview
t.string :product
t.string :productCreator
t.integer :album_id
t.timestamps
end

end

def self.down
drop_table :reviews
end
end

Rand db:migrate and checked my structure in the console and it’s still
the previous version without album_id…

Any thoughts?

On Fri, 2008-09-26 at 08:31 -0700, Jon wrote:

same.
t.timestamps
I then put in a coloumn called album_id

Any thoughts?


The general idea is that when you want to change a model (i.e., add a
column), that you created a new migration that adds the column to the
table rather than migrate down and lose data.

If however, you rake db:migrate VERSION=0, can you confirm indeed that
the ‘reviews’ table is indeed dropped?

Craig

Craig,

Yeah that sounds more feasible rather than migrating down…I can
definitely
confirm VERSION=0 will drop the table though because when try to load
the
structure in the console it throws me an error.

What would be the syntax to just add a change(thus adding a new
migration
then)

I want to add t.intger :album_id. I’m assuming it’s a command at the
command line that will product and 004 migration file?

Jon

I’m sorry, it doesn’t throw me an error, it says Review(Table doesn’t
exist)

On Fri, 2008-09-26 at 08:58 -0700, Jon Liu wrote:

command line that will product and 004 migration file?


something like this in the migration…

def self.up
add_column :reviews, :album_id, :integer
end

def self.down
remove_column :reviews, :album_id
end

Craig

ok thanks, but how do you actually generate that particular migrations
file
from the command line?, ie it would be 004_something

thanks craig,

I was able to generate the file w/the command and I added the
“add_column”
code. When I do my migrate I can see rail iteratively compiling the
files
and I see the

4 add_column(:reviews, :album_id, :integer)
->.2970s
4 AddAlbumIdToReviews: migrated (0.3120s)

which means that’s it’s been migrated. But when i look at my Review
structure in the console window the it still doesn’t show up…any more
thoughts?

On Fri, 2008-09-26 at 09:33 -0700, Jon Liu wrote:

ok thanks, but how do you actually generate that particular migrations
file from the command line?, ie it would be 004_something


script/generate migration some_name_of_what_you_re_trying_to_accomplish

Craig

I’m just loading it like this:

Review
Review(id: integer, productReview:text, product: string, productCreator:
string, created_at: datetime, updated_at: datetime)

It’s strange, I added an identical id to another structure called Album,
well I added “review_id”, and it worked out fine.

On Sep 27, 1:04 am, “Jon Liu” [email protected] wrote:

which means that’s it’s been migrated. But when i look at my Review
structure in the console window the it still doesn’t show up…any more
thoughts?

Just curious: how are you viewing the structure via the console? Are
you looking at an already loaded instance via the ruby console, like:

review = Review.find(:first)
review

Or are you viewing it via your db’s console?

On Sep 27, 1:33 am, “Jon Liu” [email protected] wrote:

I’m just loading it like this:

Review

Review(id: integer, productReview:text, product: string, productCreator:
string, created_at: datetime, updated_at: datetime)

It’s strange, I added an identical id to another structure called Album,
well I added “review_id”, and it worked out fine.

Have you tried restarting the console after you ran the migration?

you know that might be a good idea…

Craig,

It worked. Thank you…please excuse me while I go shoot myself for
not
trying that already. It’s funny restarting the console has worked in
some
instances like this before…but I was lazy and did “reload!” instead.
Rails is funny…

Thanks again

Jon