If you are transferring from one DB to another and want to make sure it happens properly, this is the mechanism you can use.
The same mechanism could be used to migrate multiple tables. And can be implemented in SQL stored procedure or stored procedure equivalent in the DB of your choice.
- Add these additional columns to your source table
- is_migrated: default false
- Create a DB migration script which
- Copies a batch of data from one table in source DB to one table in dB.
- The batch size could be 1000. You don’t want the batch size to be too small because it could be slow. You don’t want it too high to be able to recover better.
- Whenever a batch is a complete update the is_migrated and migrated_date in the source DB. Then pick up the next batch where is_migrated is false.
- If there is an error, set is_error to_ true _and add error_description.
- The error_description could be an exception that is thrown.
- Once the script is done, check for is_error: true and fix those errors in the script itself.
- Run the script by copying the data from PROD to your testing environment.
- Fix your scripts if you find is_error: true
- Once all is fixed, re-copy data from PROD to your testing environment and run again.
- You want to make sure everything works perfectly so that you won’t have to do much during the PROD run.
- Once you find all bugs are fixed, run the migration script in PROD.