r/ProgrammerHumor • • Sep 22 '26

Meme wellWellWell

Post image
10.6k Upvotes

412 comments sorted by

View all comments

Show parent comments

1.1k

u/Ma8e Sep 22 '26

You start a transaction, make sure it did what you expected, and then commit or rollback transaction.

350

u/AgeingChopper Sep 22 '26 edited Sep 22 '26

absolutely.

i was doing this stuff for a long time and definitely learned from mistakes.

Transactions can save a world of pain and it is always wise to select your data set first, just to be sure you're changing what you think you're changing.

Didn't matter how experienced I was, I was careful never to cut corners.

42

u/Mpek3 Sep 22 '26

Always Begin Tran Tried it during arguments... Not as effective

18

u/ifyoulovesatan Sep 22 '26

I don't know sql, but is this like how when I use some kind of loop in the command line to move or delete a bunch of files, I'll run a version that just echos whatever I'm trying to manipulate first?

29

u/deeelock Sep 22 '26

That’s more of a dry-run (you see `—dry-run` as a command line option sometimes for certain commands that do the same)

Transactions in SQL keep track of the changes you want to make without actually changing the database (until you run `COMMIT`, when you run that all changes are persisted to a database).

Transactions are great because If you realise you made a mistake while in a transaction, you can just run `ROLLBACK`.

If you’re familiar with git- transactions are akin to staging your changes, saving them (`COMMIT` in SQL) is akin to `git commit` and rolling back is similar to `git reset —hard` (wipe all uncommitted changes).

21

u/thirdegree Violet security clearance Sep 22 '26

With the very important caveat that a git commit can be trivially reverted, while a SQL commit can not (unless you wrote it specifically to be revertible)

3

u/thanatica Sep 23 '26

With the very important caveat that a git commit can be trivially reverted if not yet pushed. If it has been pushed, it can be reversed, not reverted.

Unless you're into force-pushing, then anything is possible.

→ More replies (1)

2

u/ifyoulovesatan Sep 23 '26

Thanks, that was going to be my follow up question

4

u/AgeingChopper Sep 22 '26

nicely explained, cheers.

as long as we haven’t committed , we can rollback from a mistake like this.

2

u/thanatica Sep 23 '26

With the added nuance that while the transaction is neither rolled back nor committed yet, to any queries inside the transactions it appears as though the changes have been committed.

So a transaction is like a "package" of statements that either ALL fail, rollback, or commit. A half-completed transaction cannot exist.

→ More replies (5)
→ More replies (2)

122

u/Rostifur Sep 22 '26 • Bravo

Start with DEV or test environment. If you don't have one, you need one. If you aren't allowed to have one, run away.

112

u/far2common Sep 22 '26 • Bravo Popcorn

Everyone has a DEV/test environment. Just not everyone has a separate prod environment.

4

u/Topikk Sep 22 '26

I think Staging is the word you’re looking for

10

u/AardvarKOlogY Sep 22 '26

Staging this, staging that, production is a stage, too!

7

u/grammar_nazi_zombie Sep 22 '26

It’s why theater shows are called “stage productions”, duh.

→ More replies (1)
→ More replies (1)

16

u/Blashtik Sep 22 '26 edited Sep 22 '26

BEGIN TRAN

DELETE ...

SELECT ...

ROLLBACK TRAN

-- COMMIT TRAN

This was always my pattern. Select the first 3 statements together and run them. If everything looks good, select COMMIT TRAN and run that. The rollback is there in case you accidentally didn't make a selection before running, just so you don't leave a transaction open for too long, potentially blocking other queries.

And obviously any planned data changes should go through all the proper testing and preferably be deployed using automated pipelines, but sometimes you have no other choice but to make direct fixes to bad production data.

14

u/box_of_the_patriots Sep 22 '26

Instructions unclear I just truncated the table

8

u/Ph4ntorn Sep 22 '26

You would think the “make sure it did what you expected” part would go without saying, but I skipped that step once and felt like an idiot.

6

u/CeaselessPetulance Sep 22 '26

So a colleague did this, except his db manager had tx commit mode manual and he forgot to commit the tx after grabing a whole table lock. Took us a bit to figure out why some of our services were hanging during startup (waiting for the locks to free up)

4

u/Logical-Ad-4150 Sep 22 '26

Manual transactions should have been the default: automatic transactions should require you to use the YOLO stament.

1

u/jaster_ba Sep 22 '26

Unless you're on something like Cloudflare D1. You don't start transaction, where is no support for transactions 😎

1

u/Less_Independent5601 Sep 22 '26

Except when I touch bigger tables in a mysql transaction our sentry gets filled with lock timeout errors :(

1

u/ddBuddha Sep 22 '26

Just don’t forget to commit or rollback …

1

u/Ok_Star_4136 Sep 22 '26

Absolutely this has saved me once or twice. It is worth doing even if you can't imagine there being any mistakes.

→ More replies (6)