rustango 0.65.0

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
//! Editable DB-override layer for translations — issue #532 Slice 1.
//!
//! `Translator::from_directory` loads `locales/<lang>.json` on boot and
//! that's the *only* ingest path: fixing a typo in `fr.json` means an
//! editor + a redeploy. This module adds the hybrid storage layer the
//! issue's Slice 1 calls for — a `rustango_translations` table whose
//! rows override the file catalogs, so operators (eventually via the
//! Slice 2 admin UI) can edit translations without shipping code.
//!
//! Lookup order in [`crate::i18n::Translator::translate`] becomes:
//!
//! 1. DB override (this table, loaded into the in-memory override map
//!    via [`refresh_overrides_pool`])
//! 2. File-loaded JSON catalog (read-only seed, unchanged)
//! 3. Default-locale catalog (existing fallback chain)
//!
//! Files stay the source of truth for shipped defaults;
//! [`seed_from_translator_pool`] copies them into the DB once
//! (`ON CONFLICT DO NOTHING`), and edits live as DB rows on top.
//!
//! The table is created by [`ensure_table_pool`] across all three
//! dialects (mirroring `audit` / `contenttypes`) rather than the
//! migration graph, so it works for any app holding a [`Pool`] —
//! tenancy or not. `migrate` creates it; tenancy apps keep it on the
//! registry pool.

use crate::i18n::Translator;
use crate::sql::{Auto, ExecError, Pool};
use crate::Model;

/// One editable translation, table `rustango_translations`. The
/// `(locale, key)` pair is unique — one editable value per key per
/// locale.
///
/// `managed = false`: created by [`ensure_table_pool`], not the
/// migration graph. `migrate` runs it on the single database or the
/// tenancy registry.
///
/// Registry scope: the overrides are one process-wide map, so a tenant
/// admin must not list them (#2360).
///
/// `created_at` / `updated_at` carry the Slice 2 audit trail. Mapped
/// onto the model as of #1464 so the ORM writes them: left DB-defaulted
/// they arrived legacy-shaped forever on an upgraded SQLite file, whose
/// `CURRENT_TIMESTAMP` default `ALTER TABLE` cannot replace.
#[derive(Model, Debug, Clone, serde::Serialize)]
#[rustango(
    table = "rustango_translations",
    access = "open",
    managed = false,
    scope = "registry"
)]
pub struct Translation {
    #[rustango(primary_key)]
    pub id: Auto<i64>,
    #[rustango(max_length = 16)]
    #[rustango(help_text = "Language code, e.g. `de` or `pt-BR`.")]
    #[rustango(verbose_name = "Locale")]
    pub locale: String,
    #[rustango(max_length = 200)]
    #[rustango(
        help_text = "The message id being translated, e.g. `nav.home`. One value per key and locale."
    )]
    #[rustango(verbose_name = "Key")]
    pub key: String,
    #[rustango(help_text = "The translated text. Overrides the file translation for this key.")]
    #[rustango(verbose_name = "Value")]
    pub value: String,
    #[rustango(max_length = 200, default = "")]
    #[rustango(help_text = "Who last changed it. Empty when unknown.")]
    #[rustango(verbose_name = "Updated by")]
    pub updated_by: String,
    #[rustango(auto_now_add)]
    #[rustango(help_text = "When the row was created. Set by the database.")]
    #[rustango(verbose_name = "Created at")]
    pub created_at: Auto<chrono::DateTime<chrono::Utc>>,
    #[rustango(auto_now)]
    #[rustango(help_text = "When the row last changed. Set on every save.")]
    #[rustango(verbose_name = "Updated at")]
    pub updated_at: Auto<chrono::DateTime<chrono::Utc>>,
}

/// `max_length` of `locale` and `key`.
const LOCALE_LEN: u32 = 16;
const KEY_LEN: u32 = 200;

/// `true` when `(locale, key)` fits the table; a longer catalog key stays
/// file-only rather than failing the rows beside it.
pub(crate) fn storable(locale: &str, key: &str) -> bool {
    let fits = |s: &str, max: u32| u32::try_from(s.chars().count()).is_ok_and(|n| n <= max);
    let ok = fits(locale, LOCALE_LEN) && fits(key, KEY_LEN);
    if !ok {
        tracing::warn!(
            target: "rustango::i18n",
            locale,
            key_chars = key.chars().count(),
            "translation key longer than {KEY_LEN} chars (or locale over {LOCALE_LEN}) \
             is not stored in the database; it keeps its file value"
        );
    }
    ok
}

/// The `CREATE TABLE` for this dialect.
///
/// Both timestamp columns take type *and* `DEFAULT` from
/// [`crate::sql::Dialect::timestamp_now_column`]. That also moves MySQL
/// off `TIMESTAMP` (whole seconds) onto `DATETIME(6)`, now that the ORM
/// binds microseconds into them — but `IF NOT EXISTS` leaves an
/// existing MySQL table truncating, which needs a migration.
///
/// **No `--` comments inside the returned string.** Its newlines
/// collapse before the driver sees it, so a `--` swallows the rest of
/// the statement and SQLite rejects it with `incomplete input`.
pub(crate) fn ddl(dialect: &dyn crate::sql::Dialect) -> String {
    let ts = dialect.timestamp_now_column();
    // Case-sensitive keys: a MySQL `_ci` default would make `Key` and `key` collide.
    let (locale, key) = (
        dialect.cs_text_type(LOCALE_LEN),
        dialect.cs_text_type(KEY_LEN),
    );
    match dialect.name() {
        "mysql" => format!(
            r"
CREATE TABLE IF NOT EXISTS `rustango_translations` (
    `id`         BIGINT AUTO_INCREMENT PRIMARY KEY,
    `locale`     {locale} NOT NULL,
    `key`        {key} NOT NULL,
    `value`      TEXT NOT NULL,
    `updated_by` VARCHAR(200) NOT NULL DEFAULT '',
    `created_at` {ts},
    `updated_at` {ts},
    CONSTRAINT `rustango_translations_locale_key_uq` UNIQUE (`locale`, `key`)
);
"
        ),
        "sqlite" => format!(
            r#"
CREATE TABLE IF NOT EXISTS "rustango_translations" (
    "id"         INTEGER PRIMARY KEY AUTOINCREMENT,
    "locale"     TEXT NOT NULL,
    "key"        TEXT NOT NULL,
    "value"      TEXT NOT NULL,
    "updated_by" TEXT NOT NULL DEFAULT '',
    "created_at" {ts},
    "updated_at" {ts},
    CONSTRAINT "rustango_translations_locale_key_uq" UNIQUE ("locale", "key")
);
"#
        ),
        // Postgres + any future dialect: the standard `"`-quoted DDL.
        _ => format!(
            r#"
CREATE TABLE IF NOT EXISTS "rustango_translations" (
    "id"         BIGSERIAL PRIMARY KEY,
    "locale"     {locale} NOT NULL,
    "key"        {key} NOT NULL,
    "value"      TEXT NOT NULL,
    "updated_by" VARCHAR(200) NOT NULL DEFAULT '',
    "created_at" {ts},
    "updated_at" {ts},
    CONSTRAINT "rustango_translations_locale_key_uq" UNIQUE ("locale", "key")
);
"#
        ),
    }
}

/// Create `rustango_translations` if absent — idempotent, dispatched per
/// dialect. Call once at startup (and the Slice 2 `seed-translations`
/// verb calls it too).
///
/// # Errors
/// Driver / SQL failures other than the duplicate-object errors that
/// [`crate::sql::run_ddl_idempotent`] swallows. A refused collation upgrade
/// only warns: the table keeps working, case-insensitively.
pub async fn ensure_table_pool(pool: &Pool) -> Result<(), sqlx::Error> {
    crate::sql::run_ddl_idempotent(pool, &ddl(pool.dialect())).await?;
    if refused(pool) {
        return Ok(());
    }
    if let Err(e) = case_sensitive_keys(pool).await {
        tracing::warn!(
            error = %e,
            "rustango_translations keeps case-insensitive keys: moving them needs ALTER"
        );
        // Each pool tries once per process, not on every ensure call.
        refused_pools().push(crate::sql::PoolId::of(pool));
    }
    Ok(())
}

// The admin lists translations wherever they live.
inventory::submit! {
    crate::migrate::ensure::BootstrapTable {
        table: <Translation as crate::core::Model>::SCHEMA.table,
        holder: crate::migrate::ensure::BootstrapHolder::Shared,
        ensure: |pool| Box::pin(ensure_table_pool(pool)),
        ddl: |d| {
            Ok(ddl(d)
                .split(';')
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_owned)
                .collect())
        },
    }
}

fn refused_pools() -> std::sync::MutexGuard<'static, Vec<crate::sql::PoolId>> {
    static REFUSED: std::sync::Mutex<Vec<crate::sql::PoolId>> = std::sync::Mutex::new(Vec::new());
    REFUSED
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// `true` when this pool's upgrade already failed; drops closed pools' entries.
fn refused(pool: &Pool) -> bool {
    let mut ids = refused_pools();
    ids.retain(|id| !id.is_closed());
    ids.iter().any(|id| id.is(pool))
}

/// A table an older release created took the database's collation, which
/// may ignore case: move its `locale` / `key` to [`ddl`]'s.
async fn case_sensitive_keys(pool: &Pool) -> Result<(), ExecError> {
    let d = pool.dialect();
    let Some(probe) = d.ci_columns_sql("rustango_translations") else {
        return Ok(());
    };
    let found: Vec<(String,)> = crate::sql::raw_query_pool(&probe.sql, probe.params, pool).await?;
    for (column,) in found {
        let len = match column.as_str() {
            "locale" => LOCALE_LEN,
            "key" => KEY_LEN,
            _ => continue,
        };
        let sql = format!(
            "ALTER TABLE {} MODIFY COLUMN {} {} NOT NULL",
            d.quote_ident("rustango_translations"),
            d.quote_ident(&column),
            d.cs_text_type(len),
        );
        crate::sql::raw_execute_pool(pool, &sql, Vec::new()).await?;
    }
    Ok(())
}

/// Every override row, for the admin list view / [`refresh_overrides_pool`].
///
/// # Errors
/// As the ORM fetch path ([`ExecError`]).
pub async fn all_pool(pool: &Pool) -> Result<Vec<Translation>, ExecError> {
    use crate::sql::FetcherPool as _;
    Translation::objects().fetch(pool).await
}

/// Insert-or-update the editable value for `(locale, key)` — the
/// "edit without redeploy" write. `updated_by` records who made the
/// change (operator id / username); pass `""` if not tracked.
///
/// # Errors
/// As the ORM upsert path ([`ExecError`]). A `key` over 200 chars or a
/// `locale` over 16 fails validation with `MaxLengthExceeded` before
/// any SQL runs; the seed and the editor skip those keys instead.
pub async fn upsert_pool(
    pool: &Pool,
    locale: &str,
    key: &str,
    value: &str,
    updated_by: &str,
) -> Result<(), ExecError> {
    let row = Translation {
        id: Auto::Unset,
        locale: locale.to_owned(),
        key: key.to_owned(),
        value: value.to_owned(),
        updated_by: updated_by.to_owned(),
        created_at: Auto::Unset,
        updated_at: Auto::Unset,
    };
    // `updated_at` is in the DO UPDATE list: an upsert that lands on
    // the conflict branch is an edit, and the audit trail is the point
    // of the column.
    Translation::bulk_upsert_pool(
        &[row],
        &["locale", "key"],
        &["value", "updated_by", "updated_at"],
        pool,
    )
    .await
}

/// Seed the DB layer from a [`Translator`]'s file catalogs. Existing
/// `(locale, key)` rows are left untouched (`ON CONFLICT DO NOTHING`),
/// so files stay the source of truth for shipped defaults and only
/// previously-unknown keys are added. Idempotent — safe to run on every
/// boot. Returns the number of file entries processed. A key longer than
/// the `key` column is skipped with a warning and keeps its file value.
///
/// The on-disk path is the caller's: load with
/// `Translator::from_directory(dir, default)?` then hand the result here
/// (keeps the filesystem error out of [`ExecError`]).
///
/// # Errors
/// As the ORM bulk-insert path ([`ExecError`]).
pub async fn seed_from_translator_pool(
    pool: &Pool,
    translator: &Translator,
) -> Result<usize, ExecError> {
    let rows: Vec<Translation> = translator
        .entries()
        .into_iter()
        .filter(|(locale, key, _)| storable(locale, key))
        .map(|(locale, key, value)| Translation {
            id: Auto::Unset,
            locale,
            key,
            value,
            updated_by: String::new(),
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        })
        .collect();
    let n = rows.len();
    if n > 0 {
        Translation::bulk_insert_or_ignore_pool(&rows, pool).await?;
    }
    Ok(n)
}

/// Delete every override row for `key` (all locales) — the Slice 2
/// editor's "delete key" action. Returns the number of rows removed (0
/// if the key had no overrides). The file-catalog default, if any, is
/// untouched, so a deleted key falls back to its shipped value.
///
/// # Errors
/// As the ORM delete path ([`ExecError`]).
pub async fn delete_key_pool(pool: &Pool, key: &str) -> Result<u64, ExecError> {
    use crate::core::Model as _; // brings `Translation::SCHEMA` into scope
    use crate::core::{DeleteQuery, Filter, Op, SqlValue, WhereExpr};
    crate::sql::delete_pool(
        pool,
        &DeleteQuery {
            model: Translation::SCHEMA,
            where_clause: WhereExpr::and_predicates(vec![Filter {
                column: "key",
                op: Op::Eq,
                value: SqlValue::from(key),
            }]),
        },
    )
    .await
}

/// Delete one locale's override for `key`, so that locale falls back to
/// the file catalog (#2091). Returns the rows removed (0 or 1).
///
/// # Errors
/// As the ORM delete path ([`ExecError`]).
pub async fn delete_override(pool: &Pool, locale: &str, key: &str) -> Result<u64, ExecError> {
    use crate::core::Model as _;
    use crate::core::{DeleteQuery, Filter, Op, SqlValue, WhereExpr};
    crate::sql::delete_pool(
        pool,
        &DeleteQuery {
            model: Translation::SCHEMA,
            where_clause: WhereExpr::and_predicates(vec![
                Filter {
                    column: "locale",
                    op: Op::Eq,
                    value: SqlValue::from(locale),
                },
                Filter {
                    column: "key",
                    op: Op::Eq,
                    value: SqlValue::from(key),
                },
            ]),
        },
    )
    .await
}

/// Translators the admin translations editor refreshes after each save.
static LIVE_TRANSLATORS: std::sync::Mutex<Vec<std::sync::Weak<Translator>>> =
    std::sync::Mutex::new(Vec::new());

/// Let the admin translations editor refresh `translator` after each save.
/// Only this process is refreshed: other replicas still need their own
/// [`refresh_overrides_pool`] (on a timer or a signal).
pub fn register_live_translator(translator: &std::sync::Arc<Translator>) {
    let mut live = LIVE_TRANSLATORS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    live.retain(|w| w.strong_count() > 0);
    live.push(std::sync::Arc::downgrade(translator));
}

/// [`refresh_overrides_pool`] for every [`register_live_translator`] one.
///
/// # Errors
/// The first failed reload.
pub async fn refresh_live_translators(pool: &Pool) -> Result<(), ExecError> {
    let live: Vec<std::sync::Arc<Translator>> = LIVE_TRANSLATORS
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .iter()
        .filter_map(std::sync::Weak::upgrade)
        .collect();
    for t in live {
        refresh_overrides_pool(&t, pool).await?;
    }
    Ok(())
}

/// Reload `translator`'s override layer from the DB so subsequent
/// `translate(...)` calls reflect persisted edits — call after seeding,
/// on boot, and after each admin save. Returns the row count loaded.
///
/// # Errors
/// As the ORM fetch path ([`ExecError`]).
pub async fn refresh_overrides_pool(
    translator: &Translator,
    pool: &Pool,
) -> Result<usize, ExecError> {
    let rows = all_pool(pool).await?;
    let n = rows.len();
    translator.load_overrides(rows.into_iter().map(|t| (t.locale, t.key, t.value)));
    Ok(n)
}