rustango 0.34.0

Django-shaped batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! `ForeignKey<T, K>` — lazy-loaded parent reference, generic over
//! the parent's primary-key type `K` (defaults to `i64`).
//!
//! `#[derive(Model)] struct Post { author: ForeignKey<User>, … }` —
//! the same shape that's been working since v0.7. The generic `K`
//! parameter (default `i64`) lets the same wrapper carry non-integer
//! PKs:
//!
//! ```ignore
//! #[derive(Model)]
//! struct Comment {
//!     #[rustango(primary_key)] id: Auto<i64>,
//!     // String FK to users.user_uuid (parent's PK is String)
//!     user_uuid: ForeignKey<User, String>,
//!     body: String,
//! }
//! ```
//!
//! State machine:
//!
//! * Just-fetched rows hold `ForeignKey::Unloaded(pk)` — sqlx's
//!   `Decode` impl reads the column and builds this variant.
//! * After the first `.get(&pool)`, the variant becomes
//!   `ForeignKey::Loaded { pk, value }` with the parent cached in a
//!   `Box<T>`. Subsequent `.get()` calls are zero-cost (no SQL).
//!
//! On the write path, `ForeignKey<T, K>` lowers to `K`'s `SqlValue`
//! variant regardless of state — matches the column's declared type.
//!
//! Supported `K` values:
//! `i32`, `i64`, `String`, `Uuid`, `chrono::DateTime<Utc>`,
//! `chrono::NaiveDate`, `bool`, `f32`, `f64`. Anything that implements
//! `Into<SqlValue> + Clone + sqlx::Decode + sqlx::Type` works.
//!
//! Limitations:
//! * Target's PK column defaults to `"id"` for the
//!   `Relation::Fk { on: … }` schema entry. Override with
//!   `#[rustango(on = "user_uuid")]` on the FK field.

use crate::core::SqlValue;
// PG-typed surfaces below import these. Pulled in only when the
// `postgres` feature is on; sqlite/mysql-only builds get just the
// tri-dialect `get_pool` entry point further down.
#[cfg(feature = "postgres")]
use crate::core::{Model, Op};
#[cfg(feature = "postgres")]
use crate::query::QuerySet;
#[cfg(feature = "postgres")]
use sqlx::postgres::{PgPool, PgRow};
#[cfg(feature = "postgres")]
use sqlx::FromRow;

#[cfg(feature = "postgres")]
use super::ExecError;

/// Lazy-loaded reference to a parent row. See module docs.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ForeignKey<T, K = i64> {
    /// Just-deserialized state — only the PK is known.
    Unloaded(K),
    /// Resolved state — the parent row is cached on the field.
    Loaded {
        /// Carried alongside the value so writes can grab the PK
        /// without inspecting `T` (whose PK could be `Auto<…>` or a
        /// plain field).
        pk: K,
        value: Box<T>,
    },
}

impl<T, K> ForeignKey<T, K> {
    /// Construct from a known PK without loading. Equivalent to
    /// `pk.into()`.
    #[must_use]
    pub fn unloaded(pk: K) -> Self {
        Self::Unloaded(pk)
    }

    /// Construct from an already-loaded parent. Caller supplies the
    /// PK explicitly because `ForeignKey<T, K>` does not assume a
    /// fixed shape for `T`'s PK field.
    #[must_use]
    pub fn loaded(pk: K, value: T) -> Self {
        Self::Loaded {
            pk,
            value: Box::new(value),
        }
    }

    /// `true` once `.get()` (or `loaded()`) has populated the cache.
    #[must_use]
    pub fn is_loaded(&self) -> bool {
        matches!(self, Self::Loaded { .. })
    }

    /// Borrow the cached parent if loaded.
    #[must_use]
    pub fn value(&self) -> Option<&T> {
        match self {
            Self::Loaded { value, .. } => Some(value),
            Self::Unloaded(_) => None,
        }
    }

    /// Consume and yield the cached parent if loaded.
    #[must_use]
    pub fn into_value(self) -> Option<T> {
        match self {
            Self::Loaded { value, .. } => Some(*value),
            Self::Unloaded(_) => None,
        }
    }

    /// Borrow the PK regardless of state. Cheap, no-clone variant of
    /// [`Self::pk`] for callers that just want to peek.
    #[must_use]
    pub fn pk_ref(&self) -> &K {
        match self {
            Self::Unloaded(pk) | Self::Loaded { pk, .. } => pk,
        }
    }
}

impl<T, K: Clone> ForeignKey<T, K> {
    /// The PK regardless of state. Returns by clone — cheap for the
    /// integer PK types most apps use, and small allocation for
    /// `String`/`Uuid`. Use [`Self::pk_ref`] if you need a borrow.
    #[must_use]
    pub fn pk(&self) -> K {
        self.pk_ref().clone()
    }
}

/// Serialize as the PK regardless of loaded/unloaded state. Lets
/// audited models include FK columns in `audit(track = "...")` and
/// have the audit JSON record the parent's PK without forcing every
/// FK target to also derive `Serialize`.
impl<T, K: serde::Serialize> serde::Serialize for ForeignKey<T, K> {
    fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        self.pk_ref().serialize(ser)
    }
}

impl<T, K> From<K> for ForeignKey<T, K> {
    fn from(pk: K) -> Self {
        Self::Unloaded(pk)
    }
}

/// Always lowers to the PK regardless of state. Saves & inserts of
/// the *child's* row only need the FK column value, not the loaded
/// parent object.
impl<T, K: Clone + Into<SqlValue>> From<ForeignKey<T, K>> for SqlValue {
    fn from(fk: ForeignKey<T, K>) -> Self {
        match fk {
            ForeignKey::Unloaded(k) | ForeignKey::Loaded { pk: k, .. } => k.into(),
        }
    }
}

/// `ForeignKey<T, K>` decodes from the underlying column type into
/// the `Unloaded` variant. The lazy-load happens later via `.get()`.
#[cfg(feature = "postgres")]
impl<'r, T, K> sqlx::Decode<'r, sqlx::Postgres> for ForeignKey<T, K>
where
    K: sqlx::Decode<'r, sqlx::Postgres>,
{
    fn decode(
        value: <sqlx::Postgres as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        Ok(Self::Unloaded(<K as sqlx::Decode<sqlx::Postgres>>::decode(
            value,
        )?))
    }
}

/// `ForeignKey<T, K>` claims `K`'s Postgres type. The DDL writer
/// emits whatever column type the FK field's declared type maps to,
/// so this matches by construction.
#[cfg(feature = "postgres")]
impl<T, K> sqlx::Type<sqlx::Postgres> for ForeignKey<T, K>
where
    K: sqlx::Type<sqlx::Postgres>,
{
    fn type_info() -> sqlx::postgres::PgTypeInfo {
        <K as sqlx::Type<sqlx::Postgres>>::type_info()
    }

    fn compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool {
        <K as sqlx::Type<sqlx::Postgres>>::compatible(ty)
    }
}

/// MySQL Decode mirror for the bi-dialect path.
#[cfg(feature = "mysql")]
impl<'r, T, K> sqlx::Decode<'r, sqlx::MySql> for ForeignKey<T, K>
where
    K: sqlx::Decode<'r, sqlx::MySql>,
{
    fn decode(
        value: <sqlx::MySql as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        Ok(Self::Unloaded(<K as sqlx::Decode<sqlx::MySql>>::decode(
            value,
        )?))
    }
}

#[cfg(feature = "mysql")]
impl<T, K> sqlx::Type<sqlx::MySql> for ForeignKey<T, K>
where
    K: sqlx::Type<sqlx::MySql>,
{
    fn type_info() -> sqlx::mysql::MySqlTypeInfo {
        <K as sqlx::Type<sqlx::MySql>>::type_info()
    }

    fn compatible(ty: &sqlx::mysql::MySqlTypeInfo) -> bool {
        <K as sqlx::Type<sqlx::MySql>>::compatible(ty)
    }
}

/// SQLite Decode mirror for the bi-dialect path. Mirrors the
/// Postgres + MySQL impls so `#[derive(Model)]` types with
/// `ForeignKey<T>` fields satisfy `FromRow<SqliteRow>`.
#[cfg(feature = "sqlite")]
impl<'r, T, K> sqlx::Decode<'r, sqlx::Sqlite> for ForeignKey<T, K>
where
    K: sqlx::Decode<'r, sqlx::Sqlite>,
{
    fn decode(
        value: <sqlx::Sqlite as sqlx::Database>::ValueRef<'r>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        Ok(Self::Unloaded(<K as sqlx::Decode<sqlx::Sqlite>>::decode(
            value,
        )?))
    }
}

#[cfg(feature = "sqlite")]
impl<T, K> sqlx::Type<sqlx::Sqlite> for ForeignKey<T, K>
where
    K: sqlx::Type<sqlx::Sqlite>,
{
    fn type_info() -> sqlx::sqlite::SqliteTypeInfo {
        <K as sqlx::Type<sqlx::Sqlite>>::type_info()
    }

    fn compatible(ty: &sqlx::sqlite::SqliteTypeInfo) -> bool {
        <K as sqlx::Type<sqlx::Sqlite>>::compatible(ty)
    }
}

// ============================================================ PG-typed get / get_on

#[cfg(feature = "postgres")]
impl<T, K> ForeignKey<T, K>
where
    T: Model + for<'r> FromRow<'r, PgRow> + Send + Unpin + crate::sql::LoadRelated,
    K: Clone + Into<SqlValue> + Send + Sync + 'static,
{
    /// Resolve the parent row and cache it on the field. Subsequent
    /// calls return the cached reference without hitting the DB.
    ///
    /// PG-typed shim — for the tri-dialect entry point see
    /// [`Self::get_pool`].
    ///
    /// # Errors
    /// * [`ExecError::ForeignKeyTargetMissing`] — no row in the
    ///   target table has the FK's PK. Usually means the parent was
    ///   deleted under a non-CASCADE constraint, or the FK was hand-
    ///   built with an out-of-band value.
    /// * [`ExecError::MissingPrimaryKey`] — the target model has no
    ///   `#[rustango(primary_key)]` field (programming error).
    /// * Any [`ExecError`] produced by the underlying [`Fetcher`].
    pub async fn get(&mut self, pool: &PgPool) -> Result<&T, ExecError> {
        self.get_on(pool).await
    }

    /// Like [`Self::get`] but accepts any sqlx executor — needed for
    /// tenant-scoped lookups, where the calling connection has the
    /// `search_path` already set and a fresh checkout from `&PgPool`
    /// would land in the wrong schema.
    ///
    /// PG-only by design (`sqlx::Executor<Database = Postgres>`) —
    /// schema-mode tenancy is a Postgres feature.
    ///
    /// # Errors
    /// As [`Self::get`].
    pub async fn get_on<'c, E>(&mut self, executor: E) -> Result<&T, ExecError>
    where
        E: sqlx::Executor<'c, Database = sqlx::Postgres>,
    {
        if matches!(self, Self::Unloaded(_)) {
            let pk = self.pk_ref().clone();
            let pk_field = T::SCHEMA
                .primary_key()
                .ok_or(ExecError::MissingPrimaryKey {
                    table: T::SCHEMA.table,
                })?;
            let mut rows: Vec<T> = QuerySet::<T>::new()
                .filter(pk_field.column, Op::Eq, pk.clone())
                .fetch_on(executor)
                .await?;
            let value = rows.pop().ok_or_else(|| {
                // Render the missing PK using its `Into<SqlValue>` shape
                // so error messages stay readable for non-integer keys.
                let sv: SqlValue = pk.clone().into();
                ExecError::ForeignKeyTargetMissing {
                    table: T::SCHEMA.table,
                    pk: sv.to_display_string(),
                }
            })?;
            *self = Self::Loaded {
                pk,
                value: Box::new(value),
            };
        }
        match self {
            Self::Loaded { value, .. } => Ok(value),
            Self::Unloaded(_) => unreachable!("just transitioned to Loaded above"),
        }
    }
}

// ============================================================ tri-dialect get_pool

/// Tri-dialect FK resolution: works against any backend the
/// [`crate::sql::Pool`] enum carries. Bounds mirror
/// [`crate::sql::FetcherPool`] — every `#[derive(Model)]` target
/// satisfies them via the macro's per-backend `FromRow` + `LoadRelated`
/// emissions. Use this in new framework code so sqlite/mysql apps
/// can lazy-load FK targets without going through the PG-typed
/// [`Self::get`].
///
/// v0.35: gated on `postgres` while `FetcherPool` still requires
/// `FromRow<PgRow>` unconditionally. v0.35 slice 6 introduces a
/// `MaybePgFromRow` marker trait paralleling the existing
/// `MaybeMyFromRow` / `MaybeSqliteFromRow` so this bound becomes
/// universally satisfiable + the gate drops.
#[cfg(feature = "postgres")]
impl<T, K> ForeignKey<T, K>
where
    T: Model
        + for<'r> sqlx::FromRow<'r, sqlx::postgres::PgRow>
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
    K: Clone + Into<SqlValue> + Send + Sync + 'static,
{
    /// Resolve the parent row and cache it on the field. Backend-
    /// agnostic counterpart of [`Self::get`] — routes through
    /// [`crate::sql::FetcherPool::fetch_pool`].
    ///
    /// # Errors
    /// As [`Self::get`].
    pub async fn get_pool(&mut self, pool: &crate::sql::Pool) -> Result<&T, ExecError> {
        use crate::sql::FetcherPool as _;
        if matches!(self, Self::Unloaded(_)) {
            let pk = self.pk_ref().clone();
            let pk_field = T::SCHEMA
                .primary_key()
                .ok_or(ExecError::MissingPrimaryKey {
                    table: T::SCHEMA.table,
                })?;
            let mut rows: Vec<T> = QuerySet::<T>::new()
                .filter(pk_field.column, Op::Eq, pk.clone())
                .fetch_pool(pool)
                .await?;
            let value = rows.pop().ok_or_else(|| {
                let sv: SqlValue = pk.clone().into();
                ExecError::ForeignKeyTargetMissing {
                    table: T::SCHEMA.table,
                    pk: sv.to_display_string(),
                }
            })?;
            *self = Self::Loaded {
                pk,
                value: Box::new(value),
            };
        }
        match self {
            Self::Loaded { value, .. } => Ok(value),
            Self::Unloaded(_) => unreachable!("just transitioned to Loaded above"),
        }
    }
}

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

    #[test]
    fn unloaded_constructor_and_pk_accessor() {
        let fk: ForeignKey<()> = ForeignKey::unloaded(42);
        assert_eq!(fk.pk(), 42);
        assert!(!fk.is_loaded());
        assert!(fk.value().is_none());
    }

    #[test]
    fn loaded_constructor_caches_value() {
        let fk = ForeignKey::loaded(7_i64, "alice".to_string());
        assert_eq!(fk.pk(), 7);
        assert!(fk.is_loaded());
        assert_eq!(fk.value(), Some(&"alice".to_string()));
    }

    #[test]
    fn from_i64_yields_unloaded() {
        let fk: ForeignKey<()> = 99_i64.into();
        match fk {
            ForeignKey::Unloaded(pk) => assert_eq!(pk, 99),
            ForeignKey::Loaded { .. } => panic!("expected Unloaded"),
        }
    }

    #[test]
    fn into_sqlvalue_gives_i64_in_either_state() {
        let unloaded: ForeignKey<()> = ForeignKey::unloaded(1_i64);
        let loaded = ForeignKey::loaded(2_i64, ());
        assert!(matches!(SqlValue::from(unloaded), SqlValue::I64(1)));
        assert!(matches!(SqlValue::from(loaded), SqlValue::I64(2)));
    }

    #[test]
    fn into_value_consumes_when_loaded() {
        let loaded = ForeignKey::loaded(3_i64, 100_u32);
        assert_eq!(loaded.into_value(), Some(100));
        let unloaded: ForeignKey<u32> = ForeignKey::unloaded(4_i64);
        assert_eq!(unloaded.into_value(), None);
    }

    // ----- Non-i64 PK shapes -----

    #[test]
    fn string_pk_unloaded_round_trip() {
        let fk: ForeignKey<(), String> = ForeignKey::unloaded("alice-uuid".to_owned());
        assert_eq!(fk.pk_ref(), "alice-uuid");
        assert_eq!(fk.pk(), "alice-uuid");
        assert!(!fk.is_loaded());
    }

    #[test]
    fn string_pk_lowers_to_sqlvalue_string() {
        let fk: ForeignKey<(), String> = ForeignKey::unloaded("k".to_owned());
        match SqlValue::from(fk) {
            SqlValue::String(s) => assert_eq!(s, "k"),
            other => panic!("expected SqlValue::String, got {other:?}"),
        }
    }

    #[test]
    fn uuid_pk_round_trip() {
        let id = uuid::Uuid::nil();
        let fk: ForeignKey<(), uuid::Uuid> = ForeignKey::unloaded(id);
        assert_eq!(fk.pk(), id);
        match SqlValue::from(fk) {
            SqlValue::Uuid(u) => assert_eq!(u, id),
            other => panic!("expected SqlValue::Uuid, got {other:?}"),
        }
    }

    #[test]
    fn from_string_yields_unloaded() {
        let fk: ForeignKey<(), String> = "x".to_owned().into();
        assert!(matches!(fk, ForeignKey::Unloaded(ref s) if s == "x"));
    }
}