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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With