Reputation: 1619
I have 2 Rails apps with the same models apart from a couple of attributes. I now need to merge these apps and I'm trying to work out how to merge their data. The challenges are:
I'm aware of seed_dump and yaml_db but I'm not sure they're a good solution for my problem
Upvotes: 1
Views: 155
Reputation: 1204
I faced a similar situation some time ago, with an app designed with database multitenancy. To merge all data into a single database I ended up creating a rake task, that would go through every record and association and recreate them in the new database.
The task had two arguments SOURCE
and DESTINATION
, and it would be run like:
rake db:merge SOURCE=some_database DESTINATION=new_database
. The task was run for each database I needed to merge.
Inside the rake task the idea was something like this:
namespace :db do
task :merge => [:environment, :load_config] do |t, args|
raise "Empty SOURCE provided" if not ENV["SOURCE"] or ENV["SOURCE"].empty?
@source = ENV['SOURCE'].to_sym
@destination = (ENV['RAILS_ENV'] || 'development').to_sym
# for each model
ActiveRecord::Base.establish_connection(@source)
# obtain the records and its associations
ActiveRecord::Base.establish_connection(@destination)
# loop through each record and build all associated records
# apply any necesary transformation to data
# then build the record and fill it with the new associations
# skip callbacks / validations if needed
# then save
In some cases to keep track of ids in associations I used hashes, that tracked the original and new id.
I know the complexities of each app can make this process really hard, but in my case this idea resulted to be very usefull as a starting point.
Upvotes: 1