Hi All, I want a migration to change the Amount column of the Expense table to the precision/scale to 10/2. It''s easy to say in one sentence. It''s a little harder to do. 1 Ran: ruby script/generate migration ChangeExpenseTbl_AmountCol 2.Got db/migrate/20100313180401_change_expense_tbl_amount_col.rb 3.Modified the migration as follows: class ChangeExpenseTblAmountCol < ActiveRecord::Migration def self.up class Expense change_column :amount, decimal :precision=>10, scale=>2 end end def self.down end end This looks like it could be DRYed to this: class Expense < ActiveRecord::Migration def self.up change_column :amount, decimal :precision=>10, scale=>2 end def self.down end end Do either of these look correct? Thanks in Advance, Richard -- 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.
On 13 March 2010 19:35, RichardOnRails <RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> Hi All, > > I want a migration to change the Amount column of the Expense table to > the precision/scale to 10/2. > > It''s easy to say in one sentence. It''s a little harder to do. > > 1 Ran: ruby script/generate migration ChangeExpenseTbl_AmountCol > 2.Got db/migrate/20100313180401_change_expense_tbl_amount_col.rb > 3.Modified the migration as follows: > > class ChangeExpenseTblAmountCol < ActiveRecord::Migration > def self.up > class Expense > change_column :amount, decimal :precision=>10, scale=>2 > end > end > > def self.down > end > end > > This looks like it could be DRYed to this: > > class Expense < ActiveRecord::Migration > def self.up > change_column :amount, decimal :precision=>10, scale=>2 > end > > def self.down > end > end > > Do either of these look correct?Why not try it and see? Provided you have a dump of the database (and you do have your code under source control in git (or svn) don''t you) then if it messes something up no harm is done. Did you check the params required for change_column from the docs ( http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)? It looks to me like you have a missing parameter. Colin -- 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.
RichardOnRails
2010-Mar-13 23:13 UTC
Re: Coding a migration to change a column''s attribute
Hi Colin, Thanks for some additional guidance. I know I appear to be a blithering idiot by virtue of my myriad mistakes, misunderstandings and apparent stupidity.> Why not try it and see? Provided you have a dump of the database (and > you do have your code under source control in git (or svn) don''t you)I didn''t try it because I have enough trouble going forward. I don''t want to again suffer the burden of 2 days to recover from stupid mistakes, like changing MySQL versions as I just did unnecessarily. Dump of DB unnecessary: I only have a little toy data created by testing. SVN backup: I will get to that in a few weeks; right now I make date&time-named backups of my project daily to an external hard-drive: code and documentation.> Did you check the params required for change_column from the docs > (http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)?< It looks to me like you have a missing parameter. Good site, but I didn''t find my exact requirement. Yes, I was missing the table-name parm. Here''s my current incarnation: class ChangeExpenseTblAmountCol < ActiveRecord::Migration def self.up change_column :expense, :amount, :decimal :precision => 10, :scale => 2 end def self.down end end No matter what permutation I try, the db:migrate task finds an error (usually syntax). When I was in USAF Radio School at the beginning of the Korean "Police Action", I discovered the Cryptographic School next door and "knew" I belonged there. But the effort to crack the code proves I was wrong those many years ago, ''cause I can''t crack this code. Does anything leap out at you? As usual, thanks for the gift of your time and expertise, Richard -- 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.
Frederick Cheung
2010-Mar-13 23:45 UTC
Re: Coding a migration to change a column''s attribute
On Mar 13, 11:13 pm, RichardOnRails <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote:> > class ChangeExpenseTblAmountCol < ActiveRecord::Migration > def self.up > change_column :expense, :amount, :decimal :precision => > 10, :scale => 2 > endThe first parameter is the table name ( ie plural) and you''re missing a comma towards the end. 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.
RichardOnRails
2010-Mar-13 23:57 UTC
Re: Coding a migration to change a column''s attribute
OK, Collin, Thanks to that migrations link you posted last time, I saw another format, the old C-style coding as opposed to the new Rails-DSL- style. Below is the migration code, the db:migration script execution and the results in the database. You don''t need to look at all that stuff. I just added to show that the old style does work and pique your curiosity that the new styles don''t seem to. Again, Colin, thanks for all your help on this and other programming "hardships". == execution results == • This format works: class ChangeExpenseTblAmountCol < ActiveRecord::Migration def self.up change_column( "expenses", "amount", "decimal(10, 2)" ) end def self.down end end • Apply the migration K:\_Projects\Ruby\_Rails_Apps\_EIMS\RTS>rake db:migrate (in K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS) == ChangeExpenseTblAmountCol: migrating =====================================-- change_column("expenses", "amount", "decimal(10, 2)") -> 0.7030s == ChangeExpenseTblAmountCol: migrated (0.7190s) ============================ K:\_Projects\Ruby\_Rails_Apps\_EIMS\RTS> • Testing the database confirms it: mysql> describe expenses; +------------+--------------- | Field | Type +------------+--------------- | id | int(11) | purpose | varchar(255) | vendor | varchar(255) | type | varchar(255) | date | date | amount | decimal(10,2) [snip] -- 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.
RichardOnRails
2010-Mar-14 23:55 UTC
Re: Coding a migration to change a column''s attribute
Hi Frederick, Thanks for taking the time to look at my newbie code. I now have both styles: change_column :expenses, :amount, :decimal, :precision => 10, :scale => 2 # DSL-style change_column( "expenses", "amount", "decimal(10, 2)" ) # C-style working. Thank you for pointing the things I was missing when I fumbled here. I wont have any more problems in this area. But I''m moving on to a new area now :-) Best wishes, Richard On Mar 13, 7:45 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 13, 11:13 pm, RichardOnRails > > <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote: > > > class ChangeExpenseTblAmountCol < ActiveRecord::Migration > > def self.up > > change_column :expense, :amount, :decimal :precision => > > 10, :scale => 2 > > end > > The first parameter is the table name ( ie plural) and you''re missing > a comma towards the end. > > 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.
RichardOnRails
2010-Mar-15 00:00 UTC
Re: Coding a migration to change a column''s attribute
Hi Craig, As I mentioned to Frederick, I''ve got both the DSL-style and C-Style for change_column working after a bunch of stumbles. I expect to be much better with future migrations I code. In that context, I''m sure the "--trace" option you pointed out will be very helpful. Thanks for taking the time to look in on this problem. Best wishes, Richard On Mar 13, 7:57 pm, Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org> wrote:> On Sat, 2010-03-13 at 15:45 -0800, Frederick Cheung wrote: > > > On Mar 13, 11:13 pm, RichardOnRails > > <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote: > > > > class ChangeExpenseTblAmountCol < ActiveRecord::Migration > > > def self.up > > > change_column :expense, :amount, :decimal :precision => > > > 10, :scale => 2 > > > end > > > The first parameter is the table name ( ie plural) and you''re missing > > a comma towards the end. > > ---- > in the thought that give a man a fish and feed him for a day and teach a > man to fish, he can feed himself the rest of his life... > > rake SOME_COMMAND --trace > > gives you more meaningful output on what went wrong > > Craig > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean.-- 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.