this post was submitted on 11 Oct 2023
418 points (97.9% liked)

Programming

17309 readers
326 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 [email protected]



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
[–] [email protected] 6 points 1 year ago

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

[–] o11c 5 points 1 year 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)
[–] [email protected] 5 points 1 year ago

I don't know about the others but oracle has savepoints which allows you to rollback a transaction, so before you do anything big you create a savepoint then when you break it you can roll back

[–] [email protected] 5 points 1 year ago

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

[–] [email protected] 4 points 1 year 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.

[–] MarekKnapek 4 points 1 year ago

Sh1t already happened, but on the bright side, you learn for the next time. Some tips:

  • Make clone of production environment into testing environment. Do stuff there, and after it is tested / verified, do the same in production. Emphasis on "do exactly the same".
  • Make regular automatic backup. From time to time, do a backup restore to another computer (VM) and verify that it is restored successfully and is actually usable.
  • Poor man's backup is automatic daily (hourly?) snapshots on BTRFS / ZFS / EXT4 filesystems or VolumeShadowCopy on NTFS filesystem.
  • Before any UPDATE do a SELECT and verify the data you are modifying are the data you actually want to modify.
  • Use a transactions in form of BEGIN TRANSACTION; UPDATE stuff; ROLLBACK; and observe the DB behavior, or see how many rows are affected.
  • Somebody has even moar tips, I'm sure. Please write them below.
[–] [email protected] 4 points 1 year ago (1 children)

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

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

[–] [email protected] 4 points 1 year ago (1 children)

That's what they're doing...

load more comments (1 replies)
[–] [email protected] 4 points 1 year ago

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

[–] sjpwarren 4 points 1 year ago

I would run a SELECT with a WHERE clause to see the data I was about to remove to make sure it looked right. Then I would make the delete and Copy and Paste (can you see where this is going? - pun intended) the WHERE clause from the Select into the Delete and run it. But I didn't. This was a production database. It got so hot all of a sudden as I realised what I had done! Lucky we only lost a few hours work.

I am sorry for your loss

[–] [email protected] 3 points 1 year ago

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

[–] [email protected] 3 points 1 year ago

Ctrl+z bro

Jk, sounds tough

[–] [email protected] 3 points 1 year ago

One of the reasons I hate working on databases with a passion. I always feel uncomfortable doing anything on DBs.

[–] [email protected] 2 points 1 year ago* (last edited 1 year 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.

load more comments
view more: ‹ prev next ›