mercredi 6 juillet 2016

Restore part of a database

We have a legacy system with a shaky admin interface and one of the users managed to wipe out some records. We have a backup from a few days ago but we don't want to just remove the DB and restore from the backup - I only need a few types of objects from there (lots of rows, but only a few associated objects).

How can I restore only some records? I thought the following would work:

  • ActiveRecord::Base.establish_connection to connect to the backup db
  • Grab the records I want to restore (e.g. widgets = Widget.all)
  • Change the connection to the production db (again with establish_connection)
  • Save the widgets (widgets.save) - but it doesn't work?

Is there something intrinsic in an ActiveRecord object that I can change to make it save with the same id and everything to the production database, even though it's originally from the backup database, so they are pretty much identical to how they were before (apart from the created_at, of course, which is ok)

This is probably not the best way to do it, but I can't really determine what the best way is. The database is fairly hefty so I can't really get raw sql or something like that.

Any help appreciated!

Aucun commentaire:

Enregistrer un commentaire