launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.5k stars 1.28k forks source link

Running `sqlx prepare` without Cargo #3555

Open alexkirsz opened 1 month ago

alexkirsz commented 1 month ago

Is your feature request related to a problem? Please describe.

We're using Bazel and rules_rust to build all of our Rust libraries and binaries. We don't use Cargo at all, which means that we don't have any Cargo.toml file to describe our workspace or crates, and as such cargo build cannot compile our project. Everything is defined in BUILD.bazel files instead.

Describe the solution you'd like

Provide a sqlx prepare command that doesn't require being run through Cargo, possibly taking in a .rlib instead and using a solution akin to UniFFI's metadata macros: embed the generated queries within the generated binary or library, and extract it as part of the sqlx prepare command.

Describe alternatives you've considered

Another way to fix this issue would be to provide first-class Bazel support, but that would not scale well to other cargo-less setups.

TheLortex commented 1 month ago

If I understand correctly, running rustc in "check" mode (the same way it's invoked by cargo check) with a few environment variables should be sufficient. cargo sqlx prepare invokes cargo check with SQLX_TMP, SQLX_OFFLINE set to false, and SQLX_OFFLINE_DIR points to the .sqlx folder.

So it should be possible to set up such a rule in Bazel to generate the .sqlx.