larust-orm 0.12.0

A Laravel-shaped web framework for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use larust_core::AppError;
use sqlx::any::{AnyConnectOptions, AnyPoolOptions};
use sqlx::AnyPool;
use std::future::Future;
use std::str::FromStr;
use std::sync::OnceLock;

static POOL: OnceLock<AnyPool> = OnceLock::new();
static BACKEND: OnceLock<Backend> = OnceLock::new();

/// Which real database engine `DATABASE_URL` selected - set once, inside
/// [`connect`], by reading the URL's own scheme (`sqlite://` vs
/// `mysql://`). `larust-orm` deliberately doesn't ask `sqlx` this
/// (`sqlx::any::AnyKind` is `#[deprecated = "not used or returned by any
/// API"]` in 0.8) - it already knows the answer the moment it parses the
/// URL to connect, so it just remembers it.
///
/// Every other framework crate that needs to emit backend-specific SQL
/// (an `AUTOINCREMENT` vs `AUTO_INCREMENT` `CREATE TABLE`, `INSERT OR
/// IGNORE` vs `INSERT IGNORE`, ...) branches on [`backend`] rather than
/// each reimplementing its own detection - see
/// `larust-permissions`/`larust-sanctum`/`larust-notifications`/
/// `larust-queue`/`larust-cache` for the pattern.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
    Sqlite,
    MySql,
    Postgres,
}

tokio::task_local! {
    /// Set only by `larust_testing::test_transaction` (via
    /// `with_pool_override`) - everywhere else this is simply never set,
    /// and `pool()` falls through to the process-wide `POOL` exactly as
    /// before. Task-local, not process-wide: unlike `POOL` itself, this
    /// doesn't need "first writer wins" semantics, since each test that
    /// uses it sets its own value in its own task, isolated from every
    /// other task doing the same thing concurrently.
    static POOL_OVERRIDE: &'static AnyPool;
    /// The pool-override's own backend - always scoped together with
    /// [`POOL_OVERRIDE`] by [`with_pool_override`], never independently.
    /// Closes a real correctness gap `POOL_OVERRIDE` alone didn't: before
    /// this existed, [`backend`] always answered from the process-wide
    /// [`BACKEND`] `OnceLock` regardless of which pool a given task was
    /// actually routed to via `POOL_OVERRIDE` - harmless as long as every
    /// `with_pool_override` call in a process happens to use the *same*
    /// backend `BACKEND` was first set to (true of every caller today,
    /// `test_transaction`'s own `connect_isolated` is SQLite-only), but a
    /// real, silent-wrong-SQL bug waiting for the first caller that
    /// doesn't - e.g. a future desktop-app DB-embed flow overriding to a
    /// different backend than whatever the app's own `connect()` first
    /// established. Found and documented, not yet exercised by a real
    /// caller before this fix - closed proactively rather than waiting for
    /// it to actually break something.
    static BACKEND_OVERRIDE: Backend;
}

/// Connects to the database and stores the pool process-wide (same
/// `OnceLock` pattern as `larust-http`'s route-name registry). Call once
/// at startup, after config/`.env` has been loaded.
///
/// Builds an `sqlx::any::AnyPool` (runtime-generic - dispatches to
/// whichever real driver `database_url`'s scheme selects) rather than a
/// concretely-typed `SqlitePool`/`MySqlPool`, so every other framework
/// crate and every `#[derive(Model)]` struct works unchanged regardless
/// of which backend a given app actually runs against - see
/// `docs/ARCHITECTURE.md`'s database-backends section.
///
/// `AnyConnectOptions` is deliberately opaque (just a parsed `Url` -
/// confirmed by reading `sqlx-core`'s own source, it exposes no builder
/// methods for engine-specific settings like SQLite's WAL mode), so the
/// SQLite-only tuning this crate has always applied (WAL journal mode,
/// a 5s busy timeout so brief lock contention waits instead of failing
/// immediately, foreign keys on) can't be set through it directly.
/// `PoolOptions::after_connect` - generic over any backend, called once
/// per new physical connection - is the portable way to apply it: a
/// plain `PRAGMA` statement, only ever run when [`backend`] is actually
/// `Sqlite` (running a SQLite `PRAGMA` against a MySQL connection would
/// error). `create_if_missing(true)`'s equivalent is SQLite's own
/// `?mode=rwc` URL query parameter (confirmed against
/// `sqlx-sqlite`'s own URL parser) - appended automatically here so an
/// app's `.env` doesn't need to change to keep today's "just works on
/// first run" behavior.
pub async fn connect(database_url: &str) -> Result<(), AppError> {
    sqlx::any::install_default_drivers();

    let backend = ensure_backend(database_url)?;

    // `sqlx::any::AnyConnectOptions::from_str` parses `database_url` with
    // the `url` crate's strict RFC 3986 parser (confirmed by reading
    // `sqlx-core`'s source) - unlike the old, SQLite-specific
    // `SqliteConnectOptions::from_str` this crate used before adding
    // MySQL support, which treated everything after `sqlite://` as a raw
    // filesystem path via plain string splitting and never went through a
    // URL parser at all. A Windows *absolute* path handed to `.display()`
    // (backslash separators, a `C:` drive letter right after `sqlite://`)
    // parses under RFC 3986 without erroring, but wrongly - `//` starts an
    // "authority" component, so `C:` gets read as `host:port`-shaped
    // (confirmed empirically: without this fix, the resulting connection
    // fails with SQLite's own "unable to open database file", meaning the
    // driver received a mangled path). Rewriting to `sqlite:///C:/...`
    // (three slashes: an explicitly *empty* authority followed by an
    // absolute path starting with `/`) sidesteps the ambiguity entirely -
    // the standard way any URL scheme represents "no host, absolute path"
    // (`file:///...` follows the identical convention). A *relative*
    // SQLite path (the common case - `sqlite://database/database.sqlite`,
    // this codebase's own scaffold default) is left alone beyond backslash
    // normalization: forcing a leading `/` onto it would silently turn it
    // into an absolute path rooted at the filesystem root, a real
    // behavioral change, not a parsing fix. This regression would
    // otherwise hit every framework crate's own `sqlite://{tempdir}/
    // test.sqlite`-shaped test helper (`larust-testing`, `larust-queue`,
    // `larust-permissions`, ...) on every Windows dev machine, since
    // `tempfile::tempdir()` always returns this exact absolute-path shape.
    let normalized;
    let database_url = match backend {
        Backend::Sqlite => {
            normalized = normalize_sqlite_url(database_url);
            normalized.as_str()
        }
        Backend::MySql | Backend::Postgres => database_url,
    };

    let connect_url = match backend {
        Backend::Sqlite if !database_url.contains('?') => {
            format!("{database_url}?mode=rwc")
        }
        Backend::Sqlite => format!("{database_url}&mode=rwc"),
        Backend::MySql | Backend::Postgres => database_url.to_string(),
    };
    let options =
        AnyConnectOptions::from_str(&connect_url).map_err(|e| AppError::Config(Box::new(e)))?;

    // SQLite gets exactly one connection; MySQL/Postgres get a real pool.
    // SQLite has exactly one writer lock for the whole database file,
    // period - WAL mode (below) lets readers avoid blocking on a writer
    // and vice versa, but it never lets two writers proceed at once, no
    // matter how many separate connections a pool hands out. A 10-
    // connection pool against SQLite doesn't buy any write concurrency;
    // it just means up to 10 connections can simultaneously discover the
    // lock is held and enter SQLite's own `busy_timeout` retry loop at
    // once - a real, reproduced production failure mode: a session-write-
    // heavy app saw `sqlx::query`'s own "slow statement" warnings climb
    // into the 5-17s range and then hard `(code: 5) database is locked`
    // errors once write volume was high enough, which a single, always-
    // uncontended connection (queued fairly through sqlx's own async
    // `PoolConnection` acquire instead of SQLite's blocking busy-handler)
    // doesn't hit at all - there's nothing else to contend with. This is a
    // well-established sqlx+SQLite pattern, not a Larust-specific
    // workaround. `busy_timeout` stays set regardless (see below) - it's
    // still real protection against a genuinely separate process (e.g. a
    // zero-downtime restart handoff's outgoing and incoming processes
    // both briefly holding a connection to the same file) contending for
    // the lock, which one connection *within* this process can't prevent.
    let max_connections = match backend {
        Backend::Sqlite => 1,
        Backend::MySql | Backend::Postgres => 10,
    };

    let pool = AnyPoolOptions::new()
        .min_connections(1)
        .max_connections(max_connections)
        .after_connect(move |conn, _meta| {
            Box::pin(async move {
                // `conn.execute(&str)` (the `Executor` trait method,
                // called directly on the connection - matching the exact
                // pattern sqlx's own `tests/any/pool.rs` uses in an
                // `after_connect` closure) accepts multiple `;`-separated
                // statements in one call, unlike `sqlx::query(...)`, which
                // only ever prepares one.
                use sqlx::Executor;
                match backend {
                    Backend::Sqlite => {
                        conn.execute(
                            "PRAGMA journal_mode = WAL; \
                             PRAGMA busy_timeout = 5000; \
                             PRAGMA foreign_keys = ON;",
                        )
                        .await?;
                    }
                    Backend::MySql => {
                        // Every identifier throughout this codebase - the
                        // query builder, `#[derive(Model)]`'s generated
                        // SQL, every ecosystem crate's own queries - is
                        // double-quoted (`"table"`, `"column"`), the
                        // ANSI-SQL/SQLite convention. MySQL's *default*
                        // `sql_mode` instead treats a double-quoted token
                        // as a string literal, not an identifier - so
                        // without this, every one of those queries would
                        // fail outright against a stock MySQL server.
                        // `ANSI_QUOTES` makes MySQL accept the same
                        // double-quote convention SQLite already uses,
                        // avoiding a rewrite of every quoted identifier in
                        // the framework. Appended via `CONCAT` (not a bare
                        // `SET sql_mode = 'ANSI_QUOTES'`) so it adds to
                        // the session's existing modes rather than
                        // silently dropping MySQL's own sane defaults
                        // (`STRICT_TRANS_TABLES`, etc.).
                        conn.execute("SET SESSION sql_mode = CONCAT(@@sql_mode, ',ANSI_QUOTES')")
                            .await?;
                    }
                    Backend::Postgres => {
                        // No session pragma needed - double-quoted
                        // identifiers (`"table"`, `"column"`) are
                        // Postgres's own native standard-SQL convention,
                        // unlike MySQL's non-standard default `sql_mode`
                        // above. Every query this framework emits already
                        // quotes identifiers that way, so Postgres accepts
                        // them with zero configuration.
                    }
                }
                Ok(())
            })
        })
        .connect_with(options)
        .await
        .map_err(|e| AppError::Internal(Box::new(e)))?;

    POOL.set(pool).map_err(|_| {
        AppError::Internal(Box::new(std::io::Error::other(
            "connect() called more than once",
        )))
    })?;

    Ok(())
}

/// This app's database backend - see [`Backend`]. Checks the task-local
/// override first (set only inside `with_pool_override`, alongside
/// `POOL_OVERRIDE`), then falls back to the process-wide [`BACKEND`] -
/// the identical two-step [`pool`] itself already uses for `POOL`/
/// `POOL_OVERRIDE`, kept consistent for the same reason: code that
/// branches on `backend()` to decide which dialect of SQL to emit must
/// agree with whichever pool `pool()` actually resolves to in the same
/// task, not whatever backend happened to connect first in the process.
/// Panics if neither is set, matching `larust_core::config()`'s own "real
/// caller-contract violation" reasoning (nothing before `connect()`/
/// `with_pool_override` could plausibly need to know the backend either).
pub fn backend() -> Backend {
    if let Ok(overridden) = BACKEND_OVERRIDE.try_with(|backend| *backend) {
        return overridden;
    }

    *BACKEND
        .get()
        .expect("larust_orm::backend() called before connect()")
}

/// Sets [`BACKEND`] from `database_url`'s scheme, without touching the
/// process-wide [`POOL`] - unlike [`connect`], safe to call more than
/// once per process (every call after the first is a harmless no-op, the
/// same `.ok()`-swallowed idempotency [`connect`] itself already gives
/// `BACKEND`). For `larust_testing::test_transaction`'s own
/// `connect_isolated`, which deliberately builds its own dedicated pool
/// *outside* the process-wide singleton (see that function's own doc
/// comment for why) but still needs `backend()` to resolve correctly for
/// whatever it runs - every framework crate's own bootstrap SQL branches
/// on it.
pub fn ensure_backend(database_url: &str) -> Result<Backend, AppError> {
    let backend = parse_backend(database_url)?;
    BACKEND.set(backend).ok();
    Ok(backend)
}

/// Rewrites a `sqlite:` URL so `sqlx::any::AnyConnectOptions::from_str`'s
/// strict RFC 3986 parsing (via the `url` crate) resolves it the same way
/// the old, lenient, SQLite-specific `SqliteConnectOptions::from_str` this
/// crate used before adding MySQL support always did - see [`connect`]'s
/// own doc comment for the full explanation of *why* this is needed at
/// all (in short: a Windows absolute path's `C:` drive letter, sitting
/// right after `sqlite://`, gets misread as a `host:port`-shaped URL
/// authority component otherwise).
///
/// Two independent fixes, both SQLite-only:
/// - Backslash path separators become forward slashes (accepted by every
///   SQLite/Windows filesystem API too, and never ambiguous with anything
///   URL syntax uses).
/// - A path that looks like a Windows drive-absolute path (`C:/...`) gets
///   an explicit empty authority inserted (`sqlite:///C:/...`, three
///   slashes) - the standard "no host, absolute path" URL convention
///   (`file:///...` follows the identical shape). A *relative* path
///   (`sqlite://database/database.sqlite`, this codebase's own scaffold
///   default) is deliberately left as a two-slash URL: forcing a leading
///   `/` onto it would silently turn it into a path rooted at the
///   filesystem root, a real behavior change, not a parsing fix.
pub fn normalize_sqlite_url(database_url: &str) -> String {
    let forward_slashes = database_url.replace('\\', "/");
    let path = forward_slashes
        .strip_prefix("sqlite://")
        .unwrap_or(&forward_slashes);
    let is_windows_drive_absolute = path.as_bytes().first().is_some_and(u8::is_ascii_alphabetic)
        && path.as_bytes().get(1) == Some(&b':');
    if is_windows_drive_absolute {
        format!("sqlite:///{path}")
    } else {
        forward_slashes
    }
}

/// One placeholder for `backend` at 1-based position `n` in left-to-right
/// order across a statement - `"?"` for SQLite/MySQL, `"$n"` for Postgres
/// (confirmed by reading `sqlx-core`'s own doc comment: `sqlx::Any` does
/// **not** rewrite placeholders between dialects, so every framework crate
/// with a fixed, statically-known placeholder count in a hand-written SQL
/// string uses this instead of a bare `"?"` literal - `larust-cache`,
/// `larust-queue`, `larust-sanctum`, `larust-permissions`,
/// `larust-notifications`, `larust-http::session`, and this crate's own
/// `migrate.rs` all do. `QueryBuilder`'s own placeholder rendering
/// (`query_builder.rs`) has a genuinely *dynamic* count instead - a running
/// counter threaded through its own condition renderer - and doesn't use
/// this helper, but follows the identical `?`/`$n` split.
pub fn placeholder(backend: Backend, n: usize) -> String {
    match backend {
        Backend::Sqlite | Backend::MySql => "?".to_string(),
        Backend::Postgres => format!("${n}"),
    }
}

fn parse_backend(database_url: &str) -> Result<Backend, AppError> {
    if database_url.starts_with("sqlite:") {
        Ok(Backend::Sqlite)
    } else if database_url.starts_with("mysql:") {
        Ok(Backend::MySql)
    } else if database_url.starts_with("postgres:") || database_url.starts_with("postgresql:") {
        Ok(Backend::Postgres)
    } else {
        Err(AppError::Config(Box::new(std::io::Error::other(format!(
            "unsupported DATABASE_URL scheme in {database_url:?} - expected \
             \"sqlite:\", \"mysql:\", \"postgres:\", or \"postgresql:\""
        )))))
    }
}

/// Returns the pool every `#[derive(Model)]` method and `QueryBuilder`
/// call resolves its connection through - this single resolution point
/// (not a parameter threaded through every generated method) is what
/// makes `with_pool_override` below work at all. Checks the task-local
/// override first (set only inside `larust_testing::test_transaction`),
/// then falls back to the process-wide pool. Errors (rather than panics)
/// if neither is set - a misconfigured startup order is a real
/// possibility (e.g. a route handler running before `main` finishes
/// wiring up the database), not a truly unreachable state.
pub fn pool() -> Result<&'static AnyPool, AppError> {
    if let Ok(overridden) = POOL_OVERRIDE.try_with(|pool| *pool) {
        return Ok(overridden);
    }

    POOL.get().ok_or_else(|| {
        AppError::Internal(Box::new(std::io::Error::other(
            "database not connected; call larust_orm::connect() \
             (via larust_support::orm::connect) at startup before serving requests",
        )))
    })
}

/// Runs `fut` with `pool` (and `backend`, its own [`backend`] override -
/// always scoped together, never independently, so [`pool`] and
/// [`backend`] can never disagree about which database `fut` is actually
/// talking to) resolved by every `pool()`/`backend()` call made from
/// within it - and from anything it directly `.await`s, since a
/// `tokio::task_local!` is visible throughout one task's execution. A
/// future `fut` hands off to `tokio::spawn` as a *separate* detached
/// task would **not** see this override (spawned tasks don't inherit
/// their parent's task-locals) - confirmed nothing in `larust-orm`'s or
/// `larust-macros`' generated code does that (`grep` for `tokio::spawn`/
/// `join_all` in both turns up nothing), so this is safe for every
/// existing `#[derive(Model)]`/`QueryBuilder` call path today.
///
/// Used by `larust_testing::test_transaction`; not meant for application
/// code - there is deliberately no equivalent re-exported through
/// `larust_support::orm`.
pub async fn with_pool_override<F: Future>(
    pool: &'static AnyPool,
    backend: Backend,
    fut: F,
) -> F::Output {
    POOL_OVERRIDE
        .scope(pool, BACKEND_OVERRIDE.scope(backend, fut))
        .await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_backend_recognizes_sqlite() {
        assert_eq!(
            parse_backend("sqlite://database/database.sqlite").unwrap(),
            Backend::Sqlite
        );
    }

    #[test]
    fn parse_backend_recognizes_mysql() {
        assert_eq!(
            parse_backend("mysql://root@127.0.0.1/app").unwrap(),
            Backend::MySql
        );
    }

    #[test]
    fn parse_backend_recognizes_postgres() {
        assert_eq!(
            parse_backend("postgres://root@127.0.0.1/app").unwrap(),
            Backend::Postgres
        );
        assert_eq!(
            parse_backend("postgresql://root@127.0.0.1/app").unwrap(),
            Backend::Postgres
        );
    }

    #[test]
    fn normalize_sqlite_url_leaves_a_relative_path_alone() {
        assert_eq!(
            normalize_sqlite_url("sqlite://database/database.sqlite"),
            "sqlite://database/database.sqlite"
        );
    }

    #[test]
    fn normalize_sqlite_url_converts_backslashes_in_a_windows_absolute_path() {
        assert_eq!(
            normalize_sqlite_url(r"sqlite://C:\Users\me\AppData\Local\Temp\abc\test.sqlite"),
            "sqlite:///C:/Users/me/AppData/Local/Temp/abc/test.sqlite"
        );
    }

    #[test]
    fn normalize_sqlite_url_adds_the_third_slash_for_a_drive_absolute_path_already_using_forward_slashes(
    ) {
        assert_eq!(
            normalize_sqlite_url("sqlite://C:/Users/me/test.sqlite"),
            "sqlite:///C:/Users/me/test.sqlite"
        );
    }

    #[test]
    fn normalize_sqlite_url_leaves_a_unix_style_absolute_path_alone() {
        assert_eq!(
            normalize_sqlite_url("sqlite:///var/lib/app/database.sqlite"),
            "sqlite:///var/lib/app/database.sqlite"
        );
    }

    #[test]
    fn parse_backend_rejects_an_unsupported_scheme() {
        assert!(parse_backend("mongodb://localhost/app").is_err());
    }
}