Skip to main content

umbral_core/
backend.rs

1//! The database backend abstraction.
2//!
3//! `DatabaseBackend` is the seam where dialect differences live. The
4//! trait sits on top of sea-query (which already abstracts dialect
5//! rendering) and sqlx (which abstracts drivers); umbral adds the
6//! umbral-specific reasoning layer on top so the system check (`check`)
7//! and the migration engine (M5, `06-migration-engine.md`) can ask the
8//! same questions of every backend.
9//!
10//! M4 ships two backends:
11//!
12//! - [`SqliteBackend`] — the runtime default. SQLite is what the M0–M3
13//!   pool already opens; this just gives it a queryable identity in the
14//!   check phase.
15//! - [`PostgresBackend`] — declared and queryable for compatibility
16//!   checks, but the umbral pool is still `sqlx::SqlitePool` at M4. The
17//!   real `sqlx::PgPool` wiring lands when there's a real user need;
18//!   the trait is in place so M5's migration engine can render Postgres
19//!   DDL today and run it tomorrow.
20//!
21//! `MySqlBackend`, `OracleBackend`, and friends stay in the deferred
22//! backlog per PRD §14.
23//!
24//! See `docs/specs/05-backends-and-system-check.md` for the target
25//! design and the rationale for each `BackendFeature` variant.
26
27use std::sync::OnceLock;
28
29/// One umbral-supported relational backend.
30///
31/// Trait surface kept narrow at M4: identity (`name`), feature queries
32/// (`supports`), and SQL-type mapping for the migration engine
33/// (`map_type`). `quote_identifier`, `render_upsert`, and dialect-
34/// specific rendering helpers get added when M5's migration engine and
35/// bulk-insert paths need them; sea-query exposes those via per-backend
36/// `QueryBuilder` types rather than a single dialect enum, so umbral
37/// dispatches through `name()` for now and adds typed rendering helpers
38/// when there's a real consumer.
39pub trait DatabaseBackend: std::fmt::Debug + Send + Sync + 'static {
40    /// Stable string identifier. `"postgres"`, `"sqlite"`, etc. Used as
41    /// the matching key in `FieldSpec::supported_backends`, and shown
42    /// in system-check error messages.
43    fn name(&self) -> &'static str;
44
45    /// Whether this backend supports the given feature. Used by the
46    /// system check to gate Postgres-only field types (Array, HStore,
47    /// jsonb) and by the migration engine to choose between
48    /// `INSERT ... RETURNING` and `INSERT; last_insert_rowid()`.
49    fn supports(&self, feature: BackendFeature) -> bool;
50
51    /// Map an umbral `SqlType` to the sea-query `ColumnType` that
52    /// renders the right native SQL column type on this backend. The
53    /// migration engine (M5) reads this when generating `CREATE TABLE`.
54    fn map_type(&self, ty: crate::orm::SqlType) -> sea_query::ColumnType;
55
56    /// Map a full column (type + per-column hints like `max_length`)
57    /// to its sea-query `ColumnType`. Default impl delegates to
58    /// `map_type` — backends that want to lift hints (Postgres
59    /// rendering `Text + max_length=N` as `VARCHAR(N)`, for example)
60    /// override this. The migration engine prefers this over
61    /// `map_type` so the per-column attributes flow into DDL.
62    fn map_column(&self, col: &crate::migrate::Column) -> sea_query::ColumnType {
63        self.map_type(col.ty)
64    }
65}
66
67/// Backend feature flags surfaced to umbral.
68///
69/// New variants land alongside new backend behaviour. Each variant
70/// represents one capability that umbral reasons about explicitly; the
71/// system check or the migration engine asks via `supports(feature)`
72/// rather than hard-coding `if backend.name() == "postgres"`.
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
74pub enum BackendFeature {
75    /// `INSERT ... RETURNING column[, ...]` on inserts. Postgres + SQLite
76    /// (3.35+); MySQL doesn't have it natively.
77    InsertReturning,
78    /// `INSERT ... ON CONFLICT (col) DO UPDATE` upserts. Postgres + SQLite.
79    UpsertOnConflict,
80    /// Array column types (`text[]`, `int[]`, etc.). Postgres only.
81    ArrayColumns,
82    /// `HStoreField` analogue: `key => value` text maps. Postgres only.
83    HStoreColumns,
84    /// Native `jsonb` column with index / operator support. Postgres only;
85    /// SQLite supports JSON-as-TEXT but without the operator surface, so
86    /// this flag is more honest as "real jsonb" than "any JSON."
87    JsonbColumns,
88    /// Native full-text search (`tsvector` + `to_tsquery`). Postgres only.
89    FullTextSearch,
90    /// CIDR / INET / MACADDR network address column types. Postgres only.
91    CidrInet,
92    /// Native `UUID` column type. Postgres only; SQLite encodes UUIDs as
93    /// `TEXT` instead.
94    UuidNative,
95    /// Native `BOOLEAN` column type. Postgres + SQLite (since 3.23); MySQL
96    /// historically encodes as TINYINT.
97    Boolean,
98}
99
100/// Postgres backend. **Specified, not yet wired at runtime.**
101///
102/// The M0–M4 pool is still `sqlx::SqlitePool`; this struct exists so
103/// the system check can flag field-type incompatibilities consistently
104/// today, and so the M5 migration engine can render Postgres DDL ahead
105/// of the runtime wiring. Switching the live pool happens when a real
106/// user lands with a Postgres workload (deferred backlog entry).
107#[derive(Debug)]
108pub struct PostgresBackend;
109
110/// SQLite backend. The umbral runtime default through M3.
111#[derive(Debug)]
112pub struct SqliteBackend;
113
114// =========================================================================
115// Trait impls — methods filled in by the M4 fan-out subagent A.
116// =========================================================================
117
118impl DatabaseBackend for PostgresBackend {
119    fn name(&self) -> &'static str {
120        "postgres"
121    }
122
123    /// Postgres feature catalogue. Source of truth: spec
124    /// `docs/specs/05-backends-and-system-check.md` §7.1. Postgres carries
125    /// every `BackendFeature` umbral reasons about today; `HStoreColumns`
126    /// is reported true and the HSTORE extension stays a DBA concern.
127    fn supports(&self, feature: BackendFeature) -> bool {
128        match feature {
129            BackendFeature::InsertReturning
130            | BackendFeature::UpsertOnConflict
131            | BackendFeature::ArrayColumns
132            | BackendFeature::HStoreColumns
133            | BackendFeature::JsonbColumns
134            | BackendFeature::FullTextSearch
135            | BackendFeature::CidrInet
136            | BackendFeature::UuidNative
137            | BackendFeature::Boolean => true,
138        }
139    }
140
141    /// Postgres lifts `Text + max_length = N` to `VARCHAR(N)` so the
142    /// length cap is enforced at the database level. `Text` without
143    /// `max_length` stays `TEXT` (unbounded). SQLite ignores the
144    /// length entirely — `VARCHAR(N)` and `TEXT` carry the same
145    /// affinity there — so its `map_column` keeps the default impl.
146    fn map_column(&self, col: &crate::migrate::Column) -> sea_query::ColumnType {
147        use crate::orm::SqlType;
148        use sea_query::ColumnType;
149        if matches!(col.ty, SqlType::Text) && col.max_length > 0 {
150            return ColumnType::String(sea_query::StringLen::N(col.max_length));
151        }
152        self.map_type(col.ty)
153    }
154
155    /// Postgres `SqlType` -> `sea_query::ColumnType` mapping. Source of
156    /// truth: spec `05-backends-and-system-check.md` §7.1.
157    fn map_type(&self, ty: crate::orm::SqlType) -> sea_query::ColumnType {
158        use crate::orm::SqlType;
159        use sea_query::ColumnType;
160        match ty {
161            SqlType::SmallInt => ColumnType::SmallInteger,
162            SqlType::Integer => ColumnType::Integer,
163            SqlType::BigInt => ColumnType::BigInteger,
164            SqlType::Real => ColumnType::Float,
165            SqlType::Double => ColumnType::Double,
166            SqlType::Boolean => ColumnType::Boolean,
167            SqlType::Text => ColumnType::Text,
168            SqlType::Date => ColumnType::Date,
169            SqlType::Time => ColumnType::Time,
170            SqlType::Timestamptz => ColumnType::TimestampWithTimeZone,
171            SqlType::Uuid => ColumnType::Uuid,
172            // Postgres has both `json` and `jsonb`; we always pick `jsonb`
173            // because that's the variant with index support and the
174            // operator surface (`@>`, `->`, `->>`). The performance gap
175            // vs `json` is meaningful for any real workload; the storage
176            // overhead is negligible.
177            SqlType::Json => ColumnType::JsonBinary,
178            // Postgres array. The inner type round-trips through this
179            // same map_type recursively (lifting ArrayElement to its
180            // SqlType equivalent), which keeps the per-element rendering
181            // in one place and lets future SqlType variants pick up
182            // array support automatically once they're added to
183            // ArrayElement.
184            SqlType::Array(elem) => {
185                ColumnType::Array(std::sync::Arc::new(self.map_type(elem.to_sql_type())))
186            }
187            SqlType::Inet => ColumnType::Inet,
188            SqlType::Cidr => ColumnType::Cidr,
189            SqlType::MacAddr => ColumnType::MacAddr,
190            // sea-query has no built-in variant for these text-backed
191            // Postgres types — render the native column type through
192            // ColumnType::Custom. `bit varying` is the variable-length
193            // bit string (v1 doesn't pin a width). gaps2 #70.
194            SqlType::Xml => ColumnType::custom("xml"),
195            SqlType::Ltree => ColumnType::custom("ltree"),
196            SqlType::Bit => ColumnType::custom("bit varying"),
197            // sea-query has no built-in `tsvector` variant — go through
198            // ColumnType::Custom to render it. Populate via Postgres
199            // trigger or GENERATED clause; umbral's migration engine
200            // emits the bare column declaration.
201            SqlType::FullText => ColumnType::custom("tsvector"),
202            // ForeignKey is stored as BIGINT in the DB; the REFERENCES
203            // clause is appended separately by the migration engine's
204            // `build_column_def_*` helpers (sea-query doesn't have a
205            // first-class FK DDL API at our version).
206            SqlType::ForeignKey => ColumnType::BigInteger,
207            // Postgres BYTEA. sea_query renders ColumnType::Blob as
208            // `bytea` for Postgres and `blob` for SQLite, which is
209            // exactly the dual we want.
210            SqlType::Bytes => ColumnType::Blob,
211            // BUG-10: NUMERIC(19, 4) — same shape on Postgres
212            // (`NUMERIC(p, s)`) and SQLite (`NUMERIC` w/ affinity
213            // inheriting precision via stored TEXT). v1 fixes the
214            // dimensions; a future attribute lifts that.
215            SqlType::Decimal => ColumnType::Decimal(Some((19, 4))),
216        }
217    }
218}
219
220impl DatabaseBackend for SqliteBackend {
221    fn name(&self) -> &'static str {
222        "sqlite"
223    }
224
225    /// SQLite feature catalogue. Source of truth: spec
226    /// `docs/specs/05-backends-and-system-check.md` §7.1. SQLite carries
227    /// the modern transactional features (RETURNING since 3.35, ON
228    /// CONFLICT since 3.24) and native `BOOLEAN`, but no array / hstore /
229    /// jsonb / full-text / network / native-UUID surface. UUIDs go
230    /// through `TEXT` instead; see `map_type` below.
231    fn supports(&self, feature: BackendFeature) -> bool {
232        match feature {
233            BackendFeature::InsertReturning
234            | BackendFeature::UpsertOnConflict
235            | BackendFeature::Boolean => true,
236            BackendFeature::ArrayColumns
237            | BackendFeature::HStoreColumns
238            | BackendFeature::JsonbColumns
239            | BackendFeature::FullTextSearch
240            | BackendFeature::CidrInet
241            | BackendFeature::UuidNative => false,
242        }
243    }
244
245    /// SQLite `SqlType` -> `sea_query::ColumnType` mapping. Source of
246    /// truth: spec `05-backends-and-system-check.md` §7.1. `Uuid` lands
247    /// on `Text` because SQLite has no native UUID type, which is the
248    /// reason `supports(UuidNative)` reports false above.
249    fn map_type(&self, ty: crate::orm::SqlType) -> sea_query::ColumnType {
250        use crate::orm::SqlType;
251        use sea_query::ColumnType;
252        match ty {
253            SqlType::SmallInt => ColumnType::SmallInteger,
254            SqlType::Integer => ColumnType::Integer,
255            SqlType::BigInt => ColumnType::BigInteger,
256            SqlType::Real => ColumnType::Float,
257            SqlType::Double => ColumnType::Double,
258            SqlType::Boolean => ColumnType::Boolean,
259            SqlType::Text => ColumnType::Text,
260            SqlType::Date => ColumnType::Date,
261            SqlType::Time => ColumnType::Time,
262            SqlType::Timestamptz => ColumnType::TimestampWithTimeZone,
263            SqlType::Uuid => ColumnType::Text,
264            // ForeignKey stored as BIGINT; the REFERENCES clause is
265            // appended by the migration engine separately.
266            SqlType::ForeignKey => ColumnType::BigInteger,
267            // SQLite has no native JSON column type — the JSON1 extension
268            // operates on TEXT values. Storing the document as TEXT keeps
269            // the round-trip portable through sqlx's `json` feature (which
270            // serializes `serde_json::Value` to a JSON string and decodes
271            // back). Future work: add a JSON1 system check so JSON
272            // operators on SQLite fail at boot when the extension isn't
273            // compiled in (rare but possible on bare-builds).
274            SqlType::Json => ColumnType::Text,
275            // Postgres-only. The M4 `field.backend` system check fires
276            // at boot when an Array field is registered against SQLite,
277            // so reaching this arm at runtime means the boot path was
278            // bypassed (low-level test seeding, hand-rolled
279            // backend::init, etc.). Panic with a clear pointer rather
280            // than rendering a SQL fragment SQLite can't parse.
281            SqlType::Array(_) => panic!(
282                "umbral::backend::SqliteBackend::map_type: SqlType::Array is Postgres-only. \
283                 The field.backend system check should have failed boot; if you reached this \
284                 panic, either the model registry wasn't initialised before map_type ran or \
285                 the check was disabled. For portable list storage, use SqlType::Json instead."
286            ),
287            // Postgres-only network address types. field.backend gates
288            // these at boot; reaching the SQLite map_type means the
289            // boot path was bypassed.
290            SqlType::Inet | SqlType::Cidr | SqlType::MacAddr => panic!(
291                "umbral::backend::SqliteBackend::map_type: SqlType::Inet/Cidr/MacAddr are \
292                 Postgres-only. The field.backend system check should have failed boot."
293            ),
294            // gaps2 #70 — text-backed Postgres types are equally
295            // Postgres-only; the field.backend check gates them at boot.
296            SqlType::Xml | SqlType::Ltree | SqlType::Bit => panic!(
297                "umbral::backend::SqliteBackend::map_type: SqlType::Xml/Ltree/Bit are \
298                 Postgres-only. The field.backend system check should have failed boot."
299            ),
300            SqlType::FullText => panic!(
301                "umbral::backend::SqliteBackend::map_type: SqlType::FullText is Postgres-only. \
302                 The field.backend system check should have failed boot."
303            ),
304            // SQLite BLOB. sea_query renders ColumnType::Blob as the
305            // dialect's right keyword (`blob` here, `bytea` for PG).
306            SqlType::Bytes => ColumnType::Blob,
307            // BUG-10: Decimal is Postgres-only at v1 (sqlx's
308            // `rust_decimal` Encode/Decode doesn't ship a SQLite
309            // implementation). The field.backend system check
310            // should have failed boot before this map runs.
311            SqlType::Decimal => panic!(
312                "umbral::backend::SqliteBackend::map_type: SqlType::Decimal is Postgres-only. \
313                 The field.backend system check should have failed boot."
314            ),
315        }
316    }
317}
318
319// =========================================================================
320// Ambient registration. The active backend is published into a process-
321// wide `OnceLock` by `AppBuilder::build()`, alongside the pool and the
322// settings. Mirrors the pattern from `crate::db` and `crate::settings`.
323// =========================================================================
324
325static ACTIVE: OnceLock<&'static dyn DatabaseBackend> = OnceLock::new();
326
327/// Initialize the ambient backend. Called by `AppBuilder::build()` only.
328pub(crate) fn init(backend: &'static dyn DatabaseBackend) {
329    ACTIVE
330        .set(backend)
331        .expect("umbral::backend::init called more than once");
332}
333
334/// Return the active backend.
335///
336/// # Panics
337///
338/// Panics if `App::build()` hasn't run.
339pub fn active() -> &'static dyn DatabaseBackend {
340    *ACTIVE
341        .get()
342        .expect("umbral: backend not initialised — did you call App::build()?")
343}
344
345/// Detect the right backend for the given database URL by scheme.
346///
347/// Used by `AppBuilder::build()` to publish the ambient backend before
348/// the system check runs. URLs that name an unshipped backend (mysql,
349/// oracle) fail at boot with a clear error rather than continuing into
350/// the system check phase.
351pub fn detect(url: &str) -> Result<&'static dyn DatabaseBackend, BackendDetectError> {
352    let scheme = url
353        .split("://")
354        .next()
355        .and_then(|s| s.split(':').next())
356        .unwrap_or(url);
357    match scheme {
358        "sqlite" => Ok(&SqliteBackend),
359        "postgres" | "postgresql" => Ok(&PostgresBackend),
360        other => Err(BackendDetectError::Unsupported(other.to_owned())),
361    }
362}
363
364/// Error returned by `detect` when the URL scheme names an unshipped
365/// backend.
366#[derive(Debug)]
367pub enum BackendDetectError {
368    /// The URL scheme is one umbral hasn't implemented yet (mysql, oracle).
369    Unsupported(String),
370}
371
372impl std::fmt::Display for BackendDetectError {
373    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
374        match self {
375            BackendDetectError::Unsupported(scheme) => write!(
376                f,
377                "umbral: no backend shipped for URL scheme `{scheme}://`. \
378                 M4 supports `sqlite://` and `postgres://`. \
379                 MySQL, Oracle, and other backends are in the deferred backlog."
380            ),
381        }
382    }
383}
384
385impl std::error::Error for BackendDetectError {}