SQLx
Postgres, MySQL and SQLite pools from ulo-db-sqlx, with the options, features and TLS behaviour of the store.
ulo-db-sqlx binds SQLx 0.9's own pool: sqlx::PgPool, MySqlPool or SqlitePool. It needs Rust 1.94, the floor SQLx declares.
[dependencies]
ulo-db-sqlx = { version = "=0.1.0-alpha.3", features = ["postgres"] }
# Only to switch on `macros`, for `sqlx::migrate!`.
sqlx = { version = "0.9", features = ["macros"] }The feature flags are postgres, mysql and sqlite, which select engines, and tls, on by default. tls enables SQLx's tls-rustls-ring-native-roots: rustls over ring with the platform's roots. mysql-rsa is not enabled, since over TLS MySQL's caching_sha2_password authenticates without RSA key exchange.
sqlx::migrate! expands to paths that resolve only in a crate that depends on sqlx under that name, so the application depends on sqlx directly with features = ["macros"]. Cargo unifies that with the sqlx that ulo-db-sqlx re-exports as ulo_db_sqlx::sqlx, so the build holds one SQLx. Import SQLx's types through that re-export:
use ulo_db_sqlx::sqlx::{self, PgPool};
use ulo_db_sqlx::{Postgres, SqlxModule};ulo-db-seaorm shares the same SQLx. A second copy of SQLx in the build is a different type with the same path, which Dep<PgPool> cannot find. wire() reports it as a missing binding whose help names the same type from two versions of its crate. cargo tree -d lists the copies.
Options
The section the module reads contains:
| Key | Default | Meaning |
|---|---|---|
url | required | postgres://user:password@host/database, mysql://..., or sqlite: followed by a path or sqlite::memory:. A Secret. |
max_connections | SQLx's 10 | Pool size. |
min_connections | 0 | The pool is lazy, so a minimum is filled in the background and never delays the start. |
acquire_timeout_secs | 5 | The wait for a pooled connection. It is also the length of one connect attempt against an unreachable server. |
max_connections must be at least 2 when migrations run on Postgres or MySQL, because the lock holds one connection.
The store checks the URL's scheme itself, since SQLx's parser accepts any, then parses it. A failure names the key and prints none of the URL.
SQLx opens a SQLite file read-write and does not create it. sqlite:data.db?mode=rwc creates a missing file. Without rwc a missing file fails the first ping, and the failure is permanent.
TLS
MySQL over TLS is exercised by the crate's tests: a certificate issued by a test CA, ssl-mode=verify_identity and ssl-ca naming that CA, with a different CA and a plaintext connect refused. Postgres over TLS is not exercised by any test, and SQLite has none.
The store reads no environment of its own. The root certificate comes through the URL, which configuration can fill from its Env source. SQLx's option parser reads the libpq variables PGSSLROOTCERT, PGSSLCERT and PGSSLKEY itself. A certificate file that is missing or unreadable over a host and port is a permanent failure at the first ping, whatever the source of the path.
rumqttc brings aws-lc-rs, so an application that links ulo-rpc-mqtt links two crypto providers whatever this crate enables.
A connection moved off the runtime
A PoolConnection returns to its pool through a task SQLx spawns in its Drop, which needs a tokio context. A connection dropped on a thread without one, such as a plain std::thread, panics with "this functionality requires a Tokio context". If another crate in the build compiles in SQLx's runtime-smol, the return task goes to smol's global executor instead, which starts threads the application did not ask for. Drop the connection on a tokio thread, or hold it in a task spawned on the app's runtime.
Authentication cost
SQLx's Postgres driver derives its SCRAM key on the thread that polls the connect. Measured against postgres:18 with scram-sha-256, the longest gap between two ticks of a 5 ms ticker on a one-thread runtime was 6.6 to 7.6 ms, so the derivation costs the thread about 2 ms.
Run the example
docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=pw postgres:18
APP_DATABASE__URL=postgres://postgres:pw@127.0.0.1/postgres APP_DATABASE__MAX_CONNECTIONS=5 PORT=7000 cargo run -p ex-35-db-sqlx
curl -d '{"title":"first"}' -H 'content-type: application/json' http://127.0.0.1:7000/notes