moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
785 stars 89 forks source link

Using deep clone to extract a subset db from production #136

Closed mestachs closed 3 years ago

mestachs commented 3 years ago

I'm sorry to use the issue tracker for "questions"

I already use your great gem to "version" a graph of objects But I'd like to reuse a bit the same mechanism to subset my production database and store this graph in a separate db (dev or staging) (ideally preserving the ids ?)

I assumed I could load the graph then call ActiveRecord::Base.establish_connection(config) Since I don't know much about the implementation of ActiveRecord and your gem.

Do you think it's a viable solution :

moiristo commented 3 years ago

Hi,

What you are trying to do is not very common.. I think only most recent versions of AR do support switching databases, see https://guides.rubyonrails.org/active_record_multiple_databases.html.

That being said: I think you should first call deep_clone while connected to the prod db. This will create a tree structure in memory of the data that is in your prod db. After that, you should somehow save the duped record to the dev db, something like ActiveRecord::Base.connected_to(role: :writing, shard: :default) { new_project.save! }. But all this is highly theoretical imo :)

As an alternative, I would suggest that creating (JSON) export/import functionality would probably be more robust.

mestachs commented 3 years ago

Thanks.