News for hackers. Stuff that matters.
Re:Why use Rust on the back end? by dijit on Monday March 20, 2023 @04:45PM (#35238420)
> I really like using Diesel because it generates all your SQL queries for you, from a typed SQL schema that it generates from your SQL migrations.

Fair that the author says that this is their opinion, however... I can't agree.

Maybe I'm too stupid, but for me SQLx is the real killer crate when it comes to databases.

Everything, from the migrate macro, to the way it validates at compile-time the types in the DB, feels (to me) very ergonomic.

Diesel, by contrast, I spent so long trying to set up and I never really understood the three different files that need to somehow be in sync and generated. I just never really grokked the split of these files and when to run the diesel-cli commands...

but SQLx is great, I often lament that there isn't anything comparable in Golang.

Re:Why use Rust on the back end? by necubi on Monday March 20, 2023 @05:36PM (#35239119)
We originally used SQLx but eventually moved away for a couple of reasons:

* The sqlx::query! macro is a neat hack, but the fact that it calls out to a database in a proc macro is pretty insane. It significantly slows down compilation with a lot of queries, and doesn't work reliably with rust-analyzer.

* Inconsistent handling of nullability that would regularly break compilation

We now use Cornucopia (https://cornucopia-rs.netlify.app/) which I think is a much better approach. You write your queries in a separate SQL file, alongside annotations that let you specify nullability. A standard build.rs integration performs codegen from that, which means compilation itself doesn't require a DB and IDE features work well. It also lets you share data types between queries which reduces boilerplate significantly.

Re:Why use Rust on the back end? by biorach on Monday March 20, 2023 @06:06PM (#35239444)
> * The sqlx::query! macro is a neat hack, but the fact that it calls out to a database in a proc macro is pretty insane.

wait... proc macros are run at compile time, right? so db queries at compile time?!

but.. but.. why?

Re:Why use Rust on the back end? by imron on Monday March 20, 2023 @06:38PM (#35239850)
> but.. but.. why?

Compile time verification that you haven't made a typo in your SQL, and that if you have updated your db schema that your code also reflects these changes.

Turning runtime errors in to compile time errors is one of Rust's superpowers.

Re:Why use Rust on the back end? by Klonoar on Monday March 20, 2023 @06:14PM (#35239550)
It's not running the queries, just sanity checking them against table structures/etc. It's actually quite nice and worth checking out.

The real issue is that in performance benchmarks Diesel beats SQLx, at least last I checked. I'm hopeful the rewrite/upgrades to SQLx that are coming shorten this gap.

Re:Why use Rust on the back end? by chatmasta on Monday March 20, 2023 @06:53PM (#35240013)
Doesn't it need to run introspection queries to check the table structures? This is still a significant overhead compared to code-only compilation, and it also requires an additional component (the database) in your build stack, which also needs to have a schema in sync with other developers and whatever deployment target to which you intend to push the compiled program. That also means that if you're building with a local copy of the DB, and deploying to a remote with its own DB, you have another source of risk for disparity between compile and runtime (I assume such an error would be caught quickly, so maybe it's not so much about additional "risk" as it is increased operational complexity).
Re:Why use Rust on the back end? by hdevalence on Monday March 20, 2023 @07:40PM (#35240486)
Yes, that’s exactly the issue. You trade that friction for not having the friction of type errors in queries at runtime. Whether that tradeoff is a good one depends on your situation and preferences.

(You can also check in the results of the database queries, so you only need the db in the build stack when you touch db-related code).

Re:Why use Rust on the back end? by winrid on Monday March 20, 2023 @07:05PM (#35240106)
I mean, the db schema has to stay in sync with the code no matter what right?

This is just ensuring it is valid at compile time.

On deploy you just run your migrations first. Obviously you have to do some planning.

It's a major productivity boost honestly, after building a semi large app with it.

And yes, it does actually run the queries. It populates dummy data and runs it in a transaction from what I can tell.

Re:Why use Rust on the back end? by preseinger on Monday March 20, 2023 @11:02PM (#35242066)
The .sql files or migrations or whatever else in your repo represents an intent, or contract, defined by your application. But those files aren't the source of truth, the database is the authority. Whether a SQL query is or is not valid is, by definition, something that can only be evaluated at runtime.
Re:Why use Rust on the back end? by winrid on Tuesday March 21, 2023 @01:09AM (#35242771)
Yes, with sqlx, the migrations must be the source of truth for your DB schema. You cannot (ideally), manage them elsewhere. This is why the sqlx migration tool is so good, you kinda have to use it.

It's honestly very nice. The code, my migrations, my DB browser, all in one window in my IDE.

And the query is validated by running it, at compile time, by inserting mock inputs inside a transaction. It works really well.

Even if you disagree that the DB must be the source of authority and it can differ, the development boon of the process is worth the 5% chance there is some issue later, which can probably be easily resolved.

Re:Why use Rust on the back end? by preseinger on Tuesday March 21, 2023 @04:14AM (#35243657)
a .sql migration file is like a .tf terraform file, it describes intent

your infrastructure is not that .tf definition, it's whatever is currently deployed to ec2

in the same way, your schema is not that .sql file, it's whatever the database says it is :)

Re:Why use Rust on the back end? by winrid on Tuesday March 21, 2023 @11:31AM (#35247646)
I don't really get your point. Yes, the db validates the requests. But that's based off the schema. The schema is determined from the migrations. You run the new migrations before any deploy.

You can keep pushing this argument but it doesn't mean anything in any practical sense.

Re:Why use Rust on the back end? by preseinger on Sunday March 26, 2023 @12:12AM (#35310846)
my point is basically this: when the code in your application interacts with the DB, and it encounters a (runtime) error from a mismatch between the app's understanding of the DB schema and the DB's understanding of the DB schema, is that error handled gracefully? if yes, good! if no, because the app code assumes this is somehow impossible, that's a problem. that's all.
Re:Why use Rust on the back end? by imron on Tuesday March 21, 2023 @12:22AM (#35242525)
> Doesn't it need to run introspection queries to check the table structures?

Yes, but it can also run in 'offline' mode where it builds against saved metadata [0]

0: https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/READM...

Re:Why use Rust on the back end? by lars_francke on Monday March 20, 2023 @06:17PM (#35239587)
So compilation can fail if your SQL query doesn't match what's actually in the database.

The idea is kinda neat but....

Re:Why use Rust on the back end? by sp0ck on Monday March 20, 2023 @07:12PM (#35240183)
FYI: You can disable SQLx compile time db verification since macros support an offline mode.
Re:Why use Rust on the back end? by necubi on Monday March 20, 2023 @08:13PM (#35240769)
This sort of works, but is finnicky to set up in each environment and introduces the risk that you forget to update it and your queries are broken.
Re:Why use Rust on the back end? by winrid on Tuesday March 21, 2023 @01:17AM (#35242797)
Cornucopia looks cool.

Regarding your second bullet point, can you elaborate more? I haven't seen this. You have to explicitly use Option<> for non null columns but that's what I expect.

Re:Why use Rust on the back end? by Pepe1vo on Monday March 20, 2023 @05:02PM (#35238663)
Maybe I'm holding it wrong, but my experience with sqlx is a bit more lukewarm. It's really great that compilation success means that you didn't fuck up any queries, but... there's just SO much boilerplate. I find that due to Rust's extremely strict type system, I have to create dedicated record structs for all of my domain models representing how a model is stored in the database. So once everything's up and running it almost always implies there's essentially no bugs, but man is getting there a drag.

Also, testing is quite annoying too. If you're using sqlite, you can use in-memory mode and that's quite nice. But for other databases you basically have only two options: 1. setup and teardown an actual testing db 2. wrap your sqlx interfacing code in something vaguely resembling the repository pattern and create an in-memory implementation for testing. I'd call both of these options less than ideal.

Re:Why use Rust on the back end? by nightpool on Monday March 20, 2023 @05:39PM (#35239152)
> But for other databases you basically have only two options: 1. setup and teardown an actual testing db 2. wrap your sqlx interfacing code in something vaguely resembling the repository pattern and create an in-memory implementation for testing. I'd call both of these options less than ideal.

Why not just BEGIN a transaction and then ROLLBACK it after every test case? That's what every other significant backend codebase (using SQL) that I've worked on does. Maybe that's what you meant by #1? Not sure why you'd want to avoid it then—it will help significantly in testing out any piece of code you have that's even slightly outside of the CRUD norms. What does Diesel do, reimplement a completely separate "testing" version for all of its SQL queries?

Re:Why use Rust on the back end? by weiznich on Tuesday March 21, 2023 @03:15AM (#35243335)
Just using a transaction that will be rolled back after each test case is exactly what diesel suggests. There is a separate function for this on diesels connection trait[1]

[1]: https://docs.diesel.rs/2.0.x/diesel/connection/trait.Connect...

Re:Why use Rust on the back end? by zamnos on Monday March 20, 2023 @05:11PM (#35238803)
The question is what's the cost of a bug? Lost hours for customer support to collect user reports (which take some time and usually multiple reports before being believed), time spend by CSRs to individually, time by the dev or ops team to fix the affected accounts en-masse, more time by the CSRs to manually fix up affected accounts and give them individual credits where credits are due (which may be manual and thus error-prone process), and finally time spent by the dev team to identify and fix the bug for future users, which is time spent not adding new features or paying down other tech-debt. That's not to say using scripting languages isn't necessarily worth it for increased speed of development, but the time spent above may be impossible to quantify in aggregate. If you're in a low stakes environment, where a bug costs you single digit basis points/bips, not a problem. If you're somewhere like crypto where a bug that an attacker discovered ends up costing the exchange, your employer, 9-figures, or worse, the medical or aerospace industry where bug(s) literally costs people's lives, then avoiding bugs is of paramount importance.

The other option is avoid such a high stakes environment, but someones got to be in those fields.

Re:Why use Rust on the back end? by preseinger on Monday March 20, 2023 @11:11PM (#35242110)
The database schema is owned by the database, the types in your program are owned by your program, these two things change over time independent of each other, mapping from schema to type is an inherently fallible operation, success can only be determined at runtime, type checking SQL queries against .sql definitions at compile time can be a sort of optimization but doesn't provide any kind of runtime guarantee, and you gotta handle syscall errors gracefully anyway if you want your program to be anything close to reliable, so it's all a bit moot I guess
Re:Why use Rust on the back end? by adamch on Monday March 20, 2023 @04:49PM (#35238463)
Hello, author here. My coworker Olivia (of https://losslessbits.com/) really likes SQLx, I haven't personally used it though. I'd love to try it for a future project, as long as it can help typecheck my SQL queries.
Re:Why use Rust on the back end? by Thaxll on Monday March 20, 2023 @06:09PM (#35239487)
Why don't you use sqlc + pgx? What's missing from the two?

https://github.com/kyleconroy/sqlc

https://github.com/jackc/pgx

Re:Why use Rust on the back end? by jeremychone on Tuesday March 21, 2023 @12:02AM (#35242408)
Agree, for our approach, sqlx has the right level of abstraction. Not too high, not too low, and close enough of a sql builder pattern to be very useful. (We are not using the compile time type validation though).
Re:Why use Rust on the back end? by davidatbu on Tuesday March 21, 2023 @08:12AM (#35245131)
Personally, what I'm looking for from an ORM is composability and type safety. To my knowledge, it's impossible with sqlx, for e.g., to conditionally add a WHERE clause, and have compile time checked queries. With diesel, it is possible.

I 100% agree with your point about Diesel's dev experience leaving a lot to be desired when it comes to docs and ease-of-learning though. But to me, that is a straightforward problem to solve when compared to the fundamental limitation of sqlx that I described above.

Re:Why use Rust on the back end? by weiznich on Tuesday March 21, 2023 @08:28AM (#35245267)
Please reach out on the diesel discussion forum[1] about the lacking dev experience. I'm happy to discuss these issues and potential solutions there.

[1] https://github.com/diesel-rs/diesel/discussions

Re:Why use Rust on the back end? by davidatbu on Thursday March 23, 2023 @02:00AM (#35270961)
Thank you weiznich! Tbh, it's really hard to come up with concrete proposals to solve these problems, and I still haven't figured out how much of the "not-super-easy-to-learn" issue with Diesel is inherent complexity that can't be avoided if one wants to have runtime-constructed-and-yet-compile-time-checked db queries in Rust.

I think it will require some thought. I hope that, once I get around to using diesel in a second substantial project (already using it for my first), I will write up my thoughts on how to make DX better for diesel.