Forum: Ruby on Rails migration to be called inside others migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
10c122532c00465b809dbf9dc35806a7?d=identicon&s=25 Paolo Negri (Guest)
on 2006-06-05 20:15
(Received via mailing list)
Hi list

I'm writing some migrations to move a legacy db on a rails app. I have
a bunch of tables which need some custom modifications and the columns
updated_at to be added etc...

Now since the rails related columns will be the same for many (but not
all) tables I was thinking... can I write a Migration that sounds like
that

class AddRailsFields < ActiveRecord::Migration

  def self.up(tbname)
    add_column tbname, "lock_version", :integer, :default => 0
    rename_column tbname, "oldtimestamp", "updated_at"
  end

  def self.down(tbname)
    remove_column tbname, "lock_version"
    rename_column tbname, "updated_at", "oldtimestamp"
  end

end

And call it inside other migrations as

AddRailsFields.up("tablename")

I've tried this but AddRailsFields (which is located in dedicated file
in db/migrations/) seems to be not defined in other migrations. Maybe
a 'require' in the right place could fix the problem, but I would
avoid to add the require to each migration or to put it in some place
too generic.

Thanks for any help

Paolo
This topic is locked and can not be replied to.