Skip to content

Instantly share code, notes, and snippets.

@NickClark
Created March 8, 2011 15:20
Show Gist options
  • Select an option

  • Save NickClark/860398 to your computer and use it in GitHub Desktop.

Select an option

Save NickClark/860398 to your computer and use it in GitHub Desktop.
Importing Multiple databases

Hey all,

I am writing a rake script to import data from an old php database, into my shiny new rails app. In the script I define a few temp ActiveRecord classes to represent the tables from the old system as shown in the excerpt below. This works fine with one legacy database. The problem is that I have many databases with the same tables but some those tables columns differ. My code takes that into account appropriatly. The problem is that when I change the underlying database being used to the next one, then the models seem to not update properly, any idea on a better way to do this?

databases_to_import = ENV['DATABASES'].split(',')
class LegacyDB < ActiveRecord::Base
self.abstract_class = true
establish_connection(:legacy)
end
module Legacy
class Club < LegacyDB
end
#define other classes...
end
databases_to_import.each do |database_to_import|
LegacyDB.connection.execute("use `#{database_to_import}`")
# do stuff....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment