I accidentally removed the WHERE clause from my SQL query in a personal tool. Every row is now the same. I overwrote 206,000+ rows. I have no backup, I am stupid.

“UPDATE table_name SET w = $1, x = $2, z = $4 WHERE y = $3 RETURNING *”,

does not do the same as

“UPDATE table_name SET w = $1, x = $2, y = $3, z = $4 RETURNING *”,

It’s 2 am and my mind blanked out the WHERE, and just wanted the numbers neatly in order of 1234.

idiot.

FML.

superfes,

I know it’s too late to be helpful now, but I always write the WHERE first, because you are not the first person to have done this…

fury,

Pressing F to pay respects. R.I.P. in pieces

Depending on how mission critical your data is…Set up delayed replicas and backups (and test that your backups can actually be restored from). Get a second pair of eyeballs on your query. Set up test environments and run it there before running it in production. The more automated testing you put into your pipeline, the better. Every edit should be committed and tested. (Kubernetes and GitLab Auto DevOps makes this kind of thing a cinch, every branch has a new test environment set up automatically)

Don’t beat yourself up too much though. It happens even to seasoned pros.

Malfeasant,

I watched someone make this mistake during a screen share, she hit execute and I screamed “wait! You forgot the where!” Fortunately, it was such a huge database that SQL spun for a moment I guess deciding how it was going to do it before actually doing it, she was able to cancel it and ran a couple checks to confirm it hadn’t actually changed anything yet. I don’t think anything computer related has ever gotten my adrenaline going like that before or since

Wakmrow,

I learned this lesson too

SeabassDan,

Ctrl+z bro

Jk, sounds tough

olafurp,

Always SELECT first. No exceptions.

drekly,

I did when I made the query a year ago. Dumdum sleep deprived brain thought it would look more organised this way

adrian783,

mind explaining?

nailbar,

By running a select query first, you get a nice list of the rows you are going to change. If the list is the entire set, you’ll likely notice.

If it looks good, you run the update query using the same where clause.

But that’s for manual changes. OP’s update statement looks like it might be generated from code, in which case this wouldn’t have helped.

Dark_Arc, (edited )
@Dark_Arc@social.packetloss.gg avatar

Better yet… Always use a transaction when trying new SQL/doing manual steps and have backups.

Blackmist,

You’re not the first. You won’t be the last. I’m just glad my DB of choice uses transactions by default, so I can see “rows updated: 3,258,123” and back the fuck out of it.

I genuinely believe that UPDATE and DELETE without a WHERE clause should be considered a syntax error. If you want to do all rows for some reason, it should have been something like UPDATE table SET field=value ALL.

drekly,

Because I’m relatively new at this type of thing, how does that appear on the front end? I’m using a js/html front end and a jsnode backend. Would I just see a popup before I make any changes?

Blackmist,

No idea. My tools connect directly to the DB server, rather than going though any web server shenanigans.

aravindan_v,

If you’re asking about the information about the number of rows, oracle db clients do that. For nodejs, oracle’s library will provide this number in the response to a dml statement execution. So you can retrieve it in your backend code. You have to write additional code to bring this message to the front-end.

oracle.github.io/node-oracledb/

drekly,

Awesome, thanks for the info. Definitely super useful for debug mode whilst I’m fixing and tampering!

Jambone,

In MSSQL, you can do a BEGIN TRAN before your UPDATE statement.

Then if the number of affected rows is not about what you’d expect, doing a ROLLBACK would undo the changes.

If the number of affected rows did look about right, doing a COMMIT would make the changes permanent.

lobut,

Yup, exact tip I was gonna write!

I have them commented out and highlight the COMMIT when I’m ready.

TeenieBopper,

I once dropped a table in a production database.

I never should have had write permissions on that database. You can bet they changed that when clinicians had to redo four days of work because the hosting company or whatever only had weekly backups, not daily.

So, I feel your pain.

Bazz,

I don’t know if it makes you feel better but Tom Scott had a similar experience: youtu.be/X6NJkWbM1xk

dbilitated,
@dbilitated@aussie.zone avatar

I did that once when I moved from one DB IDE to another and didn’t realise the new one only ran the highlighted part of the query.

there were thousands of medical students going through a long process to find placements with doctors and we had a database and custom state machine to move them through the stages of application and approval.

a bug meant a student had been moved to the wrong state. so I used a snippet of SQL to reset that one student, and as a nervous habit highlighted parts of the query as I reread them to be sure it was correct.

then hit run with the first half highlighted, without the where clause, so everyone in the entire database got moved to the wrong fucking state.

we had 24 hourly backups but I did it late in the evening, and because it was a couple of days before the hard deadline for the students to get their placements done hundreds of students had been updating information that day.

I spent until 4am the next day working out ways to imply what state everyone was in by which other fields had been updated to what, and incidentally found the original bug in the process 😒

anyway, I hope you feel better soon buddy. it sucks but it happens, and not just to you. good luck.

gatelike,

SQL scouts credo: I will never use indexes, I will always use column names.

MonkCanatella, (edited )

this folks, is why you don’t raw dog sql like some caveman

drekly,

Me only know caveman. Not have big brain only smooth brain

olafurp,

Raw dog is the fastest way to finish a task.

  • productivity
  • risk

It’s a trade-off

Sharpiemarker,

There’s no way you’re endorsing the way OP handled their data right?

olafurp, (edited )

No, but people are sometimes forced to do these things because of pressure from management and/or lack of infrastructure to do it in any other way.

Definitely don’t endorse it but I have done it. Think of a “Everything is down” situation that can be fixed in 1 minute with SQL.

Sharpiemarker,

Got it. I’m with you.

groucho,
@groucho@lemmy.sdf.org avatar

Yep. If you’re in a situation where you have to write SQL on the fly in prod, you have already failed.

MonkCanatella,

it’s time to commit sqlpukku

Malfeasant,

Tell that to my former employer…

groucho,
@groucho@lemmy.sdf.org avatar

Yeah, I swear it’s part of the culture at some places. At my first full-time job, my boss dropped the production database the week before I started. They lost at least a day of records because of it and he spent most of the first day telling me why writing sql in prod was bad.

XTornado, (edited )

Me doing it for multiple years in a Bank…Uhm…

(let’s just say I am not outting my money near them… and not just because of that but other things…)

XTornado,

But the adrenaline man… some of us are jonkies of adrenaline but we are too afraid of anything more of physically dangerous…

MonkCanatella,

You may be interested in suicide linux then. it’s a distro that wipes your entire hard drive if you mistype a command

Anonymousllama,

A gut wrenching mistake, hopefully you’ll only make it once!

redcalcium,

Periodic, versioned backups are the ultimate defense against bugs.

Aganim,

Periodic, versioned and tested backups.

It absolutely, totally, never ever happened to me that I had a bunch of backups available that turned out to be effectively unrestorable the moment I needed them. 😭

nous,

Or like that time gitlab found out that none of its 5 backup/replications worked and lost 6 hours of data.

redcalcium,

The worse feeling than realizing you don’t have backup is realizing your backup archives are useless.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines