Austin Bonander 25cbeedab4
feat: create sqlx.toml format (#3383)
* feat: create `sqlx.toml` format

* feat: add support for ignored_chars config to sqlx_core::migrate

* chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)

https://en.wikipedia.org/wiki/Byte_order_mark

* refactor: make `Config` always compiled

simplifies usage while still making parsing optional for less generated code

* refactor: add origin information to `Column`

* feat(macros): implement `type_override` and `column_override` from `sqlx.toml`

* refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates`

* feat: make macros aware of `macros.preferred-crates`

* feat: make `sqlx-cli` aware of `database-url-var`

* feat: teach macros about `migrate.table-name`, `migrations-dir`

* feat: teach macros about `migrate.ignored-chars`

* chore: delete unused source file `sqlx-cli/src/migration.rs`

* feat: teach `sqlx-cli` about `migrate.defaults`

* feat: teach `sqlx-cli` about `migrate.migrations-dir`

* feat: teach `sqlx-cli` about `migrate.table-name`

* feat: introduce `migrate.create-schemas`

* WIP feat: create multi-tenant database example

* fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements

* feat: progress on axum-multi-tenant example

* feat(config): better errors for mislabeled fields

* WIP feat: filling out axum-multi-tenant example

* feat: multi-tenant example

No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.

* chore(ci): test multi-tenant example

* fixup after merge

* fix(ci): enable `sqlx-toml` in CLI build for examples

* fix: CI, README for `multi-tenant`

* fix: clippy warnings

* fix: multi-tenant README

* fix: sequential versioning inference for migrations

* fix: migration versioning with explicit overrides

* fix: only warn on ambiguous crates if the invocation relies on it

* fix: remove unused imports

* fix: doctest

* fix: `sqlx mig add` behavior and tests

* fix: restore original type-checking order

* fix: deprecation warning in `tests/postgres/macros.rs`

* feat: create postgres/multi-database example

* fix: examples/postgres/multi-database

* fix: cargo fmt

* chore: add tests for config `migrate.defaults`

* fix: sqlx-cli/tests/add.rs

* feat(cli): add `--config` override to all relevant commands

* chore: run `sqlx mig add` test with `RUST_BACKTRACE=1`

* fix: properly canonicalize config path for `sqlx mig add` test

* fix: get `sqlx mig add` test passing

* fix(cli): test `migrate.ignored-chars`, fix bugs

* feat: create `macros.preferred-crates` example

* fix(examples): use workspace `sqlx`

* fix: examples

* fix(sqlite): unexpected feature flags in `type_checking.rs`

* fix: run `cargo fmt`

* fix: more example fixes

* fix(ci): preferred-crates setup

* fix(examples): enable default-features for workspace `sqlx`

* fix(examples): issues in `preferred-crates`

* chore: adjust error message for missing param type in `query!()`

* doc: mention new `sqlx.toml` configuration

* chore: add `CHANGELOG` entry

Normally I generate these when cutting the release, but I wanted to take time to editorialize this one.

* doc: fix new example titles

* refactor: make `sqlx-toml` feature non-default, improve errors

* refactor: eliminate panics in `Config` read path

* chore: remove unused `axum` dependency from new examples

* fix(config): restore fallback to default config for macros

* chore(config): remove use of `once_cell` (to match `main`)
2025-06-30 16:34:46 -07:00

121 lines
4.2 KiB
Rust

use accounts::AccountsManager;
use color_eyre::eyre;
use color_eyre::eyre::{Context, OptionExt};
use payments::PaymentsManager;
use rand::distributions::{Alphanumeric, DistString};
use sqlx::Connection;
#[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install()?;
let _ = dotenvy::dotenv();
tracing_subscriber::fmt::init();
let mut conn = sqlx::PgConnection::connect(
// `env::var()` doesn't include the variable name in the error.
&dotenvy::var("DATABASE_URL").wrap_err("DATABASE_URL must be set")?,
)
.await
.wrap_err("could not connect to database")?;
let accounts = AccountsManager::setup(
dotenvy::var("ACCOUNTS_DATABASE_URL")
.wrap_err("ACCOUNTS_DATABASE_URL must be set")?
.parse()
.wrap_err("error parsing ACCOUNTS_DATABASE_URL")?,
1,
)
.await
.wrap_err("error initializing AccountsManager")?;
let payments = PaymentsManager::setup(
dotenvy::var("PAYMENTS_DATABASE_URL")
.wrap_err("PAYMENTS_DATABASE_URL must be set")?
.parse()
.wrap_err("error parsing PAYMENTS_DATABASE_URL")?,
)
.await
.wrap_err("error initializing PaymentsManager")?;
// For simplicity's sake, imagine each of these might be invoked by different request routes
// in a web application.
// POST /account
let user_email = format!("user{}@example.com", rand::random::<u32>());
let user_password = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
// Requires an externally managed transaction in case any application-specific records
// should be created after the actual account record.
let mut txn = conn.begin().await?;
let account_id = accounts
// Takes ownership of the password string because it's sent to another thread for hashing.
.create(&user_email, user_password.clone())
.await
.wrap_err("error creating account")?;
txn.commit().await?;
println!(
"created account ID: {}, email: {user_email:?}, password: {user_password:?}",
account_id.0
);
// POST /session
// Log the user in.
let session = accounts
.create_session(&user_email, user_password.clone())
.await
.wrap_err("error creating session")?;
// After this, session.session_token should then be returned to the client,
// either in the response body or a `Set-Cookie` header.
println!("created session token: {}", session.session_token.0);
// POST /purchase
// The client would then pass the session token to authenticated routes.
// In this route, they're making some kind of purchase.
// First, we need to ensure the session is valid.
// `session.session_token` would be passed by the client in whatever way is appropriate.
//
// For a pure REST API, consider an `Authorization: Bearer` header instead of the request body.
// With Axum, you can create a reusable extractor that reads the header and validates the session
// by implementing `FromRequestParts`.
//
// For APIs where the browser is intended to be the primary client, using a session cookie
// may be easier for the frontend. By setting the cookie with `HttpOnly: true`,
// it's impossible for malicious Javascript on the client to access and steal the session token.
let account_id = accounts
.auth_session(&session.session_token.0)
.await
.wrap_err("error authenticating session")?
.ok_or_eyre("session does not exist")?;
let purchase_amount: rust_decimal::Decimal = "12.34".parse().unwrap();
// Then, because the user is making a purchase, we record a payment.
let payment = payments
.create(account_id, "USD", purchase_amount)
.await
.wrap_err("error creating payment")?;
println!("created payment: {payment:?}");
let purchase_id = sqlx::query_scalar!(
"insert into purchase(account_id, payment_id, amount) values ($1, $2, $3) returning purchase_id",
account_id.0,
payment.payment_id.0,
purchase_amount
)
.fetch_one(&mut conn)
.await
.wrap_err("error creating purchase")?;
println!("created purchase: {purchase_id}");
conn.close().await?;
Ok(())
}