Always take care to proceed with caution.

24年 9月 20日 Friday (edited)
351 words
2 minutes

What's happening?

Today, I'm assigned to do an automation task for inserting a CSV file into the MySQL database. The CSV format is a mess; I need to reorganize it into usable data. I'm not familiar with such a job, so I coded it in a hacky and unsafe way with Javascript.

The problem

It's ugly, but it works anyway. After I've tested the script on my local DB and local machine, I deployed the script to the production server, set up the database connection and ran it without a second thought. And guess what? It worked on my local machine but it didn't on the server, the local and remote database aren't fully synchronized causing an unexpected condition where it threw an error. back then I didn't fully realize how valuable the transaction was so I didn't prepare for it. the progress now is stalled and left the partial data residing in the database.

The solution

I've to clean up the problem I've created on my own. Fortunately, I printed the id to stdout each time a new record was inserted to the table, so I managed to filter the id with grep, construct a series of DELETE operation on the SQL file with awk. and finally execute the SQL file with foreign constraint check disabled.

The partial data problem solved, and I found the culprit that caused the error, Happy ending. From this tragedy, I've learn that proceed anything with caution can benefit your time even when it's seems take more time. Trust me, it will cost more time to clean up a mess than to proceed things with caution.

The conclusion

as a note for myself, in the next occasion, please do:

  1. apply more robust unit test
  2. ensure your code will work properly as expected
  3. proper error handling is necessary for mitigation to prevent unwanted things
  4. use transaction, commit, rollback
  5. the following option are actually optional and might be considered on advance scenario/level: remote and local database synchronization, multiple production databases (cloned) that are synchronized in real-time

It's not a worst cases scenario where I broke everything, it cost my time more than it should though, but now I've learned something new.

Title:Always take care to proceed with caution.

Author:ReYuki

Link:https://www.reyuki.site/posts/today-automating-sql-related-tasks [copy]

Last updated:


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. You are free to share and adapt it, as long as you give appropriate credit, don’t use it for commercial purposes, and distribute your contributions under the same license. Provided under license CC BY-NC-SA 4.0