Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a single record between environments

I have an app which is in different stages of development (live, test, development)

When the testers report an issue with the test database they will specify a user id which is only on the test database, which then makes it quite difficult to reproduce the problem in development (In some cases)

I would like to be able to pull out everything tied to a particular id in a table (including objects from other tables linked by foreign keys) and be able to import that data into the development database.

How can that be done?

  • can I deep copy all the linked relationships?
  • can I export that object and then import it
  • can I access two databases from that migration script?
like image 861
Joseph Le Brech Avatar asked Nov 25 '25 02:11

Joseph Le Brech


1 Answers

What you're trying to achieve is not a trivial thing.

If you want to pull out all the info about a user, you'll have to make a script that access all his info and related info or extract the info directly from the DB tables.

If you prefer the first alternative, you'll have to look carefully to the relationships and extract all of them using ActiveRecord.

Maybe it will be useful to you the use of the "attributes" method, if you have for example a User model and a @user instance, you could do:

user_attributes = @user.attributes

and extract his attributes in a hash, later you could do the following:

User.create(user_attributes)

to create an User with the same attributes, but you will have problems with protected_attributes or relationships.

Maybe the strategy you're trying to follow is not the best to solve your problem.

You can't access test environment for example with ssh? This way you could check the database and see the problem as is.

like image 87
Juan de Dios H. Avatar answered Nov 26 '25 16:11

Juan de Dios H.