this post was submitted on 11 Oct 2023
354 points (97.6% liked)

Programming

16952 readers
404 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS
 

"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.

(page 2) 50 comments
sorted by: hot top controversial new old
[–] elbucho@lemmy.world 6 points 11 months ago

Oof. Been there, done that, 0 stars; would not recommend.

[–] Bazz@feddit.de 5 points 11 months ago

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

[–] o11c@programming.dev 5 points 11 months ago (3 children)

This is about the one thing where SQL is a badly designed language, and you should use a frontend that forces you to write your queries in the order (table, filter, columns) for consistency.

UPDATE table_name WHERE y = $3 SET w = $1, x = $2, z = $4 RETURNING *
FROM table_name SELECT w, x, y, z
load more comments (3 replies)
[–] gatelike@feddit.de 4 points 11 months ago

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

[–] books@lemmy.world 4 points 11 months ago

Things like this make me glad I can only query my db.

[–] evatronic@lemm.ee 4 points 11 months ago (1 children)

Unrelated, but use placeholders instead of interpolation right into the query.

See: Little Bobby Tables. https://xkcd.com/327/

[–] sim642@lemm.ee 4 points 11 months ago (1 children)

That's what they're doing...

[–] evatronic@lemm.ee 1 points 11 months ago

If true, great. I've not run across a language / RDBMs library that uses numbered place holders over the standard ?, but I'm sure someone's done it.

[–] Techmaster@lemm.ee 4 points 11 months ago

If it's Microsoft SQL you should be able to replay the transaction log. But you should be doing something like daily full backups and hourly incremental or differential backups to avoid this situation in the first place.

[–] SeabassDan@lemmy.world 4 points 11 months ago

Ctrl+z bro

Jk, sounds tough

[–] fury@lemmy.world 4 points 11 months ago

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.

[–] Tarte@kbin.social 2 points 11 months ago* (last edited 11 months ago)

I‘m using DataGrip (IntelliJ) for any manual SQL tomfoolery. I have been where you are. Luckily for me, the tool asks for additional confirmation when doing any update/delete without where clause.

Also, backups are a must, for all the right reasons and for any project.

[–] Wakmrow@lemmy.world 2 points 11 months ago

I learned this lesson too

[–] oldfart@lemm.ee 1 points 11 months ago

Been there, done that, I hope you have a recent backup!

load more comments
view more: ‹ prev next ›