As most of you who follow COF know, I have absolutely no shame when it comes to blogging about stuff I’ve never done before in programming AKA things that are new to me. When I learn from a mistake I inevitably will make, I am certain to document it here from start to finish.
I love a well written database. One where all the columns and rows line up and no one names said columns or rows something arbitrary like Fibonacci Seq(x1) Description: <- I just made that up as possibly the worst thing you could name an attribute in your database. I could be wrong, just sayin’
Anyway moving right along, my task this week was to help a client out by fixing some accidentally deleted data from their site.
To avoid reverting an entire website back to before the weekend, I was asked to restore a copy of the previous db before the accidental deletions and put the db somewhere I can look at it to pull out the accidentally deleted ids, did I mention for an entire client website not just my little side project app.
My knowledge base of working with OPD (Other People’s Data) prior to this:
Yeah, this falls into “things they don’t teach you in dev bootcamp, because they really can’t”, and “things you don’t learn online”. Being realistic, in a bootcamp or when teaching yourself programming, whose database do you really need to copy to production? You are basically working with your own app, created by you, with all your own data that no one but you has touched.
Hmmmm, what to do, what to do.
Well the first step is to ask your System Crew (I call mine Pilots of the Millennium Falcon) to make a copy of the production db. *note, they will need a date from you, for the data that you want copied. In my case I asked for 12 hours before the content was accidentally deleted.
So, now that I have a copy of the client’s data in a safe place on my computer, I can mess with that data all day long and not break anything. So here is what I did.
1.) I cd’d into that backup file
2.) I opened up a rails console
RAILS_ENV=local rails c
3.) I recreated that deleted taxonomy_id by writing a lil code
restored_taxon = Taxonomy.create(tree_path: ":x:xx:", has_child: false, title: "My Awesome Taxonomy Title", description: "My Awesome Taxonomy Title and related topics", sorting_order: 00, active: true, selectable: true)
4.) I plugged in all the missing data for the first taxonomy (there were 6 deleted due to associations)
first_ids = [12345, 22345, 32345, 42345, ...]
4.) I recreated ids/taxonomies associations
first_ids.each do |first_id|
FirstTaxonomy.create(first_id: first_id, taxonomy_id: restored_taxon.id)
After 6 rounds of this, all deleted content was restored and all the client needed to do was some magic on their end to see it all back to normal.
What was super strange to me wasn’t with creating the scripts to fix this issue, but the process of how to work with OPD (other people’s data) in a safe and secure spot so as not to dump an entire website’s data and take ‘er down.
In all honesty, I think breaking things to the point of irreparable damage is one of the biggest fears of a Jr Dev, and this is something I personally struggle with daily. It’s that fine line between just trying something and not trying something cos you might screw it ALL up. I’m getting there though.
Let me know in the comments when you had your moment of “I’m just gonna hit this button here and not be scared that I’ll break anything” AND how to get better at collaborating processes like working with OPD.