Adam Stegman
2010-Feb-11 19:23 UTC
Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
I have a migration that saves correctly to the database when it''s run by itself through db:migrate:up, but not when it''s run with other migrations through db:migrate. class SetActiveOnExistingItems < ActiveRecord::Migration def self.up Item.all.each do |item| if item.item_status == Item::STATUS_REVIEW item.active = false elsif item.item_status == Item::STATUS_IN_PROGRESS item.active = false else item.active = true end puts "setting item_id = #{item.id} to active = #{item.active}" item.save! puts "setting item_id = #{item.id} to active = #{item.active}" item = Item.find(item.id) puts "setting item_id = #{item.id} to active = #{item.active}" end end def self.down Item.all.each {|item| item.active = nil; item.save(false)} end end The output during db:migrate is: setting item_id = 1 to active = true # (this one is before the save) setting item_id = 1 to active = true # (this one is after the save) setting item_id = 1 to active = # (this one is after the lookup) ... During db:migrate:up, it works as expected: setting item_id = 1 to active = true setting item_id = 1 to active = true setting item_id = 1 to active = true ... For some reason, the data appears to not be actually committed to the database during db:migrate, while it is during db:migrate:up. There''s no exception thrown by save!, no indicator that I can see that the save is unsuccessful. Even if it was, why would it only be unsuccessful during a regular migration run, and not during db:migrate:up? Has anyone seen this before? Do you have any idea what could cause this? Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Adam Stegman
2010-Feb-11 19:35 UTC
Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
Sorry, I should have mentioned - the migration before it is one where that active column is added. The migration after alters a column in a different table. The column is definitely added before this migration runs. On Feb 11, 1:23 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a migration that saves correctly to the database when it''s run > by itself through db:migrate:up, but not when it''s run with other > migrations through db:migrate. > > class SetActiveOnExistingItems < ActiveRecord::Migration > def self.up > Item.all.each do |item| > if item.item_status == Item::STATUS_REVIEW > item.active = false > elsif item.item_status == Item::STATUS_IN_PROGRESS > item.active = false > else > item.active = true > end > puts "setting item_id = #{item.id} to active = #{item.active}" > item.save! > puts "setting item_id = #{item.id} to active = #{item.active}" > item = Item.find(item.id) > puts "setting item_id = #{item.id} to active = #{item.active}" > end > end > > def self.down > Item.all.each {|item| item.active = nil; item.save(false)} > end > end > > The output during db:migrate is: > setting item_id = 1 to active = true # (this one is before the save) > setting item_id = 1 to active = true # (this one is after the save) > setting item_id = 1 to active = # (this one is after the lookup) > ... > > During db:migrate:up, it works as expected: > setting item_id = 1 to active = true > setting item_id = 1 to active = true > setting item_id = 1 to active = true > ... > > For some reason, the data appears to not be actually committed to the > database during db:migrate, while it is during db:migrate:up. There''s > no exception thrown by save!, no indicator that I can see that the > save is unsuccessful. Even if it was, why would it only be > unsuccessful during a regular migration run, and not during > db:migrate:up? > > Has anyone seen this before? Do you have any idea what could cause > this? > > Thanks.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Adam Stegman
2010-Feb-11 20:58 UTC
Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
I tried doing the whole thing in one migration - adding the column and setting the value, and I got the exact same behavior with this migration: class AddActiveToItems < ActiveRecord::Migration def self.up add_column :items, :active, :boolean Item.all.each do |item| if item.item_status == Item::STATUS_REVIEW or item.item_status == Item::STATUS_IN_PROGRESS item.active = false else item.active = true end item.save! puts "id: #{item.id}, active: #{item.active}" puts "id: #{item.id}, active: #{Item.find(item.id).active}" end end def self.down remove_column :items, :active end end for db:migrate: id: 1, active: true id: 1, active: for db:migrate:up: id: 1, active: true id: 1, active: true There must be something weird about this data manipulation. On Feb 11, 1:35 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry, I should have mentioned - the migration before it is one where > that active column is added. The migration after alters a column in a > different table. The column is definitely added before this migration > runs. > > On Feb 11, 1:23 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have a migration that saves correctly to the database when it''s run > > by itself through db:migrate:up, but not when it''s run with other > > migrations through db:migrate. > > > class SetActiveOnExistingItems < ActiveRecord::Migration > > def self.up > > Item.all.each do |item| > > if item.item_status == Item::STATUS_REVIEW > > item.active = false > > elsif item.item_status == Item::STATUS_IN_PROGRESS > > item.active = false > > else > > item.active = true > > end > > puts "setting item_id = #{item.id} to active = #{item.active}" > > item.save! > > puts "setting item_id = #{item.id} to active = #{item.active}" > > item = Item.find(item.id) > > puts "setting item_id = #{item.id} to active = #{item.active}" > > end > > end > > > def self.down > > Item.all.each {|item| item.active = nil; item.save(false)} > > end > > end > > > The output during db:migrate is: > > setting item_id = 1 to active = true # (this one is before the save) > > setting item_id = 1 to active = true # (this one is after the save) > > setting item_id = 1 to active = # (this one is after the lookup) > > ... > > > During db:migrate:up, it works as expected: > > setting item_id = 1 to active = true > > setting item_id = 1 to active = true > > setting item_id = 1 to active = true > > ... > > > For some reason, the data appears to not be actually committed to the > > database during db:migrate, while it is during db:migrate:up. There''s > > no exception thrown by save!, no indicator that I can see that the > > save is unsuccessful. Even if it was, why would it only be > > unsuccessful during a regular migration run, and not during > > db:migrate:up? > > > Has anyone seen this before? Do you have any idea what could cause > > this? > > > Thanks.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jarin Udom
2010-Feb-11 21:30 UTC
Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
You need to put "Item.reset_column_information" at the top of the migration so Rails reloads the altered table. Jarin Udom Robot Mode LLC http://robotmo.de On Feb 11, 12:58 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried doing the whole thing in one migration - adding the column and > setting the value, and I got the exact same behavior with this > migration: > > class AddActiveToItems < ActiveRecord::Migration > def self.up > add_column :items, :active, :boolean > Item.all.each do |item| > if item.item_status == Item::STATUS_REVIEW or item.item_status > == Item::STATUS_IN_PROGRESS > item.active = false > else > item.active = true > end > item.save! > puts "id: #{item.id}, active: #{item.active}" > puts "id: #{item.id}, active: #{Item.find(item.id).active}" > end > end > > def self.down > remove_column :items, :active > end > end > > for db:migrate: > id: 1, active: true > id: 1, active: > > for db:migrate:up: > id: 1, active: true > id: 1, active: true > > There must be something weird about this data manipulation. > > On Feb 11, 1:35 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Sorry, I should have mentioned - the migration before it is one where > > that active column is added. The migration after alters a column in a > > different table. The column is definitely added before this migration > > runs. > > > On Feb 11, 1:23 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have a migration that saves correctly to the database when it''s run > > > by itself through db:migrate:up, but not when it''s run with other > > > migrations through db:migrate. > > > > class SetActiveOnExistingItems < ActiveRecord::Migration > > > def self.up > > > Item.all.each do |item| > > > if item.item_status == Item::STATUS_REVIEW > > > item.active = false > > > elsif item.item_status == Item::STATUS_IN_PROGRESS > > > item.active = false > > > else > > > item.active = true > > > end > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > item.save! > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > item = Item.find(item.id) > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > end > > > end > > > > def self.down > > > Item.all.each {|item| item.active = nil; item.save(false)} > > > end > > > end > > > > The output during db:migrate is: > > > setting item_id = 1 to active = true # (this one is before the save) > > > setting item_id = 1 to active = true # (this one is after the save) > > > setting item_id = 1 to active = # (this one is after the lookup) > > > ... > > > > During db:migrate:up, it works as expected: > > > setting item_id = 1 to active = true > > > setting item_id = 1 to active = true > > > setting item_id = 1 to active = true > > > ... > > > > For some reason, the data appears to not be actually committed to the > > > database during db:migrate, while it is during db:migrate:up. There''s > > > no exception thrown by save!, no indicator that I can see that the > > > save is unsuccessful. Even if it was, why would it only be > > > unsuccessful during a regular migration run, and not during > > > db:migrate:up? > > > > Has anyone seen this before? Do you have any idea what could cause > > > this? > > > > Thanks.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jarin Udom
2010-Feb-11 21:33 UTC
Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
Oh also "Item.reset_column_information" needs to be after the add_column and before the Item.all.each if you are adding the column and manipulating the data in the same migration. If it is 2 separate migrations, just put it at the top of the 2nd one. Jarin Udom Robot Mode LLC http://robotmo.de On Feb 11, 1:30 pm, Jarin Udom <ja...-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote:> You need to put "Item.reset_column_information" at the top of the > migration so Rails reloads the altered table. > > Jarin Udom > Robot Mode LLChttp://robotmo.de > > On Feb 11, 12:58 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I tried doing the whole thing in one migration - adding the column and > > setting the value, and I got the exact same behavior with this > > migration: > > > class AddActiveToItems < ActiveRecord::Migration > > def self.up > > add_column :items, :active, :boolean > > Item.all.each do |item| > > if item.item_status == Item::STATUS_REVIEW or item.item_status > > == Item::STATUS_IN_PROGRESS > > item.active = false > > else > > item.active = true > > end > > item.save! > > puts "id: #{item.id}, active: #{item.active}" > > puts "id: #{item.id}, active: #{Item.find(item.id).active}" > > end > > end > > > def self.down > > remove_column :items, :active > > end > > end > > > for db:migrate: > > id: 1, active: true > > id: 1, active: > > > for db:migrate:up: > > id: 1, active: true > > id: 1, active: true > > > There must be something weird about this data manipulation. > > > On Feb 11, 1:35 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Sorry, I should have mentioned - the migration before it is one where > > > that active column is added. The migration after alters a column in a > > > different table. The column is definitely added before this migration > > > runs. > > > > On Feb 11, 1:23 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have a migration that saves correctly to the database when it''s run > > > > by itself through db:migrate:up, but not when it''s run with other > > > > migrations through db:migrate. > > > > > class SetActiveOnExistingItems < ActiveRecord::Migration > > > > def self.up > > > > Item.all.each do |item| > > > > if item.item_status == Item::STATUS_REVIEW > > > > item.active = false > > > > elsif item.item_status == Item::STATUS_IN_PROGRESS > > > > item.active = false > > > > else > > > > item.active = true > > > > end > > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > > item.save! > > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > > item = Item.find(item.id) > > > > puts "setting item_id = #{item.id} to active = #{item.active}" > > > > end > > > > end > > > > > def self.down > > > > Item.all.each {|item| item.active = nil; item.save(false)} > > > > end > > > > end > > > > > The output during db:migrate is: > > > > setting item_id = 1 to active = true # (this one is before the save) > > > > setting item_id = 1 to active = true # (this one is after the save) > > > > setting item_id = 1 to active = # (this one is after the lookup) > > > > ... > > > > > During db:migrate:up, it works as expected: > > > > setting item_id = 1 to active = true > > > > setting item_id = 1 to active = true > > > > setting item_id = 1 to active = true > > > > ... > > > > > For some reason, the data appears to not be actually committed to the > > > > database during db:migrate, while it is during db:migrate:up. There''s > > > > no exception thrown by save!, no indicator that I can see that the > > > > save is unsuccessful. Even if it was, why would it only be > > > > unsuccessful during a regular migration run, and not during > > > > db:migrate:up? > > > > > Has anyone seen this before? Do you have any idea what could cause > > > > this? > > > > > Thanks.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Adam
2010-Feb-11 21:42 UTC
Re: Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
Sure enough, I see that exact information in the Migration documentation. http://api.rubyonrails.org/classes/ActiveRecord/Migration.html It works great now. Thank you. Adam On Thu, Feb 11, 2010 at 3:33 PM, Jarin Udom <jarin-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote:> Oh also "Item.reset_column_information" needs to be after the > add_column and before the Item.all.each if you are adding the column > and manipulating the data in the same migration. If it is 2 separate > migrations, just put it at the top of the 2nd one. > > Jarin Udom > Robot Mode LLC > http://robotmo.de > > On Feb 11, 1:30 pm, Jarin Udom <ja...-vTN78maM9PiELgA04lAiVw@public.gmane.org> wrote: > > You need to put "Item.reset_column_information" at the top of the > > migration so Rails reloads the altered table. > > > > Jarin Udom > > Robot Mode LLChttp://robotmo.de > > > > On Feb 11, 12:58 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > I tried doing the whole thing in one migration - adding the column and > > > setting the value, and I got the exact same behavior with this > > > migration: > > > > > class AddActiveToItems < ActiveRecord::Migration > > > def self.up > > > add_column :items, :active, :boolean > > > Item.all.each do |item| > > > if item.item_status == Item::STATUS_REVIEW or item.item_status > > > == Item::STATUS_IN_PROGRESS > > > item.active = false > > > else > > > item.active = true > > > end > > > item.save! > > > puts "id: #{item.id}, active: #{item.active}" > > > puts "id: #{item.id}, active: #{Item.find(item.id).active}" > > > end > > > end > > > > > def self.down > > > remove_column :items, :active > > > end > > > end > > > > > for db:migrate: > > > id: 1, active: true > > > id: 1, active: > > > > > for db:migrate:up: > > > id: 1, active: true > > > id: 1, active: true > > > > > There must be something weird about this data manipulation. > > > > > On Feb 11, 1:35 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Sorry, I should have mentioned - the migration before it is one where > > > > that active column is added. The migration after alters a column in a > > > > different table. The column is definitely added before this migration > > > > runs. > > > > > > On Feb 11, 1:23 pm, Adam Stegman <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I have a migration that saves correctly to the database when it''s > run > > > > > by itself through db:migrate:up, but not when it''s run with other > > > > > migrations through db:migrate. > > > > > > > class SetActiveOnExistingItems < ActiveRecord::Migration > > > > > def self.up > > > > > Item.all.each do |item| > > > > > if item.item_status == Item::STATUS_REVIEW > > > > > item.active = false > > > > > elsif item.item_status == Item::STATUS_IN_PROGRESS > > > > > item.active = false > > > > > else > > > > > item.active = true > > > > > end > > > > > puts "setting item_id = #{item.id} to active > #{item.active}" > > > > > item.save! > > > > > puts "setting item_id = #{item.id} to active > #{item.active}" > > > > > item = Item.find(item.id) > > > > > puts "setting item_id = #{item.id} to active > #{item.active}" > > > > > end > > > > > end > > > > > > > def self.down > > > > > Item.all.each {|item| item.active = nil; item.save(false)} > > > > > end > > > > > end > > > > > > > The output during db:migrate is: > > > > > setting item_id = 1 to active = true # (this one is before the > save) > > > > > setting item_id = 1 to active = true # (this one is after the save) > > > > > setting item_id = 1 to active = # (this one is after the > lookup) > > > > > ... > > > > > > > During db:migrate:up, it works as expected: > > > > > setting item_id = 1 to active = true > > > > > setting item_id = 1 to active = true > > > > > setting item_id = 1 to active = true > > > > > ... > > > > > > > For some reason, the data appears to not be actually committed to > the > > > > > database during db:migrate, while it is during db:migrate:up. > There''s > > > > > no exception thrown by save!, no indicator that I can see that the > > > > > save is unsuccessful. Even if it was, why would it only be > > > > > unsuccessful during a regular migration run, and not during > > > > > db:migrate:up? > > > > > > > Has anyone seen this before? Do you have any idea what could cause > > > > > this? > > > > > > > Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-11 22:53 UTC
Re: Migration commits when run by itself (db:migrate:up), but not with other migrations (db:migrate)
On Feb 11, 9:42 pm, Adam <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sure enough, I see that exact information in the Migration documentation.http://api.rubyonrails.org/classes/ActiveRecord/Migration.html > It works great now. Thank you. >You should also be careful about using model classes in migrations - because your source is updated in one go but migrations run one by one it''s easy enough to run into trouble. For example if your model has a validation on a column that is added by migration two and migration one tries to create some objects of that class then it will fail because of the validation on the not yet existent column Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Feb-11 23:57 UTC
Re: Migration commits when run by itself (db:migrate:up), bu
Frederick Cheung wrote:> On Feb 11, 9:42�pm, Adam <adam.steg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Sure enough, I see that exact information in the Migration documentation.http://api.rubyonrails.org/classes/ActiveRecord/Migration.html >> It works great now. Thank you. >> > > You should also be careful about using model classes in migrations - > because your source is updated in one go but migrations run one by one > it''s easy enough to run into trouble. For example if your model has a > validation on a column that is added by migration two and migration > one tries to create some objects of that class then it will fail > because of the validation on the not yet existent columnAnd this is why it''s now considered preferable to use rake db:schema:load when setting up a server from scratch. Migrations are primarily for migration> > FredBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.