Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging Rails databases

I have two databases with the same structure. The tables have an integer as a primary key as used in Rails.

If I have a patients table, I will have one patient using primary key 123 in one database and another patient using the same primary key in the other database.

What would you suggest for merging the data from both databases?

like image 880
hectorsq Avatar asked Oct 03 '08 23:10

hectorsq


People also ask

Is Ruby on Rails a database?

Rails comes with built-in support for SQLite, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.


1 Answers

Set both your databases up with entries in config/database.yml, then generate a new migration.

Use ActiveRecord::Base.establish_connection to switch between the two databases in the migration like this:

def self.up
  ActiveRecord::Base.establish_connection :development
  patients = Patient.find(:all)
  ActiveRecord::Base.establish_connection :production
  patients.each { |patient| Patient.create patient.attributes.except("id") }
end

YMMV depending on the number of records and the associations between models.

like image 97
user6325 Avatar answered Oct 04 '22 02:10

user6325