Handling migrations with multiple databases in Rails

I have a Rails reporting application that will generate reports for other Rails applications.

As such I need to import legacy databases, but might also over time, change them.

How do I handle migrations with having, and connecting to, x number of databases?

I've defined the 'other' databases connections in my classes:

class OtherApp < ActiveRecord::Base   self.abstract_class = true   establish_connection :otherApp1_development # defined in database.yml

  def initialize     self.establish_connection   end end

Yet when I use:

class CreateApplicationExampleOne < OtherApp   def self.up     # definitions etc   end end

and then try to migrate, it fails.

I hope I'm missing something small here.

I have a Rails reporting application that will generate reports for other Rails applications.

As such I need to import legacy databases, but might also over time, change them.

How do I handle migrations with having, and connecting to, x number of databases?

You'd want to establish_connection on ActiveRecord::Base, since that's what create_table etc will use. I'm not sure what will happen with your schema_migrations table (which is how it determines what migrations to one), out of the box rails will always look at your primary database for this. You might find it easier to define a rails env for each of these databases and then run migrate for each such environment.

Fred

Thanks. It does seem like multiple DBs and migrations do not necessarily play well.

Would be interesting to see a recipe for this somewhere.

Frederick Cheung wrote in post #972014: