日本語

SQL the compiler checks

sqlx's query! macros check each query against the database's tables when the program is built, and the .sqlx directory lets a machine without a database build it.

The query macros

The routes, actions, and signals the page uses are Rust types. SQL is a string that hypersig does not read, but sqlx's query! and query_as! macros check it when the program is built: each asks the database to prepare its query against the tables the migrations made, so a misspelled column, a parameter of the wrong type, or a struct field the result does not fill fails to compile. The macros need sqlx's macros feature, and migrate for sqlx::migrate!.

query_file! and query_file_as! read the query from a .sql file, so the SQL stays in files of its own. examples/postgres keeps each query under queries/ this way, and reads the list with sqlx::query_file_as!(Todo, "queries/list_todos.sql"), where Todo is the struct each row fills. In the file, id AS "id: TodoId" tells the macro to read the BIGINT column id into TodoId, a newtype of i64:

queries/list_todos.sql
SELECT id AS "id: TodoId", title, done
FROM todos
ORDER BY id

The queries of examples/database and of the Cloudflare template are plain strings, as is the one on Storing data, and a mistake in them fails when the query runs.

Checking a query again after an edit

A proc macro cannot tell cargo which files it read, so after an edit to a .sql file alone, cargo would keep the old build and its old check. The example's build.rs names queries/, migrations/, and .sqlx/ with cargo::rerun-if-changed, and an edited query is checked at the next build:

build.rs
//! Rebuilds the example when a file the sqlx macros read changes.

fn main() {
    // `query_file!` reads `queries/`, `migrate!` reads `migrations/`, and an
    // offline build reads `.sqlx/`. A proc macro cannot tell cargo about a
    // file it reads, so without these lines an edited query keeps the old
    // build and its old check.
    println!("cargo::rerun-if-changed=queries");
    println!("cargo::rerun-if-changed=migrations");
    println!("cargo::rerun-if-changed=.sqlx");
}

Building without a database

In your own project, cargo sqlx prepare writes what the macros need to the crate's .sqlx directory from a database with the migrations applied, and a build with SQLX_OFFLINE=true reads .sqlx in place of asking a database. Set it under [env] in the project's .cargo/config.toml, so a DATABASE_URL set for the server does not send the build to a database and a machine or a CI runner without one still builds:

.cargo/config.toml
[env]
SQLX_OFFLINE = "true"

A query that .sqlx does not hold then fails to compile with a message to run cargo sqlx prepare. After changing a query or a migration, run it again and commit .sqlx:

cargo install --locked sqlx-cli --no-default-features --features postgres,rustls
export DATABASE_URL=postgres://postgres:password@localhost/postgres
cargo sqlx migrate run
cargo sqlx prepare

In CI, cargo sqlx prepare --check fails when .sqlx was not written again. examples/postgres is set up this way.

Next