migrations not so consistent

0 views
Skip to first unread message

spencer roan

unread,
Apr 7, 2009, 3:00:09 PM4/7/09
to Utah Ruby Users Group
Thought i'd ask the local talent first, has anyone run into a similar
situation or found a thread that explains my situation:

I have a migration that is supposed to add data, but the data does not
save when I run:
> rake db:migrate

it does save if it is the last migration and i run
> rake db:migrate:redo

if it is not the last migration i can get the data to save like this:
> rake db:migrate
> rake db:migrate:down VERSION=x
> rake db:migrate:up VERSION=x

ruby is 1.8.6
rails is 2.3.2

here is a chopped down version of the up and down:

def self.up
image_mappings = {
"Announcements" => "announcements.gif",
"Appliances" => "appliances.gif",
}
image_mappings.each{|key,value|
cat = GeCategory.find_by_description(key)
image_path = "/images/general/icons/" + value
cat.image_path = image_path
cat.save

# debug stuff to see what happened

cat.reload
puts cat.image_path
}
end

def self.down
GeCategory.update_all("image_path = null")
end

the return of save has been true. But after reload, the data is gone!
I've tried save(false) and update_attribute. any other ideas? I guess
I need to dig into the transactions or something or see what is
different between single and mass migrations.

spencer roan

unread,
Apr 7, 2009, 3:59:35 PM4/7/09
to Utah Ruby Users Group
interesting find with GeCategory.column_names:

rake db::migrate does not include the column :image_path
rake db::migrate::(redo || (down && up)) does include :image_path

this column was not added in the original creation of the table, it
was added after the fact. Apparently add_column doesn't reload the
table definition into memory, but create table does...

I'll see if I can find a workaround to get rails to see the extra
column.

Brian Palmer

unread,
Apr 7, 2009, 4:10:34 PM4/7/09
to ur...@googlegroups.com
See "Using a model after changing its table" on this page: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

(silly rdoc not generating html anchors)

-- Brian

grady player

unread,
Apr 7, 2009, 4:18:02 PM4/7/09
to ur...@googlegroups.com
It probably has to do with the way that the new migrations work, check to make sure that the migration timestamp isn't in the table where it stores already executed migrations.
which I don't remember right now, and maybe could look up, but my connection is slow.
i think it might be called "migrations"; which would make sense.

Jason Edwards

unread,
Apr 7, 2009, 4:21:48 PM4/7/09
to ur...@googlegroups.com
This is just my $0.02, but you should give a lot of thought to
managing data in migrations. Even though migrations seem like a great
fit, in the long run it will likely break down.

I had a project where I used the migrations to manage data in the
database, and after a year and a half it had become so painful I would
dread making changes. It was tedious and brittle. Not to mention all
the hacks I had to make to get the tests working properly.

The answer is what is known as "seed data." Just do a search for
"rails seed data", or something similar, and you'll see all kinds of
discussions on the subject.

Cheers,

Jason

spencer roan

unread,
Apr 7, 2009, 5:48:56 PM4/7/09
to Utah Ruby Users Group
solution found!

GeCategory.reset_column_information causes rails to know what is going
on with the table! Then I can add my data.

David Richards

unread,
Apr 8, 2009, 3:52:49 PM4/8/09
to Utah Ruby Users Group
Sounds like you're set. I worked around that on uber, if you still
have access to that source. Also, I've gotten into the habit of
moving those kinds of things into rake tasks so that migration can be
just schema. The reason for this is when I deploy, I have different
data needs sometimes. My deploy scripts can use my rake tasks or not,
depending on what's appropriate.
Reply all
Reply to author
Forward
0 new messages