For the love of your Migrations, Reset your column info !
So a co-worker and I were hacking on migrations when one of them began to fail silently. It a migration we created to just add some data and not actually do anything structurally. The migration itself looked something like this...
class IMINURDBMIGRATINGURDATAZ <> def self.up
fruits.each do |f|
a = Fruits.find_by_type(f[0])
a.smelliness = f[1]
a.save!
end
end
def self.down
end
def self.fruits
[['apple', 'like a flower'],
['orange', 'like citrus heaven'],
['kiwi', 'like sweet tarts in syrup'],
['onion', 'like how you would imagine death would smell']]
end
end
So what the up is doing is basically finding each fruit by type and setting the smelliness for each one. Looks good right? We thought so too. We ran it and it didn't work. So we popped into console (ruby/script console) and tried the exact same code there, it works. Hmm, that's odd. We blow all the tables away and try it again. No bueno. 45 minutes of head scratching and "WTF!?!" later and with the help of our boss, we figure out that it's because we kept running all of the migrations in succession. Because we were doing so and that a separate, previous migration was adding the smelliness table, we couldn't write to the smelliness column yet because we weren't reloading the column information from the object itself.
So, we fixed it by adding
Fruit.reset_column_information
right above our iteration in the up method, like so...
class IMINURDBMIGRATINGURDATAZ <> Fruit.reset_column_information
def self.up
fruits.each do |f|
a = Fruits.find_by_type(f[0])
a.smelliness = f[1]
a.save!
end
end
def self.down
end
def self.fruits
[['apple', 'like a flower'],
['orange', 'like citrus heaven'],
['kiwi', 'like sweet tarts in syrup'],
['onion', 'like how you would imagine death would smell']]
end
end
I hope this saves someone else out there some time because it wasn't easy to find.
class IMINURDBMIGRATINGURDATAZ <> def self.up
fruits.each do |f|
a = Fruits.find_by_type(f[0])
a.smelliness = f[1]
a.save!
end
end
def self.down
end
def self.fruits
[['apple', 'like a flower'],
['orange', 'like citrus heaven'],
['kiwi', 'like sweet tarts in syrup'],
['onion', 'like how you would imagine death would smell']]
end
end
So what the up is doing is basically finding each fruit by type and setting the smelliness for each one. Looks good right? We thought so too. We ran it and it didn't work. So we popped into console (ruby/script console) and tried the exact same code there, it works. Hmm, that's odd. We blow all the tables away and try it again. No bueno. 45 minutes of head scratching and "WTF!?!" later and with the help of our boss, we figure out that it's because we kept running all of the migrations in succession. Because we were doing so and that a separate, previous migration was adding the smelliness table, we couldn't write to the smelliness column yet because we weren't reloading the column information from the object itself.
So, we fixed it by adding
Fruit.reset_column_information
right above our iteration in the up method, like so...
class IMINURDBMIGRATINGURDATAZ <> Fruit.reset_column_information
def self.up
fruits.each do |f|
a = Fruits.find_by_type(f[0])
a.smelliness = f[1]
a.save!
end
end
def self.down
end
def self.fruits
[['apple', 'like a flower'],
['orange', 'like citrus heaven'],
['kiwi', 'like sweet tarts in syrup'],
['onion', 'like how you would imagine death would smell']]
end
end
I hope this saves someone else out there some time because it wasn't easy to find.
Labels: migrations, quirk, ruby on rails
2 Comments:
At 3:21 AM, Unknown said…
Thanks! Saved me tons of time!
At 6:30 AM, Curtis said…
you're welcome
Post a Comment
<< Home