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

Bappity
link
fedilink
English
18
edit-2
1Y

who thought it was a good idea to make the where condition in SQL syntax only correct after the set?? disaster waiting to happen

xigoi
link
fedilink
61Y

The people designing SQL, not having learned from the mistakes of COBOL, thought that having the syntax as close to English as possible will make it more readable.

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

you could use dbeaver that warns you for update and delete queries without a where clause, independently of the db system. I hope the functionality it’s still there since, for totally unrelated motivations, I always use a where clause, even when buying groceries.

This doesn’t help you but may help others. I always run my updates and deletes as selects first, validate the results are what I want including their number and then change the select to delete, update, whatever

I learned this one very early on in my career as a physical security engineer working with access control databases. You only do it to one customer ever. 🤷‍♂️

@evatronic@lemm.ee
link
fedilink
English
41Y

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

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

That’s what they’re doing…

@evatronic@lemm.ee
link
fedilink
English
11Y

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.

You all run queries against production from your local? Insanity.

If coffee simply does not get you out of bed in the mornings any more, you need to find your fix elsewhere 💁

Tarte
link
fedilink
17
edit-2
1Y

The distinctions get blurry if you’re the sole user.

@drekly@lemmy.world
creator
link
fedilink
English
6
edit-2
1Y

My only education is a super helpful guy from Reddit who taught me the basics of setting up a back end with nodejs and postgres. After that it’s just been me, the references and stack overflow.

I have NO education about actual practises and protocol. This was just a tool I made to make my work easier and faster, which I check in and update every few months to make it better.

I just open vscode, run node server.js to get started, and within server.js is a direct link to my database using the SQL above. It works, has worked for a year or two, and I don’t know any other way I should be working. Happy to learn though!

(but of course this has set me back so much it would have been quicker not to make the tool at all)

Have a look at an ORM, if you are indeed executing plain SQL like I’m assuming from your comment. Sequelize might be nice to start with. What it does is create a layer between your application and your database. Using which, you can define the way a database object looks (like a class) and execute functions on that. For instance, if you’re creating a library, you could do book.update(), library.addBook(), etc. Since it adds a layer in between, it also helps you prevent common vulnerabilities such as SQL injection. This is because you aren’t writing the SQL queries in the first place. If you want to know more, let me know.

@drekly@lemmy.world
creator
link
fedilink
English
21Y

Thanks, I’ll look into it! I’m interested in why you got downvoted though! 😅

I didn’t downvote but some people like ideologically dislike orms. The reasons I’ve heard are usually “I can write better SQL by hand”, “I don’t want to use/learn another library”, “it has some limitations”

Those things can be true. Writing better SQL by hand definitely is a big “it depends”, though.

I can see why people might dislike them. Adds some bloat perhaps. But at the same time, I like the idea that my input is definitely sanitised since the ORM was written by people who know what they’re doing. That’s not to say it won’t have any vulnerabilities at all, but the chance of them existing is a lot lower than when I write the queries by hand. A lapse of judgement is all it takes. Even more relevant for beginning developers who might not be aware of such vulnerabilities existing.

@drekly@lemmy.world
creator
link
fedilink
English
11Y

For a personal tool that runs locally I can handle some bloat in the name of safety!

@max@feddit.nl
link
fedilink
2
edit-2
1Y

Short story, haters gonna hate ¯\_(ツ)_/¯ Long story, see my comment to the commenter below you. :)

With that amount of instruction you’ve done well

There’s probably lots of stuff you don’t even know you don’t know.

Automated testing is a big part of professional software development, for example, and helps you catch things like this issue before they go live.

@drekly@lemmy.world
creator
link
fedilink
English
1
edit-2
1Y

I’m up to 537 lines of server code, 2278 lines in my script, and 226 in my API interfacing, I’m actually super proud of it haha.

But you’re totally right, there are things I read that I just have no clue what they even mean or if I should know it, and probably use all the wrong terminology. I feel like I should probably go back to the start and find a course to teach me properly. I’ve probably learned so many bad habits. It doesn’t help that I learned JS before ES6 so I need to force myself not to use var and force myself to understand and use arrow functions.

I absolutely know that the way I’ve written the program will make some people cringe, but I don’t know any better. There are a few sections where I’m like “would that actually be what a real, commercial web app would do, or have I convoluted everything?”

For example, the entire thing is just one 129-line html file. I just hide and unhide divs when I need a new page or anything gets changed. I’m assuming that’s a bad thing, but it works, it looks good, and I don’t know any better!

Everyone has a production system. Some may even have a separate testing environment!

xepp
link
fedilink
10
edit-2
1Y

Thanks for sharing your painfull lesson. I don’t directly query DB’s a lot, but WHEN I do in the future I’ll select first and validate… Such things can happen so fast. No self hate, man!

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

Pro tip: transactions are your friend

They are - until you leave them open and go home…

Temporarily locked overnight >>> broken stuff in prod

fmstrat
link
fedilink
English
11Y

This is the way.

Completely agree, transactions are amazing for this kind of thing. In a previous team we also had a policy of always pairing if you need to do any db surgery in prod so you have a second pair of eyes + rubber duck to explain what you’re doing.

SuperFola
link
fedilink
English
271Y

There is still the journal you could use to recover the old state of your database. I assume you commited after your update query, thus you would need to copy first the journal, remove the updates from it, and reconstruct the db from the altered journal.

This might be harder than what I’m saying and heavily depends on which db you used, but if it was a transactional one it has to have a journal (not sure about nosql ones).

@drekly@lemmy.world
creator
link
fedilink
English
41Y

It is after the event that I find that postgres’ WAL journalling is off by default 🙃

WHO, WHAT, WHERE, WHEN, WHY, HOW

Tarte
link
fedilink
2
edit-2
1Y

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.

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.

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

I have done this too. Shit happens.

One of my co-workers used to write UPDATE statements backwards limit then where etc, to prevent this stuff, feels like a bit of a faff to me.

I always write it as a select, before turning it into a delete or update. I have burned myself too often already.

DacoTaco
link
fedilink
21Y

^ this is a very good tip that ive been using myself too

@drekly@lemmy.world
creator
link
fedilink
English
21Y

Oh I did that like a year ago.

And then last night had an error that led me back near this code and stupidly thought “hey it’d look neater if those numbers were in order”

Create a post

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



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog