Coding a migration to change a column's attribute

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

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

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 (ActiveRecord::Migration)?

< 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

The first parameter is the table name ( ie plural) and you're missing a comma towards the end.

Fred

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

> > 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.

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 :slight_smile:

Best wishes, Richard

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