umbral_core/migrate.rs
1//! The migration engine — the north star.
2//!
3//! Implements the **declare → migrate → change → migrate** cycle from
4//! `arch.md §0`. Users declare or change a model, run `migrate`, and the
5//! framework either generates the missing migration file (via `make`)
6//! or applies pending migration files (via `run`).
7//!
8//! At M5 (this milestone) the surface ships:
9//!
10//! - A process-wide [`ModelRegistry`] populated by
11//! `App::builder().model::<T>()`.
12//! - A [`Snapshot`] of every registered model's metadata, JSON-
13//! serialisable so it can live inside a migration file's
14//! `snapshot_after`.
15//! - An [`Operation`] enum with the two minimum-viable ops:
16//! [`Operation::CreateTable`] and [`Operation::DropTable`]. Column-
17//! level ops (`AddColumn`, `DropColumn`, `AlterColumn`) land at M8
18//! alongside rename detection (per `arch.md §7` and
19//! `docs/specs/06-migration-engine.md`). The "M5.1" label in the
20//! `UnsupportedChange` error message is shorthand for the same slot.
21//! - A [`MigrationFile`] format (one JSON file per migration) carrying
22//! `id`, `operations`, and `snapshot_after`.
23//! - The `umbral_migrations` tracking table (one row per applied
24//! migration, keyed by `(plugin, name)`).
25//! - High-level entry points: [`make`], [`run`], [`show`].
26//!
27//! Reserved for M5.1+:
28//!
29//! - Column-level ops (`AddColumn`, `DropColumn`, `AlterColumn`).
30//! - Rename-detection vs drop+add disambiguation (spec 06 §M8).
31//! - `RunSql` / `RunCode` data-migration ops.
32//! - Squashing, `--fake`, `--fake-initial` (PRD F-MIG-6 P2).
33//! - Cross-plugin migration dependencies (needs M7 Plugin contract).
34//!
35//! See `docs/specs/06-migration-engine.md` for the full target shape.
36
37use std::path::{Path, PathBuf};
38use std::sync::OnceLock;
39
40use serde::{Deserialize, Serialize};
41
42use crate::backend::DatabaseBackend;
43use crate::orm::{FieldSpec, Model, SqlType};
44
45/// Per-process model registry. Published by `AppBuilder::build()`
46/// after `.model::<T>()` calls and `.plugin(...)` registrations
47/// collected metadata into the builder.
48///
49/// Stored as a flat vector of `(plugin_name, model)` pairs so M5's
50/// existing `registered_models()` keeps working (drop the plugin
51/// names) and the M7 plugin-aware walks (`registered_plugins`,
52/// `models_for_plugin`) can read the same source of truth without a
53/// second registry. The plugin name `"app"` covers models registered
54/// via `.model::<T>()`; every other name is a real Plugin's.
55static REGISTRY: OnceLock<Vec<(String, ModelMeta)>> = OnceLock::new();
56
57/// Initialize the registry with one entry per plugin.
58///
59/// `App::build()` calls this after collecting `.model::<T>()` into the
60/// implicit `"app"` plugin and walking every registered plugin's
61/// `Plugin::models()`. Plugins missing from the map contribute zero
62/// models (default-noop `models()` returns an empty vec; the entry
63/// can be omitted).
64pub(crate) fn init_plugins(per_plugin: std::collections::HashMap<String, Vec<ModelMeta>>) {
65 let mut flat: Vec<(String, ModelMeta)> = Vec::new();
66 let mut plugin_names: Vec<String> = per_plugin.keys().cloned().collect();
67 plugin_names.sort();
68 for plugin in plugin_names {
69 for m in per_plugin.get(&plugin).cloned().unwrap_or_default() {
70 flat.push((plugin.clone(), m));
71 }
72 }
73 REGISTRY
74 .set(flat)
75 .expect("umbral::migrate::init_plugins called more than once");
76}
77
78/// Return every registered model, flat. Drops the per-plugin grouping;
79/// useful when the caller only needs the model set (e.g. M5's `make`
80/// when the codebase only had a single `"app"` plugin).
81///
82/// # Panics
83///
84/// Panics if `App::build()` hasn't run.
85pub fn registered_models() -> Vec<ModelMeta> {
86 registered_models_opt()
87 .expect("umbral: model registry not initialised — did you call App::build()?")
88}
89
90/// [`registered_models`] without the panic: `None` when the registry hasn't been
91/// initialised yet.
92///
93/// For callers that legitimately run before (or without) `App::build()` — the
94/// boot system checks are the case in point, and a check that panics is a check
95/// that can never report anything.
96pub fn registered_models_opt() -> Option<Vec<ModelMeta>> {
97 Some(REGISTRY.get()?.iter().map(|(_, m)| m.clone()).collect())
98}
99
100/// Whether the model registry has been initialised. False before
101/// `App::build()` has run; true after the phase-3 `init_plugins`
102/// call publishes the per-plugin map. Used by system checks that
103/// walk the registry — they return an empty result when the
104/// registry isn't ready rather than panicking (so low-level tests
105/// that drive `check::run_all` without booting an App keep working).
106pub fn is_initialised() -> bool {
107 REGISTRY.get().is_some()
108}
109
110/// PK lift Pass E — cached `(pk_column_name, pk_sql_type)` lookup
111/// keyed by table name. Used by the FK decode path
112/// (`fk_target_pk_sql_type` in `orm/dynamic.rs`) and the
113/// select_related hydrators, both of which previously cloned the
114/// full `Vec<ModelMeta>` per call and linear-scanned for the
115/// target's PK column.
116///
117/// REGISTRY is a `OnceLock` set once during `App::build`; this cache
118/// reads from it the first time anyone asks for a PK lookup AFTER
119/// initialisation, then serves from a `HashMap` for every
120/// subsequent call. Eliminates the per-row `registered_models()`
121/// clone in hot decode loops.
122///
123/// Returns `None` when the registry isn't initialised (the cache
124/// stays uninstantiated so a follow-up call after `App::build`
125/// gets the real table set), OR when the named table isn't in the
126/// registry (orphan / system / typo).
127pub fn pk_meta_for_table(table: &str) -> Option<(String, crate::orm::SqlType)> {
128 if !is_initialised() {
129 // Defer cache init until App::build has populated REGISTRY.
130 // The cache MUST NOT memoize an empty map; otherwise
131 // post-init callers would see no PK metadata forever.
132 return None;
133 }
134 static CACHE: std::sync::OnceLock<
135 std::collections::HashMap<String, (String, crate::orm::SqlType)>,
136 > = std::sync::OnceLock::new();
137 let map = CACHE.get_or_init(|| {
138 let mut out = std::collections::HashMap::new();
139 for m in registered_models() {
140 if let Some(pk) = m.pk_column() {
141 out.insert(m.table.clone(), (pk.name.clone(), pk.ty));
142 }
143 }
144 out
145 });
146 map.get(table).cloned()
147}
148
149/// Cached model lookup by SQL table name.
150///
151/// Unlike [`registered_models`], this does not deep-clone the full
152/// registry on every call. It clones only the matched [`ModelMeta`],
153/// which keeps row-by-row dynamic serializers from paying
154/// O(registry-size) per row.
155pub fn model_meta_for_table(table: &str) -> Option<ModelMeta> {
156 if !is_initialised() {
157 return None;
158 }
159 static CACHE: std::sync::OnceLock<std::collections::HashMap<String, ModelMeta>> =
160 std::sync::OnceLock::new();
161 let map = CACHE.get_or_init(|| {
162 registered_models()
163 .into_iter()
164 .map(|m| (m.table.clone(), m))
165 .collect()
166 });
167 map.get(table).cloned()
168}
169
170/// The SQL type a column's value actually binds / decodes as (PK lift).
171/// Equals `col.ty` for everything except a `ForeignKey`, where it resolves
172/// to the referenced model's PK type via [`pk_meta_for_table`] — so an FK
173/// pointing at a `String`-slug- or `Uuid`-PK target is handled as text /
174/// uuid instead of being forced through i64. Falls back to `BigInt` (the
175/// historical default) when the target can't be resolved (registry not yet
176/// initialised, or an unregistered target table).
177///
178/// The single source of truth for "what shape is this FK really?", used by
179/// `backup` (dump/load) and the dynamic filter helpers.
180pub fn fk_effective_type(col: &Column) -> crate::orm::SqlType {
181 if matches!(col.ty, crate::orm::SqlType::ForeignKey) {
182 col.fk_target
183 .as_deref()
184 .and_then(pk_meta_for_table)
185 .map(|(_, ty)| ty)
186 .unwrap_or(crate::orm::SqlType::BigInt)
187 } else {
188 col.ty
189 }
190}
191
192/// Return the registered plugin names that contributed at least one
193/// model. Sorted deterministically. Used as a fallback when no
194/// topological order is published; the M7 walk used this directly,
195/// and M8 prefers [`plugin_order`] when it's been set.
196pub fn registered_plugins() -> Vec<String> {
197 let mut names: Vec<String> = REGISTRY
198 .get()
199 .expect("umbral: model registry not initialised — did you call App::build()?")
200 .iter()
201 .map(|(p, _)| p.clone())
202 .collect();
203 names.sort();
204 names.dedup();
205 names
206}
207
208/// The topological plugin order published by `App::build()` after its
209/// phase 1.5 sort. `None` until that runs; the CLI subcommands
210/// (`makemigrations`, `migrate`, `showmigrations`) call `App::build()`
211/// via `boot_for_management` before reaching the migration engine.
212static PLUGIN_ORDER: OnceLock<Vec<String>> = OnceLock::new();
213
214/// Per-model database alias (`Model::NAME -> alias`) published by
215/// `App::build()` after walking each registered plugin's
216/// `Plugin::database()`. Models whose plugin returned `None` are
217/// absent from the map; QuerySet's `resolve_pool` falls back to the
218/// `"default"` alias for those. Lookup is `O(1)` on a `HashMap`.
219static MODEL_ALIASES: OnceLock<std::collections::HashMap<String, String>> = OnceLock::new();
220
221/// Publish the topological plugin order. Called by `App::build()` once
222/// the phase 1.5 sort has produced the order. Must include the
223/// implicit `"app"` plugin even when no real plugins are registered.
224pub(crate) fn init_plugin_order(order: Vec<String>) {
225 PLUGIN_ORDER
226 .set(order)
227 .expect("umbral::migrate::init_plugin_order called more than once");
228}
229
230/// Return the topological plugin order if `App::build()` published
231/// one; otherwise fall back to [`registered_plugins`] (sorted by
232/// name). The fallback keeps existing M5 / M6 tests working without
233/// requiring them to wire a full plugin sort.
234pub fn plugin_order() -> Vec<String> {
235 PLUGIN_ORDER
236 .get()
237 .cloned()
238 .unwrap_or_else(registered_plugins)
239}
240
241/// The client-facing API endpoints every registered plugin advertised
242/// via `Plugin::api_endpoints()`, collected by `App::build()`. `None`
243/// until that runs; an app with no advertising plugins publishes an
244/// empty vec.
245static API_ENDPOINTS: OnceLock<Vec<crate::plugin::ApiEndpoint>> = OnceLock::new();
246
247/// Publish the collected `Plugin::api_endpoints()`. Called once by
248/// `App::build()` after walking every registered plugin.
249pub(crate) fn init_api_endpoints(endpoints: Vec<crate::plugin::ApiEndpoint>) {
250 let _ = API_ENDPOINTS.set(endpoints);
251}
252
253/// Every callable endpoint registered plugins advertised for service
254/// discovery, in plugin-registration order. Empty until `App::build()`
255/// has run. A REST API root (or any discovery surface) reads this to
256/// list plugin endpoints without depending on those plugins' crates.
257pub fn registered_api_endpoints() -> Vec<crate::plugin::ApiEndpoint> {
258 API_ENDPOINTS.get().cloned().unwrap_or_default()
259}
260
261/// Publish the per-model alias routing. Called by `App::build()`
262/// during phase 3 after walking every plugin's `Plugin::database()`.
263/// Plugins that returned `None` contribute no entries; only the
264/// explicit overrides land here.
265pub(crate) fn init_model_aliases(map: std::collections::HashMap<String, String>) {
266 MODEL_ALIASES
267 .set(map)
268 .expect("umbral::migrate::init_model_aliases called more than once");
269}
270
271/// Look up the database alias for a SQL table name — the reverse of
272/// the `Model::NAME → alias` lookup that [`model_alias`] does. Walks
273/// the registered model metas to find the one whose `table` matches
274/// (snake_case of the struct name + any `#[umbral(table = "...")]`
275/// override) and returns its alias if set. Falls back to `"default"`
276/// when no model owns the table (e.g. orphan schema, the
277/// `umbral_migrations` table itself) — those land on the main pool.
278///
279/// Used by the migration engine's per-DB dispatch in [`run_in`] to
280/// route each operation to the right pool.
281pub fn table_alias(table_name: &str) -> String {
282 for meta in registered_models() {
283 if meta.table == table_name {
284 return model_alias(&meta.name).unwrap_or_else(|| "default".to_string());
285 }
286 }
287 "default".to_string()
288}
289
290/// Look up the database alias for one model. Returns `None` if the
291/// model isn't routed explicitly (the caller falls back to the
292/// `"default"` pool); returns `None` even when the alias map hasn't
293/// been initialised so low-level tests that drive `init_plugins`
294/// directly don't have to wire a second call.
295pub fn model_alias(model_name: &str) -> Option<String> {
296 MODEL_ALIASES.get()?.get(model_name).cloned()
297}
298
299static MODEL_META_BY_NAME: OnceLock<std::collections::HashMap<String, ModelMeta>> = OnceLock::new();
300
301/// Cached `&ModelMeta` lookup by model name. Returns `None` before
302/// `App::build` populates the registry (low-level tests), which the routing
303/// seam treats as "fall back to legacy static routing".
304pub fn model_meta_ref(name: &str) -> Option<&'static ModelMeta> {
305 if !is_initialised() {
306 return None;
307 }
308 MODEL_META_BY_NAME
309 .get_or_init(|| {
310 registered_models()
311 .into_iter()
312 .map(|m| (m.name.clone(), m))
313 .collect()
314 })
315 .get(name)
316}
317
318/// Return the models registered against a specific plugin. Empty if
319/// no plugin by that name registered models.
320pub fn models_for_plugin(plugin: &str) -> Vec<ModelMeta> {
321 REGISTRY
322 .get()
323 .expect("umbral: model registry not initialised — did you call App::build()?")
324 .iter()
325 .filter(|(p, _)| p == plugin)
326 .map(|(_, m)| m.clone())
327 .collect()
328}
329
330/// Static metadata for one registered model, copied off the `Model`
331/// trait's `const`s when the user calls `App::builder().model::<T>()`.
332///
333/// Owned (no lifetimes) so the registry can hold an arbitrary number
334/// without the lifetime contortions a slice of trait references would
335/// need. The cost is one Vec at `App::build` time; the win is
336/// `registered_models()` having a plain `&'static [ModelMeta]` signature.
337#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
338pub struct ModelMeta {
339 /// The struct name (`Model::NAME`). Identifies the model across
340 /// snapshot diffs even if the table is renamed.
341 pub name: String,
342 /// The SQL table name (`Model::TABLE`).
343 pub table: String,
344 /// One owned column descriptor per field, in declaration order.
345 /// Owned (`Column`, not the underlying static `FieldSpec`) so the
346 /// snapshot round-trips cleanly through serde.
347 pub fields: Vec<Column>,
348 /// Human-readable display name from `Model::DISPLAY`. Defaults to
349 /// `Model::NAME` when no `#[umbral(display = "...")]` is present.
350 #[serde(default)]
351 pub display: String,
352 /// Lucide icon slug from `Model::ICON`. Defaults to `"database"`.
353 #[serde(default = "default_icon")]
354 pub icon: String,
355 /// Database alias from `Model::DATABASE`, when set. `None` means
356 /// "fall back to the owning plugin's `Plugin::database()`, then
357 /// the `default` pool." Captured here so `App::build`'s alias
358 /// routing can read it without re-reaching into the trait at a
359 /// later phase.
360 #[serde(default, skip_serializing_if = "Option::is_none")]
361 pub database: Option<String>,
362 /// Mirrors `Model::SINGLETON`. Closes BUG-9 in
363 /// `bugs/tests/testBugs.md`. Default `false`; admin renderers
364 /// read it to auto-redirect list-view to the edit form.
365 #[serde(default, skip_serializing_if = "is_false")]
366 pub singleton: bool,
367 /// Mirrors `Model::UNIQUE_TOGETHER`. Composite-UNIQUE constraints,
368 /// each inner `Vec<String>` listing the columns of one constraint.
369 /// Closes BUG-6.
370 #[serde(default, skip_serializing_if = "Vec::is_empty")]
371 pub unique_together: Vec<Vec<String>>,
372 /// Mirrors `Model::INDEXES`. Each inner `Vec<String>` lists the
373 /// columns of one multi-column index. Closes BUG-7.
374 #[serde(default, skip_serializing_if = "Vec::is_empty")]
375 pub indexes: Vec<Vec<String>>,
376 /// Mirrors `Model::ORDERING`. Each tuple is `(column, descending)`
377 /// — `descending == true` lowers to `ORDER BY col DESC`. Closes
378 /// BUG-8.
379 #[serde(default, skip_serializing_if = "Vec::is_empty")]
380 pub ordering: Vec<(String, bool)>,
381 /// Mirrors `Model::M2M_RELATIONS`. Many-to-many relations declared
382 /// on this model. The migration engine uses this to auto-generate
383 /// junction tables. Closes BUG-16.
384 #[serde(default, skip_serializing_if = "Vec::is_empty")]
385 pub m2m_relations: Vec<M2MRelation>,
386 /// Mirrors `Model::SOFT_DELETE` (`#[umbral(soft_delete)]`). The
387 /// dynamic / annotate paths read this to auto-exclude
388 /// `deleted_at IS NULL` children from correlated counts and to
389 /// drive trash-aware admin views without re-reaching into the
390 /// typed trait. Shared enabler for gaps2 #35 + #39a.
391 #[serde(default, skip_serializing_if = "is_false")]
392 pub soft_delete: bool,
393 /// `#[umbral(audited)]`. Lives on `ModelMeta` (not re-derived per path) so
394 /// the DYNAMIC write path — which is what admin and REST run on — reads the
395 /// same source of truth the typed one does.
396 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
397 pub audited: bool,
398 /// features #73 — `#[umbral(view = "...")]`. `Some(sql)` means this model is
399 /// backed by a database VIEW whose body is `sql`; the migration engine emits
400 /// `CREATE VIEW` for it and never `CREATE TABLE`. Carried on `ModelMeta` (not
401 /// just the trait) because the *snapshot* is what `makemigrations` diffs — a
402 /// view whose SQL changed has to be detectable without the model being in scope.
403 #[serde(default, skip_serializing_if = "Option::is_none")]
404 pub view: Option<String>,
405 /// features #73 — the view is MATERIALIZED (Postgres-only). Meaningless unless
406 /// `view` is `Some`.
407 #[serde(default, skip_serializing_if = "is_false")]
408 pub materialized: bool,
409 /// The app label (the owning plugin's name), mirrors `Model::APP_LABEL`.
410 /// Sourced from `#[umbral(plugin = "...")]`; `"app"` when absent.
411 /// Authoritative for permission codenames (gaps2 #80g): replaces the
412 /// old table-name-split heuristic that collided distinct models. The
413 /// `#[serde(default)]` keeps pre-#80g snapshot JSON round-tripping.
414 #[serde(default = "default_app_label")]
415 pub app_label: String,
416}
417
418fn default_app_label() -> String {
419 "app".to_string()
420}
421
422impl Default for ModelMeta {
423 fn default() -> Self {
424 Self {
425 name: String::new(),
426 table: String::new(),
427 fields: Vec::new(),
428 display: String::new(),
429 icon: default_icon(),
430 database: None,
431 singleton: false,
432 unique_together: Vec::new(),
433 indexes: Vec::new(),
434 ordering: Vec::new(),
435 m2m_relations: Vec::new(),
436 soft_delete: false,
437 audited: false,
438 view: None,
439 materialized: false,
440 app_label: default_app_label(),
441 }
442 }
443}
444
445impl Default for Column {
446 /// A nullable-free `BigInt` column with every optional marker off, so a
447 /// hand-built fixture can name the two or three fields it cares about and
448 /// `..Column::default()` the rest.
449 ///
450 /// `db_constraint` defaults to `true`, matching the serde default: an FK
451 /// emits a physical `REFERENCES` clause unless the field opts out. Getting
452 /// this backwards would silently drop constraints AND the plugin ordering
453 /// edges derived from them (see `app::fk_plugin_edges`).
454 fn default() -> Self {
455 Self {
456 name: String::new(),
457 ty: crate::orm::SqlType::BigInt,
458 primary_key: false,
459 nullable: false,
460 fk_target: None,
461 noform: false,
462 privileged: false,
463 private: false,
464 secret: false,
465 db_constraint: true,
466 noedit: false,
467 is_string_repr: false,
468 max_length: 0,
469 choices: Vec::new(),
470 choice_labels: Vec::new(),
471 default: String::new(),
472 is_multichoice: false,
473 unique: false,
474 on_delete: crate::orm::FkAction::NoAction,
475 on_update: crate::orm::FkAction::NoAction,
476 index: false,
477 auto_now_add: false,
478 auto_now: false,
479 auto_user_add: false,
480 auto_user: false,
481 trim: false,
482 lowercase: false,
483 case_insensitive: false,
484 help: String::new(),
485 example: String::new(),
486 widget: None,
487 supported_backends: Vec::new(),
488 min: None,
489 max: None,
490 text_format: None,
491 slug_from: None,
492 }
493 }
494}
495
496/// Owned mirror of `orm::M2MRelationSpec` so `ModelMeta` can be
497/// serialised into migration JSON without lifetimes.
498#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
499pub struct M2MRelation {
500 pub field_name: String,
501 pub target_table: String,
502 pub target_name: String,
503}
504
505fn default_icon() -> String {
506 "database".to_string()
507}
508
509/// Serde default for [`Operation::CreateM2MTable`]'s `parent_ty` /
510/// `child_ty` fields. Older snapshot files (pre-phase-2) had no
511/// per-side PK type and assumed `BigInt` on both ends — this keeps
512/// them round-tripping without rewrites.
513fn default_bigint() -> crate::orm::SqlType {
514 crate::orm::SqlType::BigInt
515}
516
517impl ModelMeta {
518 /// The primary-key column on this model. Every umbral model
519 /// has exactly one PK by construction (the derive enforces
520 /// it), but the lookup is `Option`-shaped because nothing
521 /// stops a hand-written `ModelMeta` (test fixtures, etc.)
522 /// from omitting it.
523 pub fn pk_column(&self) -> Option<&Column> {
524 self.fields.iter().find(|c| c.primary_key)
525 }
526
527 /// Read static metadata off `T: Model` into an owned `ModelMeta`.
528 /// Called from `AppBuilder::model::<T>()`.
529 pub fn for_<T: Model>() -> Self {
530 Self {
531 name: T::NAME.to_string(),
532 table: T::TABLE.to_string(),
533 fields: T::FIELDS.iter().map(Column::from).collect(),
534 display: T::DISPLAY.to_string(),
535 icon: T::ICON.to_string(),
536 database: T::DATABASE.map(|s| s.to_string()),
537 singleton: T::SINGLETON,
538 unique_together: T::UNIQUE_TOGETHER
539 .iter()
540 .map(|group| group.iter().map(|s| s.to_string()).collect())
541 .collect(),
542 indexes: T::INDEXES
543 .iter()
544 .map(|group| group.iter().map(|s| s.to_string()).collect())
545 .collect(),
546 ordering: T::ORDERING
547 .iter()
548 .map(|(col, desc)| (col.to_string(), *desc))
549 .collect(),
550 m2m_relations: T::M2M_RELATIONS
551 .iter()
552 .map(|r| M2MRelation {
553 field_name: r.field_name.to_string(),
554 target_table: r.target_table.to_string(),
555 target_name: r.target_name.to_string(),
556 })
557 .collect(),
558 soft_delete: T::SOFT_DELETE,
559 audited: T::AUDITED,
560 view: T::VIEW.map(|s| s.to_string()),
561 materialized: T::MATERIALIZED,
562 app_label: T::APP_LABEL.to_string(),
563 }
564 }
565}
566
567/// A snapshot of every registered model at a point in time.
568///
569/// Serialised into the `snapshot_after` field of a migration file so
570/// future `makemigrations` runs can diff against it without replaying
571/// every prior migration's operations.
572#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
573pub struct Snapshot {
574 /// Models sorted by name so the JSON is deterministic and the
575 /// snapshot_hash is stable across runs that produce equivalent
576 /// content.
577 pub models: Vec<ModelMeta>,
578}
579
580impl Snapshot {
581 /// Build a snapshot from the live registry (the current state of
582 /// the application's models, post-`App::build`).
583 pub fn current() -> Self {
584 let mut models = registered_models().to_vec();
585 models.sort_by(|a, b| a.name.cmp(&b.name));
586 Self { models }
587 }
588
589 /// Build a snapshot containing only the models registered
590 /// against the given plugin. Used by `make_in` to diff each
591 /// plugin's migrations independently against its own prior
592 /// snapshot, so cross-plugin model sets don't bleed into one
593 /// migration file.
594 pub fn current_for(plugin: &str) -> Self {
595 let mut models = models_for_plugin(plugin);
596 models.sort_by(|a, b| a.name.cmp(&b.name));
597 Self { models }
598 }
599
600 /// Compute the snapshot's SHA-256 hash, hex-encoded. Stored in the
601 /// `umbral_migrations.snapshot_hash` column for drift detection.
602 pub fn hash(&self) -> String {
603 use sha2::{Digest, Sha256};
604 let json = serde_json::to_string(self).expect("Snapshot serializes");
605 let digest = Sha256::digest(json.as_bytes());
606 hex(&digest[..])
607 }
608}
609
610fn hex(bytes: &[u8]) -> String {
611 const HEX: &[u8; 16] = b"0123456789abcdef";
612 let mut s = String::with_capacity(bytes.len() * 2);
613 for b in bytes {
614 s.push(HEX[(b >> 4) as usize] as char);
615 s.push(HEX[(b & 0x0f) as usize] as char);
616 }
617 s
618}
619
620/// One operation inside a migration. The migration engine renders each
621/// operation to SQL via the active backend (M4 `DatabaseBackend::
622/// map_type`) and runs them in declaration order inside one
623/// transaction per migration file.
624///
625/// M5 v1 shipped table-level ops; M8 v1 adds `AddColumn` and
626/// `DropColumn`. `AlterColumn`, index / constraint ops, and
627/// `RunSql` / `RunCode` are deferred (see `docs/specs/06-migration-
628/// engine.md`).
629#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
630#[serde(tag = "kind")]
631pub enum Operation {
632 /// Create a new table. `columns` is in declaration order; the
633 /// engine builds a sea-query `Table::create()` over them and runs
634 /// the rendered DDL. `unique_together` lowers to inline
635 /// `UNIQUE (col1, col2)` clauses; `indexes` lowers to follow-up
636 /// `CREATE INDEX` statements after the table is created. Both
637 /// default to empty for backward-compat with older snapshots.
638 CreateTable {
639 table: String,
640 columns: Vec<Column>,
641 #[serde(default, skip_serializing_if = "Vec::is_empty")]
642 unique_together: Vec<Vec<String>>,
643 #[serde(default, skip_serializing_if = "Vec::is_empty")]
644 indexes: Vec<Vec<String>>,
645 },
646 /// Drop an existing table.
647 DropTable { table: String },
648 /// features #73 — create a database VIEW from `#[umbral(view = "...")]`.
649 ///
650 /// There is no `AlterView`, and there never will be: a view holds no rows, so
651 /// changing one is a drop and a recreate. That is why views escape every hard
652 /// problem the table side of this engine spent its life on — no data-preserving
653 /// ALTER, no rename-vs-drop disambiguation, nothing to back up.
654 CreateView {
655 /// The view's name in the database (the model's `TABLE`).
656 name: String,
657 /// The SELECT body, verbatim from the attribute. Opaque to the engine.
658 sql: String,
659 /// `CREATE MATERIALIZED VIEW` (Postgres-only) rather than `CREATE VIEW`.
660 #[serde(default, skip_serializing_if = "is_false")]
661 materialized: bool,
662 },
663 /// features #73 — drop a database VIEW.
664 DropView {
665 /// The view's name in the database.
666 name: String,
667 /// Whether it was created as MATERIALIZED — the DROP statement differs.
668 #[serde(default, skip_serializing_if = "is_false")]
669 materialized: bool,
670 },
671 /// Add a new column to an existing table. Rendered as
672 /// `ALTER TABLE x ADD COLUMN y TYPE [NOT NULL]`. SQLite refuses a
673 /// non-nullable add against a populated table without a default;
674 /// the engine surfaces that as a sqlx error at apply time (M8 v1).
675 /// A future op `AddColumnWithDefault` lifts the restriction once
676 /// the `#[umbral(default = ...)]` attribute lands.
677 AddColumn { table: String, column: Column },
678 /// Drop a column from an existing table. Rendered as
679 /// `ALTER TABLE x DROP COLUMN y`. SQLite 3.35+ and Postgres
680 /// support this natively; older SQLite would need a table-
681 /// recreation dance the engine doesn't implement.
682 DropColumn { table: String, column: String },
683 /// Alter a column's nullable flag (the only safe in-place change
684 /// the engine ships at M5.1). Self-contained: carries the full
685 /// new column list so the SQLite table-recreation dance can
686 /// rebuild the schema without re-reading the snapshot. The
687 /// `column` field names the specific column that triggered the
688 /// alter (used for the filename suffix and diagnostics); the
689 /// `new_columns` list is the post-change schema.
690 AlterColumn {
691 table: String,
692 column: String,
693 new_columns: Vec<Column>,
694 /// The table's composite UNIQUE groups (audit_2 core-migrate #10). The
695 /// SQLite recreation dance rebuilds the table from scratch, so it must
696 /// re-emit these — otherwise a nullable-flip / safe-cast alter silently
697 /// DROPS every composite UNIQUE constraint (duplicates become
698 /// insertable — integrity loss). Postgres alters in place and ignores
699 /// this. `serde(default)` keeps older on-disk migrations deserialising.
700 #[serde(default, skip_serializing_if = "Vec::is_empty")]
701 unique_together: Vec<Vec<String>>,
702 /// The table's composite (multi-column) index groups (audit_2
703 /// core-migrate #10). Re-created by the SQLite dance after the rebuild,
704 /// for the same reason as `unique_together`. Single-column / FK /
705 /// soft-delete indexes are re-derived from `new_columns`.
706 #[serde(default, skip_serializing_if = "Vec::is_empty")]
707 indexes: Vec<Vec<String>>,
708 /// Snapshot of the table's columns *before* this alter. Carried
709 /// so the Postgres renderer can decide per-column whether it
710 /// needs a TYPE/USING clause vs a SET/DROP NOT NULL — without
711 /// re-walking the snapshot file. `Option` + `serde(default)`
712 /// keeps older on-disk migrations deserialising cleanly; ops
713 /// produced before this field existed get `None` and fall back
714 /// to the legacy nullable-only Postgres path.
715 #[serde(default, skip_serializing_if = "Option::is_none")]
716 prev_columns: Option<Vec<Column>>,
717 },
718 /// Rename an existing table. Emitted by `diff` when a model's table
719 /// name changes but its `Model::NAME` (the Rust struct name) stays
720 /// the same (first-pass detection), or when the column shapes are
721 /// bit-identical and the struct name changed too (second-pass
722 /// heuristic detection). Both SQLite and Postgres render as
723 /// `ALTER TABLE "<from>" RENAME TO "<to>"`.
724 ///
725 /// The migration tracking table records `(plugin, name)` of each
726 /// applied migration — it is not affected by a table rename inside
727 /// the migration.
728 RenameTable { from: String, to: String },
729 /// Create a many-to-many junction table. Auto-emitted when a model
730 /// gains an `M2M<T>` field. Closes BUG-16 phase 2.
731 ///
732 /// The junction table name is `parent_table_field_name`. Columns:
733 /// `parent_id` (FK to parent), `child_id` (FK to target), both with
734 /// `ON DELETE CASCADE`. Composite PK `(parent_id, child_id)`.
735 ///
736 /// `parent_ty` and `child_ty` carry the SQL types of the
737 /// referenced PK columns — `BigInt` for an `i64` PK, `Text` for a
738 /// `String` slug, `Uuid` for a `uuid::Uuid`. The renderer maps
739 /// these to the right column type per backend; without them the
740 /// junction's `child_id INTEGER` would reject a string codename
741 /// at insert time. `#[serde(default)]` keeps older snapshot files
742 /// (pre-phase-2) round-tripping — they default to `BigInt`,
743 /// matching the original i64-only behaviour.
744 CreateM2MTable {
745 junction_table: String,
746 parent_table: String,
747 parent_col: String,
748 child_table: String,
749 child_col: String,
750 #[serde(default = "default_bigint")]
751 parent_ty: crate::orm::SqlType,
752 #[serde(default = "default_bigint")]
753 child_ty: crate::orm::SqlType,
754 },
755 /// Drop a many-to-many junction table. Auto-emitted when an `M2M<T>`
756 /// field is removed from a model.
757 DropM2MTable { junction_table: String },
758 /// Gap 88: rename a column on an existing table. Emitted by the
759 /// diff engine when a single column with one shape was dropped
760 /// and one with the same shape was added in the same diff —
761 /// the heuristic match for "the user renamed `title` to
762 /// `headline`." Both SQLite (3.25+) and Postgres render as
763 /// `ALTER TABLE "<t>" RENAME COLUMN "<from>" TO "<to>"`.
764 ///
765 /// `column` carries the post-rename column shape so the
766 /// snapshot stays in sync. The migration only renames; never
767 /// alters other column attributes — a rename combined with a
768 /// type change emits a RenameColumn AND a follow-on AlterColumn
769 /// against the new name.
770 RenameColumn {
771 table: String,
772 from: String,
773 to: String,
774 #[serde(default, skip_serializing_if = "Option::is_none")]
775 column: Option<Column>,
776 },
777 /// gaps3 #43: set (or clear) a column's database comment from
778 /// `#[umbral(help = "...")]`. Renders as `COMMENT ON COLUMN "<t>"."<c>" IS
779 /// '<comment>'` on Postgres, and to *nothing* on SQLite, which has no
780 /// comment facility. An empty `comment` clears it with `IS NULL` — Postgres
781 /// distinguishes "no comment" from "the empty comment".
782 ///
783 /// `CreateTable` and `AddColumn` emit their own comments inline, so this op
784 /// exists for the case they can't cover: the help text on an *existing*
785 /// column changed. It touches no rows and takes no lock worth naming, so it
786 /// classifies as `OpSafety::Safe`.
787 SetColumnComment {
788 table: String,
789 column: String,
790 /// Empty string means "remove the comment".
791 comment: String,
792 },
793 /// Gap #69: a raw-SQL **data** migration. Unlike every other
794 /// variant it changes *rows*, not the schema model — so the
795 /// autodetector NEVER emits it (it has no model-state effect), and
796 /// a migration carrying only `RunSql` ops has
797 /// `snapshot_after == snapshot_before`. It is always hand-authored:
798 /// generate an empty migration with `makemigrations --empty
799 /// <plugin>`, then add the `RunSql` op by editing the file.
800 ///
801 /// `sql` is the forward statement(s), executed verbatim on the
802 /// per-migration transaction — same string on both backends (raw
803 /// SQL the renderer passes through untouched), so the author owns
804 /// portability. `reverse_sql` is the optional un-apply statement
805 /// (used by a future `migrate --reverse`); `None` means
806 /// irreversible.
807 ///
808 /// Under schema-per-tenant the op runs **per tenant schema** (the
809 /// schema-migrate loop applies every op under the
810 /// `<schema>, public` search_path), so a tenant-app `RunSql` writes
811 /// tenant rows while reading shared `public` lookup tables — the
812 /// boundary-spanning data migration. A shared-app `RunSql` runs once
813 /// in `public` via the normal `migrate`.
814 RunSql {
815 sql: String,
816 #[serde(default, skip_serializing_if = "Option::is_none")]
817 reverse_sql: Option<String>,
818 },
819 /// Create a composite index, or a composite UNIQUE constraint, on an
820 /// EXISTING table. Emitted by `diff` when a model gains a
821 /// `unique_together` group or a multi-column `indexes` entry with no
822 /// accompanying column change — a case that previously produced NO
823 /// migration at all, so the constraint was silently never created.
824 ///
825 /// `unique` selects `CREATE UNIQUE INDEX` (a `unique_together` group)
826 /// vs a plain `CREATE INDEX` (an `indexes` group). The index NAME is
827 /// deterministic — `uniq_<table>_<cols>` when unique, `idx_<table>_<cols>`
828 /// otherwise — so the matching [`DropIndex`](Operation::DropIndex) can
829 /// name it. Rendered `IF NOT EXISTS` on both backends, so it is a safe
830 /// no-op when a same-migration `AlterColumn` already rebuilt the table
831 /// with the constraint (the SQLite dance) — the two never conflict.
832 AddIndex {
833 table: String,
834 columns: Vec<String>,
835 #[serde(default)]
836 unique: bool,
837 },
838 /// Drop a composite index / UNIQUE constraint previously created by an
839 /// [`AddIndex`](Operation::AddIndex), or by a `CreateTable` that renders
840 /// its `unique_together`/`indexes` as the same deterministically-named
841 /// indexes. Emitted by `diff` when a model LOSES a `unique_together`
842 /// group or `indexes` entry. Rendered `DROP INDEX IF EXISTS <name>` on
843 /// both backends (the name is recomputed from `table` + `columns` +
844 /// `unique`, matching `AddIndex`).
845 DropIndex {
846 table: String,
847 columns: Vec<String>,
848 #[serde(default)]
849 unique: bool,
850 },
851}
852
853impl Operation {
854 /// The primary table this operation targets. For `RenameTable`,
855 /// returns the source name (the post-rename `to` lives in the new
856 /// snapshot, but routing decisions look up the model meta by its
857 /// pre-rename `from`).
858 ///
859 /// Used by `run_in`'s per-DB dispatch loop to route each op to the
860 /// pool where its table actually lives.
861 pub fn table_name(&self) -> &str {
862 match self {
863 Operation::CreateView { name, .. } | Operation::DropView { name, .. } => name,
864 Operation::CreateTable { table, .. }
865 | Operation::DropTable { table }
866 | Operation::AddColumn { table, .. }
867 | Operation::DropColumn { table, .. }
868 | Operation::AlterColumn { table, .. }
869 | Operation::RenameColumn { table, .. }
870 | Operation::SetColumnComment { table, .. }
871 | Operation::AddIndex { table, .. }
872 | Operation::DropIndex { table, .. } => table,
873 Operation::RenameTable { from, .. } => from,
874 Operation::CreateM2MTable { junction_table, .. }
875 | Operation::DropM2MTable { junction_table } => junction_table,
876 // A data migration targets no single table. The empty name
877 // routes it to the `"default"` alias via `table_alias`'s
878 // fallback (see `op_targets_alias`).
879 Operation::RunSql { .. } => "",
880 }
881 }
882}
883
884/// One column inside a [`Operation::CreateTable`].
885///
886/// Mirrors the structure of [`FieldSpec`] but is fully owned for
887/// serialisation. Reconstructed from a `FieldSpec` at diff time.
888#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
889pub struct Column {
890 pub name: String,
891 pub ty: SqlType,
892 pub primary_key: bool,
893 pub nullable: bool,
894 /// For `SqlType::ForeignKey` columns: the SQL table name of the
895 /// referenced model. `None` for all non-FK columns.
896 #[serde(default, skip_serializing_if = "Option::is_none")]
897 pub fk_target: Option<String>,
898 /// When `true`, this field is never shown on any admin form (create or
899 /// edit). Propagated from `FieldSpec::noform`.
900 #[serde(default)]
901 pub noform: bool,
902 /// When `true`, this field is a privileged/server-managed column that the
903 /// untrusted JSON write path (`insert_json`/`update_json`, i.e. REST
904 /// create/update + admin form-submit) strips UNLESS the caller explicitly
905 /// authorizes it via [`DynQuerySet::allow_privileged`]. Propagated from
906 /// `FieldSpec::privileged`. This is the default-DENY guard for mass
907 /// assignment of fields like `is_superuser`/`is_staff`/ownership FKs
908 /// (audit_2 H3): they can't be set by an unprivileged writer even if the
909 /// model exposes them on its create/update surface. Purely a write-auth
910 /// concern — never part of the schema shape, so it is skipped from
911 /// migration snapshots when at its `false` default.
912 #[serde(default, skip_serializing_if = "is_false")]
913 pub privileged: bool,
914 /// Confidential: stripped from serialized output unless the read unlocks it via
915 /// `DynQuerySet::allow_private`. Propagated from `FieldSpec::private`. Purely a
916 /// read-auth concern — never part of the schema shape, so it is skipped from migration
917 /// snapshots at its `false` default, exactly like `privileged`.
918 #[serde(default, skip_serializing_if = "is_false")]
919 pub private: bool,
920 /// Never serialized to a client, with no unlock. Propagated from `FieldSpec::secret`
921 /// (and set automatically for every `Masked<T>` field). Also not schema shape.
922 #[serde(default, skip_serializing_if = "is_false")]
923 pub secret: bool,
924 /// For FK columns: whether to emit a physical `FOREIGN KEY ...
925 /// REFERENCES` constraint. Propagated from `FieldSpec::db_constraint`.
926 /// `false` (set via `#[umbral(db_constraint = false)]`) keeps the
927 /// logical FK (column + `fk_target`) but renders no `REFERENCES`
928 /// clause — the only valid shape for a cross-database FK. Closes
929 /// gaps2 #22. Defaults to `true` so existing migration JSON
930 /// round-trips unchanged (omitted from JSON when at its default).
931 #[serde(default = "default_true", skip_serializing_if = "is_true")]
932 pub db_constraint: bool,
933 /// When `true`, this field appears on the edit form as read-only.
934 /// Propagated from `FieldSpec::noedit`.
935 #[serde(default)]
936 pub noedit: bool,
937 /// Display-string marker — propagated from
938 /// `FieldSpec::is_string_repr`. The admin uses the first column
939 /// with this flag as the default `list_display` label when no
940 /// explicit one is configured.
941 #[serde(default)]
942 pub is_string_repr: bool,
943 /// Display truncation cap — propagated from `FieldSpec::max_length`.
944 /// `0` means no truncation.
945 #[serde(default)]
946 pub max_length: u32,
947 /// Closed-set DB values for a choices column. Propagated from
948 /// `FieldSpec::choices`. Non-empty when the model field carries
949 /// `#[umbral(choices)]`; the migration engine emits a Postgres
950 /// `CHECK (col IN (...))` constraint when this slice is non-empty.
951 /// Empty for every non-choices column.
952 #[serde(default, skip_serializing_if = "Vec::is_empty")]
953 pub choices: Vec<String>,
954 /// Human labels matching `choices` position-for-position. Carried
955 /// alongside `choices` so the admin's `<select>` widget has labels
956 /// without the runtime needing to reflect on the model type.
957 #[serde(default, skip_serializing_if = "Vec::is_empty")]
958 pub choice_labels: Vec<String>,
959 /// SQL `DEFAULT` value — propagated from `FieldSpec::default`.
960 /// Empty string means no default. The migration engine reads this
961 /// at DDL-emit time for both `CREATE TABLE` and `ALTER TABLE ADD
962 /// COLUMN`. Set via `#[umbral(default = "...")]` on the model field.
963 #[serde(default, skip_serializing_if = "String::is_empty")]
964 pub default: String,
965 /// Distinguishes a multi-valued [`MultiChoice<E>`] column from a
966 /// single-valued choices column. Both share `ty: Text` plus the same
967 /// `choices` / `choice_labels` metadata; this flag is the only
968 /// signal that the value is a CSV. Empty / false for every other
969 /// column.
970 ///
971 /// [`MultiChoice<E>`]: crate::orm::MultiChoice
972 #[serde(default, skip_serializing_if = "is_false")]
973 pub is_multichoice: bool,
974
975 /// Carries `FieldSpec::unique` into the migration snapshot. The
976 /// DDL builders emit a `UNIQUE` clause on this column at
977 /// `CREATE TABLE` time when set. Default `false` keeps existing
978 /// migration JSON files round-tripping unchanged (the field is
979 /// omitted on serialise when default).
980 #[serde(default, skip_serializing_if = "is_false")]
981 pub unique: bool,
982
983 /// Carries `FieldSpec::on_delete` into the migration snapshot.
984 /// FK columns only — the DDL builders emit
985 /// `ON DELETE <action>` when this is anything other than
986 /// `NoAction`. Default `NoAction` is omitted from JSON so
987 /// existing migration files round-trip without churn.
988 #[serde(default, skip_serializing_if = "is_no_action")]
989 pub on_delete: crate::orm::FkAction,
990
991 /// Carries `FieldSpec::on_update` into the migration snapshot.
992 /// Same shape as `on_delete`; emits `ON UPDATE <action>`.
993 #[serde(default, skip_serializing_if = "is_no_action")]
994 pub on_update: crate::orm::FkAction,
995
996 /// Carries `FieldSpec::index` into the migration snapshot. The
997 /// CreateTable + AddColumn render paths emit a matching
998 /// `CREATE INDEX idx_<table>_<col>` for every column whose
999 /// flag is set. Default `false` keeps existing migration JSON
1000 /// round-tripping unchanged.
1001 #[serde(default, skip_serializing_if = "is_false")]
1002 pub index: bool,
1003
1004 /// Carries `FieldSpec::auto_now_add` into the migration
1005 /// snapshot. The dynamic write path (`DynQuerySet::insert_json`)
1006 /// auto-populates the column with `Utc::now()` when the body
1007 /// omits it. Default `false` so existing migration JSON
1008 /// round-trips unchanged.
1009 #[serde(default, skip_serializing_if = "is_false")]
1010 pub auto_now_add: bool,
1011 /// Carries `FieldSpec::auto_user_add` to the runtime (dyn) write path, so
1012 /// admin and REST stamp the author too — they run on `DynQuerySet`, not the
1013 /// typed one. Not a schema property: skipped in snapshots.
1014 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1015 pub auto_user_add: bool,
1016 /// Carries `FieldSpec::auto_user`.
1017 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
1018 pub auto_user: bool,
1019
1020 /// Carries `FieldSpec::auto_now` into the migration snapshot.
1021 /// Same shape as `auto_now_add` but fires on update too.
1022 #[serde(default, skip_serializing_if = "is_false")]
1023 pub auto_now: bool,
1024
1025 /// Carries `FieldSpec::trim` into the snapshot. Behavioral (dynamic write
1026 /// path strips surrounding whitespace), not schema-affecting — excluded
1027 /// from the schema diff like `auto_now`. Default `false` so existing
1028 /// migration JSON round-trips unchanged.
1029 #[serde(default, skip_serializing_if = "is_false")]
1030 pub trim: bool,
1031
1032 /// Carries `FieldSpec::lowercase` into the snapshot. Behavioral (dynamic
1033 /// write path lowercases the value), not schema-affecting. Default `false`
1034 /// so existing migration JSON round-trips unchanged.
1035 #[serde(default, skip_serializing_if = "is_false")]
1036 pub lowercase: bool,
1037
1038 /// Carries `FieldSpec::case_insensitive` into the snapshot. Schema-affecting
1039 /// (Postgres `citext` / SQLite `COLLATE NOCASE`), but — like `unique` — the
1040 /// diff comparator (`column_shape`) does NOT watch it, so it applies at
1041 /// CREATE TABLE and toggling it on a live column needs a hand-written
1042 /// migration. Default `false` so existing migration JSON round-trips.
1043 #[serde(default, skip_serializing_if = "is_false")]
1044 pub case_insensitive: bool,
1045
1046 /// Carries `FieldSpec::help` into the migration snapshot.
1047 /// Default empty string is omitted from JSON so existing
1048 /// migration files round-trip unchanged.
1049 #[serde(default, skip_serializing_if = "String::is_empty")]
1050 pub help: String,
1051
1052 /// Carries `FieldSpec::example` into the migration snapshot.
1053 /// Same shape as `help`.
1054 #[serde(default, skip_serializing_if = "String::is_empty")]
1055 pub example: String,
1056
1057 /// Carries `FieldSpec::widget` into the migration snapshot — the
1058 /// form-renderer presentation hint (features.md #4). Presentation
1059 /// only, no DB effect, so it's excluded from the schema diff the
1060 /// same way `help` / `example` are. `None` is omitted from JSON so
1061 /// existing migration files round-trip unchanged.
1062 #[serde(default, skip_serializing_if = "Option::is_none")]
1063 pub widget: Option<String>,
1064
1065 /// Carries `FieldSpec::supported_backends` into the migration
1066 /// snapshot. When non-empty, the boot system check rejects the
1067 /// model on any backend not listed. Closes IMP-5 from
1068 /// `bugs/tests/testBugs.md`. Default empty (works on every
1069 /// backend); JSON skip-when-empty so existing migration files
1070 /// don't churn.
1071 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1072 pub supported_backends: Vec<String>,
1073
1074 /// IMP-3: numeric lower bound. `None` means "no minimum"; the
1075 /// DDL emits a `CHECK (col >= N)` constraint when set.
1076 #[serde(default, skip_serializing_if = "Option::is_none")]
1077 pub min: Option<i64>,
1078
1079 /// IMP-3: numeric upper bound. Same shape as `min`.
1080 #[serde(default, skip_serializing_if = "Option::is_none")]
1081 pub max: Option<i64>,
1082
1083 /// BUG-11/12/13: constrained-text marker. `None` is plain text;
1084 /// `Some("slug" | "email" | "url")` flags the column as a
1085 /// `Slug` / `Email` / `Url` wrapper. OpenAPI emits the
1086 /// corresponding `format` / `pattern`; the REST plugin
1087 /// pre-validates the body via `validate_text_format`.
1088 #[serde(default, skip_serializing_if = "Option::is_none")]
1089 pub text_format: Option<String>,
1090
1091 /// Gap 109: auto-derive source. When `Some("title")`, the slug is
1092 /// computed from the row's `title` column at write time if the
1093 /// slug column itself is empty / missing on the body. Pure
1094 /// runtime behaviour — has no DDL effect, so the diff engine
1095 /// ignores changes to this field. `#[serde(default)]` keeps
1096 /// older snapshots round-tripping.
1097 #[serde(default, skip_serializing_if = "Option::is_none")]
1098 pub slug_from: Option<String>,
1099}
1100
1101fn is_no_action(a: &crate::orm::FkAction) -> bool {
1102 matches!(a, crate::orm::FkAction::NoAction)
1103}
1104
1105/// Build a portable `CREATE INDEX IF NOT EXISTS idx_<table>_<col>
1106/// ON "<table>" ("<col>")` statement. Same DDL on SQLite and
1107/// Postgres — both accept `CREATE INDEX IF NOT EXISTS` and the
1108/// `idx_<table>_<col>` name convention is unique enough that the
1109/// migration engine can re-emit it idempotently on subsequent
1110/// applies. Used by [`render_operation_sqlite`] / `_postgres`
1111/// after a `CreateTable` or `AddColumn` op whose column carries
1112/// the `#[umbral(index)]` flag. Closes BUG-4.
1113/// Postgres `COMMENT ON COLUMN` for one column (gaps3 #43).
1114///
1115/// An empty `comment` renders `IS NULL`, which is Postgres's "this column has no
1116/// comment" — distinct from `IS ''`, an empty comment that `\d+` would print as
1117/// a blank line.
1118///
1119/// Help text is prose, and prose has apostrophes, so the literal is escaped by
1120/// doubling single quotes. The value originates in a `#[umbral(help = "...")]`
1121/// attribute (a compile-time literal, not user input), but a migration file is
1122/// hand-editable and an unescaped quote would produce a syntax error at apply
1123/// time rather than a diagnostic at generate time.
1124fn comment_on_column_stmt(table: &str, column: &str, comment: &str) -> String {
1125 let t = table.replace('"', "\"\"");
1126 let c = column.replace('"', "\"\"");
1127 if comment.is_empty() {
1128 return format!("COMMENT ON COLUMN \"{t}\".\"{c}\" IS NULL");
1129 }
1130 let body = comment.replace('\'', "''");
1131 format!("COMMENT ON COLUMN \"{t}\".\"{c}\" IS '{body}'")
1132}
1133
1134fn create_index_stmt(table: &str, column: &str) -> String {
1135 let t = table.replace('"', "\"\"");
1136 let c = column.replace('"', "\"\"");
1137 format!(
1138 "CREATE INDEX IF NOT EXISTS \"idx_{table}_{column}\" ON \"{t}\" (\"{c}\")",
1139 table = table.replace('"', ""),
1140 column = column.replace('"', ""),
1141 )
1142}
1143
1144/// Build a Postgres `CREATE INDEX ... USING GIN` for a `tsvector`
1145/// (`SqlType::FullText`) column (#33). A tsvector column is useless for
1146/// search without a GIN index, so the migration engine emits one
1147/// automatically for every full-text column — the caller never has to
1148/// hand-write it. **Postgres-only**: GIN is Postgres syntax and FullText
1149/// columns are system-check-gated to Postgres, so this only ever renders
1150/// from `render_operation_postgres`. The `_gin` name suffix keeps it
1151/// distinct from any plain index on the same column.
1152fn create_gin_index_stmt(table: &str, column: &str) -> String {
1153 let t = table.replace('"', "\"\"");
1154 let c = column.replace('"', "\"\"");
1155 format!(
1156 "CREATE INDEX IF NOT EXISTS \"idx_{table}_{column}_gin\" ON \"{t}\" USING GIN (\"{c}\")",
1157 table = table.replace('"', ""),
1158 column = column.replace('"', ""),
1159 )
1160}
1161
1162/// Multi-column variant of [`create_index_stmt`]. Closes BUG-7.
1163/// Renders `CREATE INDEX IF NOT EXISTS idx_<table>_<col1>_<col2>
1164/// ON "<table>" ("<col1>", "<col2>")`. Both backends accept the
1165/// same form. Empty groups render no statement (defensive — the
1166/// macro layer rejects them before the engine sees them, but the
1167/// helper still returns a no-op SQL string to keep the caller
1168/// simple).
1169fn create_multi_index_stmt(table: &str, columns: &[String]) -> String {
1170 // A plain composite index IS an `AddIndex { unique: false }` render —
1171 // delegate so the NAME (`idx_<table>_<cols>`) is defined in exactly one
1172 // place and a `CreateTable`'s composite index and a later `DropIndex`
1173 // always agree on it.
1174 add_index_stmt(table, columns, false)
1175}
1176
1177/// Deterministic name for a composite index. `unique` selects the `uniq_`
1178/// prefix (a `unique_together` group), otherwise `idx_`. Derived purely
1179/// from the quote-stripped table + column list so an [`Operation::AddIndex`]
1180/// and the later [`Operation::DropIndex`] that reverses it always compute
1181/// the same name. The `uniq_`/`idx_` split means a UNIQUE and a plain index
1182/// on the SAME columns never collide.
1183fn index_name(table: &str, columns: &[String], unique: bool) -> String {
1184 let t = table.replace('"', "");
1185 let suffix = columns
1186 .iter()
1187 .map(|c| c.replace('"', ""))
1188 .collect::<Vec<_>>()
1189 .join("_");
1190 let prefix = if unique { "uniq" } else { "idx" };
1191 format!("{prefix}_{t}_{suffix}")
1192}
1193
1194/// `CREATE [UNIQUE] INDEX IF NOT EXISTS "<name>" ON "<table>" (cols)` —
1195/// identical syntax on SQLite and Postgres. The index NAME is a bare
1196/// identifier (via [`index_name`]); the ON-clause table reference is a
1197/// *quoted* identifier with inner quotes doubled. An empty column list
1198/// renders an empty string (defensive no-op; the macro layer rejects
1199/// empty groups upstream).
1200fn add_index_stmt(table: &str, columns: &[String], unique: bool) -> String {
1201 if columns.is_empty() {
1202 return String::new();
1203 }
1204 let name = index_name(table, columns, unique);
1205 let t_esc = table.replace('"', "\"\"");
1206 let col_list = columns
1207 .iter()
1208 .map(|c| format!("\"{}\"", c.replace('"', "\"\"")))
1209 .collect::<Vec<_>>()
1210 .join(", ");
1211 let unique_kw = if unique { "UNIQUE " } else { "" };
1212 format!("CREATE {unique_kw}INDEX IF NOT EXISTS \"{name}\" ON \"{t_esc}\" ({col_list})")
1213}
1214
1215/// `DROP INDEX IF EXISTS "<name>"` — same on both backends. Postgres
1216/// resolves the unqualified name via the search_path (so a schema-per-tenant
1217/// migrate drops the index inside the active schema).
1218fn drop_index_stmt(name: &str) -> String {
1219 let n = name.replace('"', "\"\"");
1220 format!("DROP INDEX IF EXISTS \"{n}\"")
1221}
1222
1223/// Lower an M2M junction column's PK type into the SQLite column
1224/// declaration string used inside the raw `CREATE TABLE` template.
1225/// SQLite has affinity types: every integer width stores as `INTEGER`
1226/// (one ROWID-aliased column), and TEXT covers `String` / `Uuid`.
1227/// Closes BUG-16 phase 2.
1228fn m2m_pk_sql_type_sqlite(ty: crate::orm::SqlType) -> &'static str {
1229 use crate::orm::SqlType;
1230 match ty {
1231 SqlType::SmallInt | SqlType::Integer | SqlType::BigInt | SqlType::ForeignKey => "INTEGER",
1232 // A junction's `child_id` must be declared as whatever the CHILD's primary key
1233 // actually is, or the `REFERENCES` clause points at a column of a different type.
1234 // sqlx stores a `Uuid` as its 16 raw bytes on SQLite, so that is BLOB (gaps3 #80).
1235 SqlType::Uuid => "BLOB",
1236 SqlType::Text => "TEXT",
1237 // The macro-side classifier only sets these for PK columns
1238 // when the user wrote a non-standard PK type. If we ever
1239 // see one here that doesn't make sense as a junction column
1240 // (Boolean, Date, Real, …), TEXT is the safest catch-all
1241 // affinity — SQLite will accept it and the rest of the
1242 // ORM will surface the deeper "this can't be a PK" error
1243 // through the system check.
1244 _ => "TEXT",
1245 }
1246}
1247
1248/// Lower an M2M junction column's PK type into the Postgres column
1249/// declaration string. Postgres is strict about types — `BIGINT` for
1250/// 64-bit integers, `INTEGER` for 32-bit, `SMALLINT` for 16-bit,
1251/// `TEXT` for `String`, `UUID` for `uuid::Uuid`. Mirrors the choices
1252/// `build_column_def_postgres` makes for the same `SqlType` variants.
1253fn m2m_pk_sql_type_postgres(ty: crate::orm::SqlType) -> &'static str {
1254 use crate::orm::SqlType;
1255 match ty {
1256 SqlType::SmallInt => "SMALLINT",
1257 SqlType::Integer => "INTEGER",
1258 SqlType::BigInt | SqlType::ForeignKey => "BIGINT",
1259 SqlType::Text => "TEXT",
1260 SqlType::Uuid => "UUID",
1261 _ => "TEXT",
1262 }
1263}
1264
1265/// Build the ` ON DELETE <action> ON UPDATE <action>` suffix for a
1266/// FK column. Each half is emitted only when its action is anything
1267/// other than `NoAction` — keeps the generated DDL minimal and
1268/// matches the SQL standard's default (NO ACTION when the clause is
1269/// omitted).
1270///
1271/// Closes gap #68. Shared between the SQLite and Postgres builders
1272/// because the REFERENCES tail syntax is identical on both.
1273fn fk_action_suffix(col: &Column) -> String {
1274 let mut s = String::new();
1275 if let Some(kw) = col.on_delete.sql_keyword() {
1276 s.push_str(" ON DELETE ");
1277 s.push_str(kw);
1278 }
1279 if let Some(kw) = col.on_update.sql_keyword() {
1280 s.push_str(" ON UPDATE ");
1281 s.push_str(kw);
1282 }
1283 s
1284}
1285
1286fn is_false(b: &bool) -> bool {
1287 !*b
1288}
1289
1290/// serde default for `Column::db_constraint`: a FK emits its physical
1291/// `REFERENCES` constraint unless the model opts out. Older migration
1292/// JSON predating gaps2 #22 has no `db_constraint` key, so it must
1293/// deserialize as `true` to preserve the historical "always emit"
1294/// behaviour.
1295fn default_true() -> bool {
1296 true
1297}
1298
1299fn is_true(b: &bool) -> bool {
1300 *b
1301}
1302
1303impl From<&FieldSpec> for Column {
1304 fn from(f: &FieldSpec) -> Self {
1305 Self {
1306 name: f.name.to_string(),
1307 ty: f.ty,
1308 primary_key: f.primary_key,
1309 nullable: f.nullable,
1310 fk_target: f.fk_target.map(|s| s.to_string()),
1311 noform: f.noform,
1312 privileged: f.privileged,
1313 private: f.private,
1314 secret: f.secret,
1315 db_constraint: f.db_constraint,
1316 noedit: f.noedit,
1317 is_string_repr: f.is_string_repr,
1318 max_length: f.max_length,
1319 choices: f.choices.iter().map(|s| s.to_string()).collect(),
1320 choice_labels: f.choice_labels.iter().map(|s| s.to_string()).collect(),
1321 default: f.default.to_string(),
1322 is_multichoice: f.is_multichoice,
1323 unique: f.unique,
1324 on_delete: f.on_delete,
1325 on_update: f.on_update,
1326 index: f.index,
1327 auto_now_add: f.auto_now_add,
1328 auto_user_add: f.auto_user_add,
1329 auto_user: f.auto_user,
1330 auto_now: f.auto_now,
1331 trim: f.trim,
1332 lowercase: f.lowercase,
1333 case_insensitive: f.case_insensitive,
1334 help: f.help.to_string(),
1335 example: f.example.to_string(),
1336 widget: f.widget.map(|s| s.to_string()),
1337 supported_backends: f.supported_backends.iter().map(|s| s.to_string()).collect(),
1338 min: f.min,
1339 max: f.max,
1340 text_format: f.text_format.map(|s| s.to_string()),
1341 slug_from: f.slug_from.map(|s| s.to_string()),
1342 }
1343 }
1344}
1345
1346/// The on-disk shape of one migration. Files in `migrations/<plugin>/`
1347/// deserialize into this struct.
1348#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1349pub struct MigrationFile {
1350 /// Stable id, matches the filename minus `.json`.
1351 pub id: String,
1352 /// The plugin that owns this migration. M5 hardcodes `"app"` for
1353 /// the user's binary; M7 generalises to one directory per plugin.
1354 pub plugin: String,
1355 /// Predecessor migrations, in `(plugin, id)` form. Within-plugin
1356 /// predecessors are implicit (the prior numeric file); cross-
1357 /// plugin predecessors land at M7.
1358 #[serde(default)]
1359 pub depends_on: Vec<MigrationRef>,
1360 /// Ordered operations applied when this migration runs.
1361 pub operations: Vec<Operation>,
1362 /// The full snapshot of every model after this migration has run.
1363 /// Source of truth for the next `make` to diff against.
1364 pub snapshot_after: Snapshot,
1365 /// gaps2 #100: when non-empty, this is a *squash* — it collapses the
1366 /// listed predecessor migrations into one optimized file. The originals
1367 /// are kept on disk (non-destructive) so older deploys still migrate; the
1368 /// runner treats the squash and its replaced set as mutually exclusive
1369 /// (see [`squash_plan`]). Empty for an ordinary migration; `#[serde(default)]`
1370 /// so every pre-squash file on disk deserializes unchanged.
1371 #[serde(default, skip_serializing_if = "Vec::is_empty")]
1372 pub replaces: Vec<MigrationRef>,
1373}
1374
1375/// A pointer to one (plugin, migration_id) pair.
1376#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1377pub struct MigrationRef {
1378 pub plugin: String,
1379 pub migration: String,
1380}
1381
1382/// gaps2 #100 — what the runner does with one on-disk migration file, given the
1383/// tracking-table's applied-set. The decision is pure and backend-agnostic; the
1384/// only DB-specific step downstream is *how* a tracking row is written, never
1385/// *whether* the file's operations run.
1386#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1387pub enum ApplyDecision {
1388 /// Run the file's operations, then record it in the tracking table. The
1389 /// normal "pending migration" path.
1390 Apply,
1391 /// Do nothing: the migration is already applied, or it's an original that a
1392 /// squash the runner is using has shadowed (running it too would
1393 /// double-apply the schema the squash already built).
1394 Skip,
1395 /// A squash whose entire replaced set is already applied. Its operations
1396 /// would rebuild schema that already exists, so DON'T run them — but insert
1397 /// a tracking row so future runs treat the squash as applied and the now
1398 /// redundant original files can be deleted.
1399 RecordOnly,
1400}
1401
1402/// gaps2 #100 — decide, per on-disk migration file for ONE plugin, whether to
1403/// [`Apply`](ApplyDecision::Apply) / [`Skip`](ApplyDecision::Skip) /
1404/// [`RecordOnly`](ApplyDecision::RecordOnly) it, honoring squash `replaces`.
1405///
1406/// `files` is every migration file for the plugin (the returned Vec is in the
1407/// same order). `applied` is the `(plugin, id)` set from the tracking table.
1408/// The function is pure: no DB, no IO — so both the SQLite and Postgres apply
1409/// loops route through it and inherit identical squash semantics.
1410///
1411/// For a squash `S` replacing set `R`:
1412/// - **all of `R` applied** → `S` is `RecordOnly` (or `Skip` if `S` itself is
1413/// already recorded); every member of `R` is `Skip` (already applied).
1414/// - **none of `R` applied** → `S` is `Apply` (or `Skip` if already recorded);
1415/// every member of `R` is `Skip` (the squash builds their schema).
1416/// - **some of `R` applied** (an interrupted transition) → fall back to the
1417/// individual originals if they are ALL still on disk: `S` is `Skip` and each
1418/// unapplied member of `R` applies as usual. If any original is gone, the
1419/// history can't be reconciled → [`MigrateError::SquashInconsistent`].
1420///
1421/// A plain migration (empty `replaces`) is `Skip` if applied or shadowed by an
1422/// active squash, else `Apply`.
1423fn squash_plan(
1424 files: &[MigrationFile],
1425 applied: &std::collections::HashSet<(String, String)>,
1426) -> Result<Vec<ApplyDecision>, MigrateError> {
1427 use std::collections::HashSet;
1428
1429 let is_applied =
1430 |plugin: &str, id: &str| applied.contains(&(plugin.to_string(), id.to_string()));
1431 let on_disk: HashSet<&str> = files.iter().map(|f| f.id.as_str()).collect();
1432
1433 let mut decisions = vec![ApplyDecision::Apply; files.len()];
1434 // Ids covered by a squash the runner is USING (fully-applied or fully-
1435 // unapplied). Their originals must be skipped so they don't double-apply.
1436 let mut shadowed: HashSet<String> = HashSet::new();
1437
1438 // First pass: classify every squash and collect the shadow set.
1439 for (i, f) in files.iter().enumerate() {
1440 if f.replaces.is_empty() {
1441 continue;
1442 }
1443 let applied_in_r = f
1444 .replaces
1445 .iter()
1446 .filter(|m| is_applied(&m.plugin, &m.migration))
1447 .count();
1448 let self_applied = is_applied(&f.plugin, &f.id);
1449
1450 if applied_in_r == f.replaces.len() {
1451 // Whole history already applied: adopt the squash without running it.
1452 decisions[i] = if self_applied {
1453 ApplyDecision::Skip
1454 } else {
1455 ApplyDecision::RecordOnly
1456 };
1457 for m in &f.replaces {
1458 shadowed.insert(m.migration.clone());
1459 }
1460 } else if applied_in_r == 0 {
1461 // Fresh: the squash builds the whole schema in one shot.
1462 decisions[i] = if self_applied {
1463 ApplyDecision::Skip
1464 } else {
1465 ApplyDecision::Apply
1466 };
1467 for m in &f.replaces {
1468 shadowed.insert(m.migration.clone());
1469 }
1470 } else {
1471 // Partial transition: prefer the originals if they all survive.
1472 let missing: Vec<String> = f
1473 .replaces
1474 .iter()
1475 .filter(|m| !on_disk.contains(m.migration.as_str()))
1476 .map(|m| format!("{}/{}", m.plugin, m.migration))
1477 .collect();
1478 if missing.is_empty() {
1479 // Inactive squash: skip it, let the originals run individually.
1480 decisions[i] = ApplyDecision::Skip;
1481 } else {
1482 return Err(MigrateError::SquashInconsistent {
1483 plugin: f.plugin.clone(),
1484 squash: f.id.clone(),
1485 missing,
1486 });
1487 }
1488 }
1489 }
1490
1491 // Second pass: plain migrations (applied or shadowed → Skip, else Apply).
1492 for (i, f) in files.iter().enumerate() {
1493 if !f.replaces.is_empty() {
1494 continue;
1495 }
1496 if shadowed.contains(&f.id) || is_applied(&f.plugin, &f.id) {
1497 decisions[i] = ApplyDecision::Skip;
1498 }
1499 }
1500
1501 // A squash can itself be shadowed by a larger squash (squash-of-squashes):
1502 // an active outer squash covers it, so it must not run either.
1503 for (i, f) in files.iter().enumerate() {
1504 if shadowed.contains(&f.id) {
1505 decisions[i] = ApplyDecision::Skip;
1506 }
1507 }
1508
1509 Ok(decisions)
1510}
1511
1512/// At M5 every migration belongs to a single placeholder plugin. M7's
1513/// Plugin contract replaces this with `Plugin::name()`.
1514pub const APP_PLUGIN_NAME: &str = "app";
1515
1516/// Default directory for migration files. `make` writes into
1517/// `migrations/<plugin>/`; `run` reads from the same place. Override
1518/// with `--migrations-dir` once the CLI grows real arg parsing (M5+).
1519pub const MIGRATIONS_DIR: &str = "migrations";
1520
1521/// The state of a single migration from the perspective of drift detection.
1522/// Returned inside [`DriftReport`] so callers can decide how to handle each
1523/// state independently.
1524#[derive(Debug, Clone, PartialEq, Eq)]
1525pub enum MigrationStatus {
1526 /// The migration is recorded in the tracking table AND the file
1527 /// exists on disk. Normal applied state.
1528 Applied,
1529 /// The migration is recorded in the tracking table BUT the
1530 /// corresponding file is missing from disk. The database is ahead
1531 /// of what version control has; recovering requires restoring the
1532 /// file or running with `--allow-drift`.
1533 AppliedButMissing,
1534 /// The migration file exists on disk AND its sequence number is
1535 /// lower than the highest applied migration for this plugin, but it
1536 /// is not recorded in the tracking table. Looks like someone dropped
1537 /// a migration file back into a directory after a teammate already
1538 /// applied later ones. Should warn, not error.
1539 OutOfOrder,
1540 /// Normal pending state: the file is on disk and its sequence number
1541 /// is higher than anything applied. Ready to apply.
1542 Pending,
1543}
1544
1545/// Per-migration entry inside a [`DriftReport`].
1546#[derive(Debug, Clone, PartialEq, Eq)]
1547pub struct MigrationEntry {
1548 pub plugin: String,
1549 pub name: String,
1550 pub status: MigrationStatus,
1551}
1552
1553/// The output of [`detect_drift`]: one entry per migration (applied or
1554/// on-disk), categorised into the four states above.
1555///
1556/// The caller inspects `has_critical_drift()` to decide whether to abort
1557/// before applying migrations. Surfaced by `show_in_with_drift` for
1558/// `showmigrations` and checked by `run_in_with_drift_check` before
1559/// executing any SQL.
1560#[derive(Debug, Clone, Default)]
1561pub struct DriftReport {
1562 pub entries: Vec<MigrationEntry>,
1563}
1564
1565impl DriftReport {
1566 /// Returns true when at least one migration is `AppliedButMissing`.
1567 /// This state means the tracking table references a file that no
1568 /// longer exists on disk — the operator needs to act before it is
1569 /// safe to continue applying new migrations.
1570 pub fn has_critical_drift(&self) -> bool {
1571 self.entries
1572 .iter()
1573 .any(|e| e.status == MigrationStatus::AppliedButMissing)
1574 }
1575
1576 /// All migrations with `AppliedButMissing` status. Convenience
1577 /// accessor for building the error message.
1578 pub fn missing_on_disk(&self) -> Vec<&MigrationEntry> {
1579 self.entries
1580 .iter()
1581 .filter(|e| e.status == MigrationStatus::AppliedButMissing)
1582 .collect()
1583 }
1584
1585 /// Migrations on disk this binary knows about that the database has NOT
1586 /// applied yet — the readiness blocker (Kikosi #5 / gaps3 #38). Empty means
1587 /// the schema is at least as new as this binary's code.
1588 ///
1589 /// Deliberately excludes `AppliedButMissing` (the database is *ahead*: a
1590 /// valid rollback / backward-compatible-migration state where an older
1591 /// binary should keep serving) and `OutOfOrder` (a stray file the migrate
1592 /// engine only warns about). Only `Pending` — "the schema is behind the
1593 /// code" — should hold traffic off a freshly-booted pod.
1594 pub fn pending(&self) -> Vec<&MigrationEntry> {
1595 self.entries
1596 .iter()
1597 .filter(|e| e.status == MigrationStatus::Pending)
1598 .collect()
1599 }
1600}
1601
1602/// Errors the migration engine can produce.
1603#[derive(Debug)]
1604pub enum MigrateError {
1605 /// IO error reading or writing a migration file or directory.
1606 Io(std::io::Error),
1607 /// JSON parse error on a migration file.
1608 Json(serde_json::Error),
1609 /// sqlx error executing a migration's SQL or touching the
1610 /// tracking table.
1611 Sqlx(sqlx::Error),
1612 /// `make` ran but found no differences against the latest snapshot,
1613 /// so there's nothing to write. Surfaced so the CLI can print
1614 /// "no changes detected" instead of an empty migration file.
1615 NoChanges,
1616 /// The current models diverge from the snapshot in a way M5 v1
1617 /// can't represent yet (anything other than create/drop table).
1618 /// M5.1 lifts this when column-level ops land.
1619 UnsupportedChange(String),
1620 /// A column-level change the engine can't apply automatically:
1621 /// type change, or a nullable flip on a populated SQLite table.
1622 /// Surfaces from `diff` so the build stops before producing a
1623 /// migration that would lose data or fail to apply. The user
1624 /// resolves by hand-writing the migration with the appropriate
1625 /// data-preserving steps. Carries the model / column / reason.
1626 UnsafeAlter {
1627 model: String,
1628 column: String,
1629 reason: String,
1630 },
1631 /// The tracking table records migrations that no longer have
1632 /// corresponding files on disk. Carries the list of missing names.
1633 /// The operator must either restore the files from VCS or run with
1634 /// `--allow-drift` to proceed despite the inconsistency.
1635 DriftDetected { missing: Vec<(String, String)> },
1636 /// A schema-scoped migration ([`run_for_schema`]) was requested against a
1637 /// SQLite pool. SQLite has no schemas, so schema-per-tenant is Postgres-only
1638 /// (mirrors how `Inet`/`Cidr` gate on backend). Carries the schema name.
1639 SchemaUnsupportedOnSqlite { schema: String },
1640 /// `makemigrations --empty <plugin>` named a plugin that isn't
1641 /// registered. Carries the requested name and the registered set so
1642 /// the CLI can list the valid choices.
1643 UnknownPlugin {
1644 requested: String,
1645 known: Vec<String>,
1646 },
1647 /// audit_2 H23 — the column-shape rename heuristic found an unpaired
1648 /// dropped model and an unpaired created model with **identical** column
1649 /// shapes. That's genuinely ambiguous: it's either a model rename (move the
1650 /// old table's rows to the new name) or two unrelated models that happen to
1651 /// share a shape (drop the old, create the new empty). Auto-applying either
1652 /// silently loses or mis-associates data, so `diff` refuses to guess and
1653 /// fails closed. The operator resolves it explicitly via
1654 /// `UMBRAL_MIGRATIONS_ASSUME_RENAMES` (`assume` → rename, `independent` →
1655 /// drop+create) or by hand-writing the op. Carries the two table names.
1656 AmbiguousRename {
1657 from_table: String,
1658 to_table: String,
1659 },
1660 /// audit_2 core-migrate #7 — couldn't acquire the Postgres migration
1661 /// advisory lock within the timeout: another process has been holding it
1662 /// (running a long migration, or wedged). Carries the alias/schema the lock
1663 /// was keyed on and the seconds waited. The operator retries once the other
1664 /// migrator finishes, or investigates a stuck migration.
1665 MigrationLockTimeout {
1666 discriminator: String,
1667 waited_secs: u64,
1668 },
1669 /// gaps2 #100 — a squash migration is stuck in a partially-applied state:
1670 /// SOME of the migrations it replaces are recorded as applied and others
1671 /// aren't, and the unapplied originals are no longer on disk to fall back
1672 /// to. The engine can't safely apply the squash (it would re-run schema the
1673 /// applied originals already built) nor complete the originals (their files
1674 /// are gone). Carries the plugin and squash id. The operator restores the
1675 /// missing original files from VCS (finish the transition), or resets the
1676 /// tracking rows for this plugin to a consistent point.
1677 SquashInconsistent {
1678 plugin: String,
1679 squash: String,
1680 missing: Vec<String>,
1681 },
1682 /// gaps2 #100 — `squashmigrations <plugin>` couldn't produce a squash:
1683 /// fewer than two squashable migrations, an unknown plugin, or the history
1684 /// already contains a squash (nested squashing isn't supported yet). Carries
1685 /// the plugin and a human-readable reason.
1686 CannotSquash { plugin: String, reason: String },
1687}
1688
1689impl std::fmt::Display for MigrateError {
1690 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1691 match self {
1692 MigrateError::Io(e) => write!(f, "umbral migrate: io: {e}"),
1693 MigrateError::Json(e) => write!(f, "umbral migrate: json: {e}"),
1694 MigrateError::Sqlx(e) => write!(f, "umbral migrate: sqlx: {e}"),
1695 MigrateError::NoChanges => write!(
1696 f,
1697 "umbral migrate: no changes detected; declare or change a model first"
1698 ),
1699 MigrateError::UnsupportedChange(msg) => {
1700 write!(f, "umbral migrate: unsupported change at M5 v1: {msg}")
1701 }
1702 MigrateError::UnsafeAlter {
1703 model,
1704 column,
1705 reason,
1706 } => write!(
1707 f,
1708 "umbral migrate: unsafe column change on `{model}.{column}`: {reason}; \
1709 hand-write the migration with a data-preserving step"
1710 ),
1711 MigrateError::DriftDetected { missing } => {
1712 let names: Vec<String> = missing
1713 .iter()
1714 .map(|(plugin, name)| format!("{plugin}/{name}"))
1715 .collect();
1716 write!(
1717 f,
1718 "umbral migrate: drift detected — the following migrations are recorded in \
1719 the tracking table but their files are missing from disk:\n {}\n\
1720 Restore the files from VCS or run `umbral migrate --allow-drift` to \
1721 proceed despite the inconsistency.",
1722 names.join("\n ")
1723 )
1724 }
1725 MigrateError::SchemaUnsupportedOnSqlite { schema } => write!(
1726 f,
1727 "umbral migrate: schema-per-tenant migration into `{schema}` requires \
1728 Postgres; SQLite has no schemas. Point the app at a Postgres pool."
1729 ),
1730 MigrateError::UnknownPlugin { requested, known } => write!(
1731 f,
1732 "umbral makemigrations --empty: no registered plugin named `{requested}`. \
1733 Known plugins: {}",
1734 known.join(", ")
1735 ),
1736 MigrateError::AmbiguousRename {
1737 from_table,
1738 to_table,
1739 } => write!(
1740 f,
1741 "umbral makemigrations: ambiguous rename — the dropped model `{from_table}` and \
1742 the new model `{to_table}` have identical column shapes, so this is either a \
1743 rename (move `{from_table}`'s rows to `{to_table}`) or two unrelated models. \
1744 Refusing to guess: auto-renaming would hand one model's rows to another and skip \
1745 the intended drop, while auto-dropping would delete `{from_table}`'s rows — both \
1746 silent data bugs. Resolve it: set UMBRAL_MIGRATIONS_ASSUME_RENAMES=assume to \
1747 treat every shape match as a rename, or =independent to treat them as unrelated \
1748 (drop + create), or hand-write the intended op into the migration file."
1749 ),
1750 MigrateError::MigrationLockTimeout {
1751 discriminator,
1752 waited_secs,
1753 } => write!(
1754 f,
1755 "umbral migrate: timed out after {waited_secs}s waiting for the Postgres \
1756 migration lock (alias/schema `{discriminator}`). Another process is holding it — \
1757 a long-running migration on another replica, or a wedged migrator. Retry once it \
1758 finishes; if nothing is migrating, check for a stuck backend holding \
1759 pg_advisory_lock."
1760 ),
1761 MigrateError::SquashInconsistent {
1762 plugin,
1763 squash,
1764 missing,
1765 } => write!(
1766 f,
1767 "umbral migrate: squash `{plugin}/{squash}` is half-applied — some of the \
1768 migrations it replaces are recorded as applied and the unapplied ones are \
1769 missing from disk:\n {}\nRestore those original migration files from VCS so \
1770 the transition can finish, then re-run migrate.",
1771 missing.join("\n ")
1772 ),
1773 MigrateError::CannotSquash { plugin, reason } => write!(
1774 f,
1775 "umbral squashmigrations: can't squash `{plugin}`: {reason}"
1776 ),
1777 }
1778 }
1779}
1780
1781impl std::error::Error for MigrateError {}
1782
1783impl From<std::io::Error> for MigrateError {
1784 fn from(e: std::io::Error) -> Self {
1785 Self::Io(e)
1786 }
1787}
1788
1789impl From<serde_json::Error> for MigrateError {
1790 fn from(e: serde_json::Error) -> Self {
1791 Self::Json(e)
1792 }
1793}
1794
1795impl From<sqlx::Error> for MigrateError {
1796 fn from(e: sqlx::Error) -> Self {
1797 Self::Sqlx(e)
1798 }
1799}
1800
1801// =========================================================================
1802// Top-level entry points.
1803// =========================================================================
1804
1805/// Generate one migration file per registered plugin that has changes,
1806/// diffing each plugin's current model set against the latest snapshot
1807/// in `migrations/<plugin>/`. Each new file lands inside its own
1808/// plugin directory with the next sequence number and a `_<short_name>`
1809/// suffix derived from the dominant operation.
1810///
1811/// Returns the paths of every file written, one per plugin that had a
1812/// non-empty diff. Returns `MigrateError::NoChanges` if no plugin
1813/// produced any changes at all.
1814pub async fn make() -> Result<Vec<PathBuf>, MigrateError> {
1815 make_in(Path::new(MIGRATIONS_DIR)).await
1816}
1817
1818/// Same as [`make`] but takes an explicit base directory. Used by
1819/// tests to avoid touching the cwd.
1820///
1821/// Iterates [`plugin_order`], which is the topological order
1822/// published by `App::build()`'s phase 1.5 sort. Cross-plugin FKs
1823/// land in dependency order this way (a plugin's `CreateTable` for
1824/// the FK target runs before the dependent plugin's `CreateTable`).
1825/// Falls back to [`registered_plugins`] when no order has been
1826/// published (e.g. low-level tests that init the registry directly).
1827pub async fn make_in(dir: &Path) -> Result<Vec<PathBuf>, MigrateError> {
1828 let mut written: Vec<PathBuf> = Vec::new();
1829
1830 for plugin in plugin_order() {
1831 let plugin_dir = dir.join(&plugin);
1832
1833 // The previous snapshot is the `snapshot_after` of the highest-
1834 // numbered migration file (filenames are zero-padded so lexical
1835 // sort matches numeric order). An empty or missing directory
1836 // means "no prior state", the first-run case for this plugin.
1837 let existing = list_migration_files(&plugin_dir)?;
1838 let previous = match existing.last() {
1839 Some(path) => read_migration_file(path)?.snapshot_after,
1840 None => Snapshot::default(),
1841 };
1842
1843 let current = Snapshot::current_for(&plugin);
1844 let operations = diff(&previous, ¤t)?;
1845 if operations.is_empty() {
1846 continue;
1847 }
1848
1849 let seq = (existing.len() + 1) as u32;
1850 let suffix = suffix_for(&operations);
1851 let id = format!("{seq:04}_{suffix}");
1852 let filename = format!("{id}.json");
1853
1854 let file = MigrationFile {
1855 id: id.clone(),
1856 plugin: plugin.clone(),
1857 depends_on: Vec::new(),
1858 operations,
1859 snapshot_after: current,
1860 replaces: Vec::new(),
1861 };
1862
1863 std::fs::create_dir_all(&plugin_dir)?;
1864 let path = plugin_dir.join(filename);
1865 let json = serde_json::to_string_pretty(&file)?;
1866 std::fs::write(&path, json)?;
1867 written.push(path);
1868 }
1869
1870 if written.is_empty() {
1871 return Err(MigrateError::NoChanges);
1872 }
1873 Ok(written)
1874}
1875
1876/// Write an **empty** migration for one plugin: the current snapshot
1877/// with an empty `operations` list, the authoring stub for a
1878/// hand-written data migration (`Operation::RunSql`). The developer
1879/// opens the file and adds a `RunSql { sql, reverse_sql }` op.
1880///
1881/// The empty op-list means `snapshot_after == snapshot_before`, so the
1882/// next `make` diffs against the same state and produces nothing — a
1883/// data migration never disturbs the schema-snapshot chain. Mirror of
1884/// [`make`] for the `--empty <plugin>` CLI path.
1885pub async fn make_empty(plugin: &str) -> Result<PathBuf, MigrateError> {
1886 make_empty_in(Path::new(MIGRATIONS_DIR), plugin).await
1887}
1888
1889/// Same as [`make_empty`] but takes an explicit base directory. The
1890/// seam tests drive.
1891pub async fn make_empty_in(dir: &Path, plugin: &str) -> Result<PathBuf, MigrateError> {
1892 // The plugin must be registered, else the snapshot/sequence would be
1893 // meaningless. Fail loudly with the known set.
1894 let known = plugin_order();
1895 if !known.iter().any(|p| p == plugin) {
1896 return Err(MigrateError::UnknownPlugin {
1897 requested: plugin.to_string(),
1898 known,
1899 });
1900 }
1901
1902 let plugin_dir = dir.join(plugin);
1903
1904 // Carry the latest snapshot forward verbatim: an empty migration has
1905 // NO schema effect, so `snapshot_after` equals the previous one. The
1906 // current model snapshot is the same as the prior file's
1907 // `snapshot_after` (no model changed); use the current registry state
1908 // so the file is self-consistent even on a plugin's very first
1909 // migration.
1910 let existing = list_migration_files(&plugin_dir)?;
1911 let snapshot = match existing.last() {
1912 Some(path) => read_migration_file(path)?.snapshot_after,
1913 None => Snapshot::current_for(plugin),
1914 };
1915
1916 let seq = (existing.len() + 1) as u32;
1917 let id = format!("{seq:04}_empty");
1918 let filename = format!("{id}.json");
1919
1920 let file = MigrationFile {
1921 id: id.clone(),
1922 plugin: plugin.to_string(),
1923 depends_on: Vec::new(),
1924 operations: Vec::new(),
1925 snapshot_after: snapshot,
1926 replaces: Vec::new(),
1927 };
1928
1929 std::fs::create_dir_all(&plugin_dir)?;
1930 let path = plugin_dir.join(filename);
1931 let json = serde_json::to_string_pretty(&file)?;
1932 std::fs::write(&path, json)?;
1933 Ok(path)
1934}
1935
1936/// The numeric sequence prefix of a migration id (`0003_add_x` → `0003`),
1937/// used to name a squash `0001_squashed_0003`.
1938fn seq_prefix(id: &str) -> &str {
1939 id.split('_').next().unwrap_or(id)
1940}
1941
1942/// Outcome of [`squash_in`]: where the squash landed and what it collapsed.
1943pub struct SquashOutcome {
1944 /// Path of the written squash file.
1945 pub path: PathBuf,
1946 /// Id of the new squash migration (e.g. `0001_squashed_0005`).
1947 pub id: String,
1948 /// Ids of the original migrations it replaces — kept on disk, non-destructive.
1949 pub replaced: Vec<String>,
1950}
1951
1952/// gaps2 #100 — collapse a plugin's entire linear migration history into ONE
1953/// optimized squash file, **non-destructively**: the originals stay on disk so
1954/// older deploys still migrate, and the runner ([`squash_plan`]) treats the
1955/// squash and its originals as mutually exclusive.
1956///
1957/// The squash's operations are the diff from an empty schema to the last
1958/// migration's `snapshot_after` — i.e. one `CreateTable` per model with every
1959/// intermediate `AlterColumn` already folded into the final column set. That's
1960/// the minimal replay of the whole history, which is the entire point (a 20-file
1961/// history with repeated alters collapses to N clean creates).
1962///
1963/// Refuses (with [`MigrateError::CannotSquash`]) when it can't produce a safe
1964/// squash: fewer than two migrations, a history that already contains a squash
1965/// (nested squashing is out of scope), or a history containing a `RunSql` data
1966/// migration — a snapshot diff can't see hand-written data steps, so squashing
1967/// across one would silently drop it. The operator squashes the schema-only
1968/// prefix, or leaves that history intact.
1969pub fn squash_in(dir: &Path, plugin: &str) -> Result<SquashOutcome, MigrateError> {
1970 let plugin_dir = dir.join(plugin);
1971 let paths = list_migration_files(&plugin_dir)?;
1972 let files: Vec<MigrationFile> = paths
1973 .iter()
1974 .map(|p| read_migration_file(p))
1975 .collect::<Result<_, _>>()?;
1976
1977 if files.len() < 2 {
1978 return Err(MigrateError::CannotSquash {
1979 plugin: plugin.to_string(),
1980 reason: format!(
1981 "need at least 2 migrations to squash, found {}",
1982 files.len()
1983 ),
1984 });
1985 }
1986 if files.iter().any(|f| !f.replaces.is_empty()) {
1987 return Err(MigrateError::CannotSquash {
1988 plugin: plugin.to_string(),
1989 reason: "this history already contains a squash; nested squashing isn't supported \
1990 yet. Delete the now-redundant original files once every deploy has migrated, \
1991 then squash again"
1992 .to_string(),
1993 });
1994 }
1995 if files.iter().any(|f| {
1996 f.operations
1997 .iter()
1998 .any(|op| matches!(op, Operation::RunSql { .. }))
1999 }) {
2000 return Err(MigrateError::CannotSquash {
2001 plugin: plugin.to_string(),
2002 reason: "this history contains a RunSql data migration, which a snapshot diff can't \
2003 reconstruct — squashing across it would silently drop the data step. Squash \
2004 the schema-only migrations before/after it, or leave this history intact"
2005 .to_string(),
2006 });
2007 }
2008
2009 let first = &files[0];
2010 let last = files.last().expect("len >= 2 checked above");
2011
2012 // Optimal from-scratch replay: empty schema → final snapshot. Every
2013 // intermediate alter is already baked into `snapshot_after`.
2014 let empty = Snapshot { models: Vec::new() };
2015 let operations = diff(&empty, &last.snapshot_after)?;
2016
2017 let id = format!(
2018 "{}_squashed_{}",
2019 seq_prefix(&first.id),
2020 seq_prefix(&last.id)
2021 );
2022 let replaced: Vec<String> = files.iter().map(|f| f.id.clone()).collect();
2023 let squash = MigrationFile {
2024 id: id.clone(),
2025 plugin: plugin.to_string(),
2026 // Preserve the first migration's cross-plugin predecessors: the squash
2027 // stands in for the whole run, so it inherits the run's external deps.
2028 depends_on: first.depends_on.clone(),
2029 operations,
2030 snapshot_after: last.snapshot_after.clone(),
2031 replaces: replaced
2032 .iter()
2033 .map(|mid| MigrationRef {
2034 plugin: plugin.to_string(),
2035 migration: mid.clone(),
2036 })
2037 .collect(),
2038 };
2039
2040 let path = plugin_dir.join(format!("{id}.json"));
2041 let json = serde_json::to_string_pretty(&squash)?;
2042 std::fs::write(&path, json)?;
2043 Ok(SquashOutcome { path, id, replaced })
2044}
2045
2046/// Create a table for every registered model, deriving the schema from the
2047/// models themselves. **For tests.**
2048///
2049/// This is the migration engine pointed at an empty database: diff `Snapshot`
2050/// nothing against [`Snapshot::current`], render the resulting `CreateTable`
2051/// operations for the ambient backend, and run them. Same `ModelMeta` the ORM
2052/// reads, same renderer [`run`] uses — so the schema it produces cannot
2053/// disagree with your models.
2054///
2055/// # Why this exists
2056///
2057/// The alternative, and what most test suites reach for, is a hand-written
2058/// `CREATE TABLE` in the test file. That is a **second source of truth** for the
2059/// schema, and it drifts: add a column to the model and the test's table
2060/// silently lacks it. The ORM then queries a column that does not exist, and any
2061/// error-swallowing on the path (an `unwrap_or(false)`) turns that failure into a
2062/// test that *passes with the wrong answer* rather than failing. That is not a
2063/// hypothetical — a soft-delete column drifting out of a fixture is exactly what
2064/// made an authorization test report that a user was already a moderator when
2065/// they were not.
2066///
2067/// # Not a substitute for `migrate`
2068///
2069/// It writes no ledger row and runs no migration file. It cannot ALTER an
2070/// existing table, so it will not carry a database from one shape to another —
2071/// which is the entire job of [`run`] and the reason `makemigrations` exists.
2072/// Point it at a throwaway database, never a real one.
2073///
2074/// Idempotent: an "already exists" error is skipped, so a shared test boot that
2075/// runs twice is harmless. Returns the number of statements executed.
2076pub async fn create_tables_for_tests() -> Result<u64, MigrateError> {
2077 create_tables_for_tests_on("default").await
2078}
2079
2080/// [`create_tables_for_tests`], against a named database alias.
2081///
2082/// A read/write-split or multi-database test registers more than one pool
2083/// (`.database("default", ..).database("replica", ..)`) and needs the schema on
2084/// each of them — a replica with no tables is not a replica. The ambient-pool
2085/// variant only ever reaches `"default"`.
2086pub async fn create_tables_for_tests_on(alias: &str) -> Result<u64, MigrateError> {
2087 let ops = diff(&Snapshot { models: Vec::new() }, &Snapshot::current())?;
2088 let pool = crate::db::pool_for_dispatched(alias);
2089 let backend = match pool {
2090 crate::db::DbPool::Sqlite(_) => "sqlite",
2091 crate::db::DbPool::Postgres(_) => "postgres",
2092 };
2093
2094 let mut executed = 0u64;
2095 for op in &ops {
2096 for sql in render_operation_for(op, backend) {
2097 let res = match crate::db::pool_for_dispatched(alias) {
2098 crate::db::DbPool::Sqlite(p) => sqlx::query(&sql).execute(p).await.map(|_| ()),
2099 crate::db::DbPool::Postgres(p) => sqlx::query(&sql).execute(p).await.map(|_| ()),
2100 };
2101 match res {
2102 Ok(()) => executed += 1,
2103 // A shared `OnceCell` boot may land here twice; a test that dies
2104 // on its own setup is worse than useless.
2105 Err(e) if e.to_string().to_lowercase().contains("already exists") => {}
2106 Err(e) => return Err(MigrateError::Sqlx(e)),
2107 }
2108 }
2109 }
2110 Ok(executed)
2111}
2112
2113/// Apply every pending migration across every registered plugin's
2114/// `migrations/<plugin>/` directory to the ambient pool. Reads the
2115/// `umbral_migrations` tracking table to determine "pending"; each
2116/// migration runs in its own transaction along with its tracking-table
2117/// insert.
2118///
2119/// Returns the total number of migrations applied (zero if every
2120/// plugin's migrations were already in the tracking table).
2121///
2122/// This variant performs a drift check before executing any SQL. If
2123/// any migration is `AppliedButMissing` (in the DB but not on disk),
2124/// the call returns [`MigrateError::DriftDetected`] listing the
2125/// missing names. Pass `allow_drift = true` (via [`run_checked_in`])
2126/// to suppress the error and proceed anyway (with a warning printed to
2127/// stderr).
2128pub async fn run() -> Result<u64, MigrateError> {
2129 run_checked(false).await
2130}
2131
2132/// Same as [`run`] but controls drift handling.
2133/// `allow_drift = true` corresponds to the `--allow-drift` CLI flag:
2134/// the command logs a warning and proceeds even if some applied
2135/// migrations are missing on disk.
2136pub async fn run_checked(allow_drift: bool) -> Result<u64, MigrateError> {
2137 run_checked_in(Path::new(MIGRATIONS_DIR), allow_drift).await
2138}
2139
2140/// Same as [`run_checked`] but takes an explicit base directory.
2141pub async fn run_checked_in(dir: &Path, allow_drift: bool) -> Result<u64, MigrateError> {
2142 let mut total: u64 = 0;
2143 // Walk every registered DB. Drift-detection on the default pool
2144 // is the dominant flow; secondary pools currently use the same
2145 // tracking-table-vs-disk comparison but only against the
2146 // migration files whose ops actually targeted that DB. A future
2147 // pass can teach `detect_all_drift` to be alias-aware so drift
2148 // warnings name the offending pool — today it warns once per
2149 // checked DB if the issue is present in any.
2150 for alias in crate::db::registered_aliases() {
2151 match crate::db::pool_for_dispatched(&alias) {
2152 crate::db::DbPool::Sqlite(p) => {
2153 total += run_in_sqlite_checked(dir, p, allow_drift, &alias).await?
2154 }
2155 crate::db::DbPool::Postgres(p) => {
2156 total += run_in_postgres_checked(dir, p, allow_drift, &alias).await?
2157 }
2158 }
2159 }
2160 Ok(total)
2161}
2162
2163/// Same as [`run`] but takes an explicit base directory. Used by
2164/// tests to avoid touching the cwd.
2165///
2166/// Iterates `registered_plugins()` in sorted-by-name order. M7 v1
2167/// accepts this as a limitation: cross-plugin FK ordering wants
2168/// topological order across plugins (the FK target's `CreateTable`
2169/// applies before the dependent plugin's `CreateTable`), but the
2170/// engine doesn't see `Plugin::dependencies()` from inside this
2171/// standalone function. M8 lifts the limitation via a registry that
2172/// remembers the toposorted order computed at `App::build()` time.
2173///
2174/// This legacy entry point does NOT perform drift checking so the
2175/// existing tests (which bypass drift by design) keep passing. New
2176/// callers should prefer [`run_checked_in`].
2177pub async fn run_in(dir: &Path) -> Result<u64, MigrateError> {
2178 let mut total: u64 = 0;
2179 // Walk every registered DB so each pool gets its own
2180 // `umbral_migrations` table and runs only the operations targeting
2181 // tables routed to it. Order is alphabetical for determinism;
2182 // the "default" pool is always present.
2183 for alias in crate::db::registered_aliases() {
2184 match crate::db::pool_for_dispatched(&alias) {
2185 crate::db::DbPool::Sqlite(p) => {
2186 total += run_in_sqlite_for_alias(dir, &alias, p, None).await?
2187 }
2188 crate::db::DbPool::Postgres(p) => {
2189 total += run_in_postgres_for_alias(dir, &alias, p, None).await?
2190 }
2191 }
2192 }
2193 Ok(total)
2194}
2195
2196/// Apply only the **SHARED** apps' pending migrations to the default pool —
2197/// the `public`/shared half of schema-per-tenant multitenancy. This is the
2198/// mirror of [`run_for_schema_in`] (which migrates the *tenant* apps into a
2199/// tenant schema): here only plugins IN `shared_apps` migrate into `public`,
2200/// so a tenant app's tables — and crucially its M2M junctions — are NEVER
2201/// created in `public`. They live only in each tenant schema, where a junction's
2202/// FK to a SHARED child resolves via the `<schema>, public` search-path.
2203///
2204/// Use this instead of the unfiltered [`run`]/[`run_in`] when running a
2205/// schema-per-tenant app: `run_shared` (shared → public) then `migrate_schemas`
2206/// (tenant apps → each schema). On a non-multitenant app the two are equivalent
2207/// only if every app is shared; otherwise prefer plain [`run`].
2208pub async fn run_shared(
2209 shared_apps: &std::collections::HashSet<String>,
2210) -> Result<u64, MigrateError> {
2211 run_shared_in(Path::new(MIGRATIONS_DIR), shared_apps).await
2212}
2213
2214/// [`run_shared`] against an explicit migrations directory (tests / tooling).
2215pub async fn run_shared_in(
2216 dir: &Path,
2217 shared_apps: &std::collections::HashSet<String>,
2218) -> Result<u64, MigrateError> {
2219 let mut total: u64 = 0;
2220 for alias in crate::db::registered_aliases() {
2221 match crate::db::pool_for_dispatched(&alias) {
2222 crate::db::DbPool::Sqlite(p) => {
2223 total += run_in_sqlite_for_alias(dir, &alias, p, Some(shared_apps)).await?
2224 }
2225 crate::db::DbPool::Postgres(p) => {
2226 total += run_in_postgres_for_alias(dir, &alias, p, Some(shared_apps)).await?
2227 }
2228 }
2229 }
2230 Ok(total)
2231}
2232
2233/// Predicate: does `op` target a table that lives on `alias`?
2234///
2235/// Routing rule: look up the table → alias mapping via
2236/// [`table_alias`]. Tables not owned by any registered model fall
2237/// through to `"default"` so the migration engine's own
2238/// `umbral_migrations` book-keeping stays in the main DB.
2239///
2240/// A second gate consults the installed [`DatabaseRouter`]: if the
2241/// router's [`allow_migrate`](crate::db::DatabaseRouter::allow_migrate)
2242/// returns `false` for this (alias, model) pair the operation is
2243/// excluded from the alias's run. Junction / unowned tables (no
2244/// registered `ModelMeta`) are always allowed — the router has no
2245/// model to inspect.
2246fn op_targets_alias(op: &Operation, alias: &str) -> bool {
2247 if table_alias(op.table_name()) != alias {
2248 return false;
2249 }
2250 // Let the router veto migrating this table on this alias.
2251 match model_meta_for_table(op.table_name()) {
2252 Some(meta) => crate::db::router::router().allow_migrate(alias, &meta),
2253 None => true, // junction / unowned table — migrate on its alias
2254 }
2255}
2256
2257/// SQLite per-alias variant. Same shape as the legacy `run_in_sqlite`
2258/// but: filters ops to those routed to `alias`; skips files whose op
2259/// list contains nothing for this DB (so we don't stuff orphan
2260/// tracking rows into pools that didn't run any SQL).
2261async fn run_in_sqlite_for_alias(
2262 dir: &Path,
2263 alias: &str,
2264 pool: &sqlx::SqlitePool,
2265 shared_only: Option<&std::collections::HashSet<String>>,
2266) -> Result<u64, MigrateError> {
2267 ensure_tracking_table_sqlite(pool).await?;
2268 let applied = applied_names_sqlite(pool).await?;
2269
2270 let mut applied_count: u64 = 0;
2271 for plugin in plugin_order() {
2272 if let Some(shared) = shared_only {
2273 if !shared.contains(&plugin) {
2274 continue;
2275 }
2276 }
2277 let plugin_dir = dir.join(&plugin);
2278 let paths = list_migration_files(&plugin_dir)?;
2279
2280 // Read the plugin's full file set, then plan squash decisions in one
2281 // pass (gaps2 #100). `squash_plan` decides Apply / Skip / RecordOnly per
2282 // file, honoring `replaces` so a squash and its originals never both
2283 // apply. Pure and backend-agnostic — the Postgres runner plans the same.
2284 let files: Vec<MigrationFile> = paths
2285 .iter()
2286 .map(|p| read_migration_file(p))
2287 .collect::<Result<_, _>>()?;
2288 let plan = squash_plan(&files, &applied)?;
2289
2290 for (file, decision) in files.iter().zip(plan) {
2291 if decision == ApplyDecision::Skip {
2292 continue;
2293 }
2294
2295 let ops_for_this_db: Vec<&Operation> = file
2296 .operations
2297 .iter()
2298 .filter(|op| op_targets_alias(op, alias))
2299 .collect();
2300 if ops_for_this_db.is_empty() {
2301 // File's content all targets some other DB. Don't
2302 // record it here — re-runs will re-evaluate cleanly
2303 // once the right DB picks it up. The tracking rows
2304 // per-DB stay accurate to "what actually ran here."
2305 continue;
2306 }
2307
2308 let snapshot_hash = file.snapshot_after.hash();
2309 let applied_at = chrono::Utc::now().to_rfc3339();
2310 // RecordOnly: the squash's replaced set is already applied, so its
2311 // schema exists — insert the tracking row without running any ops
2312 // (an empty op slice inserts the row and commits).
2313 let ops_to_run: &[&Operation] = if decision == ApplyDecision::RecordOnly {
2314 &[]
2315 } else {
2316 &ops_for_this_db
2317 };
2318 apply_sqlite_migration_tx(
2319 pool,
2320 ops_to_run,
2321 &file.plugin,
2322 &file.id,
2323 &applied_at,
2324 &snapshot_hash,
2325 )
2326 .await?;
2327 applied_count += 1;
2328 }
2329 }
2330 Ok(applied_count)
2331}
2332
2333/// Apply one migration file's SQLite `ops` in a single transaction, then record
2334/// it in the tracking table.
2335///
2336/// When any op is an `AlterColumn` (the table-recreation dance), the
2337/// transaction is bracketed with `PRAGMA foreign_keys=OFF` … `PRAGMA
2338/// foreign_key_check` … `PRAGMA foreign_keys=ON` on a **pinned** connection
2339/// (SQLite's official recipe), so step 3's `DROP TABLE` on a table with inbound
2340/// FKs doesn't fail with `FOREIGN KEY constraint failed` (error 787, gaps3
2341/// #13). The pragma MUST be toggled outside the tx (it's a no-op inside one),
2342/// and enforcement is restored even on failure so the pooled connection never
2343/// returns with FK checks disabled. `foreign_key_check` before commit keeps the
2344/// integrity guarantee: a migration that genuinely orphans a row is aborted.
2345async fn apply_sqlite_migration_tx(
2346 pool: &sqlx::SqlitePool,
2347 ops: &[&Operation],
2348 plugin: &str,
2349 name: &str,
2350 applied_at: &str,
2351 snapshot_hash: &str,
2352) -> Result<(), MigrateError> {
2353 use sqlx::Acquire as _;
2354
2355 let needs_fk_off = ops
2356 .iter()
2357 .any(|op| matches!(op, Operation::AlterColumn { .. }));
2358
2359 let mut conn = pool.acquire().await?;
2360 if needs_fk_off {
2361 sqlx::query("PRAGMA foreign_keys=OFF")
2362 .execute(&mut *conn)
2363 .await?;
2364 }
2365
2366 let result: Result<(), MigrateError> = async {
2367 let mut tx = conn.begin().await?;
2368 for op in ops {
2369 for sql in render_operation_for(op, "sqlite") {
2370 sqlx::query(&sql).execute(&mut *tx).await?;
2371 }
2372 }
2373 if needs_fk_off {
2374 // Enforcement was off during the dance; verify the recreation left
2375 // no dangling references before we commit.
2376 let violations = sqlx::query("PRAGMA foreign_key_check")
2377 .fetch_all(&mut *tx)
2378 .await?;
2379 if !violations.is_empty() {
2380 return Err(MigrateError::Sqlx(sqlx::Error::Protocol(format!(
2381 "migration `{plugin}/{name}` would leave {} dangling foreign-key \
2382 reference(s); aborted",
2383 violations.len()
2384 ))));
2385 }
2386 }
2387 sqlx::query(
2388 "INSERT INTO umbral_migrations (plugin, name, applied_at, snapshot_hash) \
2389 VALUES (?, ?, ?, ?)",
2390 )
2391 .bind(plugin)
2392 .bind(name)
2393 .bind(applied_at)
2394 .bind(snapshot_hash)
2395 .execute(&mut *tx)
2396 .await?;
2397 tx.commit().await?;
2398 Ok(())
2399 }
2400 .await;
2401
2402 if needs_fk_off {
2403 // Restore enforcement before the connection returns to the pool, even
2404 // on failure — but never mask the primary error with a pragma error.
2405 let _ = sqlx::query("PRAGMA foreign_keys=ON")
2406 .execute(&mut *conn)
2407 .await;
2408 }
2409 result
2410}
2411
2412/// A stable 64-bit key for the Postgres migration advisory lock, derived from a
2413/// fixed namespace + a `discriminator` (the pool alias or tenant schema). FNV-1a
2414/// with fixed constants — deterministic and process-independent, so every
2415/// migrator computes the SAME key for the same target and they mutually exclude.
2416/// Different aliases/schemas get different keys, so unrelated logical databases
2417/// migrate concurrently (audit_2 core-migrate #7).
2418fn pg_migration_lock_key(discriminator: &str) -> i64 {
2419 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
2420 for b in b"umbral_migrations\0"
2421 .iter()
2422 .copied()
2423 .chain(discriminator.bytes())
2424 {
2425 hash ^= b as u64;
2426 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
2427 }
2428 hash as i64
2429}
2430
2431/// How long a migrator waits for the advisory lock before giving up. Generous
2432/// (another replica's migration set can take a while), bounded so a deploy can't
2433/// hang forever on a wedged migrator. Override with
2434/// `UMBRAL_MIGRATION_LOCK_TIMEOUT_SECS`.
2435fn pg_migration_lock_timeout() -> std::time::Duration {
2436 let secs = std::env::var("UMBRAL_MIGRATION_LOCK_TIMEOUT_SECS")
2437 .ok()
2438 .and_then(|v| v.trim().parse::<u64>().ok())
2439 .filter(|&n| n > 0)
2440 .unwrap_or(300);
2441 std::time::Duration::from_secs(secs)
2442}
2443
2444/// Acquire the session-level Postgres advisory lock keyed on `discriminator`,
2445/// holding it on `conn` for the caller to release. Non-blocking polls
2446/// (`pg_try_advisory_lock`) with a short sleep between attempts, bounded by
2447/// [`pg_migration_lock_timeout`] — so a stuck migrator surfaces as a clear
2448/// [`MigrateError::MigrationLockTimeout`] instead of an indefinite hang, and a
2449/// crashed migrator's lock auto-releases (session locks die with the backend).
2450async fn acquire_pg_migration_lock(
2451 conn: &mut sqlx::PgConnection,
2452 key: i64,
2453 discriminator: &str,
2454) -> Result<(), MigrateError> {
2455 let timeout = pg_migration_lock_timeout();
2456 let start = std::time::Instant::now();
2457 let mut warned = false;
2458 loop {
2459 let got: bool = sqlx::query_scalar("SELECT pg_try_advisory_lock($1)")
2460 .bind(key)
2461 .fetch_one(&mut *conn)
2462 .await?;
2463 if got {
2464 return Ok(());
2465 }
2466 let waited = start.elapsed();
2467 if waited >= timeout {
2468 return Err(MigrateError::MigrationLockTimeout {
2469 discriminator: discriminator.to_string(),
2470 waited_secs: waited.as_secs(),
2471 });
2472 }
2473 if !warned {
2474 tracing::info!(
2475 discriminator,
2476 "umbral migrate: another process holds the migration lock; waiting for it to \
2477 finish before applying migrations…"
2478 );
2479 warned = true;
2480 }
2481 tokio::time::sleep(std::time::Duration::from_millis(200)).await;
2482 }
2483}
2484
2485/// Release the advisory lock acquired by [`acquire_pg_migration_lock`]. Best
2486/// effort — dropping `conn` also releases a session lock, so a failure here is
2487/// logged, not propagated (it must never mask the migration's own result).
2488async fn release_pg_migration_lock(conn: &mut sqlx::PgConnection, key: i64) {
2489 if let Err(e) = sqlx::query("SELECT pg_advisory_unlock($1)")
2490 .bind(key)
2491 .execute(&mut *conn)
2492 .await
2493 {
2494 tracing::warn!("umbral migrate: failed to release the migration advisory lock: {e}");
2495 }
2496}
2497
2498/// Postgres per-alias variant. Mirror of `run_in_sqlite_for_alias`.
2499///
2500/// Wraps the apply loop in a session advisory lock (audit_2 core-migrate #7) so
2501/// two replicas deploying at once can't both read the applied set and race the
2502/// same DDL (the loser errors "relation already exists" mid-deploy). The lock is
2503/// held on a dedicated connection for the whole run; the per-migration
2504/// transactions use their own pooled connections and are unaffected.
2505async fn run_in_postgres_for_alias(
2506 dir: &Path,
2507 alias: &str,
2508 pool: &sqlx::PgPool,
2509 shared_only: Option<&std::collections::HashSet<String>>,
2510) -> Result<u64, MigrateError> {
2511 let key = pg_migration_lock_key(alias);
2512 let mut lock_conn = pool.acquire().await?;
2513 acquire_pg_migration_lock(&mut lock_conn, key, alias).await?;
2514 let result = run_in_postgres_for_alias_locked(dir, alias, pool, shared_only).await;
2515 release_pg_migration_lock(&mut lock_conn, key).await;
2516 result
2517}
2518
2519/// Run `f` under the Postgres migration advisory lock keyed on `discriminator`.
2520/// Shared by the checked-run and schema-per-tenant apply paths so they get the
2521/// same cross-process serialization as [`run_in_postgres_for_alias`].
2522async fn with_pg_migration_lock<F, Fut>(
2523 pool: &sqlx::PgPool,
2524 discriminator: &str,
2525 f: F,
2526) -> Result<u64, MigrateError>
2527where
2528 F: FnOnce() -> Fut,
2529 Fut: std::future::Future<Output = Result<u64, MigrateError>>,
2530{
2531 let key = pg_migration_lock_key(discriminator);
2532 let mut lock_conn = pool.acquire().await?;
2533 acquire_pg_migration_lock(&mut lock_conn, key, discriminator).await?;
2534 let result = f().await;
2535 release_pg_migration_lock(&mut lock_conn, key).await;
2536 result
2537}
2538
2539/// The unlocked body of [`run_in_postgres_for_alias`] — runs while the caller
2540/// holds the migration advisory lock.
2541async fn run_in_postgres_for_alias_locked(
2542 dir: &Path,
2543 alias: &str,
2544 pool: &sqlx::PgPool,
2545 shared_only: Option<&std::collections::HashSet<String>>,
2546) -> Result<u64, MigrateError> {
2547 ensure_tracking_table_postgres(pool).await?;
2548 let applied = applied_names_postgres(pool).await?;
2549
2550 let mut applied_count: u64 = 0;
2551 for plugin in plugin_order() {
2552 // Shared-filtered public migrate (multitenancy): when a shared-app set
2553 // is given, migrate ONLY those plugins into this pool, so a tenant
2554 // app's tables (and its M2M junctions) are NOT created in `public` —
2555 // they belong only in each tenant schema. `None` = migrate everything
2556 // (the default single-DB behaviour, byte-identical to before).
2557 if let Some(shared) = shared_only {
2558 if !shared.contains(&plugin) {
2559 continue;
2560 }
2561 }
2562 let plugin_dir = dir.join(&plugin);
2563 let paths = list_migration_files(&plugin_dir)?;
2564
2565 // Plan squash decisions for the plugin's whole file set (gaps2 #100),
2566 // identical to the SQLite runner — `squash_plan` is backend-agnostic.
2567 let files: Vec<MigrationFile> = paths
2568 .iter()
2569 .map(|p| read_migration_file(p))
2570 .collect::<Result<_, _>>()?;
2571 let plan = squash_plan(&files, &applied)?;
2572
2573 for (file, decision) in files.iter().zip(plan) {
2574 if decision == ApplyDecision::Skip {
2575 continue;
2576 }
2577
2578 let ops_for_this_db: Vec<&Operation> = file
2579 .operations
2580 .iter()
2581 .filter(|op| op_targets_alias(op, alias))
2582 .collect();
2583 if ops_for_this_db.is_empty() {
2584 continue;
2585 }
2586
2587 let mut tx = pool.begin().await?;
2588 // RecordOnly (squash whose replaced set is already applied): its
2589 // schema exists, so run no DDL — just insert the tracking row.
2590 if decision != ApplyDecision::RecordOnly {
2591 for op in &ops_for_this_db {
2592 for sql in render_operation(op) {
2593 sqlx::query(&sql).execute(&mut *tx).await?;
2594 }
2595 }
2596 }
2597 let snapshot_hash = file.snapshot_after.hash();
2598 let applied_at = chrono::Utc::now().to_rfc3339();
2599 sqlx::query(
2600 "INSERT INTO umbral_migrations (plugin, name, applied_at, snapshot_hash) \
2601 VALUES ($1, $2, $3, $4)",
2602 )
2603 .bind(&file.plugin)
2604 .bind(&file.id)
2605 .bind(&applied_at)
2606 .bind(&snapshot_hash)
2607 .execute(&mut *tx)
2608 .await?;
2609 tx.commit().await?;
2610 applied_count += 1;
2611 }
2612 }
2613 Ok(applied_count)
2614}
2615
2616/// Migrate the **tenant** apps into a named Postgres schema (schema-per-tenant
2617/// style). The migration engine owns all schema DDL; this is the
2618/// sanctioned `CREATE SCHEMA` / `SET search_path` exception (a plugin calls this
2619/// rather than writing raw schema SQL itself).
2620///
2621/// Steps, all inside one transaction per migration file (mirroring
2622/// [`run_in_postgres_for_alias`]):
2623/// 1. `CREATE SCHEMA IF NOT EXISTS "<schema>"` (the `Schema` was already
2624/// validated to a safe PG identifier, but is still emitted quoted).
2625/// 2. `SET LOCAL search_path TO "<schema>"` so every unqualified
2626/// `CREATE TABLE` **and** the `umbral_migrations` ledger land *inside*
2627/// `<schema>` — per-schema migration tracking falls out for free.
2628/// 3. Apply pending migrations, **filtered to the tenant apps** — every plugin
2629/// NOT in `shared_apps` (those tables live in `public` and are migrated by
2630/// the normal [`run`]). A file with no tenant-app ops for this schema is
2631/// skipped without a tracking row.
2632///
2633/// Idempotent: re-running applies only the migrations the schema's own
2634/// `umbral_migrations` ledger hasn't recorded. Postgres-only — schemas don't
2635/// exist on SQLite, so a SQLite pool is a clear error
2636/// ([`MigrateError::SchemaUnsupportedOnSqlite`]).
2637pub async fn run_for_schema(
2638 schema: &crate::db::Schema,
2639 shared_apps: &std::collections::HashSet<String>,
2640) -> Result<u64, MigrateError> {
2641 run_for_schema_in(Path::new(MIGRATIONS_DIR), schema, shared_apps).await
2642}
2643
2644/// Same as [`run_for_schema`] but takes an explicit migrations base directory.
2645/// The entry tests drive.
2646pub async fn run_for_schema_in(
2647 dir: &Path,
2648 schema: &crate::db::Schema,
2649 shared_apps: &std::collections::HashSet<String>,
2650) -> Result<u64, MigrateError> {
2651 match crate::db::pool_dispatched() {
2652 crate::db::DbPool::Postgres(p) => {
2653 // audit_2 core-migrate #7: serialize concurrent migrators of THIS
2654 // tenant schema (keyed by schema name, so different tenants still
2655 // migrate concurrently). The shared/public run uses a different key
2656 // (its alias), so a tenant migrate and the public migrate don't
2657 // block each other.
2658 with_pg_migration_lock(p, schema.as_str(), || {
2659 run_tenant_apps_in_postgres_schema(dir, schema, shared_apps, p)
2660 })
2661 .await
2662 }
2663 crate::db::DbPool::Sqlite(_) => Err(MigrateError::SchemaUnsupportedOnSqlite {
2664 schema: schema.as_str().to_string(),
2665 }),
2666 }
2667}
2668
2669/// Postgres schema-scoped variant of [`run_in_postgres_for_alias`]. Creates the
2670/// schema, pins `search_path` to it for the transaction, and applies only the
2671/// tenant apps' migrations (plugins not in `shared_apps`). The `umbral_migrations`
2672/// ledger is read/written *inside* the schema (search_path is set first), so
2673/// tracking is per-schema with no extra book-keeping.
2674async fn run_tenant_apps_in_postgres_schema(
2675 dir: &Path,
2676 schema: &crate::db::Schema,
2677 shared_apps: &std::collections::HashSet<String>,
2678 pool: &sqlx::PgPool,
2679) -> Result<u64, MigrateError> {
2680 let quoted = format!("\"{}\"", schema.as_str());
2681
2682 // Create the schema once, outside the per-file loop. IF NOT EXISTS makes
2683 // the whole call idempotent.
2684 sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS {quoted}"))
2685 .execute(pool)
2686 .await?;
2687
2688 // Ensure + read the ledger INSIDE the schema. Each block runs in its own
2689 // transaction with `SET LOCAL search_path` so the tracking table is created
2690 // in (and read from) `<schema>`, not `public` — AND the search_path is
2691 // transaction-scoped, so the pooled connection is NOT left pinned to this
2692 // schema when it returns to the pool. A plain session-level `SET` here
2693 // pollutes the pool: the next unqualified ORM query that reuses the
2694 // connection would resolve against `<schema>` instead of `public` (e.g. an
2695 // insert into the public `tenant` registry failing with "relation does not
2696 // exist") — a real cross-tenant bug, caught only against live Postgres.
2697 {
2698 let mut tx = pool.begin().await?;
2699 sqlx::query(&format!("SET LOCAL search_path TO {quoted}"))
2700 .execute(&mut *tx)
2701 .await?;
2702 ensure_tracking_table_pg_conn(&mut tx).await?;
2703 tx.commit().await?;
2704 }
2705 let applied = {
2706 let mut tx = pool.begin().await?;
2707 sqlx::query(&format!("SET LOCAL search_path TO {quoted}"))
2708 .execute(&mut *tx)
2709 .await?;
2710 let rows: Vec<(String, String)> =
2711 sqlx::query_as("SELECT plugin, name FROM umbral_migrations")
2712 .fetch_all(&mut *tx)
2713 .await?;
2714 tx.commit().await?;
2715 rows.into_iter().collect::<std::collections::HashSet<_>>()
2716 };
2717
2718 let mut applied_count: u64 = 0;
2719 for plugin in plugin_order() {
2720 // Tenant apps only — shared apps live in `public`.
2721 if shared_apps.contains(&plugin) {
2722 continue;
2723 }
2724 let plugin_dir = dir.join(&plugin);
2725 let paths = list_migration_files(&plugin_dir)?;
2726
2727 // Squash-aware planning (gaps2 #100), same as every other apply loop.
2728 let files: Vec<MigrationFile> = paths
2729 .iter()
2730 .map(|p| read_migration_file(p))
2731 .collect::<Result<_, _>>()?;
2732 let plan = squash_plan(&files, &applied)?;
2733
2734 for (file, decision) in files.iter().zip(plan) {
2735 if decision == ApplyDecision::Skip {
2736 continue;
2737 }
2738 // Belt-and-braces: skip a file whose declared plugin is shared.
2739 if shared_apps.contains(&file.plugin) {
2740 continue;
2741 }
2742
2743 let mut tx = pool.begin().await?;
2744 // Pin search_path for THIS transaction, tenant schema FIRST with
2745 // `public` as a fallback. `CREATE TABLE` / `INSERT` still land in
2746 // the tenant schema (it's first), but an unqualified reference that
2747 // ISN'T in the tenant schema resolves against `public` — which is
2748 // what makes a CROSS-BOUNDARY foreign key work: a tenant-owned
2749 // table (or an M2M junction) with an FK `REFERENCES <shared_child>`
2750 // resolves the shared child in `public` instead of erroring
2751 // `relation does not exist`. It also lets a (future) RunSql data
2752 // migration in a tenant schema read SHARED/`public` lookup tables.
2753 // The tenant-first ordering means a tenant table still shadows a
2754 // same-named public table, so no behaviour changes for the common
2755 // case where tenant and shared table names are distinct.
2756 sqlx::query(&format!("SET LOCAL search_path TO {quoted}, public"))
2757 .execute(&mut *tx)
2758 .await?;
2759 // RecordOnly: schema already built by the replaced originals — run
2760 // no DDL, just record the squash.
2761 if decision != ApplyDecision::RecordOnly {
2762 for op in &file.operations {
2763 for sql in render_operation_for(op, "postgres") {
2764 sqlx::query(&sql).execute(&mut *tx).await?;
2765 }
2766 }
2767 }
2768 let snapshot_hash = file.snapshot_after.hash();
2769 let applied_at = chrono::Utc::now().to_rfc3339();
2770 sqlx::query(
2771 "INSERT INTO umbral_migrations (plugin, name, applied_at, snapshot_hash) \
2772 VALUES ($1, $2, $3, $4)",
2773 )
2774 .bind(&file.plugin)
2775 .bind(&file.id)
2776 .bind(&applied_at)
2777 .bind(&snapshot_hash)
2778 .execute(&mut *tx)
2779 .await?;
2780 tx.commit().await?;
2781 applied_count += 1;
2782 }
2783 }
2784 Ok(applied_count)
2785}
2786
2787/// Migrate the **tenant** apps into the pool registered under `alias`
2788/// (database-per-tenant). The db-per-tenant sibling of [`run_for_schema`]:
2789/// where the schema variant pins `search_path` inside one shared Postgres
2790/// database, this targets a *whole separate database/pool* registered at
2791/// runtime via [`register_tenant_pool`](crate::db::register_tenant_pool) and
2792/// resolved here through [`pool_for_dispatched`](crate::db::pool_for_dispatched)
2793/// (which sees dynamic pools). No schema games — per-database migration
2794/// tracking is just that database's own `umbral_migrations` table.
2795///
2796/// Like the schema variant it applies only the **tenant apps**: every plugin
2797/// NOT in `shared_apps` (the shared registry/auth tables live in the default
2798/// DB and are migrated there by the normal [`run`]). A migration file whose
2799/// declared plugin is shared is skipped without a tracking row. Idempotent:
2800/// re-running applies only what the tenant DB's own ledger hasn't recorded.
2801///
2802/// Works on both backends — a tenant pool can be Postgres (the production case)
2803/// or SQLite (tests). Unlike the alias-routed [`run_in`], this does NOT filter
2804/// ops by [`table_alias`]: a tenant-owned model's static alias is still
2805/// `"default"`, so the per-alias filter would wrongly exclude it from the
2806/// tenant DB. The shared/tenant split is the *only* filter here.
2807pub async fn migrate_apps_into_pool(
2808 alias: &str,
2809 shared_apps: &std::collections::HashSet<String>,
2810) -> Result<u64, MigrateError> {
2811 migrate_apps_into_pool_in(Path::new(MIGRATIONS_DIR), alias, shared_apps).await
2812}
2813
2814/// Same as [`migrate_apps_into_pool`] but takes an explicit migrations base
2815/// directory. The entry tests drive.
2816pub async fn migrate_apps_into_pool_in(
2817 dir: &Path,
2818 alias: &str,
2819 shared_apps: &std::collections::HashSet<String>,
2820) -> Result<u64, MigrateError> {
2821 match crate::db::pool_for_dispatched(alias) {
2822 crate::db::DbPool::Postgres(p) => {
2823 migrate_tenant_apps_into_pg_pool(dir, shared_apps, p).await
2824 }
2825 crate::db::DbPool::Sqlite(p) => {
2826 migrate_tenant_apps_into_sqlite_pool(dir, shared_apps, p).await
2827 }
2828 }
2829}
2830
2831/// Postgres tenant-DB apply loop. Mirrors [`run_in_postgres_for_alias`] but the
2832/// only filter is the shared/tenant split — every plugin not in `shared_apps`
2833/// is applied in full into this database.
2834async fn migrate_tenant_apps_into_pg_pool(
2835 dir: &Path,
2836 shared_apps: &std::collections::HashSet<String>,
2837 pool: &sqlx::PgPool,
2838) -> Result<u64, MigrateError> {
2839 ensure_tracking_table_postgres(pool).await?;
2840 let applied = applied_names_postgres(pool).await?;
2841
2842 let mut applied_count: u64 = 0;
2843 for plugin in plugin_order() {
2844 if shared_apps.contains(&plugin) {
2845 continue;
2846 }
2847 let plugin_dir = dir.join(&plugin);
2848 let files: Vec<MigrationFile> = list_migration_files(&plugin_dir)?
2849 .iter()
2850 .map(|p| read_migration_file(p))
2851 .collect::<Result<_, _>>()?;
2852 let plan = squash_plan(&files, &applied)?;
2853 for (file, decision) in files.iter().zip(plan) {
2854 if decision == ApplyDecision::Skip {
2855 continue;
2856 }
2857 if shared_apps.contains(&file.plugin) {
2858 continue;
2859 }
2860 let mut tx = pool.begin().await?;
2861 if decision != ApplyDecision::RecordOnly {
2862 for op in &file.operations {
2863 for sql in render_operation_for(op, "postgres") {
2864 sqlx::query(&sql).execute(&mut *tx).await?;
2865 }
2866 }
2867 }
2868 let snapshot_hash = file.snapshot_after.hash();
2869 let applied_at = chrono::Utc::now().to_rfc3339();
2870 sqlx::query(
2871 "INSERT INTO umbral_migrations (plugin, name, applied_at, snapshot_hash) \
2872 VALUES ($1, $2, $3, $4)",
2873 )
2874 .bind(&file.plugin)
2875 .bind(&file.id)
2876 .bind(&applied_at)
2877 .bind(&snapshot_hash)
2878 .execute(&mut *tx)
2879 .await?;
2880 tx.commit().await?;
2881 applied_count += 1;
2882 }
2883 }
2884 Ok(applied_count)
2885}
2886
2887/// SQLite tenant-DB apply loop (tests). Same shape as the Postgres variant.
2888async fn migrate_tenant_apps_into_sqlite_pool(
2889 dir: &Path,
2890 shared_apps: &std::collections::HashSet<String>,
2891 pool: &sqlx::SqlitePool,
2892) -> Result<u64, MigrateError> {
2893 ensure_tracking_table_sqlite(pool).await?;
2894 let applied = applied_names_sqlite(pool).await?;
2895
2896 let mut applied_count: u64 = 0;
2897 for plugin in plugin_order() {
2898 if shared_apps.contains(&plugin) {
2899 continue;
2900 }
2901 let plugin_dir = dir.join(&plugin);
2902 let files: Vec<MigrationFile> = list_migration_files(&plugin_dir)?
2903 .iter()
2904 .map(|p| read_migration_file(p))
2905 .collect::<Result<_, _>>()?;
2906 let plan = squash_plan(&files, &applied)?;
2907 for (file, decision) in files.iter().zip(plan) {
2908 if decision == ApplyDecision::Skip {
2909 continue;
2910 }
2911 if shared_apps.contains(&file.plugin) {
2912 continue;
2913 }
2914 let snapshot_hash = file.snapshot_after.hash();
2915 let applied_at = chrono::Utc::now().to_rfc3339();
2916 // RecordOnly → empty op slice: inserts the tracking row, no DDL.
2917 let ops: Vec<&Operation> = if decision == ApplyDecision::RecordOnly {
2918 Vec::new()
2919 } else {
2920 file.operations.iter().collect()
2921 };
2922 apply_sqlite_migration_tx(
2923 pool,
2924 &ops,
2925 &file.plugin,
2926 &file.id,
2927 &applied_at,
2928 &snapshot_hash,
2929 )
2930 .await?;
2931 applied_count += 1;
2932 }
2933 }
2934 Ok(applied_count)
2935}
2936
2937/// `ensure_tracking_table_postgres` against an explicit connection (so the
2938/// caller can pin `search_path` first and have the table created in the tenant
2939/// schema rather than `public`).
2940async fn ensure_tracking_table_pg_conn(conn: &mut sqlx::PgConnection) -> Result<(), MigrateError> {
2941 sqlx::query(
2942 "CREATE TABLE IF NOT EXISTS umbral_migrations (
2943 plugin TEXT NOT NULL,
2944 name TEXT NOT NULL,
2945 applied_at TEXT NOT NULL,
2946 snapshot_hash TEXT NOT NULL,
2947 PRIMARY KEY (plugin, name)
2948 )",
2949 )
2950 .execute(conn)
2951 .await?;
2952 Ok(())
2953}
2954
2955/// SQLite drift-checking path for `run_checked_in`.
2956///
2957/// Reads the applied set, runs `detect_all_drift`, and either errors
2958/// (if `allow_drift = false` and critical drift is found) or logs a
2959/// warning and proceeds (if `allow_drift = true`). Then delegates to
2960/// `run_in_sqlite` for the actual apply loop.
2961async fn run_in_sqlite_checked(
2962 dir: &Path,
2963 pool: &sqlx::SqlitePool,
2964 allow_drift: bool,
2965 alias: &str,
2966) -> Result<u64, MigrateError> {
2967 ensure_tracking_table_sqlite(pool).await?;
2968 let applied = applied_names_sqlite(pool).await?;
2969 let report = detect_all_drift(&applied, dir)?;
2970
2971 if report.has_critical_drift() {
2972 if allow_drift {
2973 let missing = report.missing_on_disk();
2974 for entry in &missing {
2975 eprintln!(
2976 "warning: umbral migrate --allow-drift: migration {}/{} is recorded in \
2977 the tracking table but the file is missing from disk; proceeding.",
2978 entry.plugin, entry.name
2979 );
2980 }
2981 } else {
2982 let missing: Vec<(String, String)> = report
2983 .missing_on_disk()
2984 .iter()
2985 .map(|e| (e.plugin.clone(), e.name.clone()))
2986 .collect();
2987 return Err(MigrateError::DriftDetected { missing });
2988 }
2989 }
2990
2991 // Emit warnings for out-of-order files.
2992 for entry in report
2993 .entries
2994 .iter()
2995 .filter(|e| e.status == MigrationStatus::OutOfOrder)
2996 {
2997 eprintln!(
2998 "warning: umbral migrate: migration {}/{} is on disk but appears before the \
2999 last applied migration for this plugin; it looks like a file was restored \
3000 after a teammate already applied later ones.",
3001 entry.plugin, entry.name
3002 );
3003 }
3004
3005 run_in_sqlite_for_alias(dir, alias, pool, None).await
3006}
3007
3008/// Postgres drift-checking path for `run_checked_in`. Same logic as
3009/// `run_in_sqlite_checked` but uses the Postgres applied-set reader.
3010async fn run_in_postgres_checked(
3011 dir: &Path,
3012 pool: &sqlx::PgPool,
3013 allow_drift: bool,
3014 alias: &str,
3015) -> Result<u64, MigrateError> {
3016 ensure_tracking_table_postgres(pool).await?;
3017 let applied = applied_names_postgres(pool).await?;
3018 let report = detect_all_drift(&applied, dir)?;
3019
3020 if report.has_critical_drift() {
3021 if allow_drift {
3022 let missing = report.missing_on_disk();
3023 for entry in &missing {
3024 eprintln!(
3025 "warning: umbral migrate --allow-drift: migration {}/{} is recorded in \
3026 the tracking table but the file is missing from disk; proceeding.",
3027 entry.plugin, entry.name
3028 );
3029 }
3030 } else {
3031 let missing: Vec<(String, String)> = report
3032 .missing_on_disk()
3033 .iter()
3034 .map(|e| (e.plugin.clone(), e.name.clone()))
3035 .collect();
3036 return Err(MigrateError::DriftDetected { missing });
3037 }
3038 }
3039
3040 for entry in report
3041 .entries
3042 .iter()
3043 .filter(|e| e.status == MigrationStatus::OutOfOrder)
3044 {
3045 eprintln!(
3046 "warning: umbral migrate: migration {}/{} is on disk but appears before the \
3047 last applied migration for this plugin; it looks like a file was restored \
3048 after a teammate already applied later ones.",
3049 entry.plugin, entry.name
3050 );
3051 }
3052
3053 run_in_postgres_for_alias(dir, alias, pool, None).await
3054}
3055
3056/// Record a migration as applied in the `umbral_migrations` tracking
3057/// table without running its operations. The "mark as applied" path
3058/// `inspectdb --mark-applied` uses to register the introspected
3059/// `0001_initial` against an already-populated database. Idempotent:
3060/// if the `(plugin, name)` row already exists, the call is a no-op.
3061pub async fn record_applied(
3062 plugin: &str,
3063 name: &str,
3064 snapshot_hash: &str,
3065) -> Result<(), MigrateError> {
3066 let applied_at = chrono::Utc::now().to_rfc3339();
3067 match crate::db::pool_dispatched() {
3068 crate::db::DbPool::Sqlite(pool) => {
3069 ensure_tracking_table_sqlite(pool).await?;
3070 sqlx::query(
3071 "INSERT OR IGNORE INTO umbral_migrations \
3072 (plugin, name, applied_at, snapshot_hash) \
3073 VALUES (?, ?, ?, ?)",
3074 )
3075 .bind(plugin)
3076 .bind(name)
3077 .bind(&applied_at)
3078 .bind(snapshot_hash)
3079 .execute(pool)
3080 .await?;
3081 }
3082 crate::db::DbPool::Postgres(pool) => {
3083 ensure_tracking_table_postgres(pool).await?;
3084 sqlx::query(
3085 "INSERT INTO umbral_migrations \
3086 (plugin, name, applied_at, snapshot_hash) \
3087 VALUES ($1, $2, $3, $4) \
3088 ON CONFLICT (plugin, name) DO NOTHING",
3089 )
3090 .bind(plugin)
3091 .bind(name)
3092 .bind(&applied_at)
3093 .bind(snapshot_hash)
3094 .execute(pool)
3095 .await?;
3096 }
3097 }
3098 Ok(())
3099}
3100
3101// =========================================================================
3102// Drift detection — gap 24.
3103// =========================================================================
3104
3105/// Compute the drift report for a single plugin directory. Compares the
3106/// set of `(plugin, name)` pairs recorded in the tracking table against
3107/// the migration files present on disk and classifies each into one of
3108/// the four [`MigrationStatus`] states.
3109///
3110/// `applied` is the full set of `(plugin, name)` tuples already read
3111/// from the tracking table (shared across plugins to avoid extra DB
3112/// round-trips). `plugin_dir` is the on-disk directory for this plugin;
3113/// an absent directory is treated the same as an empty one.
3114///
3115/// # Classification
3116///
3117/// - File present + in DB → `Applied`
3118/// - File absent + in DB → `AppliedButMissing`
3119/// - File present + not in DB + seq ≤ max_applied_seq → `OutOfOrder`
3120/// - File present + not in DB + seq > max_applied_seq → `Pending`
3121///
3122/// The sequence number is the numeric prefix of the migration name
3123/// (e.g. `0001` in `0001_create_post`). Absence of any applied
3124/// migration for this plugin means `max_applied_seq = 0`.
3125pub fn detect_drift(
3126 plugin: &str,
3127 applied: &std::collections::HashSet<(String, String)>,
3128 plugin_dir: &Path,
3129) -> Result<Vec<MigrationEntry>, MigrateError> {
3130 // Collect on-disk migration names (the id, not the full path).
3131 let paths = list_migration_files(plugin_dir)?;
3132 let mut on_disk: Vec<String> = Vec::new();
3133 for path in &paths {
3134 let file = read_migration_file(path)?;
3135 on_disk.push(file.id.clone());
3136 }
3137
3138 // Pull every tracking-table entry for this plugin.
3139 let plugin_applied: Vec<&str> = applied
3140 .iter()
3141 .filter(|(p, _)| p == plugin)
3142 .map(|(_, n)| n.as_str())
3143 .collect();
3144
3145 // Highest sequence number among applied migrations for this plugin.
3146 let max_applied_seq: u32 = plugin_applied
3147 .iter()
3148 .filter_map(|name| name.split('_').next()?.parse::<u32>().ok())
3149 .max()
3150 .unwrap_or(0);
3151
3152 let on_disk_set: std::collections::HashSet<&str> = on_disk.iter().map(|s| s.as_str()).collect();
3153
3154 let mut entries: Vec<MigrationEntry> = Vec::new();
3155
3156 // Walk on-disk files in order.
3157 for name in &on_disk {
3158 let key = (plugin.to_string(), name.clone());
3159 let status = if applied.contains(&key) {
3160 MigrationStatus::Applied
3161 } else {
3162 // Determine this migration's sequence number.
3163 let seq: u32 = name
3164 .split('_')
3165 .next()
3166 .and_then(|s| s.parse().ok())
3167 .unwrap_or(0);
3168 if seq <= max_applied_seq && max_applied_seq > 0 {
3169 MigrationStatus::OutOfOrder
3170 } else {
3171 MigrationStatus::Pending
3172 }
3173 };
3174 entries.push(MigrationEntry {
3175 plugin: plugin.to_string(),
3176 name: name.clone(),
3177 status,
3178 });
3179 }
3180
3181 // Walk applied entries not present on disk.
3182 for name in &plugin_applied {
3183 if !on_disk_set.contains(*name) {
3184 entries.push(MigrationEntry {
3185 plugin: plugin.to_string(),
3186 name: (*name).to_string(),
3187 status: MigrationStatus::AppliedButMissing,
3188 });
3189 }
3190 }
3191
3192 // Sort: applied-but-missing entries bubble after their expected
3193 // position is not determinable; sort all entries by name for a
3194 // deterministic order. In practice, applied-but-missing names
3195 // are still prefixed with the numeric sequence so lexical sort
3196 // yields the right display order.
3197 entries.sort_by(|a, b| a.name.cmp(&b.name));
3198
3199 Ok(entries)
3200}
3201
3202/// Detect drift across every registered plugin and return a combined
3203/// [`DriftReport`]. Called by `run_in_checked` before executing SQL
3204/// and by `show_in` when displaying the four-state list.
3205///
3206/// `applied` is already fetched from the DB; `dir` is the migrations
3207/// root directory.
3208pub fn detect_all_drift(
3209 applied: &std::collections::HashSet<(String, String)>,
3210 dir: &Path,
3211) -> Result<DriftReport, MigrateError> {
3212 let mut all_entries: Vec<MigrationEntry> = Vec::new();
3213
3214 // Also surface any tracking-table entries whose plugin directory
3215 // doesn't appear in the registered-plugins list — a plugin was
3216 // removed entirely but its DB rows remain.
3217 let mut seen_plugins: std::collections::HashSet<String> = std::collections::HashSet::new();
3218
3219 for plugin in plugin_order() {
3220 seen_plugins.insert(plugin.clone());
3221 let plugin_dir = dir.join(&plugin);
3222 let entries = detect_drift(&plugin, applied, &plugin_dir)?;
3223 all_entries.extend(entries);
3224 }
3225
3226 // Any applied entries whose plugin is not in the registered set at
3227 // all — treat them as AppliedButMissing (the whole plugin is gone).
3228 for (plugin, name) in applied {
3229 if !seen_plugins.contains(plugin.as_str()) {
3230 all_entries.push(MigrationEntry {
3231 plugin: plugin.clone(),
3232 name: name.clone(),
3233 status: MigrationStatus::AppliedButMissing,
3234 });
3235 }
3236 }
3237
3238 Ok(DriftReport {
3239 entries: all_entries,
3240 })
3241}
3242
3243/// Record a migration as applied in the tracking table WITHOUT running
3244/// its SQL operations. The `--fake` recovery path: the schema already
3245/// exists (e.g. the migration was run outside umbral, or the DB was
3246/// bootstrapped from a dump) and the operator wants to bring the
3247/// tracking table into sync without re-executing the DDL.
3248///
3249/// Idempotent: if `(plugin, name)` is already in the table the call
3250/// is a no-op (same behaviour as `record_applied`).
3251///
3252/// The snapshot hash is derived from the migration file on disk.
3253/// Returns `MigrateError::Io` if the file can't be found (the caller
3254/// should verify the name before calling this).
3255pub async fn fake_apply(plugin: &str, name: &str) -> Result<(), MigrateError> {
3256 fake_apply_in(plugin, name, Path::new(MIGRATIONS_DIR)).await
3257}
3258
3259/// Same as [`fake_apply`] but takes an explicit migrations base dir.
3260/// Used by tests and by the CLI when `--migrations-dir` is passed.
3261pub async fn fake_apply_in(plugin: &str, name: &str, dir: &Path) -> Result<(), MigrateError> {
3262 let path = dir.join(plugin).join(format!("{name}.json"));
3263 let file = read_migration_file(&path)?;
3264 let snapshot_hash = file.snapshot_after.hash();
3265 record_applied(plugin, name, &snapshot_hash).await
3266}
3267
3268/// For every registered plugin's first migration (`0001_*`), check
3269/// whether the tables that migration would create already exist in the
3270/// database. If they do, fake-apply the migration (mark it applied
3271/// without running its SQL).
3272///
3273/// This is the `--fake-initial` path: the operator has a database
3274/// bootstrapped outside umbral (a dump restore, a manual `CREATE TABLE`,
3275/// or a previous schema manager) and wants to bring the tracking table
3276/// into sync so subsequent `migrate` calls apply only the genuine
3277/// deltas.
3278///
3279/// Returns the number of plugins whose `0001_*` migration was
3280/// fake-applied. Zero means either no `0001_*` file exists or the
3281/// target tables were absent (in which case normal `migrate` should be
3282/// run to create them).
3283pub async fn fake_initial() -> Result<u64, MigrateError> {
3284 fake_initial_in(Path::new(MIGRATIONS_DIR)).await
3285}
3286
3287/// Same as [`fake_initial`] but takes an explicit migrations base dir.
3288pub async fn fake_initial_in(dir: &Path) -> Result<u64, MigrateError> {
3289 match crate::db::pool_dispatched() {
3290 crate::db::DbPool::Sqlite(pool) => fake_initial_sqlite(dir, pool).await,
3291 crate::db::DbPool::Postgres(pool) => fake_initial_postgres(dir, pool).await,
3292 }
3293}
3294
3295/// SQLite path for [`fake_initial_in`].
3296async fn fake_initial_sqlite(dir: &Path, pool: &sqlx::SqlitePool) -> Result<u64, MigrateError> {
3297 ensure_tracking_table_sqlite(pool).await?;
3298 let applied = applied_names_sqlite(pool).await?;
3299 let mut count: u64 = 0;
3300
3301 for plugin in plugin_order() {
3302 let plugin_dir = dir.join(&plugin);
3303 let paths = list_migration_files(&plugin_dir)?;
3304
3305 // Find the first migration file (lowest sequence number).
3306 let first = paths.first();
3307 let first = match first {
3308 Some(p) => p,
3309 None => continue,
3310 };
3311 let file = read_migration_file(first)?;
3312
3313 // Skip if already applied.
3314 if applied.contains(&(file.plugin.clone(), file.id.clone())) {
3315 continue;
3316 }
3317
3318 // Check whether the tables the first migration would create
3319 // already exist in the database.
3320 let tables_to_create: Vec<&str> = file
3321 .operations
3322 .iter()
3323 .filter_map(|op| match op {
3324 Operation::CreateTable { table, .. } => Some(table.as_str()),
3325 _ => None,
3326 })
3327 .collect();
3328
3329 if tables_to_create.is_empty() {
3330 continue;
3331 }
3332
3333 // All tables present → fake-apply.
3334 let mut all_present = true;
3335 for table in &tables_to_create {
3336 let exists: Option<(String,)> =
3337 sqlx::query_as("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?")
3338 .bind(*table)
3339 .fetch_optional(pool)
3340 .await?;
3341 if exists.is_none() {
3342 all_present = false;
3343 break;
3344 }
3345 }
3346
3347 if all_present {
3348 let snapshot_hash = file.snapshot_after.hash();
3349 let applied_at = chrono::Utc::now().to_rfc3339();
3350 sqlx::query(
3351 "INSERT OR IGNORE INTO umbral_migrations \
3352 (plugin, name, applied_at, snapshot_hash) VALUES (?, ?, ?, ?)",
3353 )
3354 .bind(&file.plugin)
3355 .bind(&file.id)
3356 .bind(&applied_at)
3357 .bind(&snapshot_hash)
3358 .execute(pool)
3359 .await?;
3360 count += 1;
3361 }
3362 }
3363
3364 Ok(count)
3365}
3366
3367/// Postgres path for [`fake_initial_in`].
3368async fn fake_initial_postgres(dir: &Path, pool: &sqlx::PgPool) -> Result<u64, MigrateError> {
3369 ensure_tracking_table_postgres(pool).await?;
3370 let applied = applied_names_postgres(pool).await?;
3371 let mut count: u64 = 0;
3372
3373 for plugin in plugin_order() {
3374 let plugin_dir = dir.join(&plugin);
3375 let paths = list_migration_files(&plugin_dir)?;
3376
3377 let first = paths.first();
3378 let first = match first {
3379 Some(p) => p,
3380 None => continue,
3381 };
3382 let file = read_migration_file(first)?;
3383
3384 if applied.contains(&(file.plugin.clone(), file.id.clone())) {
3385 continue;
3386 }
3387
3388 let tables_to_create: Vec<&str> = file
3389 .operations
3390 .iter()
3391 .filter_map(|op| match op {
3392 Operation::CreateTable { table, .. } => Some(table.as_str()),
3393 _ => None,
3394 })
3395 .collect();
3396
3397 if tables_to_create.is_empty() {
3398 continue;
3399 }
3400
3401 let mut all_present = true;
3402 for table in &tables_to_create {
3403 let exists: Option<(String,)> = sqlx::query_as(
3404 "SELECT table_name FROM information_schema.tables \
3405 WHERE table_schema = 'public' AND table_name = $1",
3406 )
3407 .bind(*table)
3408 .fetch_optional(pool)
3409 .await?;
3410 if exists.is_none() {
3411 all_present = false;
3412 break;
3413 }
3414 }
3415
3416 if all_present {
3417 let snapshot_hash = file.snapshot_after.hash();
3418 let applied_at = chrono::Utc::now().to_rfc3339();
3419 sqlx::query(
3420 "INSERT INTO umbral_migrations \
3421 (plugin, name, applied_at, snapshot_hash) VALUES ($1, $2, $3, $4) \
3422 ON CONFLICT (plugin, name) DO NOTHING",
3423 )
3424 .bind(&file.plugin)
3425 .bind(&file.id)
3426 .bind(&applied_at)
3427 .bind(&snapshot_hash)
3428 .execute(pool)
3429 .await?;
3430 count += 1;
3431 }
3432 }
3433
3434 Ok(count)
3435}
3436
3437/// Print the per-migration state, applied or pending. Output goes to
3438/// stdout; the return value is the count of pending migrations so a
3439/// CLI can `exit(n)` on need.
3440pub async fn show() -> Result<u64, MigrateError> {
3441 show_in(Path::new(MIGRATIONS_DIR)).await
3442}
3443
3444/// Read-only migration status for a health / readiness probe (Kikosi #5,
3445/// gaps3 #38).
3446///
3447/// Returns the same [`DriftReport`] `showmigrations` computes — one entry per
3448/// migration, categorised into the four [`MigrationStatus`] states — but prints
3449/// nothing, so it is safe to call on every `/readyz` hit. Reads the
3450/// `umbral_migrations` tracking table (ensuring it exists) and the on-disk
3451/// `migrations/` tree. Use [`DriftReport::pending`] to gate readiness.
3452pub async fn drift_report() -> Result<DriftReport, MigrateError> {
3453 drift_report_in(Path::new(MIGRATIONS_DIR)).await
3454}
3455
3456/// [`drift_report`] against an explicit migrations directory. The entry tests
3457/// drive this so they can point at a temp tree.
3458pub async fn drift_report_in(dir: &Path) -> Result<DriftReport, MigrateError> {
3459 let applied = match crate::db::pool_dispatched() {
3460 crate::db::DbPool::Sqlite(pool) => {
3461 ensure_tracking_table_sqlite(pool).await?;
3462 applied_names_sqlite(pool).await?
3463 }
3464 crate::db::DbPool::Postgres(pool) => {
3465 ensure_tracking_table_postgres(pool).await?;
3466 applied_names_postgres(pool).await?
3467 }
3468 };
3469 detect_all_drift(&applied, dir)
3470}
3471
3472/// Same as [`show`] but takes an explicit base directory. Walks every
3473/// registered plugin in sorted-by-name order, printing one section per
3474/// plugin that owns at least one migration file; empty plugins are
3475/// skipped silently rather than emitting a bare header.
3476///
3477/// Four-state output (gap 24):
3478///
3479/// - `[X]` applied and file present on disk (normal)
3480/// - `[ ]` pending (on disk, not yet applied, sequence after last applied)
3481/// - `[!]` applied but missing on disk (drift — tracking table ahead of VCS)
3482/// - `[?]` on disk but out of order (sequence before last applied, not in DB)
3483pub async fn show_in(dir: &Path) -> Result<u64, MigrateError> {
3484 let report = drift_report_in(dir).await?;
3485
3486 // Group by plugin for display.
3487 let mut by_plugin: std::collections::BTreeMap<&str, Vec<&MigrationEntry>> =
3488 std::collections::BTreeMap::new();
3489 for entry in &report.entries {
3490 by_plugin
3491 .entry(entry.plugin.as_str())
3492 .or_default()
3493 .push(entry);
3494 }
3495
3496 let mut pending: u64 = 0;
3497 for (plugin, entries) in &by_plugin {
3498 if entries.is_empty() {
3499 continue;
3500 }
3501 println!("# plugin: {plugin}");
3502 for entry in entries {
3503 let marker = match entry.status {
3504 MigrationStatus::Applied => "[X]",
3505 MigrationStatus::Pending => {
3506 pending += 1;
3507 "[ ]"
3508 }
3509 MigrationStatus::AppliedButMissing => "[!]",
3510 MigrationStatus::OutOfOrder => "[?]",
3511 };
3512 println!("{marker} {}/{}", entry.plugin, entry.name);
3513 }
3514 }
3515 Ok(pending)
3516}
3517
3518/// Safety classification for a single pending migration operation.
3519///
3520/// Feature #65 (blue-green / zero-downtime). The `checkmigrations`
3521/// command walks every pending operation and tags it so an operator
3522/// deploying without a maintenance window can tell which changes are safe
3523/// under a rolling deploy (old and new code serving traffic at once) and
3524/// which need the expand-contract dance. This is advisory triage — the
3525/// engine still *applies* every op exactly as written; nothing here gates
3526/// `migrate`.
3527#[derive(Debug, Clone, PartialEq, Eq)]
3528pub enum OpSafety {
3529 /// Additive and backward-compatible — safe while old code still runs.
3530 Safe,
3531 /// Applies cleanly but can break still-running old code, lock a large
3532 /// table, or fail against unexpected production data. Review first.
3533 Warning(String),
3534 /// Destroys data or is irreversible; old code referencing the dropped
3535 /// surface errors immediately.
3536 Unsafe(String),
3537}
3538
3539impl OpSafety {
3540 /// The advisory reason for a `Warning` / `Unsafe`; empty for `Safe`.
3541 pub fn reason(&self) -> &str {
3542 match self {
3543 OpSafety::Safe => "",
3544 OpSafety::Warning(r) | OpSafety::Unsafe(r) => r,
3545 }
3546 }
3547
3548 /// True for the destructive / irreversible tier only.
3549 pub fn is_unsafe(&self) -> bool {
3550 matches!(self, OpSafety::Unsafe(_))
3551 }
3552
3553 /// True for the review-before-deploy tier only.
3554 pub fn is_warning(&self) -> bool {
3555 matches!(self, OpSafety::Warning(_))
3556 }
3557}
3558
3559/// One pending operation tagged with its [`OpSafety`] and the migration
3560/// that introduced it. The unit of output for `checkmigrations`.
3561#[derive(Debug, Clone)]
3562pub struct ClassifiedOp {
3563 pub plugin: String,
3564 pub migration: String,
3565 pub op: Operation,
3566 pub safety: OpSafety,
3567}
3568
3569/// Classify one operation for zero-downtime safety. Pure — no DB access,
3570/// no file reads — so it is trivially unit-testable and reused by both
3571/// the CLI report and any plugin that wants to gate its own deploys.
3572pub fn classify_operation(op: &Operation) -> OpSafety {
3573 match op {
3574 // Brand-new tables touch no existing rows and no old code reads
3575 // them yet.
3576 Operation::CreateTable { .. } | Operation::CreateM2MTable { .. } => OpSafety::Safe,
3577
3578 // A column comment is metadata: no rows read or written, no lock worth
3579 // naming, and no code path — old or new — reads it. A docstring edit
3580 // must never gate a zero-downtime deploy.
3581 Operation::SetColumnComment { .. } => OpSafety::Safe,
3582
3583 // Adding a column is additive — unless it's NOT NULL with no
3584 // default, in which case old code inserting a row without the
3585 // column fails. (The engine refuses such an add against a
3586 // populated SQLite table at apply time; this surfaces the same
3587 // hazard *before* the operator runs it, and for Postgres too.)
3588 Operation::AddColumn { table, column } => {
3589 if !column.nullable && column.default.is_empty() {
3590 OpSafety::Warning(format!(
3591 "adds NOT NULL column `{}.{}` with no default — old code inserting without it will fail. Add it nullable (or with a default), backfill, then tighten",
3592 table, column.name
3593 ))
3594 } else {
3595 OpSafety::Safe
3596 }
3597 }
3598
3599 // A view holds no rows of its own (a materialized one holds only derived,
3600 // recomputable rows), so creating one is free and dropping one destroys
3601 // nothing. What a DropView CAN break is old code still selecting from it —
3602 // and since every view edit lowers to drop+create, classifying this Unsafe
3603 // would trip the zero-downtime gate on a routine SQL tweak. Warning is the
3604 // honest severity: worth reading, not worth blocking.
3605 Operation::CreateView { .. } => OpSafety::Safe,
3606 Operation::DropView { name, .. } => OpSafety::Warning(format!(
3607 "drops view `{name}` — no rows are lost (a view stores none), but old code \
3608 still selecting from it breaks until the recreate lands in the same migration"
3609 )),
3610
3611 // Destructive / irreversible: data loss the moment it runs.
3612 Operation::DropTable { table } => OpSafety::Unsafe(format!(
3613 "drops table `{table}` and every row in it — irreversible, and old code still reading it breaks. Stop using it, deploy, then drop in a later migration"
3614 )),
3615 Operation::DropM2MTable { junction_table } => OpSafety::Unsafe(format!(
3616 "drops join table `{junction_table}` and every row in it — irreversible"
3617 )),
3618 Operation::DropColumn { table, column } => OpSafety::Unsafe(format!(
3619 "drops column `{table}.{column}` and its data — old code reading it breaks. Expand-contract: stop writing it, deploy, then drop"
3620 )),
3621
3622 // Renames apply atomically in the DB but NOT atomically with a
3623 // code deploy: between the migration and the rollout, one of the
3624 // two code versions references the missing name.
3625 Operation::RenameTable { from, to } => OpSafety::Warning(format!(
3626 "renames table `{from}` → `{to}` — not atomic with a code deploy; old code references `{from}`. Expand-contract: add `{to}`, dual-write, switch, then drop `{from}`"
3627 )),
3628 Operation::RenameColumn {
3629 table, from, to, ..
3630 } => OpSafety::Warning(format!(
3631 "renames column `{table}.{from}` → `{to}` — old code references `{from}`. Expand-contract: add `{to}`, backfill, switch reads, then drop `{from}`"
3632 )),
3633
3634 // An alter can rewrite a column (table lock on large data) and a
3635 // nullable→NOT NULL tightening fails on existing NULLs.
3636 Operation::AlterColumn { table, column, .. } => OpSafety::Warning(format!(
3637 "alters column `{table}.{column}` — a type change rewrites the column (locks the table on large data) and a NOT NULL tightening fails on existing NULLs; verify against production data first"
3638 )),
3639
3640 // A hand-authored data migration runs arbitrary SQL — the
3641 // engine can't reason about its row impact, so flag it for
3642 // human review (it may rewrite or delete data, and re-running
3643 // the rollout while it's mid-flight can double-apply).
3644 Operation::RunSql { .. } => OpSafety::Warning(
3645 "runs a hand-authored data migration (raw SQL) — review its row impact, ensure it's idempotent or guarded, and verify it against production data first".to_string(),
3646 ),
3647
3648 // Adding a composite UNIQUE constraint fails at apply time if
3649 // existing rows already violate it — same hazard as a single-column
3650 // UNIQUE add. A plain (non-unique) index is purely additive.
3651 Operation::AddIndex {
3652 table,
3653 columns,
3654 unique: true,
3655 } => OpSafety::Warning(format!(
3656 "adds a composite UNIQUE constraint on `{table}` ({}) — fails on existing duplicate rows; de-duplicate first or the migration aborts",
3657 columns.join(", ")
3658 )),
3659 Operation::AddIndex { unique: false, .. } => OpSafety::Safe,
3660
3661 // Dropping an index / UNIQUE constraint touches no rows. It removes
3662 // a guarantee (a later duplicate becomes insertable) but that is the
3663 // intent when a `unique_together` is removed, and no data is lost.
3664 Operation::DropIndex { .. } => OpSafety::Safe,
3665 }
3666}
3667
3668/// Classify every operation across all pending migrations against the
3669/// ambient pool. Reads the same applied-set + on-disk diff that
3670/// `migrate` / `showmigrations` use, then loads each pending migration
3671/// file and classifies its operations in order. Powers `checkmigrations`.
3672pub async fn check_pending_safety() -> Result<Vec<ClassifiedOp>, MigrateError> {
3673 check_pending_safety_in(Path::new(MIGRATIONS_DIR)).await
3674}
3675
3676/// [`check_pending_safety`] against an explicit migrations directory.
3677/// The seam tests use to point at a fixture tree.
3678pub async fn check_pending_safety_in(dir: &Path) -> Result<Vec<ClassifiedOp>, MigrateError> {
3679 let applied = match crate::db::pool_dispatched() {
3680 crate::db::DbPool::Sqlite(pool) => {
3681 ensure_tracking_table_sqlite(pool).await?;
3682 applied_names_sqlite(pool).await?
3683 }
3684 crate::db::DbPool::Postgres(pool) => {
3685 ensure_tracking_table_postgres(pool).await?;
3686 applied_names_postgres(pool).await?
3687 }
3688 };
3689
3690 let report = detect_all_drift(&applied, dir)?;
3691
3692 let mut out: Vec<ClassifiedOp> = Vec::new();
3693 for entry in &report.entries {
3694 if entry.status != MigrationStatus::Pending {
3695 continue;
3696 }
3697 let path = dir.join(&entry.plugin).join(format!("{}.json", entry.name));
3698 let file = read_migration_file(&path)?;
3699 for op in &file.operations {
3700 out.push(ClassifiedOp {
3701 plugin: entry.plugin.clone(),
3702 migration: entry.name.clone(),
3703 op: op.clone(),
3704 safety: classify_operation(op),
3705 });
3706 }
3707 }
3708 Ok(out)
3709}
3710
3711// =========================================================================
3712// Internal helpers. Crate-private; the public surface above is the only
3713// thing the rest of umbral calls into.
3714// =========================================================================
3715
3716/// Return every `*.json` migration file in `plugin_dir`, sorted by
3717/// filename (lexical sort matches numeric order because the prefix is
3718/// zero-padded). Returns an empty vec if the directory is missing.
3719fn list_migration_files(plugin_dir: &Path) -> Result<Vec<PathBuf>, MigrateError> {
3720 if !plugin_dir.exists() {
3721 return Ok(Vec::new());
3722 }
3723 let mut paths: Vec<PathBuf> = Vec::new();
3724 for entry in std::fs::read_dir(plugin_dir)? {
3725 let entry = entry?;
3726 let path = entry.path();
3727 if path.extension().and_then(|s| s.to_str()) == Some("json") {
3728 paths.push(path);
3729 }
3730 }
3731 paths.sort();
3732 Ok(paths)
3733}
3734
3735/// Read and parse one migration file.
3736fn read_migration_file(path: &Path) -> Result<MigrationFile, MigrateError> {
3737 let text = std::fs::read_to_string(path)?;
3738 let file: MigrationFile = serde_json::from_str(&text)?;
3739 Ok(file)
3740}
3741
3742/// Diff the previous snapshot against the current one and produce the
3743/// ordered operation list.
3744///
3745/// Emits `CreateTable` / `DropTable` for whole-model changes (M5 v1),
3746/// and `AddColumn` / `DropColumn` for column-level changes on a model
3747/// that appears in both snapshots (M8 v1). A column whose name stays
3748/// the same but whose type or nullable flag changed surfaces as
3749/// [`MigrateError::UnsafeAlter`]: SQLite can't ALTER COLUMN TYPE in
3750/// place, and a nullable flip on a populated table is destructive.
3751///
3752/// Gap 30 adds two-pass rename detection. `Model::NAME` (the Rust struct
3753/// name) is the stable identity key across snapshots; the SQL table name
3754/// in `Model::TABLE` may change (e.g. via the `#[umbral(plugin = "...")]`
3755/// opt-in). The two passes are:
3756///
3757/// - **First pass — struct-name match.** If a model present in `current`
3758/// but absent from `previous` (by `Model::NAME`) has the same NAME as
3759/// a model present in `previous` but absent from `current`, the table
3760/// name changed: emit `RenameTable { from, to }` instead of DropTable +
3761/// CreateTable. A stdout message names the rename so the developer can
3762/// audit `makemigrations` output.
3763/// - **Second pass — column-shape match.** Among unpaired drops and
3764/// creates, if a drop candidate and a create candidate have bit-identical
3765/// column shapes (same column names, types, nullable, fk_target), emit
3766/// `RenameTable` and log a warning so the developer can verify the
3767/// intent. Struct names differ; the shape heuristic fills in for cases
3768/// like a wholesale model rename (Foo → Bar, identical fields).
3769/// - **No-match.** Drop and create as today.
3770///
3771/// `pub` (not `pub(crate)`) so integration tests can drive the diff
3772/// directly with hand-built snapshots. Spec 06 calls the diff the
3773/// engine's contract; exposing it lets the tests pin every scenario
3774/// without laundering snapshots through the process-wide registry.
3775/// audit_2 H23 — how `diff` should treat an ambiguous column-shape rename: an
3776/// unpaired dropped model and an unpaired created model with *identical* shapes.
3777/// Driven by `UMBRAL_MIGRATIONS_ASSUME_RENAMES`.
3778enum RenameIntent {
3779 /// Auto-pair every shape match into a `RenameTable` (the pre-H23 default).
3780 Assume,
3781 /// Treat the pair as unrelated: emit drop + create, no row transfer.
3782 Independent,
3783 /// Not configured → `diff` fails closed with `AmbiguousRename`.
3784 Undecided,
3785}
3786
3787fn rename_intent() -> RenameIntent {
3788 match std::env::var("UMBRAL_MIGRATIONS_ASSUME_RENAMES") {
3789 Ok(v) => match v.trim().to_ascii_lowercase().as_str() {
3790 "assume" | "1" | "true" | "yes" | "rename" => RenameIntent::Assume,
3791 "independent" | "0" | "false" | "no" | "drop" => RenameIntent::Independent,
3792 _ => RenameIntent::Undecided,
3793 },
3794 Err(_) => RenameIntent::Undecided,
3795 }
3796}
3797
3798/// Emit `AddIndex` / `DropIndex` ops for changes to a model's TABLE-level
3799/// `unique_together` and `indexes` between two snapshots.
3800///
3801/// Column-shape changes are handled by [`diff_columns`]; this covers the
3802/// constraint-only deltas it can't see. Before this existed, adding a
3803/// `unique_together` group or a multi-column `indexes` entry with no column
3804/// change produced NO migration at all — `makemigrations` said "no changes"
3805/// and the constraint was silently never created.
3806///
3807/// A group is identified by its ORDERED column list, so re-ordering a group
3808/// (`[a, b]` → `[b, a]`) reads as drop-old + add-new — correct, since column
3809/// order changes which queries the index serves. `AddIndex`/`DropIndex`
3810/// render `IF NOT EXISTS` / `IF EXISTS`, so when a same-table `AlterColumn`
3811/// in the same migration already rebuilt the table with the new constraint
3812/// set (the SQLite dance), these ops are harmless no-ops.
3813fn diff_indexes(previous: &ModelMeta, current: &ModelMeta) -> Vec<Operation> {
3814 use std::collections::BTreeSet;
3815
3816 let mut ops: Vec<Operation> = Vec::new();
3817
3818 let mut emit = |prev_groups: &[Vec<String>], curr_groups: &[Vec<String>], unique: bool| {
3819 let prev_set: BTreeSet<&Vec<String>> = prev_groups.iter().collect();
3820 let curr_set: BTreeSet<&Vec<String>> = curr_groups.iter().collect();
3821 // Added groups → AddIndex (on the current table name).
3822 for group in curr_set.difference(&prev_set) {
3823 ops.push(Operation::AddIndex {
3824 table: current.table.clone(),
3825 columns: (*group).clone(),
3826 unique,
3827 });
3828 }
3829 // Removed groups → DropIndex (on the previous table name — a rename
3830 // in the same diff emits its own RenameTable first, and the index
3831 // travels with the table, so the drop names the post-rename table;
3832 // use current.table for that reason).
3833 for group in prev_set.difference(&curr_set) {
3834 ops.push(Operation::DropIndex {
3835 table: current.table.clone(),
3836 columns: (*group).clone(),
3837 unique,
3838 });
3839 }
3840 };
3841
3842 emit(&previous.unique_together, ¤t.unique_together, true);
3843 emit(&previous.indexes, ¤t.indexes, false);
3844
3845 // Single-column `#[umbral(index)]` flag flips. A column that gains the
3846 // flag → AddIndex; one that loses it (but still exists) → DropIndex. PK
3847 // and UNIQUE columns are excluded — they carry their own index, matching
3848 // `should_emit_btree_index`. A column that was DROPPED entirely takes its
3849 // index with it (DROP COLUMN cascades on both backends), so we don't emit a
3850 // redundant DropIndex for it. The `idx_<table>_<col>` name matches the one
3851 // `create_index_stmt` uses at CreateTable time, so add/drop stay symmetric.
3852 let indexed = |m: &ModelMeta| -> std::collections::BTreeSet<String> {
3853 m.fields
3854 .iter()
3855 .filter(|c| c.index && !c.primary_key && !c.unique)
3856 .map(|c| c.name.clone())
3857 .collect()
3858 };
3859 let curr_col_names: std::collections::BTreeSet<&str> =
3860 current.fields.iter().map(|c| c.name.as_str()).collect();
3861 let prev_indexed = indexed(previous);
3862 let curr_indexed = indexed(current);
3863 for name in curr_indexed.difference(&prev_indexed) {
3864 ops.push(Operation::AddIndex {
3865 table: current.table.clone(),
3866 columns: vec![name.clone()],
3867 unique: false,
3868 });
3869 }
3870 for name in prev_indexed.difference(&curr_indexed) {
3871 if curr_col_names.contains(name.as_str()) {
3872 ops.push(Operation::DropIndex {
3873 table: current.table.clone(),
3874 columns: vec![name.clone()],
3875 unique: false,
3876 });
3877 }
3878 }
3879 ops
3880}
3881
3882pub fn diff(previous: &Snapshot, current: &Snapshot) -> Result<Vec<Operation>, MigrateError> {
3883 use std::collections::{BTreeMap, HashSet};
3884
3885 // features #73 — views are diffed on a separate track, for a reason that runs
3886 // deeper than tidiness: every pass below exists to AVOID destroying data (the
3887 // rename heuristic, the safe-cast table, the data-preserving ALTER). A view
3888 // stores nothing, so none of that machinery applies to it, and letting a view
3889 // through it would be actively wrong — Pass 1 could "detect a rename" between a
3890 // dropped table and a created view and emit an ALTER against a relation that has
3891 // no rows to alter.
3892 //
3893 // So: hide the views from the table passes entirely, then handle them in Pass 5
3894 // with the only two operations a view can ever need.
3895 let view_prev = previous;
3896 let view_curr = current;
3897 let prev_tables = Snapshot {
3898 models: previous
3899 .models
3900 .iter()
3901 .filter(|m| m.view.is_none())
3902 .cloned()
3903 .collect(),
3904 };
3905 let curr_tables = Snapshot {
3906 models: current
3907 .models
3908 .iter()
3909 .filter(|m| m.view.is_none())
3910 .cloned()
3911 .collect(),
3912 };
3913 let previous = &prev_tables;
3914 let current = &curr_tables;
3915
3916 let prev_by_name: BTreeMap<&str, &ModelMeta> = previous
3917 .models
3918 .iter()
3919 .map(|m| (m.name.as_str(), m))
3920 .collect();
3921 let curr_by_name: BTreeMap<&str, &ModelMeta> = current
3922 .models
3923 .iter()
3924 .map(|m| (m.name.as_str(), m))
3925 .collect();
3926
3927 let mut ops: Vec<Operation> = Vec::new();
3928
3929 // gaps.md #93: every table rename `diff` decides on (old_table → new_table),
3930 // recorded so Pass 4 can rename the parent's M2M junctions instead of
3931 // dropping + recreating them (which would destroy every relationship row).
3932 let mut renamed_tables: BTreeMap<String, String> = BTreeMap::new();
3933
3934 // ---- Pass 0: Walk models present in both snapshots (same NAME). ----
3935 // Same-name models with a different table produce a first-pass rename.
3936 // Same-name models with identical table+columns produce nothing.
3937 // Same-name models with column changes produce column-level ops.
3938
3939 let mut drop_candidates: Vec<&ModelMeta> = Vec::new(); // in prev, not curr
3940 let mut create_candidates: Vec<&ModelMeta> = Vec::new(); // in curr, not prev
3941
3942 // Creates and column-level diffs, in deterministic name order.
3943 for (name, curr) in &curr_by_name {
3944 match prev_by_name.get(name) {
3945 None => {
3946 // In current but not previous — might be a create or a first-pass rename.
3947 create_candidates.push(curr);
3948 }
3949 Some(prev) if prev.table != curr.table => {
3950 // Same struct name, different table name → first-pass rename.
3951 println!(
3952 "umbral makemigrations: rename detected (struct-name match): \
3953 table `{}` → `{}`",
3954 prev.table, curr.table
3955 );
3956 ops.push(Operation::RenameTable {
3957 from: prev.table.clone(),
3958 to: curr.table.clone(),
3959 });
3960 renamed_tables.insert(prev.table.clone(), curr.table.clone());
3961 // After the rename the columns might also have changed; diff them.
3962 let col_ops = diff_columns(name, prev, curr)?;
3963 ops.extend(col_ops);
3964 // ...and the table-level constraints (unique_together/indexes).
3965 ops.extend(diff_indexes(prev, curr));
3966 }
3967 Some(prev) if prev == curr => {}
3968 Some(prev) => {
3969 ops.extend(diff_columns(name, prev, curr)?);
3970 // Constraint-only deltas (a new/removed unique_together or
3971 // composite index with no column change) — otherwise silent.
3972 ops.extend(diff_indexes(prev, curr));
3973 }
3974 }
3975 }
3976
3977 // Drops — models in prev but not curr (by NAME).
3978 for (name, prev) in &prev_by_name {
3979 if !curr_by_name.contains_key(name) {
3980 drop_candidates.push(prev);
3981 }
3982 }
3983
3984 // ---- Pass 1: Column-shape heuristic for unpaired drops + creates. ----
3985 // A sorted, canonical serialisation of (name, ty, nullable, fk_target)
3986 // is the "shape" fingerprint. Bit-identical shapes → likely a model
3987 // rename where the struct name also changed.
3988
3989 // audit_2 H23 — a bit-identical column shape between a dropped and a
3990 // created model is genuinely ambiguous (rename vs. two unrelated models).
3991 // The pre-H23 code auto-emitted a `RenameTable` with only an `eprintln!`,
3992 // silently handing one model's rows to another and skipping the intended
3993 // drop. Now the ambiguity is resolved by explicit operator intent: `assume`
3994 // restores auto-pairing, `independent` treats the pair as unrelated
3995 // (drop + create), and the unset default fails closed so no destructive
3996 // guess is ever applied silently.
3997 let intent = rename_intent();
3998 let mut paired_drop_tables: HashSet<&str> = HashSet::new();
3999 let mut paired_create_tables: HashSet<&str> = HashSet::new();
4000
4001 'creates: for create in &create_candidates {
4002 let create_shape = column_shape(&create.fields);
4003 for drop in &drop_candidates {
4004 if paired_drop_tables.contains(drop.table.as_str()) {
4005 continue;
4006 }
4007 if column_shape(&drop.fields) != create_shape {
4008 continue;
4009 }
4010 match intent {
4011 RenameIntent::Assume => {
4012 eprintln!(
4013 "umbral makemigrations: rename ASSUMED (column-shape match): \
4014 `{}` → `{}` — moving the old table's rows to the new name. Set \
4015 UMBRAL_MIGRATIONS_ASSUME_RENAMES=independent if these are unrelated \
4016 models that merely share a shape.",
4017 drop.table, create.table
4018 );
4019 ops.push(Operation::RenameTable {
4020 from: drop.table.clone(),
4021 to: create.table.clone(),
4022 });
4023 renamed_tables.insert(drop.table.clone(), create.table.clone());
4024 paired_drop_tables.insert(drop.table.as_str());
4025 paired_create_tables.insert(create.table.as_str());
4026 continue 'creates;
4027 }
4028 RenameIntent::Independent => {
4029 eprintln!(
4030 "umbral makemigrations: column-shape match `{}` ↔ `{}` treated as \
4031 UNRELATED (drop + create) per UMBRAL_MIGRATIONS_ASSUME_RENAMES=\
4032 independent. Set it to `assume` (or hand-write a RenameTable) if this \
4033 is actually a rename — otherwise `{}`'s rows are dropped.",
4034 drop.table, create.table, drop.table
4035 );
4036 // Leave both unpaired → Pass 2 creates, Pass 3 drops.
4037 continue 'creates;
4038 }
4039 RenameIntent::Undecided => {
4040 return Err(MigrateError::AmbiguousRename {
4041 from_table: drop.table.clone(),
4042 to_table: create.table.clone(),
4043 });
4044 }
4045 }
4046 }
4047 }
4048
4049 // ---- Pass 2: Emit plain CreateTable for unpaired creates. ----
4050 //
4051 // Sort the create list topologically by FK dependency so that a
4052 // table referenced by another table in this batch is created first.
4053 // Without this, Postgres rejects the second CreateTable with
4054 // `relation "<target>" does not exist`. (SQLite tolerates the wrong
4055 // order when `foreign_keys=OFF`, the historical default; once
4056 // we turned foreign_keys ON in connect_sqlite, SQLite agrees with
4057 // Postgres on the order requirement.)
4058 //
4059 // Kahn's algorithm on (table → set of FK-target tables that are
4060 // ALSO in the create batch). Self-references and FK targets outside
4061 // the batch are skipped (they're either harmless or already exist
4062 // by the time this migration runs).
4063 let creates: Vec<&&ModelMeta> = create_candidates
4064 .iter()
4065 .filter(|c| !paired_create_tables.contains(c.table.as_str()))
4066 .collect();
4067 let batch_tables: HashSet<&str> = creates.iter().map(|c| c.table.as_str()).collect();
4068 let mut deps: BTreeMap<&str, HashSet<&str>> = BTreeMap::new();
4069 for create in &creates {
4070 let mut in_batch: HashSet<&str> = HashSet::new();
4071 for col in &create.fields {
4072 if let Some(target) = col.fk_target.as_deref()
4073 && target != create.table.as_str()
4074 && batch_tables.contains(target)
4075 {
4076 in_batch.insert(target);
4077 }
4078 }
4079 deps.insert(create.table.as_str(), in_batch);
4080 }
4081 // Kahn: repeatedly pop tables with no remaining deps in the batch.
4082 // BTreeMap iteration is alphabetical → ties break alphabetically,
4083 // keeping the output stable.
4084 let mut ordered: Vec<&&ModelMeta> = Vec::with_capacity(creates.len());
4085 while !deps.is_empty() {
4086 let ready: Vec<&str> = deps
4087 .iter()
4088 .filter(|(_, d)| d.is_empty())
4089 .map(|(t, _)| *t)
4090 .collect();
4091 if ready.is_empty() {
4092 // Cyclic FK or other unresolvable dep — fall through to
4093 // the original order rather than dropping models. A cycle
4094 // here means the user's schema can't be created with
4095 // plain CreateTable anyway (Postgres needs deferrable
4096 // constraints), so we surface the user-visible error at
4097 // apply time instead of silently looping.
4098 for create in &creates {
4099 if deps.contains_key(create.table.as_str()) {
4100 ordered.push(create);
4101 }
4102 }
4103 break;
4104 }
4105 for t in &ready {
4106 if let Some(create) = creates.iter().find(|c| c.table.as_str() == *t) {
4107 ordered.push(create);
4108 }
4109 deps.remove(t);
4110 }
4111 for (_, set) in deps.iter_mut() {
4112 for t in &ready {
4113 set.remove(t);
4114 }
4115 }
4116 }
4117 for create in ordered {
4118 ops.push(Operation::CreateTable {
4119 table: create.table.clone(),
4120 columns: create.fields.clone(),
4121 unique_together: create.unique_together.clone(),
4122 indexes: create.indexes.clone(),
4123 });
4124 }
4125
4126 // ---- Pass 3: Emit plain DropTable for unpaired drops. ----
4127 for drop in &drop_candidates {
4128 if !paired_drop_tables.contains(drop.table.as_str()) {
4129 ops.push(Operation::DropTable {
4130 table: drop.table.clone(),
4131 });
4132 }
4133 }
4134
4135 // ---- Pass 4: Diff M2M relations. Closes the remaining BUG-16 gap. ----
4136 //
4137 // Treat each (parent_table, field_name) pair as a junction-table identity.
4138 // Compare the flattened set across snapshots and emit CreateM2MTable /
4139 // DropM2MTable per delta.
4140 //
4141 // gaps.md #93: when the PARENT model was renamed (Pass 0/1), its junction's
4142 // key moves from `(old_table, field)` to `(new_table, field)` — which would
4143 // otherwise read as one junction dropped and a different one created,
4144 // destroying every relationship row. Detect that case via `renamed_tables`
4145 // and emit a plain `RenameTable` on the junction instead. The junction's
4146 // columns are generic (`parent_id`/`child_id`) and its FK to the parent is
4147 // auto-updated by the parent's own rename, so a table rename is sufficient.
4148 let prev_m2m = collect_m2m_pairs(previous);
4149 let curr_m2m = collect_m2m_pairs(current);
4150
4151 // Prev junction keys consumed by a junction rename below — skip their drop.
4152 let mut renamed_prev_junctions: HashSet<(String, String)> = HashSet::new();
4153
4154 for (key, spec) in &curr_m2m {
4155 if prev_m2m.contains_key(key) {
4156 continue;
4157 }
4158 let (new_parent, field) = key;
4159 // Was this junction's parent renamed FROM some old table? If the prior
4160 // snapshot has the same field on that old parent, targeting the same
4161 // table, it's a junction rename, not a fresh create.
4162 let renamed_from = renamed_tables
4163 .iter()
4164 .find(|(_, new)| *new == new_parent)
4165 .and_then(|(old, _)| {
4166 let old_key = (old.clone(), field.clone());
4167 prev_m2m.get(&old_key).and_then(|old_spec| {
4168 (old_spec.target_table == spec.target_table).then_some(old_key)
4169 })
4170 });
4171 if let Some(old_key) = renamed_from {
4172 let old_junction = prev_m2m[&old_key].junction_table.clone();
4173 ops.push(Operation::RenameTable {
4174 from: old_junction,
4175 to: spec.junction_table.clone(),
4176 });
4177 renamed_prev_junctions.insert(old_key);
4178 continue;
4179 }
4180 // New M2M field on an existing or new model. Resolve the
4181 // target's PK column from the current snapshot.
4182 match build_create_m2m_op(spec, current) {
4183 Ok(op) => ops.push(op),
4184 Err(e) => return Err(e),
4185 }
4186 }
4187 for (key, spec) in &prev_m2m {
4188 if curr_m2m.contains_key(key) || renamed_prev_junctions.contains(key) {
4189 continue;
4190 }
4191 // M2M field removed (or its parent was dropped). The junction
4192 // table goes away.
4193 ops.push(Operation::DropM2MTable {
4194 junction_table: spec.junction_table.clone(),
4195 });
4196 }
4197
4198 // ---- Pass 5: views (features #73). ----
4199 //
4200 // Ordering is the whole feature. Drops go BEFORE the table ops and creates go
4201 // AFTER, because Postgres refuses to drop or retype a column that a live view
4202 // selects from — so a migration that alters `matchday.score` while a view reads
4203 // it fails unless the view is out of the way first, and is only whole again once
4204 // the view is back.
4205 let (view_drops, view_creates) = diff_views(view_prev, view_curr, &ops)?;
4206 let mut all = view_drops;
4207 all.extend(ops);
4208 all.extend(view_creates);
4209 Ok(all)
4210}
4211
4212/// The relations a view's SQL selects from, recovered by scanning its body for the
4213/// names of relations we know about.
4214///
4215/// The SELECT body is an opaque string — nobody is writing a SQL parser here. But
4216/// the engine already knows every table and view name in the snapshot, so it can ask
4217/// the cheaper question: *does this view's SQL mention `matchday`?* A whole-word
4218/// match (so `matchday` does not match `matchday_team`) recovers the dependency edge
4219/// that ordering needs.
4220///
4221/// The failure modes are asymmetric, which is why this is worth doing:
4222/// - A **false positive** (the name appears in a string literal or a comment) costs
4223/// one needless `DROP VIEW` + `CREATE VIEW`. A view stores nothing. Nobody notices.
4224/// - A **false negative** would order a view before the table it reads, and the
4225/// migration fails at apply time with a clear "relation does not exist".
4226///
4227/// Both are safe. Neither can corrupt data. That asymmetry is what makes a heuristic
4228/// acceptable here and unacceptable in the table passes next door.
4229fn view_dependencies(sql: &str, known: &[String], self_table: &str) -> Vec<String> {
4230 let haystack = sql.to_ascii_lowercase();
4231 let bytes = haystack.as_bytes();
4232 let is_word = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
4233
4234 let mut deps = Vec::new();
4235 for name in known {
4236 if name == self_table {
4237 continue;
4238 }
4239 let needle = name.to_ascii_lowercase();
4240 if needle.is_empty() {
4241 continue;
4242 }
4243 let mut from = 0usize;
4244 while let Some(hit) = haystack[from..].find(&needle) {
4245 let start = from + hit;
4246 let end = start + needle.len();
4247 let before_ok = start == 0 || !is_word(bytes[start - 1]);
4248 let after_ok = end == bytes.len() || !is_word(bytes[end]);
4249 if before_ok && after_ok {
4250 deps.push(name.clone());
4251 break;
4252 }
4253 from = end;
4254 }
4255 }
4256 deps
4257}
4258
4259/// Diff the view models of two snapshots into (drops, creates).
4260///
4261/// `table_ops` is what the table passes decided; a view that reads from a table
4262/// those ops touch is recreated even if its own SQL never changed.
4263fn diff_views(
4264 previous: &Snapshot,
4265 current: &Snapshot,
4266 table_ops: &[Operation],
4267) -> Result<(Vec<Operation>, Vec<Operation>), MigrateError> {
4268 use std::collections::{BTreeMap, HashSet};
4269
4270 let view_models = |snap: &Snapshot| -> Vec<ModelMeta> {
4271 snap.models
4272 .iter()
4273 .filter(|m| m.view.is_some())
4274 .cloned()
4275 .collect()
4276 };
4277 let prev_views = view_models(previous);
4278 let curr_views = view_models(current);
4279 if prev_views.is_empty() && curr_views.is_empty() {
4280 return Ok((Vec::new(), Vec::new()));
4281 }
4282
4283 let touched: HashSet<&str> = table_ops.iter().map(|o| o.table_name()).collect();
4284 let known: Vec<String> = current.models.iter().map(|m| m.table.clone()).collect();
4285
4286 let deps: BTreeMap<&str, Vec<String>> = curr_views
4287 .iter()
4288 .map(|m| {
4289 (
4290 m.name.as_str(),
4291 view_dependencies(m.view.as_deref().unwrap_or(""), &known, &m.table),
4292 )
4293 })
4294 .collect();
4295
4296 let prev_by_name: BTreeMap<&str, &ModelMeta> =
4297 prev_views.iter().map(|m| (m.name.as_str(), m)).collect();
4298
4299 // Which views are stale? Either their own definition moved, or something they
4300 // read did.
4301 let mut recreate: HashSet<&str> = HashSet::new();
4302 for m in &curr_views {
4303 let definition_changed = match prev_by_name.get(m.name.as_str()) {
4304 None => true, // brand new, or was a table until this change
4305 Some(prev) => {
4306 prev.view != m.view || prev.materialized != m.materialized || prev.table != m.table
4307 }
4308 };
4309 let reads_a_touched_table = deps[m.name.as_str()]
4310 .iter()
4311 .any(|d| touched.contains(d.as_str()));
4312 if definition_changed || reads_a_touched_table {
4313 recreate.insert(m.name.as_str());
4314 }
4315 }
4316
4317 // A view built on a view that is being recreated is itself stale. Iterate to a
4318 // fixpoint rather than assuming one level of nesting.
4319 loop {
4320 let mut grew = false;
4321 for m in &curr_views {
4322 if recreate.contains(m.name.as_str()) {
4323 continue;
4324 }
4325 let reads_a_stale_view = deps[m.name.as_str()].iter().any(|d| {
4326 curr_views
4327 .iter()
4328 .any(|v| &v.table == d && recreate.contains(v.name.as_str()))
4329 });
4330 if reads_a_stale_view {
4331 recreate.insert(m.name.as_str());
4332 grew = true;
4333 }
4334 }
4335 if !grew {
4336 break;
4337 }
4338 }
4339
4340 // ---- Creates, in dependency order (a view after the views it reads). ----
4341 let mut to_create: Vec<&ModelMeta> = curr_views
4342 .iter()
4343 .filter(|m| recreate.contains(m.name.as_str()))
4344 .collect();
4345 to_create.sort_by(|a, b| a.name.cmp(&b.name)); // deterministic before topo
4346
4347 let mut creates: Vec<Operation> = Vec::new();
4348 let mut placed: HashSet<&str> = HashSet::new();
4349 while placed.len() < to_create.len() {
4350 let before = placed.len();
4351 for m in &to_create {
4352 if placed.contains(m.table.as_str()) {
4353 continue;
4354 }
4355 let blocked = deps[m.name.as_str()].iter().any(|d| {
4356 to_create
4357 .iter()
4358 .any(|other| &other.table == d && !placed.contains(other.table.as_str()))
4359 });
4360 if blocked {
4361 continue;
4362 }
4363 placed.insert(m.table.as_str());
4364 creates.push(Operation::CreateView {
4365 name: m.table.clone(),
4366 sql: m.view.clone().unwrap_or_default(),
4367 materialized: m.materialized,
4368 });
4369 }
4370 if placed.len() == before {
4371 // A cycle. Two views cannot select from each other, so this means the
4372 // SQL scan found a self-referential edge we cannot honour. Name the
4373 // views rather than looping forever.
4374 let stuck: Vec<&str> = to_create
4375 .iter()
4376 .filter(|m| !placed.contains(m.table.as_str()))
4377 .map(|m| m.table.as_str())
4378 .collect();
4379 return Err(MigrateError::UnsupportedChange(format!(
4380 "views {stuck:?} form a dependency cycle — each one's SQL selects from \
4381 another in the set, so there is no order in which they can be created. \
4382 Break the cycle by inlining one of the definitions."
4383 )));
4384 }
4385 }
4386
4387 // ---- Drops: every view we are about to recreate, plus every view that is gone
4388 // (removed, or converted back into a table). Emitted in reverse create order so
4389 // a dependant view is dropped before the view it reads.
4390 let mut drops: Vec<Operation> = Vec::new();
4391 let curr_view_names: HashSet<&str> = curr_views.iter().map(|m| m.name.as_str()).collect();
4392 let mut to_drop: Vec<&ModelMeta> = prev_views
4393 .iter()
4394 .filter(|m| {
4395 !curr_view_names.contains(m.name.as_str()) || recreate.contains(m.name.as_str())
4396 })
4397 .collect();
4398 // Reverse-topological by the CREATE order we just computed; anything not in it
4399 // (a removed view) sorts first, since nothing we keep can depend on it.
4400 let create_pos = |table: &str| -> usize {
4401 creates
4402 .iter()
4403 .position(|op| matches!(op, Operation::CreateView { name, .. } if name == table))
4404 .map(|i| usize::MAX - i)
4405 .unwrap_or(0)
4406 };
4407 to_drop.sort_by_key(|m| (create_pos(&m.table), m.table.clone()));
4408 for m in to_drop {
4409 drops.push(Operation::DropView {
4410 name: m.table.clone(),
4411 materialized: m.materialized,
4412 });
4413 }
4414
4415 Ok((drops, creates))
4416}
4417
4418/// A flat-resolved M2M descriptor used by [`diff`] to compare snapshots.
4419/// Owns its strings so it can be keyed in a map without lifetime
4420/// gymnastics.
4421#[derive(Debug, Clone)]
4422struct M2MPair {
4423 parent_table: String,
4424 parent_pk: String,
4425 field_name: String,
4426 target_table: String,
4427 junction_table: String,
4428}
4429
4430/// Walk a snapshot and produce one [`M2MPair`] per declared M2M field.
4431/// Keyed on `(parent_table, field_name)` since that uniquely identifies
4432/// a junction table — two models can't share the same parent_table, and
4433/// one model can't declare two M2M fields with the same name.
4434fn collect_m2m_pairs(snap: &Snapshot) -> std::collections::BTreeMap<(String, String), M2MPair> {
4435 let mut out = std::collections::BTreeMap::new();
4436 for model in &snap.models {
4437 let parent_pk = model
4438 .fields
4439 .iter()
4440 .find(|c| c.primary_key)
4441 .map(|c| c.name.clone())
4442 .unwrap_or_else(|| "id".to_string());
4443 for rel in &model.m2m_relations {
4444 let key = (model.table.clone(), rel.field_name.clone());
4445 out.insert(
4446 key,
4447 M2MPair {
4448 parent_table: model.table.clone(),
4449 parent_pk: parent_pk.clone(),
4450 field_name: rel.field_name.clone(),
4451 target_table: rel.target_table.clone(),
4452 junction_table: format!("{}_{}", model.table, rel.field_name),
4453 },
4454 );
4455 }
4456 }
4457 out
4458}
4459
4460/// Lift an [`M2MPair`] into a fully-specified [`Operation::CreateM2MTable`].
4461/// The target table's PK column name is resolved from `current` (the
4462/// snapshot the diff is computing toward) — without it the DDL would
4463/// reference a column the child table doesn't have.
4464fn build_create_m2m_op(spec: &M2MPair, current: &Snapshot) -> Result<Operation, MigrateError> {
4465 // Resolve the target's PK from the current snapshot, FALLING BACK to the
4466 // global model registry. Migrations are generated per-plugin, so a
4467 // CROSS-PLUGIN M2M (parent owned by app A, target model owned by app B —
4468 // e.g. a tenant model with an M2M to a SHARED lookup table, or any app's
4469 // M2M to `umbral-auth`'s `User`) has its target in a *different* plugin's
4470 // snapshot, absent from `current`. The global registry sees every
4471 // registered model, so the junction DDL resolves the child PK no matter
4472 // which plugin owns the target. (Cross-plugin FK ordering already lets the
4473 // junction migration run after the target table's own migration.)
4474 let pk_col_and_ty = |m: &ModelMeta| -> (String, crate::orm::SqlType) {
4475 let pk = m.fields.iter().find(|c| c.primary_key);
4476 (
4477 pk.map(|c| c.name.clone())
4478 .unwrap_or_else(|| "id".to_string()),
4479 pk.map(|c| c.ty).unwrap_or(crate::orm::SqlType::BigInt),
4480 )
4481 };
4482 let (child_pk_col, child_ty) = current
4483 .models
4484 .iter()
4485 .find(|m| m.table == spec.target_table)
4486 .map(&pk_col_and_ty)
4487 .or_else(|| {
4488 // Non-panicking global lookup. `registered_models()` panics if the
4489 // registry isn't initialised (unit tests that call `diff` directly,
4490 // with no `App::build`); a `None` registry simply yields no global
4491 // fallback, so a TRULY-unregistered target is still rejected below.
4492 REGISTRY.get().and_then(|reg| {
4493 reg.iter()
4494 .find(|(_, m)| m.table == spec.target_table)
4495 .map(|(_, m)| pk_col_and_ty(m))
4496 })
4497 })
4498 .ok_or_else(|| {
4499 MigrateError::UnsupportedChange(format!(
4500 "M2M `{}.{}` targets table `{}` which is not registered \
4501 anywhere — register the target model via \
4502 `AppBuilder::model::<{}>()` or its owning plugin.",
4503 spec.parent_table, spec.field_name, spec.target_table, spec.target_table,
4504 ))
4505 })?;
4506 let parent_model = current
4507 .models
4508 .iter()
4509 .find(|m| m.table == spec.parent_table)
4510 .expect("parent model exists in snapshot — collect_m2m_pairs iterated it");
4511 let parent_ty = parent_model
4512 .fields
4513 .iter()
4514 .find(|c| c.primary_key)
4515 .map(|c| c.ty)
4516 .unwrap_or(crate::orm::SqlType::BigInt);
4517 Ok(Operation::CreateM2MTable {
4518 junction_table: spec.junction_table.clone(),
4519 parent_table: spec.parent_table.clone(),
4520 parent_col: spec.parent_pk.clone(),
4521 child_table: spec.target_table.clone(),
4522 child_col: child_pk_col,
4523 parent_ty,
4524 child_ty,
4525 })
4526}
4527
4528/// Compute a canonical, sorted column-shape fingerprint for rename
4529/// heuristic detection in `diff`. Two models whose column fingerprints
4530/// are identical are candidates for a rename (second-pass detection).
4531///
4532/// The fingerprint is a sorted `Vec` of `(name, ty, nullable, fk_target)`
4533/// tuples. Sorting by name ensures the fingerprint is independent of
4534/// declaration order.
4535fn column_shape(fields: &[Column]) -> Vec<(String, SqlType, bool, Option<String>)> {
4536 let mut shape: Vec<(String, SqlType, bool, Option<String>)> = fields
4537 .iter()
4538 .map(|c| (c.name.clone(), c.ty, c.nullable, c.fk_target.clone()))
4539 .collect();
4540 shape.sort_by(|a, b| a.0.cmp(&b.0));
4541 shape
4542}
4543
4544/// Type changes the migration engine can apply without user
4545/// intervention. The contract: every entry in this whitelist must be
4546/// data-preserving on both backends.
4547///
4548/// SQLite handles every entry trivially via the table-recreation
4549/// dance: its dynamic typing means whatever lives in a column today
4550/// reads back fine under a new column type affinity. Postgres needs
4551/// `ALTER COLUMN ... TYPE new_type USING column::new_type`, which the
4552/// renderer emits when this returns `true`.
4553///
4554/// What's *not* here is deliberate:
4555/// - `Text -> BigInt` / numeric parses can fail at runtime on non-
4556/// numeric rows. Force the user to write the migration so they own
4557/// the validation.
4558/// - Bigger int -> smaller int truncates silently.
4559/// - `Text -> Date` / `Text -> Uuid` are format-dependent.
4560/// - Anything -> JSON. Even if existing rows are JSON-shaped, that's
4561/// the user's invariant to assert.
4562fn is_safe_cast(from: SqlType, to: SqlType) -> bool {
4563 use SqlType::*;
4564 if from == to {
4565 return true;
4566 }
4567 match (from, to) {
4568 // Stringify: every scalar serialises to text losslessly. Read-
4569 // path code that wants the typed value parses it back; the
4570 // cast itself never fails.
4571 (
4572 SmallInt | Integer | BigInt | Real | Double | Boolean | Date | Time | Timestamptz
4573 | Uuid | Inet | Cidr | MacAddr | ForeignKey,
4574 Text,
4575 ) => true,
4576 // Integer widening — no data loss.
4577 (SmallInt, Integer | BigInt) => true,
4578 (Integer, BigInt) => true,
4579 // Float widening.
4580 (Real, Double) => true,
4581 // ForeignKey is stored as BigInt under the hood, so the two
4582 // directions are storage-identical. The Rust-side type is
4583 // different but the bytes on disk are not.
4584 (ForeignKey, BigInt) => true,
4585 (BigInt, ForeignKey) => true,
4586 _ => false,
4587 }
4588}
4589
4590/// Postgres type name for an `ALTER COLUMN ... TYPE <name> USING …`
4591/// clause. Matches what sea-query's `PostgresQueryBuilder` emits for
4592/// the same `SqlType` inside a `CREATE TABLE`, so the resulting
4593/// schema after the alter is identical to a freshly created table.
4594fn postgres_type_name(ty: SqlType) -> &'static str {
4595 use SqlType::*;
4596 match ty {
4597 SmallInt => "smallint",
4598 Integer => "integer",
4599 BigInt | ForeignKey => "bigint",
4600 Real => "real",
4601 Double => "double precision",
4602 Boolean => "boolean",
4603 Text => "text",
4604 Date => "date",
4605 Time => "time",
4606 // sea-query's Postgres builder emits `timestamp with time zone`
4607 // for the equivalent column type; both spellings are accepted
4608 // by Postgres, but mirroring the builder keeps the surface
4609 // consistent if a test ever round-trips DDL.
4610 Timestamptz => "timestamp with time zone",
4611 Uuid => "uuid",
4612 Json => "jsonb",
4613 Inet => "inet",
4614 Cidr => "cidr",
4615 MacAddr => "macaddr",
4616 // gaps2 #70: text-backed Postgres types. `bit varying` mirrors
4617 // what sea-query's builder emits for the CREATE TABLE path.
4618 Xml => "xml",
4619 Ltree => "ltree",
4620 Bit => "bit varying",
4621 FullText => "tsvector",
4622 Bytes => "bytea",
4623 // BUG-10: NUMERIC(19, 4) — same dimensions as the CREATE TABLE
4624 // build path. Used by the `ALTER COLUMN ... TYPE ...` render
4625 // when the safe-cast diff allows transitioning to/from
4626 // Decimal.
4627 Decimal => "numeric(19, 4)",
4628 // Arrays render as `<inner>[]` in Postgres. The migration
4629 // engine doesn't model nested element types deeply enough to
4630 // emit a precise inner type here at v1; fall back to `text[]`
4631 // and rely on the column-def renderer for the real shape when
4632 // recreating the column.
4633 Array(_) => "text[]",
4634 }
4635}
4636
4637/// Per-model column diff. Same-name columns whose type or nullable
4638/// flag changed return `UnsafeAlter` (no `AlterColumn` until M8 v1.1
4639/// covers the table-recreation dance for SQLite plus native ALTER for
4640/// Postgres). New-named columns emit `AddColumn`; missing-name columns
4641/// emit `DropColumn`. The ordering is: drops first, then adds, so a
4642/// rename-as-drop+add doesn't violate a uniqueness constraint mid-
4643/// migration on a single-row table.
4644fn diff_columns(
4645 model: &str,
4646 previous: &ModelMeta,
4647 current: &ModelMeta,
4648) -> Result<Vec<Operation>, MigrateError> {
4649 use std::collections::BTreeMap;
4650
4651 let prev_cols: BTreeMap<&str, &Column> = previous
4652 .fields
4653 .iter()
4654 .map(|c| (c.name.as_str(), c))
4655 .collect();
4656 let curr_cols: BTreeMap<&str, &Column> = current
4657 .fields
4658 .iter()
4659 .map(|c| (c.name.as_str(), c))
4660 .collect();
4661
4662 // Walk the intersection by name. Two questions per shared column:
4663 // - did the type change? If so, is the change in the safe-cast
4664 // whitelist (e.g. BigInt -> Text, SmallInt -> Integer)? Safe
4665 // casts emit AlterColumn; unsafe ones still UnsafeAlter so the
4666 // user is forced to write the data-preserving migration by
4667 // hand.
4668 // - did the nullable flag flip? AlterColumn either way.
4669 // Primary-key changes still UnsafeAlter (a PK rebuild is its own
4670 // dance and isn't shipped yet).
4671 let mut alter_columns: Vec<&str> = Vec::new();
4672 // Columns whose `#[umbral(help = "...")]` changed (gaps3 #43).
4673 let mut comment_columns: Vec<&str> = Vec::new();
4674 for (name, prev_col) in &prev_cols {
4675 if let Some(curr_col) = curr_cols.get(name) {
4676 if prev_col.primary_key != curr_col.primary_key {
4677 return Err(MigrateError::UnsafeAlter {
4678 model: model.to_string(),
4679 column: (*name).to_string(),
4680 reason: "primary-key flips need a manual data-preserving migration".to_string(),
4681 });
4682 }
4683 let type_changed = prev_col.ty != curr_col.ty;
4684 if type_changed && !is_safe_cast(prev_col.ty, curr_col.ty) {
4685 return Err(MigrateError::UnsafeAlter {
4686 model: model.to_string(),
4687 column: (*name).to_string(),
4688 reason: format!(
4689 "type change {prev_ty:?} -> {curr_ty:?} is not in the safe-cast whitelist — write a data-preserving migration by hand",
4690 prev_ty = prev_col.ty,
4691 curr_ty = curr_col.ty,
4692 ),
4693 });
4694 }
4695 if prev_col.nullable && !curr_col.nullable && curr_col.default.is_empty() {
4696 return Err(MigrateError::UnsafeAlter {
4697 model: model.to_string(),
4698 column: (*name).to_string(),
4699 reason: "nullable → NOT NULL requires a default/backfill before tightening; otherwise existing NULL rows abort the migration".to_string(),
4700 });
4701 }
4702 if !prev_col.unique && curr_col.unique {
4703 return Err(MigrateError::UnsafeAlter {
4704 model: model.to_string(),
4705 column: (*name).to_string(),
4706 reason: "adding UNIQUE to an existing column requires a duplicate pre-check/backfill migration; otherwise existing duplicate values abort the migration".to_string(),
4707 });
4708 }
4709 // gaps3 #43: `help` DOES have a DB effect now — it renders as a
4710 // Postgres `COMMENT ON COLUMN` — but it is not a column rewrite.
4711 // It gets its own cheap op rather than dragging the column through
4712 // an `AlterColumn` (a full table-recreation dance on SQLite).
4713 if prev_col.help != curr_col.help {
4714 comment_columns.push(*name);
4715 }
4716
4717 // Any schema-meaningful field change triggers AlterColumn.
4718 // UI-only flags (`noform`, `noedit`, `max_length`,
4719 // `is_string_repr`, `is_multichoice`) are intentionally
4720 // excluded — they affect admin / OpenAPI rendering but
4721 // not the database schema, so emitting an ALTER would do
4722 // no DB work. The single-column `index` flag is ALSO excluded
4723 // here: an index add/remove is not a column rewrite. Folding it
4724 // into `AlterColumn` created no index on Postgres (its native
4725 // ALTER handles TYPE/nullable/UNIQUE/DEFAULT/FK/CHECK but not
4726 // indexes) and forced a full table-recreation dance on SQLite.
4727 // `diff_indexes` now emits a proper `AddIndex`/`DropIndex` for it,
4728 // which is correct and cheap on both backends.
4729 if type_changed
4730 || prev_col.nullable != curr_col.nullable
4731 || prev_col.fk_target != curr_col.fk_target
4732 || prev_col.unique != curr_col.unique
4733 || prev_col.default != curr_col.default
4734 || prev_col.choices != curr_col.choices
4735 || prev_col.choice_labels != curr_col.choice_labels
4736 || prev_col.on_delete != curr_col.on_delete
4737 || prev_col.on_update != curr_col.on_update
4738 {
4739 alter_columns.push(*name);
4740 }
4741 }
4742 }
4743
4744 let mut ops: Vec<Operation> = Vec::new();
4745
4746 // AlterColumn ops first, in name order. One AlterColumn per
4747 // changed column; each carries the full new schema so the render
4748 // can rebuild without further context. Multiple nullable flips on
4749 // one table generate multiple AlterColumns; the apply loop runs
4750 // them sequentially (each is a table-recreation, so back-to-back
4751 // alters drop and recreate twice; the cost is acceptable while
4752 // M5.1 ships the simple case).
4753 //
4754 // audit_2 H21: the SQLite recreation dance rebuilds the table by
4755 // `INSERT INTO tmp (new_columns) SELECT new_columns FROM <old>`, so
4756 // every name in `new_columns` MUST exist in the old table at alter
4757 // time. Using `current.fields` here broke any diff that combined an
4758 // alter with an add or drop on the same table: a newly-ADDED column
4759 // is in `current` but not the old table (the SELECT hits "no such
4760 // column"), and a DROPPED column is absent from `current` so the
4761 // rebuild removed it early — the subsequent `DropColumn` op then
4762 // failed on the already-gone column. Instead, shape `new_columns`
4763 // like the PREVIOUS table (exactly the old table's columns), but
4764 // apply the CURRENT definition to every column that survives so the
4765 // type/nullable/default change still lands. A to-be-dropped column
4766 // keeps its old definition and rides through the rebuild; its
4767 // `DropColumn` op (emitted below, so it runs after) removes it. A
4768 // to-be-added column is intentionally absent; its `AddColumn` op
4769 // (also below) adds it after. Ordering alter → drop → add is what
4770 // the existing op emission already does.
4771 let new_columns: Vec<Column> = previous
4772 .fields
4773 .iter()
4774 .map(|prev_col| {
4775 curr_cols
4776 .get(prev_col.name.as_str())
4777 .map(|c| (*c).clone())
4778 .unwrap_or_else(|| prev_col.clone())
4779 })
4780 .collect();
4781 let prev_columns_snapshot: Vec<Column> = previous.fields.clone();
4782 for name in alter_columns {
4783 ops.push(Operation::AlterColumn {
4784 table: current.table.clone(),
4785 column: name.to_string(),
4786 new_columns: new_columns.clone(),
4787 prev_columns: Some(prev_columns_snapshot.clone()),
4788 // audit_2 core-migrate #10: carry the table-level constraints so the
4789 // SQLite recreation dance re-creates them instead of dropping them.
4790 unique_together: current.unique_together.clone(),
4791 indexes: current.indexes.clone(),
4792 });
4793 }
4794
4795 // gaps3 #43: comment ops, in name order. After any `AlterColumn` on the same
4796 // column, because SQLite's alter recreates the table and Postgres's rewrites
4797 // the column — a comment set first would be describing a column that is
4798 // about to be replaced.
4799 for name in comment_columns {
4800 ops.push(Operation::SetColumnComment {
4801 table: current.table.clone(),
4802 column: name.to_string(),
4803 comment: curr_cols
4804 .get(name)
4805 .map(|c| c.help.clone())
4806 .unwrap_or_default(),
4807 });
4808 }
4809
4810 // Collect the dropped + added column names. We need both lists in
4811 // memory so the rename heuristic can pair them.
4812 let mut dropped: Vec<&Column> = Vec::new();
4813 let mut added: Vec<&Column> = Vec::new();
4814 for (name, prev_col) in &prev_cols {
4815 if !curr_cols.contains_key(name) {
4816 dropped.push(prev_col);
4817 }
4818 }
4819 for col in ¤t.fields {
4820 if !prev_cols.contains_key(col.name.as_str()) {
4821 added.push(col);
4822 }
4823 }
4824
4825 // Gap 88 — column rename detection. When the same diff yields
4826 // exactly one drop and one add whose column shapes (sans name)
4827 // match bit-for-bit, the most likely interpretation is a rename
4828 // rather than a coincidental drop+add of two unrelated columns.
4829 // Emit RenameColumn instead and warn the user so they can
4830 // verify. Anything more ambiguous (multiple drops or adds, or
4831 // mismatched shapes) falls back to the drop+add path so the
4832 // rename is never inferred against the user's actual intent.
4833 //
4834 // The heuristic deliberately stays conservative: some tools ask
4835 // interactively in this case; we don't have
4836 // a prompt at v1, so the conservative auto-pair is the safest
4837 // shape. Users can always override by writing the
4838 // `RenameColumn` op into the migration file by hand.
4839 let mut paired_drop: Option<&str> = None;
4840 let mut paired_add: Option<&str> = None;
4841 if dropped.len() == 1 && added.len() == 1 {
4842 let d = dropped[0];
4843 let a = added[0];
4844 if column_shape_matches(d, a) {
4845 eprintln!(
4846 "umbral makemigrations: column rename detected on `{}`: \
4847 `{}` → `{}` — verify this is a rename and not a coincidental \
4848 shape match; edit the migration file if it's wrong",
4849 current.table, d.name, a.name,
4850 );
4851 ops.push(Operation::RenameColumn {
4852 table: current.table.clone(),
4853 from: d.name.clone(),
4854 to: a.name.clone(),
4855 column: Some(a.clone()),
4856 });
4857 paired_drop = Some(d.name.as_str());
4858 paired_add = Some(a.name.as_str());
4859 }
4860 }
4861
4862 // Drops first so a same-position add can reuse the column slot.
4863 for col in &dropped {
4864 if Some(col.name.as_str()) == paired_drop {
4865 continue;
4866 }
4867 ops.push(Operation::DropColumn {
4868 table: current.table.clone(),
4869 column: col.name.clone(),
4870 });
4871 }
4872
4873 // Then adds, in current declaration order so the schema retains
4874 // the user-written column order even after re-runs.
4875 for col in &added {
4876 if Some(col.name.as_str()) == paired_add {
4877 continue;
4878 }
4879 // Gap 97 — refuse to add a NOT NULL column without a default
4880 // (and without `auto_now_add` / `auto_now`, which fill the
4881 // column server-side at insert). SQLite + Postgres both
4882 // reject the ADD on a non-empty table; we surface the same
4883 // failure at diff time with actionable guidance so the user
4884 // doesn't ship a migration that bricks every deploy.
4885 if !col.nullable
4886 && col.default.is_empty()
4887 && !col.auto_now_add
4888 && !col.auto_now
4889 && !col.primary_key
4890 {
4891 return Err(MigrateError::UnsafeAlter {
4892 model: model.to_string(),
4893 column: col.name.clone(),
4894 reason: format!(
4895 "adding NOT NULL column `{}` without a default to existing \
4896 table `{}` would fail on every populated row. Pick one: \
4897 (a) make the field `Option<T>`, (b) add `#[umbral(default = \
4898 \"...\")]` so the migration backfills, or (c) add \
4899 `#[umbral(auto_now_add)]` for timestamp columns",
4900 col.name, current.table,
4901 ),
4902 });
4903 }
4904 ops.push(Operation::AddColumn {
4905 table: current.table.clone(),
4906 column: (*col).clone(),
4907 });
4908 }
4909
4910 Ok(ops)
4911}
4912
4913/// Gap 88 helper: compare two column snapshots for shape identity (every
4914/// schema-meaningful attribute except `name`). Used by the rename-
4915/// detection heuristic — bit-identical attrs are the signal that a
4916/// dropped column matches an added column and the diff is actually a
4917/// rename. Excludes UI-only flags (`noform`, `noedit`, `max_length`,
4918/// `is_string_repr`, `help`, `example`, `slug_from`) for the same
4919/// reason the AlterColumn diff excludes them: they have no DB effect.
4920fn column_shape_matches(a: &Column, b: &Column) -> bool {
4921 a.ty == b.ty
4922 && a.primary_key == b.primary_key
4923 && a.nullable == b.nullable
4924 && a.fk_target == b.fk_target
4925 && a.choices == b.choices
4926 && a.choice_labels == b.choice_labels
4927 && a.default == b.default
4928 && a.is_multichoice == b.is_multichoice
4929 && a.unique == b.unique
4930 && a.on_delete == b.on_delete
4931 && a.on_update == b.on_update
4932 && a.index == b.index
4933 && a.auto_now_add == b.auto_now_add
4934 && a.auto_now == b.auto_now
4935 && a.min == b.min
4936 && a.max == b.max
4937 && a.text_format == b.text_format
4938}
4939
4940/// Pick the suffix used in a migration filename. Single-op migrations
4941/// get a descriptive suffix; multi-op migrations fall back to `auto`.
4942fn suffix_for(ops: &[Operation]) -> String {
4943 match ops {
4944 [Operation::CreateTable { table, .. }] => format!("create_{table}"),
4945 [Operation::DropTable { table }] => format!("drop_{table}"),
4946 // features #73. The drop+create pair is what an EDIT to a view's SQL looks
4947 // like — by far the commonest view migration — so name it for what the user
4948 // did, not for the two statements it lowers to.
4949 [Operation::CreateView { name, .. }] => format!("create_view_{name}"),
4950 [Operation::DropView { name, .. }] => format!("drop_view_{name}"),
4951 [
4952 Operation::DropView { name: dropped, .. },
4953 Operation::CreateView { name: created, .. },
4954 ] if dropped == created => format!("update_view_{created}"),
4955 [Operation::AddColumn { table, column }] => format!("add_{}_{}", table, column.name),
4956 [Operation::DropColumn { table, column }] => format!("drop_{table}_{column}"),
4957 [Operation::AlterColumn { table, column, .. }] => format!("alter_{table}_{column}"),
4958 [Operation::RenameTable { from, to }] => format!("rename_{from}_to_{to}"),
4959 [
4960 Operation::RenameColumn {
4961 table, from, to, ..
4962 },
4963 ] => format!("rename_{table}_{from}_to_{to}"),
4964 [Operation::SetColumnComment { table, column, .. }] => {
4965 format!("comment_{table}_{column}")
4966 }
4967 [Operation::RunSql { .. }] => "run_sql".to_string(),
4968 [Operation::AddIndex { table, columns, .. }] => {
4969 format!("add_index_{table}_{}", columns.join("_"))
4970 }
4971 [Operation::DropIndex { table, columns, .. }] => {
4972 format!("drop_index_{table}_{}", columns.join("_"))
4973 }
4974 _ => "auto".to_string(),
4975 }
4976}
4977
4978/// Create the tracking table if it isn't there already. The DDL is
4979/// dialect-neutral (TEXT + composite PK is valid SQL on both shipped
4980/// backends), but the executor type isn't — sqlx::query is generic
4981/// over the database, so each backend gets its own thin wrapper.
4982///
4983/// Kept inline because this table is a chicken-and-egg case: every
4984/// other migration needs the tracking row written, so the table
4985/// itself can't be a migration.
4986async fn ensure_tracking_table_sqlite(pool: &sqlx::SqlitePool) -> Result<(), MigrateError> {
4987 sqlx::query(
4988 "CREATE TABLE IF NOT EXISTS umbral_migrations (
4989 plugin TEXT NOT NULL,
4990 name TEXT NOT NULL,
4991 applied_at TEXT NOT NULL,
4992 snapshot_hash TEXT NOT NULL,
4993 PRIMARY KEY (plugin, name)
4994 )",
4995 )
4996 .execute(pool)
4997 .await?;
4998 Ok(())
4999}
5000
5001/// Postgres counterpart to [`ensure_tracking_table_sqlite`].
5002async fn ensure_tracking_table_postgres(pool: &sqlx::PgPool) -> Result<(), MigrateError> {
5003 sqlx::query(
5004 "CREATE TABLE IF NOT EXISTS umbral_migrations (
5005 plugin TEXT NOT NULL,
5006 name TEXT NOT NULL,
5007 applied_at TEXT NOT NULL,
5008 snapshot_hash TEXT NOT NULL,
5009 PRIMARY KEY (plugin, name)
5010 )",
5011 )
5012 .execute(pool)
5013 .await?;
5014 Ok(())
5015}
5016
5017/// Pull the set of `(plugin, name)` tuples already recorded in the
5018/// tracking table (SQLite).
5019async fn applied_names_sqlite(
5020 pool: &sqlx::SqlitePool,
5021) -> Result<std::collections::HashSet<(String, String)>, MigrateError> {
5022 let rows: Vec<(String, String)> = sqlx::query_as("SELECT plugin, name FROM umbral_migrations")
5023 .fetch_all(pool)
5024 .await?;
5025 Ok(rows.into_iter().collect())
5026}
5027
5028/// Postgres counterpart to [`applied_names_sqlite`].
5029async fn applied_names_postgres(
5030 pool: &sqlx::PgPool,
5031) -> Result<std::collections::HashSet<(String, String)>, MigrateError> {
5032 let rows: Vec<(String, String)> = sqlx::query_as("SELECT plugin, name FROM umbral_migrations")
5033 .fetch_all(pool)
5034 .await?;
5035 Ok(rows.into_iter().collect())
5036}
5037
5038/// Render one operation to a list of SQL statements via sea-query.
5039///
5040/// Dispatches on the ambient backend's [`crate::backend::active`]
5041/// name; SQLite and Postgres are the two shipped dialects. Most ops
5042/// produce one statement; `AlterColumn` produces either the SQLite
5043/// table-recreation dance (`CREATE _umbral_new` + `INSERT ... SELECT`
5044/// + `DROP` + `RENAME`) or a single native `ALTER TABLE ... ALTER
5045/// COLUMN ... SET/DROP NOT NULL` on Postgres.
5046///
5047/// The apply loop in `run_in` executes each statement in order inside
5048/// the same transaction.
5049///
5050/// `AddColumn` ignores the `primary_key` flag: neither SQLite nor
5051/// Postgres lets a primary key be added to an existing table without
5052/// a table-recreation step, and the autodetector won't route a
5053/// pk-flagged column through `AddColumn` anyway. A hand-edited
5054/// migration that sets the flag is taken to mean "the user is taking
5055/// responsibility".
5056fn render_operation(op: &Operation) -> Vec<String> {
5057 render_operation_for(op, crate::backend::active().name())
5058}
5059
5060fn should_emit_btree_index(col: &Column) -> bool {
5061 !col.primary_key
5062 && !col.unique
5063 && (col.index || matches!(col.ty, SqlType::ForeignKey) || col.name == "deleted_at")
5064}
5065
5066/// Render one operation against an explicit backend name. The
5067/// dispatching seam — the public [`render_operation`] is just
5068/// `render_operation_for(op, backend::active().name())`. Splitting
5069/// the two lets tests render Postgres DDL without installing the
5070/// process-wide ambient backend (the `OnceLock` can only be set once,
5071/// so `App::build` and tests would otherwise collide).
5072///
5073/// Panics on unknown backend names; only `"sqlite"` and `"postgres"`
5074/// are shipped in Phase 2.
5075pub fn render_operation_for(op: &Operation, backend_name: &str) -> Vec<String> {
5076 match backend_name {
5077 "sqlite" => render_operation_sqlite(op),
5078 "postgres" => render_operation_postgres(op),
5079 other => panic!(
5080 "umbral::migrate: no DDL renderer for backend `{other}`; \
5081 Phase 2 ships sqlite and postgres only"
5082 ),
5083 }
5084}
5085
5086/// SQLite-dialect rendering for one operation.
5087fn render_operation_sqlite(op: &Operation) -> Vec<String> {
5088 use sea_query::{Alias, SqliteQueryBuilder, Table};
5089
5090 match op {
5091 // features #73. A view is DDL the ORM cannot express as a sea-query
5092 // statement (there is no ViewCreateStatement), and the SELECT body is an
5093 // opaque string the user wrote. This is exception (1) in CLAUDE.md's raw-SQL
5094 // rule: schema DDL, owned by the migration engine.
5095 //
5096 // A MATERIALIZED view never reaches here — the `model.materialized_view`
5097 // system check fails the boot on SQLite. Rendering it as a plain view would
5098 // be the exact "SQLite branch that quietly diverges" the design principles
5099 // forbid: the same query, silently recomputed on every read.
5100 Operation::CreateView {
5101 name,
5102 sql,
5103 materialized,
5104 } => {
5105 debug_assert!(
5106 !materialized,
5107 "materialized views are Postgres-only; the model.materialized_view \
5108 system check should have failed the boot before reaching the renderer"
5109 );
5110 vec![format!("CREATE VIEW \"{name}\" AS {sql}")]
5111 }
5112 Operation::DropView { name, .. } => vec![format!("DROP VIEW IF EXISTS \"{name}\"")],
5113 Operation::CreateTable {
5114 table,
5115 columns,
5116 unique_together,
5117 indexes,
5118 } => {
5119 // sea-query's TableCreateStatement renders columns inline.
5120 let mut stmt = Table::create();
5121 stmt.table(Alias::new(table));
5122 for col in columns {
5123 let mut def = build_column_def_sqlite(col);
5124 stmt.col(&mut def);
5125 }
5126 let mut stmts = vec![stmt.build(SqliteQueryBuilder)];
5127 // `unique_together` groups render as follow-up
5128 // `CREATE UNIQUE INDEX` statements rather than inline table
5129 // constraints. A named unique index enforces the SAME
5130 // constraint, but — unlike an inline `UNIQUE(...)` (which becomes
5131 // an un-droppable auto-index on SQLite / an implicit constraint
5132 // on Postgres) — it can be added and DROPPED by name after the
5133 // table exists. That is what makes `unique_together`
5134 // autodetection reversible: the exact statement an `AddIndex`
5135 // would emit, so a later `DropIndex` names the same index.
5136 for group in unique_together {
5137 stmts.push(add_index_stmt(table, group, true));
5138 }
5139 // Single-column explicit indexes plus ORM-required helper
5140 // indexes follow the CREATE TABLE. FK columns need indexes
5141 // for reverse/select-related queries, and soft-delete
5142 // models read through `deleted_at IS NULL` by default.
5143 for col in columns {
5144 if should_emit_btree_index(col) {
5145 stmts.push(create_index_stmt(table, &col.name));
5146 }
5147 }
5148 // BUG-7: multi-column indexes follow as plain CREATE INDEX.
5149 for group in indexes {
5150 stmts.push(create_multi_index_stmt(table, group));
5151 }
5152 stmts
5153 }
5154 // gaps3 #60 — `IF EXISTS`, matching `DROP INDEX` and `DROP VIEW`, which have always
5155 // said it. A migration's job is to make a statement about the DESIRED end state:
5156 // "this table should not exist". Erroring because the table is already gone is a
5157 // complaint that the world already agrees with you.
5158 //
5159 // It is not masking a bug. Forward application has to be idempotent, or a ledger
5160 // that drifts even slightly — a table dropped out of band, a rename replayed, an
5161 // app whose migration history was rewritten — becomes permanently unmigratable,
5162 // and the only escape the operator can see is deleting the database.
5163 Operation::DropTable { table } => vec![
5164 Table::drop()
5165 .table(Alias::new(table))
5166 .if_exists()
5167 .build(SqliteQueryBuilder),
5168 ],
5169 Operation::AddColumn { table, column } => {
5170 // SQLite-specific limitation: `ALTER TABLE ADD COLUMN`
5171 // requires a CONSTANT default. `CURRENT_TIMESTAMP` is
5172 // non-constant ("Cannot add a column with non-constant
5173 // default"). So when we're adding a NOT NULL auto_now /
5174 // auto_now_add column on top of an existing table, we
5175 // emit a two-statement sequence:
5176 // 1. ADD COLUMN as NULLABLE (no default needed).
5177 // 2. UPDATE every existing row to `datetime('now')`.
5178 // The column ends up NULL-permitting at the DB level on
5179 // SQLite — but the Rust type stays `DateTime<Utc>` (not
5180 // Option), and every INSERT through the ORM supplies a
5181 // value via the macro-emitted auto_now arm. The DB-side
5182 // NOT NULL guarantee is lost only for direct-SQL writers,
5183 // which umbral already discourages (see CLAUDE.md "Plugins
5184 // use the ORM"). Postgres has no such restriction —
5185 // `DEFAULT now()` works there in ALTER, no backfill
5186 // statement needed (see the Postgres render below).
5187 let needs_backfill = (column.auto_now || column.auto_now_add)
5188 && !column.nullable
5189 && matches!(
5190 column.ty,
5191 SqlType::Timestamptz | SqlType::Date | SqlType::Time
5192 );
5193
5194 let mut stmts = if needs_backfill {
5195 let mut nullable_col = column.clone();
5196 nullable_col.nullable = true;
5197 let mut stmt = Table::alter();
5198 stmt.table(Alias::new(table));
5199 let mut def = build_column_def_sqlite(&nullable_col);
5200 stmt.add_column(&mut def);
5201 let add_sql = stmt.build(SqliteQueryBuilder);
5202
5203 // Manual UPDATE — sea-query's update builder is
5204 // overkill for a single SET col = datetime('now').
5205 let table_quoted = table.replace('"', "\"\"");
5206 let col_quoted = column.name.replace('"', "\"\"");
5207 let backfill_sql = format!(
5208 "UPDATE \"{table_quoted}\" SET \"{col_quoted}\" = datetime('now') \
5209 WHERE \"{col_quoted}\" IS NULL"
5210 );
5211 vec![add_sql, backfill_sql]
5212 } else {
5213 let mut stmt = Table::alter();
5214 stmt.table(Alias::new(table));
5215 let mut def = build_column_def_sqlite(column);
5216 stmt.add_column(&mut def);
5217 vec![stmt.build(SqliteQueryBuilder)]
5218 };
5219 if should_emit_btree_index(column) {
5220 stmts.push(create_index_stmt(table, &column.name));
5221 }
5222 stmts
5223 }
5224 Operation::DropColumn { table, column } => vec![
5225 Table::alter()
5226 .table(Alias::new(table))
5227 .drop_column(Alias::new(column))
5228 .build(SqliteQueryBuilder),
5229 ],
5230 // gaps3 #43: SQLite has no `COMMENT` statement — no column comments, no
5231 // table comments, nothing. Rendering zero statements is the whole
5232 // implementation. This is not a silent divergence of the kind the raw-SQL
5233 // rule warns about: the columns, types, constraints and rows are
5234 // identical on both backends. Only the `psql \d+` annotation is absent,
5235 // because SQLite has nowhere to put it.
5236 Operation::SetColumnComment { .. } => Vec::new(),
5237 Operation::AlterColumn {
5238 table,
5239 column,
5240 new_columns,
5241 prev_columns,
5242 unique_together,
5243 indexes,
5244 } => {
5245 // gaps3 #24: choices are Rust-enforced on SQLite (build_column_def_sqlite
5246 // emits no CHECK), so an alter that ONLY changes this column's
5247 // choices/labels is invisible to the schema. Skip the otherwise
5248 // byte-identical table-recreation dance. Postgres still swaps its
5249 // CHECK constraint via the Postgres renderer, so the op is still
5250 // emitted and recorded — only the SQLite render short-circuits.
5251 if prev_columns
5252 .as_ref()
5253 .is_some_and(|prev| alter_is_choices_only(column, prev, new_columns))
5254 {
5255 Vec::new()
5256 } else {
5257 render_alter_column_dance_sqlite(table, new_columns, unique_together, indexes)
5258 }
5259 }
5260 Operation::CreateM2MTable {
5261 junction_table,
5262 parent_table,
5263 parent_col,
5264 child_table,
5265 child_col,
5266 parent_ty,
5267 child_ty,
5268 } => {
5269 // Junction table for many-to-many: two FK columns + composite PK.
5270 // Column types follow the referenced PKs — `BigInt` → `INTEGER`
5271 // (SQLite affinity), `Text` → `TEXT`, `Uuid` → `TEXT` on SQLite
5272 // / `UUID` on Postgres. Raw DDL is the simplest expression of
5273 // the composite-PK + per-side cascade FK shape; sea-query's
5274 // builder can't express it cleanly in one call.
5275 vec![format!(
5276 r#"CREATE TABLE "{jt}" (
5277 "parent_id" {pty} NOT NULL REFERENCES "{pt}"("{pc}") ON DELETE CASCADE,
5278 "child_id" {cty} NOT NULL REFERENCES "{ct}"("{cc}") ON DELETE CASCADE,
5279 PRIMARY KEY ("parent_id", "child_id")
5280)"#,
5281 jt = junction_table.replace('"', "\"\""),
5282 pt = parent_table.replace('"', "\"\""),
5283 pc = parent_col.replace('"', "\"\""),
5284 ct = child_table.replace('"', "\"\""),
5285 cc = child_col.replace('"', "\"\""),
5286 pty = m2m_pk_sql_type_sqlite(*parent_ty),
5287 cty = m2m_pk_sql_type_sqlite(*child_ty),
5288 )]
5289 }
5290 // gaps3 #60 — idempotent, same reasoning as `DropTable`.
5291 Operation::DropM2MTable { junction_table } => vec![
5292 Table::drop()
5293 .table(Alias::new(junction_table))
5294 .if_exists()
5295 .build(SqliteQueryBuilder),
5296 ],
5297 Operation::RenameTable { from, to } => {
5298 use sea_query::{Alias, SqliteQueryBuilder, Table};
5299 vec![
5300 Table::rename()
5301 .table(Alias::new(from.as_str()), Alias::new(to.as_str()))
5302 .build(SqliteQueryBuilder),
5303 ]
5304 }
5305 Operation::RenameColumn {
5306 table, from, to, ..
5307 } => {
5308 // SQLite 3.25+ supports `ALTER TABLE ... RENAME COLUMN`
5309 // natively. Quote both sides to allow names that need
5310 // escaping; sea-query's column-rename builder isn't
5311 // exposed cleanly so we render the DDL string directly.
5312 let t = table.replace('"', "\"\"");
5313 let f = from.replace('"', "\"\"");
5314 let tn = to.replace('"', "\"\"");
5315 vec![format!(
5316 "ALTER TABLE \"{t}\" RENAME COLUMN \"{f}\" TO \"{tn}\""
5317 )]
5318 }
5319 // A data migration renders to its raw forward SQL verbatim —
5320 // the author owns portability across backends.
5321 Operation::RunSql { sql, .. } => vec![sql.clone()],
5322 // Composite index / UNIQUE constraint add + drop. The
5323 // `CREATE [UNIQUE] INDEX IF NOT EXISTS` / `DROP INDEX IF EXISTS`
5324 // forms are identical on SQLite and Postgres, so both render arms
5325 // share the same helpers.
5326 Operation::AddIndex {
5327 table,
5328 columns,
5329 unique,
5330 } => vec![add_index_stmt(table, columns, *unique)],
5331 Operation::DropIndex {
5332 table,
5333 columns,
5334 unique,
5335 } => vec![drop_index_stmt(&index_name(table, columns, *unique))],
5336 }
5337}
5338
5339/// Postgres-dialect rendering for one operation.
5340///
5341/// Postgres has native `ALTER COLUMN` so `AlterColumn` doesn't need
5342/// the SQLite table-recreation dance; it lowers to a single statement.
5343/// Integer primary keys use sea-query's `auto_increment()` flag, which
5344/// the Postgres query builder lowers to `BIGSERIAL` / `SERIAL` rather
5345/// than SQLite's `INTEGER PRIMARY KEY AUTOINCREMENT` quirk.
5346fn render_operation_postgres(op: &Operation) -> Vec<String> {
5347 use sea_query::{Alias, PostgresQueryBuilder, Table};
5348
5349 match op {
5350 // features #73 — see the SQLite renderer for why this is raw DDL.
5351 Operation::CreateView {
5352 name,
5353 sql,
5354 materialized,
5355 } => {
5356 let kind = if *materialized {
5357 "MATERIALIZED VIEW"
5358 } else {
5359 "VIEW"
5360 };
5361 vec![format!("CREATE {kind} \"{name}\" AS {sql}")]
5362 }
5363 Operation::DropView { name, materialized } => {
5364 let kind = if *materialized {
5365 "MATERIALIZED VIEW"
5366 } else {
5367 "VIEW"
5368 };
5369 // No CASCADE. A view another view depends on should fail loudly rather
5370 // than silently take its dependants down with it — the engine emits the
5371 // dependants' drops itself, in order, when it knows about them.
5372 vec![format!("DROP {kind} IF EXISTS \"{name}\"")]
5373 }
5374 Operation::CreateTable {
5375 table,
5376 columns,
5377 unique_together,
5378 indexes,
5379 } => {
5380 let mut stmt = Table::create();
5381 stmt.table(Alias::new(table));
5382 for col in columns {
5383 let mut def = build_column_def_postgres(col);
5384 stmt.col(&mut def);
5385 }
5386 let mut stmts = vec![stmt.build(PostgresQueryBuilder)];
5387 // gaps3 #35: a `#[umbral(case_insensitive)]` text column renders as
5388 // `citext`, which needs its extension. Emit the (idempotent) create
5389 // BEFORE the table so the citext type resolves. Requires a role with
5390 // CREATE privilege on the database — the operator pre-creates it once
5391 // if the runtime role is restricted.
5392 if columns
5393 .iter()
5394 .any(|c| c.case_insensitive && matches!(c.ty, crate::orm::SqlType::Text))
5395 {
5396 stmts.insert(0, "CREATE EXTENSION IF NOT EXISTS citext".to_string());
5397 }
5398 // `unique_together` as follow-up `CREATE UNIQUE INDEX` (not an
5399 // inline constraint) so it is droppable by name — see the SQLite
5400 // render arm for the full rationale.
5401 for group in unique_together {
5402 stmts.push(add_index_stmt(table, group, true));
5403 }
5404 for col in columns {
5405 if matches!(col.ty, crate::orm::SqlType::FullText) {
5406 // tsvector columns get an auto-GIN index (#33) — they're
5407 // useless for search without one, so the engine never
5408 // makes the caller hand-write it.
5409 stmts.push(create_gin_index_stmt(table, &col.name));
5410 } else if should_emit_btree_index(col) {
5411 stmts.push(create_index_stmt(table, &col.name));
5412 }
5413 }
5414 for group in indexes {
5415 stmts.push(create_multi_index_stmt(table, group));
5416 }
5417 // gaps3 #43: `#[umbral(help = "...")]` as a column comment. Postgres
5418 // has no inline column-comment syntax, so these follow the CREATE —
5419 // commenting a column that doesn't exist yet is an error.
5420 for col in columns {
5421 if !col.help.is_empty() {
5422 stmts.push(comment_on_column_stmt(table, &col.name, &col.help));
5423 }
5424 }
5425 stmts
5426 }
5427 // gaps3 #60 — see the SQLite renderer.
5428 Operation::DropTable { table } => vec![
5429 Table::drop()
5430 .table(Alias::new(table))
5431 .if_exists()
5432 .build(PostgresQueryBuilder),
5433 ],
5434 Operation::AddColumn { table, column } => {
5435 let mut stmt = Table::alter();
5436 stmt.table(Alias::new(table));
5437 let mut def = build_column_def_postgres(column);
5438 stmt.add_column(&mut def);
5439 let mut stmts = vec![stmt.build(PostgresQueryBuilder)];
5440 if matches!(column.ty, crate::orm::SqlType::FullText) {
5441 // Auto-GIN for a tsvector column added later (#33).
5442 stmts.push(create_gin_index_stmt(table, &column.name));
5443 } else if should_emit_btree_index(column) {
5444 stmts.push(create_index_stmt(table, &column.name));
5445 }
5446 if !column.help.is_empty() {
5447 stmts.push(comment_on_column_stmt(table, &column.name, &column.help));
5448 }
5449 stmts
5450 }
5451 Operation::SetColumnComment {
5452 table,
5453 column,
5454 comment,
5455 } => vec![comment_on_column_stmt(table, column, comment)],
5456 Operation::DropColumn { table, column } => vec![
5457 Table::alter()
5458 .table(Alias::new(table))
5459 .drop_column(Alias::new(column))
5460 .build(PostgresQueryBuilder),
5461 ],
5462 Operation::AlterColumn {
5463 table,
5464 column,
5465 new_columns,
5466 prev_columns,
5467 // Postgres alters in place — indexes/UNIQUE survive the ALTER, so
5468 // it doesn't re-create them (only the SQLite recreation dance does).
5469 unique_together: _,
5470 indexes: _,
5471 } => render_alter_column_postgres(table, column, new_columns, prev_columns.as_deref()),
5472 Operation::CreateM2MTable {
5473 junction_table,
5474 parent_table,
5475 parent_col,
5476 child_table,
5477 child_col,
5478 parent_ty,
5479 child_ty,
5480 } => {
5481 vec![format!(
5482 r#"CREATE TABLE "{jt}" (
5483 "parent_id" {pty} NOT NULL REFERENCES "{pt}"("{pc}") ON DELETE CASCADE,
5484 "child_id" {cty} NOT NULL REFERENCES "{ct}"("{cc}") ON DELETE CASCADE,
5485 PRIMARY KEY ("parent_id", "child_id")
5486)"#,
5487 jt = junction_table.replace('"', "\"\""),
5488 pt = parent_table.replace('"', "\"\""),
5489 pc = parent_col.replace('"', "\"\""),
5490 ct = child_table.replace('"', "\"\""),
5491 cc = child_col.replace('"', "\"\""),
5492 pty = m2m_pk_sql_type_postgres(*parent_ty),
5493 cty = m2m_pk_sql_type_postgres(*child_ty),
5494 )]
5495 }
5496 // gaps3 #60 — idempotent, same reasoning as `DropTable`.
5497 Operation::DropM2MTable { junction_table } => vec![
5498 Table::drop()
5499 .table(Alias::new(junction_table))
5500 .if_exists()
5501 .build(PostgresQueryBuilder),
5502 ],
5503 Operation::RenameTable { from, to } => {
5504 // Postgres: ALTER TABLE "<from>" RENAME TO "<to>"
5505 // sea-query's Table::rename() emits the right form.
5506 use sea_query::{Alias, PostgresQueryBuilder, Table};
5507 vec![
5508 Table::rename()
5509 .table(Alias::new(from.as_str()), Alias::new(to.as_str()))
5510 .build(PostgresQueryBuilder),
5511 ]
5512 }
5513 Operation::RenameColumn {
5514 table, from, to, ..
5515 } => {
5516 let t = table.replace('"', "\"\"");
5517 let f = from.replace('"', "\"\"");
5518 let tn = to.replace('"', "\"\"");
5519 vec![format!(
5520 "ALTER TABLE \"{t}\" RENAME COLUMN \"{f}\" TO \"{tn}\""
5521 )]
5522 }
5523 // A data migration renders to its raw forward SQL verbatim —
5524 // the author owns portability across backends.
5525 Operation::RunSql { sql, .. } => vec![sql.clone()],
5526 // Same `CREATE [UNIQUE] INDEX IF NOT EXISTS` / `DROP INDEX IF EXISTS`
5527 // as SQLite — Postgres accepts the identical form.
5528 Operation::AddIndex {
5529 table,
5530 columns,
5531 unique,
5532 } => vec![add_index_stmt(table, columns, *unique)],
5533 Operation::DropIndex {
5534 table,
5535 columns,
5536 unique,
5537 } => vec![drop_index_stmt(&index_name(table, columns, *unique))],
5538 }
5539}
5540
5541/// The SQLite table-recreation dance for `AlterColumn`. SQLite has no
5542/// in-place `ALTER COLUMN`, so the only safe way to flip a column's
5543/// nullable flag is to rebuild the table:
5544///
5545/// 1. `CREATE TABLE _umbral_new_<table>` with the new schema.
5546/// 2. `INSERT ... SELECT` to copy every row from the old table.
5547/// 3. `DROP TABLE <table>`.
5548/// 4. `ALTER TABLE _umbral_new_<table> RENAME TO <table>`.
5549///
5550/// Wrapped in a transaction by the caller, which — when the migration contains
5551/// an `AlterColumn` — brackets that transaction with `PRAGMA foreign_keys=OFF`
5552/// … `PRAGMA foreign_key_check` … `PRAGMA foreign_keys=ON` (SQLite's official
5553/// recipe), so step 3's `DROP TABLE` on a table with **inbound** FKs doesn't
5554/// trip `FOREIGN KEY constraint failed` (gaps3 #13). Indexes, triggers, and FK
5555/// targets aren't preserved at M5.1 because umbral-core's schema model
5556/// doesn't yet carry them; once it does, this routine picks them up
5557/// by rebuilding them at step 1.
5558///
5559/// Nullable `TRUE -> FALSE` fails at step 2 if any row holds NULL,
5560/// which is the correct data-integrity behaviour. Nullable
5561/// `FALSE -> TRUE` always succeeds.
5562/// gaps3 #24: true when an `AlterColumn` on `column` changes ONLY the closed-set
5563/// `choices` / `choice_labels` — every schema-relevant field (type, nullable,
5564/// unique, default, FK, on_delete/update, pk, length, multichoice) is identical.
5565/// On SQLite such a delta is a no-op: choices are enforced in Rust there, not by
5566/// a DB CHECK, so rebuilding the table would produce a byte-identical schema.
5567fn alter_is_choices_only(column: &str, prev: &[Column], new: &[Column]) -> bool {
5568 let (Some(p), Some(n)) = (
5569 prev.iter().find(|c| c.name == column),
5570 new.iter().find(|c| c.name == column),
5571 ) else {
5572 return false;
5573 };
5574 let choices_changed = p.choices != n.choices || p.choice_labels != n.choice_labels;
5575 let schema_same = p.ty == n.ty
5576 && p.nullable == n.nullable
5577 && p.unique == n.unique
5578 && p.default == n.default
5579 && p.fk_target == n.fk_target
5580 && p.on_delete == n.on_delete
5581 && p.on_update == n.on_update
5582 && p.primary_key == n.primary_key
5583 && p.max_length == n.max_length
5584 && p.is_multichoice == n.is_multichoice;
5585 choices_changed && schema_same
5586}
5587
5588fn render_alter_column_dance_sqlite(
5589 table: &str,
5590 new_columns: &[Column],
5591 unique_together: &[Vec<String>],
5592 indexes: &[Vec<String>],
5593) -> Vec<String> {
5594 use sea_query::{Alias, SqliteQueryBuilder, Table};
5595
5596 let tmp = format!("_umbral_new_{table}");
5597
5598 // Step 1 — CREATE TABLE _umbral_new_<table>.
5599 let mut create = Table::create();
5600 create.table(Alias::new(&tmp));
5601 for col in new_columns {
5602 let mut def = build_column_def_sqlite(col);
5603 create.col(&mut def);
5604 }
5605
5606 // Step 2 — INSERT ... SELECT. The INSERT target list is the plain column
5607 // names; each is double-quoted so SQLite identifier rules don't bite on
5608 // reserved words. The SELECT side backfills any NOT-NULL-with-default column
5609 // via `COALESCE(col, <default>)` (audit_2 core-migrate #5) — a
5610 // nullable→NOT NULL tightening whose existing rows hold NULL would otherwise
5611 // copy NULL into the new NOT NULL column and abort the rebuild. COALESCE is
5612 // a harmless no-op for a column that never held NULLs.
5613 let insert_cols = new_columns
5614 .iter()
5615 .map(|c| format!("\"{}\"", c.name.replace('"', "\"\"")))
5616 .collect::<Vec<_>>()
5617 .join(", ");
5618 let select_exprs = new_columns
5619 .iter()
5620 .map(|c| {
5621 let name = format!("\"{}\"", c.name.replace('"', "\"\""));
5622 if !c.nullable && !c.default.is_empty() {
5623 format!("COALESCE({name}, {})", default_sql_literal(c, false))
5624 } else {
5625 name
5626 }
5627 })
5628 .collect::<Vec<_>>()
5629 .join(", ");
5630 let insert_sql =
5631 format!("INSERT INTO \"{tmp}\" ({insert_cols}) SELECT {select_exprs} FROM \"{table}\"");
5632
5633 // Step 3 — DROP TABLE <table>.
5634 let drop_sql = Table::drop()
5635 .table(Alias::new(table))
5636 .build(SqliteQueryBuilder);
5637
5638 // Step 4 — ALTER TABLE _umbral_new_<table> RENAME TO <table>.
5639 let rename_sql = Table::rename()
5640 .table(Alias::new(&tmp), Alias::new(table))
5641 .build(SqliteQueryBuilder);
5642
5643 let mut stmts = vec![
5644 create.build(SqliteQueryBuilder),
5645 insert_sql,
5646 drop_sql,
5647 rename_sql,
5648 ];
5649 // Step 5 — audit_2 core-migrate #10: re-create the secondary indexes and
5650 // composite UNIQUE constraints the dropped table carried, or the rebuild
5651 // silently drops them (duplicates become insertable — integrity loss).
5652 // Single-column / FK / soft-delete indexes are derived from the columns
5653 // (same rule as CreateTable); `unique_together` re-emits as a named
5654 // `CREATE UNIQUE INDEX` and composite `indexes` as plain `CREATE INDEX`.
5655 // All are `IF NOT EXISTS`, so the step is idempotent.
5656 for col in new_columns {
5657 if should_emit_btree_index(col) {
5658 stmts.push(create_index_stmt(table, &col.name));
5659 }
5660 }
5661 for group in unique_together {
5662 stmts.push(add_index_stmt(table, group, true));
5663 }
5664 for group in indexes {
5665 stmts.push(create_multi_index_stmt(table, group));
5666 }
5667 stmts
5668}
5669
5670/// Native Postgres `AlterColumn`. Postgres supports
5671/// `ALTER TABLE x ALTER COLUMN y SET NOT NULL` and
5672/// `ALTER TABLE x ALTER COLUMN y DROP NOT NULL` in place, so the
5673/// SQLite table-recreation dance isn't needed. Lowers to a single
5674/// statement.
5675///
5676/// `SET NOT NULL` fails at the server if any row holds NULL on `y`,
5677/// matching SQLite's INSERT-time failure on the dance — the
5678/// data-integrity contract is identical between backends.
5679///
5680/// `column` is the field name that triggered the flip; `new_columns`
5681/// is the post-change schema (carried for parity with the SQLite
5682/// dance, though Postgres only needs the one column).
5683fn render_alter_column_postgres(
5684 table: &str,
5685 column: &str,
5686 new_columns: &[Column],
5687 prev_columns: Option<&[Column]>,
5688) -> Vec<String> {
5689 let new = new_columns.iter().find(|c| c.name == column).expect(
5690 "umbral::migrate: AlterColumn op references a column missing from new_columns; \
5691 this is a bug in `diff_columns`",
5692 );
5693 let prev = prev_columns.and_then(|cols| cols.iter().find(|c| c.name == column));
5694
5695 let q_table = quote_pg_ident(table);
5696 let q_column = quote_pg_ident(column);
5697
5698 let mut stmts: Vec<String> = Vec::new();
5699
5700 // TYPE change: only when we have a previous snapshot AND it differs
5701 // AND the change is in the safe-cast whitelist (diff_columns has
5702 // already gated unsafe ones). Emitted before nullable so a NOT
5703 // NULL flip against the just-cast column reads the new type.
5704 if let Some(prev_col) = prev {
5705 if prev_col.ty != new.ty && is_safe_cast(prev_col.ty, new.ty) {
5706 let new_ty_sql = postgres_type_name(new.ty);
5707 stmts.push(format!(
5708 "ALTER TABLE {q_table} ALTER COLUMN {q_column} TYPE {new_ty_sql} USING {q_column}::{new_ty_sql}"
5709 ));
5710 }
5711 }
5712
5713 // NULL-flag change: skipped when prev is None (legacy migrations
5714 // with no snapshot — preserve the old "emit unconditionally" path
5715 // because it's idempotent on Postgres). With a snapshot, only emit
5716 // when the flag actually flipped.
5717 let nullable_changed = match prev {
5718 Some(prev_col) => prev_col.nullable != new.nullable,
5719 None => true,
5720 };
5721 if nullable_changed {
5722 // audit_2 core-migrate #5: backfill existing NULLs before tightening.
5723 // A nullable→NOT NULL flip whose column carries a default would abort on
5724 // any pre-existing NULL row (bare `SET NOT NULL` doesn't backfill, and
5725 // `SET DEFAULT` only affects future inserts). Emit the backfill UPDATE
5726 // first so the subsequent `SET NOT NULL` succeeds.
5727 if !new.nullable && !new.default.is_empty() {
5728 let lit = default_sql_literal(new, true);
5729 stmts.push(format!(
5730 "UPDATE {q_table} SET {q_column} = {lit} WHERE {q_column} IS NULL"
5731 ));
5732 }
5733 let clause = if new.nullable {
5734 "DROP NOT NULL"
5735 } else {
5736 "SET NOT NULL"
5737 };
5738 stmts.push(format!(
5739 "ALTER TABLE {q_table} ALTER COLUMN {q_column} {clause}"
5740 ));
5741 }
5742
5743 // From here down — all the gap #65 follow-up changes. Each branch
5744 // checks if `prev` exists (legacy migrations with no snapshot
5745 // skip these, matching the historical behaviour) and emits the
5746 // matching ALTER on real flips.
5747 if let Some(prev_col) = prev {
5748 // UNIQUE flag flip. Postgres autogen for column-level UNIQUE
5749 // at CREATE TABLE is `<table>_<col>_key`; we use the same
5750 // name when ADDing so a subsequent DROP finds it.
5751 if prev_col.unique != new.unique {
5752 let cname = format!("{table}_{column}_key");
5753 if new.unique {
5754 stmts.push(format!(
5755 "ALTER TABLE {q_table} ADD CONSTRAINT \"{cname}\" UNIQUE ({q_column})"
5756 ));
5757 } else {
5758 stmts.push(format!(
5759 "ALTER TABLE {q_table} DROP CONSTRAINT IF EXISTS \"{cname}\""
5760 ));
5761 }
5762 }
5763
5764 // DEFAULT change. Empty string in either snapshot means "no
5765 // default"; the canonical SET / DROP pair fully expresses
5766 // the transition.
5767 if prev_col.default != new.default {
5768 if new.default.is_empty() {
5769 stmts.push(format!(
5770 "ALTER TABLE {q_table} ALTER COLUMN {q_column} DROP DEFAULT"
5771 ));
5772 } else {
5773 let escaped = new.default.replace('\'', "''");
5774 stmts.push(format!(
5775 "ALTER TABLE {q_table} ALTER COLUMN {q_column} SET DEFAULT '{escaped}'"
5776 ));
5777 }
5778 }
5779
5780 // FK target / on_delete / on_update — these are all carried
5781 // on the same constraint, so any one of them flipping
5782 // requires a DROP + readd of the whole FK. Autogen name
5783 // convention `<table>_<col>_fkey` matches Postgres at CREATE
5784 // TABLE time. Only emitted when the new column is still a
5785 // FK; if the column stopped being a FK (ty changed away
5786 // from ForeignKey), the type-change branch above handles
5787 // it indirectly via the column type rewrite.
5788 let fk_changed = prev_col.fk_target != new.fk_target
5789 || prev_col.on_delete != new.on_delete
5790 || prev_col.on_update != new.on_update;
5791 if fk_changed && matches!(new.ty, SqlType::ForeignKey) {
5792 let cname = format!("{table}_{column}_fkey");
5793 stmts.push(format!(
5794 "ALTER TABLE {q_table} DROP CONSTRAINT IF EXISTS \"{cname}\""
5795 ));
5796 // gaps2 #22: only re-add the physical constraint when the FK
5797 // still wants one. A `db_constraint = false` FK keeps the
5798 // DROP (so flipping the flag tears down any prior constraint)
5799 // but emits no ADD CONSTRAINT.
5800 if let Some(target) = &new.fk_target
5801 && new.db_constraint
5802 {
5803 let q_target = quote_pg_ident(target);
5804 // Resolve the referenced PK column from the target model's
5805 // registered meta instead of hardcoding `"id"`. String/Uuid
5806 // PKs (e.g. `Permission.codename`) are first-class post-lift;
5807 // the CreateTable path already resolves via `fk_target_pk`
5808 // (build_column_def_postgres), so the re-add must match or it
5809 // aborts the migration ("column id does not exist") / attaches
5810 // the constraint to the wrong column.
5811 let (pk_col, _pk_ty) = fk_target_pk(&target.replace('"', "\"\""));
5812 let q_pk = quote_pg_ident(&pk_col);
5813 let on_delete_clause = new
5814 .on_delete
5815 .sql_keyword()
5816 .map(|k| format!(" ON DELETE {k}"))
5817 .unwrap_or_default();
5818 let on_update_clause = new
5819 .on_update
5820 .sql_keyword()
5821 .map(|k| format!(" ON UPDATE {k}"))
5822 .unwrap_or_default();
5823 stmts.push(format!(
5824 "ALTER TABLE {q_table} ADD CONSTRAINT \"{cname}\" \
5825 FOREIGN KEY ({q_column}) REFERENCES {q_target}({q_pk})\
5826 {on_delete_clause}{on_update_clause}"
5827 ));
5828 }
5829 }
5830
5831 // CHECK constraint (single-valued choices) change. MultiChoice
5832 // uses CSV storage which can't be expressed as a column-level
5833 // IN constraint; the runtime sqlx Decode path is the guard.
5834 if prev_col.choices != new.choices && !new.is_multichoice {
5835 let cname = format!("{table}_{column}_check");
5836 stmts.push(format!(
5837 "ALTER TABLE {q_table} DROP CONSTRAINT IF EXISTS \"{cname}\""
5838 ));
5839 if !new.choices.is_empty() {
5840 let values_sql = new
5841 .choices
5842 .iter()
5843 .map(|v| format!("'{}'", v.replace('\'', "''")))
5844 .collect::<Vec<_>>()
5845 .join(", ");
5846 stmts.push(format!(
5847 "ALTER TABLE {q_table} ADD CONSTRAINT \"{cname}\" \
5848 CHECK ({q_column} IN ({values_sql}))"
5849 ));
5850 }
5851 }
5852 }
5853
5854 // Defensive: if we somehow produced no statements (shouldn't
5855 // happen — diff_columns gates on at least one schema-meaningful
5856 // flag changing), fall back to a single redundant SET NULL flip
5857 // to match the legacy contract. Tests cover both branches; this
5858 // is belt-and-braces.
5859 if stmts.is_empty() {
5860 let clause = if new.nullable {
5861 "DROP NOT NULL"
5862 } else {
5863 "SET NOT NULL"
5864 };
5865 stmts.push(format!(
5866 "ALTER TABLE {q_table} ALTER COLUMN {q_column} {clause}"
5867 ));
5868 }
5869
5870 stmts
5871}
5872
5873/// Quote a SQL identifier the Postgres way: wrap in double quotes,
5874/// escape inner double quotes by doubling them. Matches sea-query's
5875/// `PostgresQueryBuilder` output for identifiers so the rendered
5876/// statements look uniform.
5877fn quote_pg_ident(ident: &str) -> String {
5878 format!("\"{}\"", ident.replace('"', "\"\""))
5879}
5880
5881/// Build a SQLite `ColumnDef`. SQLite has one important quirk: its
5882/// ROWID-alias mechanic (which gives a primary-key column auto-
5883/// increment behaviour out of the box) only fires when the column's
5884/// type is the exact text `INTEGER` — case-insensitive but no other
5885/// variant. `BIGINT PRIMARY KEY`, even on a column the M3 derive
5886/// declared as `i64`, does NOT auto-increment, so an `INSERT INTO t
5887/// (other_col) VALUES (...)` without an explicit PK value fails the
5888/// NOT NULL constraint. Every umbral user with an `id: i64` model
5889/// would hit this without the override.
5890///
5891/// The fix: when a column is a primary key with an integer SqlType
5892/// (Integer or BigInt), force the rendered type to `Integer` and
5893/// attach `auto_increment()` so the generated DDL reads `"id" integer
5894/// NOT NULL PRIMARY KEY AUTOINCREMENT`. SQLite stores both `i32` and
5895/// `i64` as INTEGER affinity anyway, so the override is a no-op
5896/// semantically — the rows that round-trip through `sqlx::FromRow`
5897/// deserialize back into `i64` cleanly.
5898///
5899/// For `SqlType::Uuid` PKs: SQLite stores UUIDs as TEXT. No
5900/// `DEFAULT gen_random_uuid()` is emitted; the application must supply
5901/// the UUID at create time (or pass `Uuid::nil()` to trigger the
5902/// omit-on-insert sentinel that leaves the column to a future default).
5903///
5904/// For `SqlType::ForeignKey` columns: rendered as `BIGINT` with a
5905/// `REFERENCES "<target>"("id")` suffix appended via `.extra()`. The
5906/// target table name comes from `col.fk_target`.
5907/// Look up the FK target model's primary-key column name and SQL
5908/// type. Walks the registered ModelMeta set to find the model whose
5909/// table matches `fk_target_table`, then picks the first column
5910/// marked `primary_key = true`. Falls back to `("id", BigInteger)`
5911/// when the target isn't registered (cross-plugin lookup miss, or
5912/// the FK points outside the framework's model registry).
5913///
5914/// Used by both the SQLite and Postgres FK column-def builders so the
5915/// generated `<col> <type> REFERENCES <tbl>(<pk_col>)` matches the
5916/// target's actual PK shape — gap #60 made non-`id`, non-i64 PKs
5917/// (e.g. `Permission.codename: String`) a real case.
5918fn fk_target_pk(fk_target_table: &str) -> (String, sea_query::ColumnType) {
5919 use sea_query::ColumnType;
5920 let unesc = fk_target_table.replace("\"\"", "\"");
5921 // Non-panicking registry read — `registered_models()` itself
5922 // panics when called outside an `App::build()` context, but the
5923 // migration engine's unit tests construct snapshots by hand and
5924 // call into DDL emit without booting the framework. Fall through
5925 // to the historical "id"/BigInteger default in that case.
5926 let Some(metas) = REGISTRY.get() else {
5927 return ("id".to_string(), ColumnType::BigInteger);
5928 };
5929 for meta in metas.iter().map(|(_, m)| m) {
5930 if meta.table != unesc {
5931 continue;
5932 }
5933 if let Some(pk) = meta.fields.iter().find(|c| c.primary_key) {
5934 // Map the PK's SqlType to a sea-query ColumnType. We can't
5935 // route through `SqliteBackend::map_column` because that
5936 // wants a `Column` and applies max_length / choices
5937 // metadata which is irrelevant to a FK column. Hand-roll
5938 // the few cases the framework supports for PKs.
5939 let ct = match pk.ty {
5940 SqlType::BigInt | SqlType::Integer => ColumnType::BigInteger,
5941 SqlType::SmallInt => ColumnType::SmallInteger,
5942 SqlType::Text => ColumnType::Text,
5943 SqlType::Uuid => ColumnType::Uuid,
5944 // Other PK types fall back to BigInteger as the
5945 // historical default. The compile-time PrimaryKey
5946 // trait keeps this list closed in practice.
5947 _ => ColumnType::BigInteger,
5948 };
5949 return (pk.name.clone(), ct);
5950 }
5951 }
5952 ("id".to_string(), ColumnType::BigInteger)
5953}
5954
5955fn build_column_def_sqlite(col: &Column) -> sea_query::ColumnDef {
5956 use sea_query::{Alias, ColumnDef, ColumnType};
5957
5958 // ForeignKey gets a special path: column type + inline REFERENCES
5959 // clause both derived from the target model's PK column.
5960 if matches!(col.ty, SqlType::ForeignKey) {
5961 let fk_target = col
5962 .fk_target
5963 .as_deref()
5964 .unwrap_or("_unknown_")
5965 .replace('"', "\"\"");
5966 let (pk_col_name, pk_col_type) = fk_target_pk(&fk_target);
5967 let mut def = ColumnDef::new_with_type(Alias::new(&col.name), pk_col_type);
5968 if !col.nullable {
5969 def.not_null();
5970 }
5971 // BUG-15: `#[umbral(unique)]` on a FK column is the
5972 // OneToOne idiom — emit UNIQUE inline so the
5973 // referencing-row uniqueness is enforced at the DB.
5974 // The FK branch used to skip this because it returned
5975 // before the non-FK unique branch ran.
5976 if col.unique {
5977 def.unique_key();
5978 }
5979 // gaps2 #22: `#[umbral(db_constraint = false)]` keeps the logical
5980 // FK (column type derived from the target PK, above) but emits
5981 // NO physical `REFERENCES` clause. This is the only valid shape
5982 // for a cross-database FK. The default (`true`) emits the
5983 // constraint as before.
5984 if col.db_constraint {
5985 def.extra(format!(
5986 "REFERENCES \"{fk_target}\"(\"{pk_col_name}\"){}",
5987 fk_action_suffix(col),
5988 ));
5989 }
5990 return def;
5991 }
5992
5993 let is_int_pk = col.primary_key && matches!(col.ty, SqlType::Integer | SqlType::BigInt);
5994
5995 let column_type = if is_int_pk {
5996 ColumnType::Integer
5997 } else {
5998 crate::backend::SqliteBackend.map_column(col)
5999 };
6000
6001 let mut def = ColumnDef::new_with_type(Alias::new(&col.name), column_type);
6002 if !col.nullable {
6003 def.not_null();
6004 }
6005 if col.primary_key {
6006 def.primary_key();
6007 if is_int_pk {
6008 def.auto_increment();
6009 }
6010 }
6011 // gaps3 #35: `#[umbral(case_insensitive)]` on SQLite lifts to a column-level
6012 // `COLLATE NOCASE`, so `=`, `UNIQUE`, and `ORDER BY` fold case while storage
6013 // keeps the original casing. Emitted before UNIQUE so the auto-created
6014 // unique index inherits the column's NOCASE collation. NOCASE folds ASCII
6015 // A–Z only (a boot check warns about Unicode); Postgres uses `citext`.
6016 if col.case_insensitive && matches!(col.ty, SqlType::Text) {
6017 def.extra("COLLATE NOCASE".to_string());
6018 }
6019 // `#[umbral(unique)]` lifts to a column-level UNIQUE clause.
6020 // Skipped on PK columns (already unique) so the DDL stays tidy.
6021 if col.unique && !col.primary_key {
6022 def.unique_key();
6023 }
6024 // IMP-3: `#[umbral(min = N)]` / `#[umbral(max = N)]` lift to a
6025 // column-level CHECK clause. Both SQLite and Postgres accept the
6026 // same syntax. The pre-validation in `insert_json`/`update_json`
6027 // catches violations earlier with a friendlier error; the CHECK
6028 // is the DB-side safety net against direct-SQL writers.
6029 if let Some(check) = check_min_max_sql(col) {
6030 def.extra(check);
6031 }
6032 // User-declared `#[umbral(default = "...")]` lifts to a DDL DEFAULT
6033 // clause. Required when emitting `ALTER TABLE ADD COLUMN` for a
6034 // NOT NULL column against a non-empty table (SQLite rejects the
6035 // ADD otherwise); on CREATE TABLE it sets the column-level default
6036 // the database uses when an INSERT omits the value.
6037 //
6038 // SQLite stores booleans as INTEGER; the literal `'true'` /
6039 // `'false'` would land as a TEXT default that fails type checks
6040 // on reads. Translate Boolean defaults to `1` / `0` so the
6041 // stored representation matches what sqlx expects on hydration
6042 // (closes IMP-2 in bugs/tests/testBugs.md).
6043 if !col.default.is_empty() {
6044 if matches!(col.ty, SqlType::Boolean) {
6045 // Pass an integer to sea-query so the rendered SQL is
6046 // `DEFAULT 1` / `DEFAULT 0` instead of the quoted-string
6047 // `DEFAULT '1'` (which sqlx rejects as TEXT on read of
6048 // a BOOLEAN column).
6049 def.default(sqlite_bool_default(&col.default));
6050 } else {
6051 def.default(col.default.clone());
6052 }
6053 }
6054 // NOTE: auto_now / auto_now_add deliberately does NOT emit a
6055 // `DEFAULT CURRENT_TIMESTAMP` here. SQLite rejects non-constant
6056 // defaults in `ALTER TABLE ADD COLUMN` ("Cannot add a column
6057 // with non-constant default") and that's the path that matters
6058 // for evolving an existing table. The SQLite `AddColumn` render
6059 // path handles the auto_now backfill via a two-statement
6060 // sequence (nullable ADD + UPDATE backfill). On CREATE TABLE
6061 // we don't need a default at all because every INSERT goes
6062 // through the macro-emitted Rust path which always supplies the
6063 // value. See `Operation::AddColumn` render below.
6064 def
6065}
6066
6067/// Render a column's `#[umbral(default = ...)]` value as a raw SQL literal for
6068/// a hand-built statement (the NOT-NULL backfill, audit_2 core-migrate #5).
6069/// sea-query quotes literals itself in the column-def path, but the backfill
6070/// `UPDATE`/`COALESCE` is a raw `format!`, so it needs the literal here.
6071/// Numeric and boolean types render unquoted (`0`, `true`); everything else is
6072/// a single-quoted string with inner quotes doubled. `is_postgres` only affects
6073/// booleans (`true`/`false` on PG, `1`/`0` on SQLite, matching each backend's
6074/// boolean storage).
6075fn default_sql_literal(col: &Column, is_postgres: bool) -> String {
6076 use crate::orm::SqlType::*;
6077 match col.ty {
6078 Boolean => {
6079 let truthy = matches!(
6080 col.default.trim().to_ascii_lowercase().as_str(),
6081 "true" | "1" | "t" | "yes"
6082 );
6083 if is_postgres {
6084 if truthy { "true" } else { "false" }.to_string()
6085 } else if truthy {
6086 "1".to_string()
6087 } else {
6088 "0".to_string()
6089 }
6090 }
6091 SmallInt | Integer | BigInt | Real | Double | Decimal | ForeignKey => {
6092 // Numeric literal — validated at derive time; emit unquoted.
6093 col.default.clone()
6094 }
6095 _ => format!("'{}'", col.default.replace('\'', "''")),
6096 }
6097}
6098
6099/// Map a user-supplied boolean default string (`"true"` / `"false"`
6100/// / `"1"` / `"0"`, case-insensitive) to the SQLite integer literal
6101/// the column expects. Anything unrecognised falls through to `0`
6102/// — a developer-visible miss (default is wrong, not stored as
6103/// text) is friendlier than the runtime decode error the textual
6104/// path produces.
6105fn sqlite_bool_default(raw: &str) -> i32 {
6106 match raw.trim().to_ascii_lowercase().as_str() {
6107 "true" | "1" | "t" | "yes" => 1,
6108 _ => 0,
6109 }
6110}
6111
6112/// IMP-3: lower `#[umbral(min = N)]` / `#[umbral(max = N)]` to a
6113/// DDL CHECK clause. Returns `None` when the column declares
6114/// neither bound. The rendered SQL works on both SQLite and
6115/// Postgres (`"<col>" >= N`, `"<col>" <= N`, joined by `AND`).
6116/// Only applied to numeric columns — applying it to text would
6117/// compare strings lexicographically and surprise everyone.
6118fn check_min_max_sql(col: &Column) -> Option<String> {
6119 if col.min.is_none() && col.max.is_none() {
6120 return None;
6121 }
6122 if !matches!(
6123 col.ty,
6124 SqlType::SmallInt | SqlType::Integer | SqlType::BigInt | SqlType::Real | SqlType::Double
6125 ) {
6126 return None;
6127 }
6128 let name = col.name.replace('"', "\"\"");
6129 let mut parts = Vec::with_capacity(2);
6130 if let Some(n) = col.min {
6131 parts.push(format!("\"{name}\" >= {n}"));
6132 }
6133 if let Some(n) = col.max {
6134 parts.push(format!("\"{name}\" <= {n}"));
6135 }
6136 Some(format!("CHECK ({})", parts.join(" AND ")))
6137}
6138
6139/// Build a Postgres `ColumnDef`. Integer primary keys use the
6140/// standard `auto_increment()` flag — sea-query's `PostgresQueryBuilder`
6141/// lowers that to `BIGSERIAL` for `BigInt` and `SERIAL` for `Integer`.
6142/// No SQLite-style INTEGER-type override needed; Postgres has proper
6143/// `BIGSERIAL` / identity columns and respects the declared width.
6144///
6145/// For `SqlType::ForeignKey` columns: rendered as `BIGINT` with a
6146/// `REFERENCES "<target>"("id")` suffix. The target table name comes
6147/// from `col.fk_target`.
6148fn build_column_def_postgres(col: &Column) -> sea_query::ColumnDef {
6149 use sea_query::{Alias, ColumnDef};
6150
6151 // ForeignKey gets a special path: column type + inline REFERENCES
6152 // clause both derived from the target model's PK.
6153 if matches!(col.ty, SqlType::ForeignKey) {
6154 let fk_target = col
6155 .fk_target
6156 .as_deref()
6157 .unwrap_or("_unknown_")
6158 .replace('"', "\"\"");
6159 let (pk_col_name, pk_col_type) = fk_target_pk(&fk_target);
6160 // sea-query's ColumnType variants are dialect-agnostic; the
6161 // same value works for both SQLite and Postgres builders here.
6162 let mut def = ColumnDef::new_with_type(Alias::new(&col.name), pk_col_type);
6163 if !col.nullable {
6164 def.not_null();
6165 }
6166 // BUG-15: `#[umbral(unique)]` on a FK column is the
6167 // OneToOne idiom — emit UNIQUE inline so the
6168 // referencing-row uniqueness is enforced at the DB.
6169 // The FK branch used to skip this because it returned
6170 // before the non-FK unique branch ran.
6171 if col.unique {
6172 def.unique_key();
6173 }
6174 // gaps2 #22: skip the physical `REFERENCES` clause when the FK
6175 // opted out of the DB constraint (cross-database FK). The
6176 // logical column + `fk_target` stay intact.
6177 if col.db_constraint {
6178 def.extra(format!(
6179 "REFERENCES \"{fk_target}\"(\"{pk_col_name}\"){}",
6180 fk_action_suffix(col),
6181 ));
6182 }
6183 return def;
6184 }
6185
6186 let column_type = crate::backend::PostgresBackend.map_column(col);
6187
6188 let mut def = ColumnDef::new_with_type(Alias::new(&col.name), column_type);
6189 if !col.nullable {
6190 def.not_null();
6191 }
6192 if col.primary_key {
6193 def.primary_key();
6194 if matches!(
6195 col.ty,
6196 SqlType::Integer | SqlType::BigInt | SqlType::SmallInt
6197 ) {
6198 def.auto_increment();
6199 }
6200 }
6201 // `#[umbral(unique)]` lifts to a column-level UNIQUE clause on
6202 // Postgres too. Skipped for PK columns (already unique).
6203 if col.unique && !col.primary_key {
6204 def.unique_key();
6205 }
6206 // IMP-3: numeric bounds CHECK. Mirrors the SQLite branch.
6207 if let Some(check) = check_min_max_sql(col) {
6208 def.extra(check);
6209 }
6210 // Single-valued Choices: emit a CHECK constraint so a third-party
6211 // process writing directly to the DB can't insert a value the Rust
6212 // enum can't model. MultiChoice carries the same choices/labels
6213 // metadata but the stored value is a CSV — a single-value `IN (...)`
6214 // constraint would reject every legal CSV. Validating "every CSV
6215 // piece is a known variant" needs a regex with per-variant
6216 // escaping, which we leave to the sqlx Decode path at v1.
6217 if !col.choices.is_empty() && !col.is_multichoice {
6218 let col_name_escaped = col.name.replace('"', "\"\"");
6219 let values_sql = col
6220 .choices
6221 .iter()
6222 .map(|v| format!("'{}'", v.replace('\'', "''")))
6223 .collect::<Vec<_>>()
6224 .join(", ");
6225 def.extra(format!("CHECK (\"{col_name_escaped}\" IN ({values_sql}))"));
6226 }
6227 // User-declared `#[umbral(default = "...")]` lifts to a DDL DEFAULT
6228 // clause. Required for `ALTER TABLE ADD COLUMN` of a NOT NULL
6229 // column against a non-empty table — Postgres needs either a
6230 // default or a separate backfill.
6231 if !col.default.is_empty() {
6232 def.default(col.default.clone());
6233 } else if (col.auto_now || col.auto_now_add)
6234 && matches!(col.ty, SqlType::Timestamptz | SqlType::Date | SqlType::Time)
6235 {
6236 // Mirror of the SQLite branch above. Without a DEFAULT
6237 // Postgres rejects `ALTER TABLE ADD COLUMN ... NOT NULL`
6238 // on a populated table. `now()` evaluates per-row during
6239 // the backfill so every existing row gets a sane value;
6240 // future INSERTs override via the macro-emitted Rust path.
6241 def.default(sea_query::Expr::cust("now()"));
6242 }
6243 def
6244}
6245
6246#[cfg(test)]
6247mod tests {
6248 use super::*;
6249 use std::collections::HashSet;
6250
6251 // ---- gaps2 #100: squash_plan (pure squash-vs-originals decision) ----
6252
6253 /// A minimal migration file for `plugin`/`id`; `replaces` lists the ids it
6254 /// squashes (all within `plugin`). Empty `replaces` = an ordinary migration.
6255 fn mf(plugin: &str, id: &str, replaces: &[&str]) -> MigrationFile {
6256 MigrationFile {
6257 id: id.to_string(),
6258 plugin: plugin.to_string(),
6259 depends_on: Vec::new(),
6260 operations: Vec::new(),
6261 snapshot_after: Snapshot::default(),
6262 replaces: replaces
6263 .iter()
6264 .map(|m| MigrationRef {
6265 plugin: plugin.to_string(),
6266 migration: m.to_string(),
6267 })
6268 .collect(),
6269 }
6270 }
6271
6272 fn applied_set(plugin: &str, ids: &[&str]) -> HashSet<(String, String)> {
6273 ids.iter()
6274 .map(|id| (plugin.to_string(), id.to_string()))
6275 .collect()
6276 }
6277
6278 #[test]
6279 fn squash_plan_plain_migrations_apply_when_unrecorded_skip_when_applied() {
6280 let files = vec![mf("app", "0001", &[]), mf("app", "0002", &[])];
6281 let applied = applied_set("app", &["0001"]);
6282 let plan = squash_plan(&files, &applied).unwrap();
6283 assert_eq!(plan, vec![ApplyDecision::Skip, ApplyDecision::Apply]);
6284 }
6285
6286 #[test]
6287 fn squash_plan_fresh_db_applies_squash_and_shadows_its_originals() {
6288 // Both the squash and its originals are on disk (Django keeps both).
6289 // Nothing applied → run the squash once, skip every original.
6290 let files = vec![
6291 mf("app", "0001", &[]),
6292 mf("app", "0002", &[]),
6293 mf("app", "0001_squashed_0002", &["0001", "0002"]),
6294 ];
6295 let applied = applied_set("app", &[]);
6296 let plan = squash_plan(&files, &applied).unwrap();
6297 assert_eq!(
6298 plan,
6299 vec![
6300 ApplyDecision::Skip, // 0001 shadowed by the squash
6301 ApplyDecision::Skip, // 0002 shadowed by the squash
6302 ApplyDecision::Apply // the squash builds the whole schema
6303 ]
6304 );
6305 }
6306
6307 #[test]
6308 fn squash_plan_existing_db_with_full_history_record_only() {
6309 // Both originals already applied → the squash records itself without
6310 // running (its schema already exists), originals skipped.
6311 let files = vec![
6312 mf("app", "0001", &[]),
6313 mf("app", "0002", &[]),
6314 mf("app", "0001_squashed_0002", &["0001", "0002"]),
6315 ];
6316 let applied = applied_set("app", &["0001", "0002"]);
6317 let plan = squash_plan(&files, &applied).unwrap();
6318 assert_eq!(
6319 plan,
6320 vec![
6321 ApplyDecision::Skip,
6322 ApplyDecision::Skip,
6323 ApplyDecision::RecordOnly
6324 ]
6325 );
6326 }
6327
6328 #[test]
6329 fn squash_plan_already_recorded_squash_is_skipped() {
6330 // The squash ran on a previous migrate; it's in the tracking table.
6331 let files = vec![
6332 mf("app", "0001", &[]),
6333 mf("app", "0002", &[]),
6334 mf("app", "0001_squashed_0002", &["0001", "0002"]),
6335 ];
6336 let applied = applied_set("app", &["0001_squashed_0002"]);
6337 let plan = squash_plan(&files, &applied).unwrap();
6338 // originals never applied but shadowed by the (already-applied) squash.
6339 assert_eq!(
6340 plan,
6341 vec![
6342 ApplyDecision::Skip,
6343 ApplyDecision::Skip,
6344 ApplyDecision::Skip
6345 ]
6346 );
6347 }
6348
6349 #[test]
6350 fn squash_plan_partial_transition_falls_back_to_surviving_originals() {
6351 // 0001 applied, 0002 not; originals still on disk → ignore the squash,
6352 // finish 0002 individually.
6353 let files = vec![
6354 mf("app", "0001", &[]),
6355 mf("app", "0002", &[]),
6356 mf("app", "0001_squashed_0002", &["0001", "0002"]),
6357 ];
6358 let applied = applied_set("app", &["0001"]);
6359 let plan = squash_plan(&files, &applied).unwrap();
6360 assert_eq!(
6361 plan,
6362 vec![
6363 ApplyDecision::Skip, // 0001 already applied
6364 ApplyDecision::Apply, // 0002 finishes individually
6365 ApplyDecision::Skip // squash ignored during the transition
6366 ]
6367 );
6368 }
6369
6370 #[test]
6371 fn squash_plan_partial_transition_with_missing_original_errors() {
6372 // 0001 applied, 0002 not, and 0002's file is GONE → can't reconcile.
6373 let files = vec![
6374 mf("app", "0001", &[]),
6375 mf("app", "0001_squashed_0002", &["0001", "0002"]),
6376 ];
6377 let applied = applied_set("app", &["0001"]);
6378 let err = squash_plan(&files, &applied).unwrap_err();
6379 match err {
6380 MigrateError::SquashInconsistent {
6381 plugin,
6382 squash,
6383 missing,
6384 } => {
6385 assert_eq!(plugin, "app");
6386 assert_eq!(squash, "0001_squashed_0002");
6387 assert_eq!(missing, vec!["app/0002".to_string()]);
6388 }
6389 other => panic!("expected SquashInconsistent, got {other:?}"),
6390 }
6391 }
6392
6393 /// audit_2 core-migrate #7 — the advisory-lock key must be deterministic
6394 /// (every process computes the same key for the same target, so they
6395 /// mutually exclude) and distinct per discriminator (different aliases /
6396 /// schemas migrate concurrently). Pins the FNV constants so a refactor that
6397 /// changes the hash — silently breaking cross-process exclusion — fails.
6398 #[test]
6399 fn pg_migration_lock_key_is_deterministic_and_distinct() {
6400 // Deterministic: same input → same key, run to run, process to process.
6401 assert_eq!(
6402 pg_migration_lock_key("default"),
6403 pg_migration_lock_key("default"),
6404 );
6405 // Distinct: different aliases/schemas get different keys.
6406 assert_ne!(
6407 pg_migration_lock_key("default"),
6408 pg_migration_lock_key("replica"),
6409 );
6410 assert_ne!(
6411 pg_migration_lock_key("tenant_a"),
6412 pg_migration_lock_key("tenant_b"),
6413 );
6414 // Pin the exact value so the hash can't drift unnoticed (two binaries on
6415 // different umbral versions must still agree on the key).
6416 assert_eq!(
6417 pg_migration_lock_key("default"),
6418 {
6419 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
6420 for b in b"umbral_migrations\0"
6421 .iter()
6422 .copied()
6423 .chain(b"default".iter().copied())
6424 {
6425 hash ^= b as u64;
6426 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
6427 }
6428 hash as i64
6429 },
6430 "the lock-key hash changed — this breaks cross-process exclusion \
6431 between an old and a new migrator; bump deliberately if intended",
6432 );
6433 }
6434
6435 /// M8 — `plugin_order()` falls back to `registered_plugins()` when
6436 /// no topological order has been published. The fallback keeps the
6437 /// engine usable from low-level paths that drive `init_plugins`
6438 /// directly (the M5 / M6 tests that pre-date phase 1.5 of
6439 /// `App::build()`).
6440 ///
6441 /// Runs in the lib's unit-test binary, which is wholly separate
6442 /// from the integration test binaries and so owns its own copies
6443 /// of `REGISTRY` and `PLUGIN_ORDER`. This test seeds `REGISTRY` via
6444 /// `init_plugins`, never touches `init_plugin_order`, and pins the
6445 /// fallback to the sorted-by-name `registered_plugins()` output.
6446 /// As the only test that touches either OnceLock in this binary,
6447 /// it has them to itself.
6448 #[test]
6449 fn plugin_order_falls_back_to_registered_plugins_when_unpublished() {
6450 let mut per_plugin: std::collections::HashMap<String, Vec<ModelMeta>> =
6451 std::collections::HashMap::new();
6452 per_plugin.insert(
6453 "zeta".to_string(),
6454 vec![ModelMeta {
6455 view: None,
6456 materialized: false,
6457 name: "ZetaModel".to_string(),
6458 table: "zeta".to_string(),
6459 fields: Vec::new(),
6460 display: "ZetaModel".to_string(),
6461 icon: "database".to_string(),
6462 database: None,
6463 singleton: false,
6464 unique_together: Vec::new(),
6465 indexes: Vec::new(),
6466 ordering: Vec::new(),
6467 m2m_relations: Vec::new(),
6468 soft_delete: false,
6469 audited: false,
6470 app_label: "app".to_string(),
6471 }],
6472 );
6473 per_plugin.insert(
6474 "alpha".to_string(),
6475 vec![ModelMeta {
6476 view: None,
6477 materialized: false,
6478 name: "AlphaModel".to_string(),
6479 table: "alpha".to_string(),
6480 fields: Vec::new(),
6481 display: "AlphaModel".to_string(),
6482 icon: "database".to_string(),
6483 database: None,
6484 singleton: false,
6485 unique_together: Vec::new(),
6486 indexes: Vec::new(),
6487 ordering: Vec::new(),
6488 m2m_relations: Vec::new(),
6489 soft_delete: false,
6490 audited: false,
6491 app_label: "app".to_string(),
6492 }],
6493 );
6494 init_plugins(per_plugin);
6495
6496 // `init_plugin_order` was never called, so `plugin_order` must
6497 // return the sorted-by-name fallback.
6498 let order = plugin_order();
6499 assert_eq!(
6500 order,
6501 vec!["alpha".to_string(), "zeta".to_string()],
6502 "fallback should sort by name; got {order:?}",
6503 );
6504 assert_eq!(
6505 order,
6506 registered_plugins(),
6507 "fallback should exactly equal registered_plugins()",
6508 );
6509 }
6510
6511 /// Gap #65: `#[umbral(unique)]` lifts to a column-level UNIQUE in
6512 /// CREATE TABLE DDL on both backends. PK columns skip the clause
6513 /// because they're already unique by virtue of being the PK.
6514 #[test]
6515 fn unique_column_emits_unique_keyword_on_both_backends() {
6516 use sea_query::{Alias, PostgresQueryBuilder, SqliteQueryBuilder, Table};
6517
6518 let id = Column {
6519 name: "id".into(),
6520 ty: SqlType::BigInt,
6521 primary_key: true,
6522 nullable: false,
6523 fk_target: None,
6524 noform: false,
6525 privileged: false,
6526 private: false,
6527 secret: false,
6528 db_constraint: true,
6529 noedit: false,
6530 auto_user_add: false,
6531 auto_user: false,
6532 is_string_repr: false,
6533 max_length: 0,
6534 choices: vec![],
6535 choice_labels: vec![],
6536 default: String::new(),
6537 is_multichoice: false,
6538 // Set even though it's a PK so we can assert below that
6539 // the emit path drops the redundant clause.
6540 unique: true,
6541 on_delete: crate::orm::FkAction::NoAction,
6542 on_update: crate::orm::FkAction::NoAction,
6543 index: false,
6544 auto_now_add: false,
6545 auto_now: false,
6546 trim: false,
6547 lowercase: false,
6548 case_insensitive: false,
6549 help: String::new(),
6550 example: String::new(),
6551 widget: None,
6552 supported_backends: Vec::new(),
6553 min: None,
6554 max: None,
6555 text_format: ::core::option::Option::None,
6556 slug_from: ::core::option::Option::None,
6557 };
6558 let username = Column {
6559 name: "username".into(),
6560 ty: SqlType::Text,
6561 primary_key: false,
6562 nullable: false,
6563 fk_target: None,
6564 noform: false,
6565 privileged: false,
6566 private: false,
6567 secret: false,
6568 db_constraint: true,
6569 noedit: false,
6570 auto_user_add: false,
6571 auto_user: false,
6572 is_string_repr: false,
6573 max_length: 0,
6574 choices: vec![],
6575 choice_labels: vec![],
6576 default: String::new(),
6577 is_multichoice: false,
6578 unique: true,
6579 on_delete: crate::orm::FkAction::NoAction,
6580 on_update: crate::orm::FkAction::NoAction,
6581 index: false,
6582 auto_now_add: false,
6583 auto_now: false,
6584 trim: false,
6585 lowercase: false,
6586 case_insensitive: false,
6587 help: String::new(),
6588 example: String::new(),
6589 widget: None,
6590 supported_backends: Vec::new(),
6591 min: None,
6592 max: None,
6593 text_format: ::core::option::Option::None,
6594 slug_from: ::core::option::Option::None,
6595 };
6596 let email = Column {
6597 name: "email".into(),
6598 ty: SqlType::Text,
6599 primary_key: false,
6600 nullable: false,
6601 fk_target: None,
6602 noform: false,
6603 privileged: false,
6604 private: false,
6605 secret: false,
6606 db_constraint: true,
6607 noedit: false,
6608 auto_user_add: false,
6609 auto_user: false,
6610 is_string_repr: false,
6611 max_length: 0,
6612 choices: vec![],
6613 choice_labels: vec![],
6614 default: String::new(),
6615 is_multichoice: false,
6616 unique: false,
6617 on_delete: crate::orm::FkAction::NoAction,
6618 on_update: crate::orm::FkAction::NoAction,
6619 index: false,
6620 auto_now_add: false,
6621 auto_now: false,
6622 trim: false,
6623 lowercase: false,
6624 case_insensitive: false,
6625 help: String::new(),
6626 example: String::new(),
6627 widget: None,
6628 supported_backends: Vec::new(),
6629 min: None,
6630 max: None,
6631 text_format: ::core::option::Option::None,
6632 slug_from: ::core::option::Option::None,
6633 };
6634
6635 for backend in ["sqlite", "postgres"] {
6636 let mut stmt = Table::create();
6637 stmt.table(Alias::new("u"));
6638 for col in [&id, &username, &email] {
6639 let mut def = if backend == "sqlite" {
6640 build_column_def_sqlite(col)
6641 } else {
6642 build_column_def_postgres(col)
6643 };
6644 stmt.col(&mut def);
6645 }
6646 let sql = if backend == "sqlite" {
6647 stmt.to_string(SqliteQueryBuilder)
6648 } else {
6649 stmt.to_string(PostgresQueryBuilder)
6650 };
6651
6652 // UNIQUE on the explicitly-marked non-PK column.
6653 assert!(
6654 sql.contains("\"username\"") && sql.to_uppercase().contains("UNIQUE"),
6655 "{backend}: expected UNIQUE on username; got: {sql}",
6656 );
6657 // No UNIQUE on `email` (flag false).
6658 let email_clause = sql
6659 .split("\"email\"")
6660 .nth(1)
6661 .unwrap_or_default()
6662 .split(',')
6663 .next()
6664 .unwrap_or_default();
6665 assert!(
6666 !email_clause.to_uppercase().contains("UNIQUE"),
6667 "{backend}: email should not be UNIQUE; clause: {email_clause}",
6668 );
6669 // PK still PK; the redundant UNIQUE flag is dropped so we
6670 // don't double up the constraint.
6671 let id_clause = sql
6672 .split("\"id\"")
6673 .nth(1)
6674 .unwrap_or_default()
6675 .split(',')
6676 .next()
6677 .unwrap_or_default();
6678 assert!(
6679 id_clause.to_uppercase().contains("PRIMARY KEY"),
6680 "{backend}: id should still be PRIMARY KEY; clause: {id_clause}",
6681 );
6682 assert!(
6683 !id_clause.to_uppercase().contains("UNIQUE"),
6684 "{backend}: PK column should not also carry UNIQUE; clause: {id_clause}",
6685 );
6686 }
6687 }
6688
6689 /// Gap #68: `on_delete` / `on_update` lift to the `REFERENCES`
6690 /// tail in DDL. `NoAction` emits no clause (the SQL default);
6691 /// any other variant emits `ON DELETE <kw>` / `ON UPDATE <kw>`
6692 /// on both backends. The clause goes inside the same `extra(...)`
6693 /// string that already carries `REFERENCES "<target>"("id")` —
6694 /// the test asserts the full tail shape so a future refactor
6695 /// that splits the FK rendering won't silently regress.
6696 #[test]
6697 fn fk_action_lifts_to_references_clause_on_both_backends() {
6698 use sea_query::{Alias, PostgresQueryBuilder, SqliteQueryBuilder, Table};
6699
6700 // Need an FK target table; the DDL renderer looks up the
6701 // PK column type for `auth_user` via `fk_target_pk`.
6702 // Using "post" since it's already registered as a real
6703 // Model in the lib (resolves to BigInt id).
6704 let plain_fk = Column {
6705 name: "owner_id".into(),
6706 ty: SqlType::ForeignKey,
6707 primary_key: false,
6708 nullable: false,
6709 fk_target: Some("post".into()),
6710 noform: false,
6711 privileged: false,
6712 private: false,
6713 secret: false,
6714 db_constraint: true,
6715 noedit: false,
6716 auto_user_add: false,
6717 auto_user: false,
6718 is_string_repr: false,
6719 max_length: 0,
6720 choices: vec![],
6721 choice_labels: vec![],
6722 default: String::new(),
6723 is_multichoice: false,
6724 unique: false,
6725 on_delete: crate::orm::FkAction::NoAction,
6726 on_update: crate::orm::FkAction::NoAction,
6727 index: false,
6728 auto_now_add: false,
6729 auto_now: false,
6730 trim: false,
6731 lowercase: false,
6732 case_insensitive: false,
6733 help: String::new(),
6734 example: String::new(),
6735 widget: None,
6736 supported_backends: Vec::new(),
6737 min: None,
6738 max: None,
6739 text_format: ::core::option::Option::None,
6740 slug_from: ::core::option::Option::None,
6741 };
6742 let cascade_fk = Column {
6743 on_delete: crate::orm::FkAction::Cascade,
6744 on_update: crate::orm::FkAction::Cascade,
6745 index: false,
6746 auto_now_add: false,
6747 auto_now: false,
6748 trim: false,
6749 lowercase: false,
6750 case_insensitive: false,
6751 help: String::new(),
6752 example: String::new(),
6753 widget: None,
6754 supported_backends: Vec::new(),
6755 ..plain_fk.clone()
6756 };
6757 let restrict_fk = Column {
6758 on_delete: crate::orm::FkAction::Restrict,
6759 ..plain_fk.clone()
6760 };
6761 let set_null_fk = Column {
6762 nullable: true,
6763 on_delete: crate::orm::FkAction::SetNull,
6764 ..plain_fk.clone()
6765 };
6766
6767 for backend in ["sqlite", "postgres"] {
6768 let render_one = |col: &Column| -> String {
6769 let mut stmt = Table::create();
6770 stmt.table(Alias::new("t"));
6771 let mut def = if backend == "sqlite" {
6772 build_column_def_sqlite(col)
6773 } else {
6774 build_column_def_postgres(col)
6775 };
6776 stmt.col(&mut def);
6777 if backend == "sqlite" {
6778 stmt.to_string(SqliteQueryBuilder)
6779 } else {
6780 stmt.to_string(PostgresQueryBuilder)
6781 }
6782 };
6783
6784 // NoAction → REFERENCES with no tail clauses.
6785 let sql = render_one(&plain_fk);
6786 assert!(
6787 sql.contains("REFERENCES")
6788 && !sql.to_uppercase().contains("ON DELETE")
6789 && !sql.to_uppercase().contains("ON UPDATE"),
6790 "{backend}: NoAction should emit REFERENCES alone; got: {sql}",
6791 );
6792
6793 // Cascade on both ON DELETE and ON UPDATE.
6794 let sql = render_one(&cascade_fk);
6795 assert!(
6796 sql.to_uppercase().contains("ON DELETE CASCADE")
6797 && sql.to_uppercase().contains("ON UPDATE CASCADE"),
6798 "{backend}: Cascade should emit both clauses; got: {sql}",
6799 );
6800
6801 // Restrict on ON DELETE only; ON UPDATE is NoAction so
6802 // no clause appears.
6803 let sql = render_one(&restrict_fk);
6804 assert!(
6805 sql.to_uppercase().contains("ON DELETE RESTRICT"),
6806 "{backend}: Restrict missing; got: {sql}",
6807 );
6808 assert!(
6809 !sql.to_uppercase().contains("ON UPDATE"),
6810 "{backend}: ON UPDATE shouldn't appear for NoAction; got: {sql}",
6811 );
6812
6813 // SET NULL renders verbatim (two-word keyword).
6814 let sql = render_one(&set_null_fk);
6815 assert!(
6816 sql.to_uppercase().contains("ON DELETE SET NULL"),
6817 "{backend}: SET NULL missing; got: {sql}",
6818 );
6819 }
6820 }
6821
6822 /// Gap #65 follow-up: the diff engine detects changes to *every*
6823 /// schema-meaningful field, not just `ty` and `nullable`. Each
6824 /// branch builds a baseline column, mutates one field, runs
6825 /// `diff_columns`, and asserts an `AlterColumn` op is produced.
6826 /// Catches the regression where toggling `unique` or `on_delete`
6827 /// would silently leave the table unchanged.
6828 #[test]
6829 fn diff_detects_all_schema_meaningful_field_changes() {
6830 fn baseline() -> Column {
6831 Column {
6832 name: "x".into(),
6833 ty: SqlType::Text,
6834 primary_key: false,
6835 nullable: false,
6836 fk_target: None,
6837 noform: false,
6838 privileged: false,
6839 private: false,
6840 secret: false,
6841 db_constraint: true,
6842 noedit: false,
6843 auto_user_add: false,
6844 auto_user: false,
6845 is_string_repr: false,
6846 max_length: 0,
6847 choices: vec![],
6848 choice_labels: vec![],
6849 default: String::new(),
6850 is_multichoice: false,
6851 unique: false,
6852 on_delete: crate::orm::FkAction::NoAction,
6853 on_update: crate::orm::FkAction::NoAction,
6854 index: false,
6855 auto_now_add: false,
6856 auto_now: false,
6857 trim: false,
6858 lowercase: false,
6859 case_insensitive: false,
6860 help: String::new(),
6861 example: String::new(),
6862 widget: None,
6863 supported_backends: Vec::new(),
6864 min: None,
6865 max: None,
6866 text_format: ::core::option::Option::None,
6867 slug_from: ::core::option::Option::None,
6868 }
6869 }
6870 fn meta_with(col: Column) -> ModelMeta {
6871 ModelMeta {
6872 view: None,
6873 materialized: false,
6874 name: "M".into(),
6875 table: "m".into(),
6876 fields: vec![col],
6877 display: "M".into(),
6878 icon: "database".into(),
6879 database: None,
6880 singleton: false,
6881 unique_together: Vec::new(),
6882 indexes: Vec::new(),
6883 ordering: Vec::new(),
6884 m2m_relations: Vec::new(),
6885 soft_delete: false,
6886 audited: false,
6887 app_label: "app".into(),
6888 }
6889 }
6890 let prev = meta_with(baseline());
6891 // Safe-to-alter changes: each must surface as an `AlterColumn`.
6892 // (`nullable` here is false→true — a *loosening*, which is safe;
6893 // the tightening direction is guarded separately below.)
6894 let safe_mutations: Vec<(&str, fn(&mut Column))> = vec![
6895 ("default", |c| c.default = "hello".into()),
6896 ("choices", |c| {
6897 c.choices = vec!["a".into(), "b".into()];
6898 c.choice_labels = vec!["A".into(), "B".into()];
6899 }),
6900 ("nullable", |c| c.nullable = true),
6901 ];
6902 for (label, mutate) in safe_mutations {
6903 let mut col = baseline();
6904 mutate(&mut col);
6905 let current = meta_with(col);
6906 let ops = diff_columns("M", &prev, ¤t).expect("diff should succeed");
6907 assert!(
6908 !ops.is_empty(),
6909 "{label}: diff should produce at least one op; got none",
6910 );
6911 assert!(
6912 ops.iter()
6913 .any(|op| matches!(op, Operation::AlterColumn { column, .. } if column == "x")),
6914 "{label}: expected AlterColumn on `x`; got: {ops:?}",
6915 );
6916 }
6917
6918 // Adding UNIQUE to an existing column is detected too, but as an
6919 // `UnsafeAlter` guard rather than a bare `AlterColumn`: dropping a
6920 // UNIQUE constraint onto a populated column aborts the migration
6921 // if duplicates already exist, so the engine refuses it with a
6922 // duplicate-pre-check message instead of silently emitting it.
6923 let mut col = baseline();
6924 col.unique = true;
6925 let current = meta_with(col);
6926 match diff_columns("M", &prev, ¤t) {
6927 Err(MigrateError::UnsafeAlter { column, reason, .. }) => {
6928 assert_eq!(column, "x");
6929 assert!(
6930 reason.contains("UNIQUE"),
6931 "unsafe-alter reason should mention UNIQUE; got: {reason}",
6932 );
6933 }
6934 other => panic!("unique add should be an UnsafeAlter guard; got: {other:?}"),
6935 }
6936 }
6937
6938 /// Gap #65 follow-up: the Postgres `AlterColumn` render handles
6939 /// the new diff types (unique, default, choices, FK actions)
6940 /// with native `ALTER TABLE ... ADD/DROP CONSTRAINT` /
6941 /// `SET/DROP DEFAULT` statements. SQLite is unchanged — the
6942 /// rebuild dance already swallows any column metadata change.
6943 #[test]
6944 fn postgres_alter_column_renders_constraint_changes() {
6945 let baseline = Column {
6946 name: "x".into(),
6947 ty: SqlType::Text,
6948 primary_key: false,
6949 nullable: false,
6950 fk_target: None,
6951 noform: false,
6952 privileged: false,
6953 private: false,
6954 secret: false,
6955 db_constraint: true,
6956 noedit: false,
6957 auto_user_add: false,
6958 auto_user: false,
6959 is_string_repr: false,
6960 max_length: 0,
6961 choices: vec![],
6962 choice_labels: vec![],
6963 default: String::new(),
6964 is_multichoice: false,
6965 unique: false,
6966 on_delete: crate::orm::FkAction::NoAction,
6967 on_update: crate::orm::FkAction::NoAction,
6968 index: false,
6969 auto_now_add: false,
6970 auto_now: false,
6971 trim: false,
6972 lowercase: false,
6973 case_insensitive: false,
6974 help: String::new(),
6975 example: String::new(),
6976 widget: None,
6977 supported_backends: Vec::new(),
6978 min: None,
6979 max: None,
6980 text_format: ::core::option::Option::None,
6981 slug_from: ::core::option::Option::None,
6982 };
6983
6984 // unique false → true: emit ADD CONSTRAINT ... UNIQUE
6985 let mut new = baseline.clone();
6986 new.unique = true;
6987 let stmts = render_alter_column_postgres("m", "x", &[new], Some(&[baseline.clone()]));
6988 let joined = stmts.join("\n");
6989 assert!(
6990 joined.contains("ADD CONSTRAINT") && joined.contains("UNIQUE"),
6991 "unique add: expected ADD CONSTRAINT UNIQUE; got: {joined}",
6992 );
6993
6994 // unique true → false: emit DROP CONSTRAINT ... IF EXISTS
6995 let prev_unique = Column {
6996 unique: true,
6997 ..baseline.clone()
6998 };
6999 let stmts =
7000 render_alter_column_postgres("m", "x", &[baseline.clone()], Some(&[prev_unique]));
7001 let joined = stmts.join("\n");
7002 assert!(
7003 joined.contains("DROP CONSTRAINT IF EXISTS"),
7004 "unique drop: expected DROP CONSTRAINT IF EXISTS; got: {joined}",
7005 );
7006
7007 // default empty → "hello": SET DEFAULT 'hello'
7008 let mut new = baseline.clone();
7009 new.default = "hello".into();
7010 let stmts = render_alter_column_postgres("m", "x", &[new], Some(&[baseline.clone()]));
7011 let joined = stmts.join("\n");
7012 assert!(
7013 joined.contains("SET DEFAULT 'hello'"),
7014 "default set: expected SET DEFAULT; got: {joined}",
7015 );
7016
7017 // default "hello" → empty: DROP DEFAULT
7018 let prev_default = Column {
7019 default: "hello".into(),
7020 ..baseline.clone()
7021 };
7022 let stmts =
7023 render_alter_column_postgres("m", "x", &[baseline.clone()], Some(&[prev_default]));
7024 let joined = stmts.join("\n");
7025 assert!(
7026 joined.contains("DROP DEFAULT"),
7027 "default drop: expected DROP DEFAULT; got: {joined}",
7028 );
7029
7030 // FK on_delete change → DROP + readd FK with new clause
7031 let fk_baseline = Column {
7032 ty: SqlType::ForeignKey,
7033 fk_target: Some("other".into()),
7034 ..baseline.clone()
7035 };
7036 let fk_cascade = Column {
7037 on_delete: crate::orm::FkAction::Cascade,
7038 ..fk_baseline.clone()
7039 };
7040 let stmts = render_alter_column_postgres("m", "x", &[fk_cascade], Some(&[fk_baseline]));
7041 let joined = stmts.join("\n");
7042 assert!(
7043 joined.contains("DROP CONSTRAINT IF EXISTS")
7044 && joined.contains("FOREIGN KEY")
7045 && joined.contains("ON DELETE CASCADE"),
7046 "FK cascade add: expected drop+readd with ON DELETE CASCADE; got: {joined}",
7047 );
7048 }
7049
7050 /// IMP-2 from bugs/tests/testBugs.md: a `#[umbral(default = "true")]`
7051 /// on a boolean column used to land as `DEFAULT 'true'` on
7052 /// SQLite, which decode-fails on read (column type is INTEGER,
7053 /// the stored TEXT can't deserialize as `bool`). The SQLite
7054 /// renderer now maps the string to `1` / `0`.
7055 #[test]
7056 fn sqlite_bool_default_translates_to_integer_literal() {
7057 use sea_query::{Alias, SqliteQueryBuilder, Table};
7058
7059 let bool_col = Column {
7060 name: "is_active".into(),
7061 ty: SqlType::Boolean,
7062 primary_key: false,
7063 nullable: false,
7064 fk_target: None,
7065 noform: false,
7066 privileged: false,
7067 private: false,
7068 secret: false,
7069 db_constraint: true,
7070 noedit: false,
7071 auto_user_add: false,
7072 auto_user: false,
7073 is_string_repr: false,
7074 max_length: 0,
7075 choices: vec![],
7076 choice_labels: vec![],
7077 default: "true".into(),
7078 is_multichoice: false,
7079 unique: false,
7080 on_delete: crate::orm::FkAction::NoAction,
7081 on_update: crate::orm::FkAction::NoAction,
7082 index: false,
7083 auto_now_add: false,
7084 auto_now: false,
7085 trim: false,
7086 lowercase: false,
7087 case_insensitive: false,
7088 help: String::new(),
7089 example: String::new(),
7090 widget: None,
7091 supported_backends: Vec::new(),
7092 min: None,
7093 max: None,
7094 text_format: ::core::option::Option::None,
7095 slug_from: ::core::option::Option::None,
7096 };
7097 let mut stmt = Table::create();
7098 stmt.table(Alias::new("t"));
7099 let mut def = build_column_def_sqlite(&bool_col);
7100 stmt.col(&mut def);
7101 let sql = stmt.to_string(SqliteQueryBuilder);
7102 assert!(
7103 sql.contains("DEFAULT 1") && !sql.contains("DEFAULT 'true'"),
7104 "bool default 'true' on sqlite should render as DEFAULT 1; got: {sql}",
7105 );
7106
7107 // "false" → 0
7108 let mut bool_col_false = bool_col.clone();
7109 bool_col_false.default = "false".into();
7110 let mut stmt = Table::create();
7111 stmt.table(Alias::new("t"));
7112 let mut def = build_column_def_sqlite(&bool_col_false);
7113 stmt.col(&mut def);
7114 let sql = stmt.to_string(SqliteQueryBuilder);
7115 assert!(
7116 sql.contains("DEFAULT 0") && !sql.contains("DEFAULT 'false'"),
7117 "bool default 'false' on sqlite should render as DEFAULT 0; got: {sql}",
7118 );
7119
7120 // Non-bool columns are untouched (text default stays
7121 // single-quoted literal).
7122 let text_col = Column {
7123 name: "label".into(),
7124 ty: SqlType::Text,
7125 default: "hello".into(),
7126 ..bool_col.clone()
7127 };
7128 let mut stmt = Table::create();
7129 stmt.table(Alias::new("t"));
7130 let mut def = build_column_def_sqlite(&text_col);
7131 stmt.col(&mut def);
7132 let sql = stmt.to_string(SqliteQueryBuilder);
7133 assert!(
7134 sql.contains("DEFAULT 'hello'"),
7135 "text default should stay quoted; got: {sql}",
7136 );
7137 }
7138
7139 /// BUG-4 from bugs/tests/testBugs.md: `#[umbral(index)]` lifts
7140 /// to a `CREATE INDEX IF NOT EXISTS idx_<table>_<col>` statement
7141 /// alongside the `CREATE TABLE`. The index is skipped on PK
7142 /// and UNIQUE columns (those are already indexed by the
7143 /// constraint).
7144 #[test]
7145 fn index_attribute_emits_create_index_alongside_create_table() {
7146 let id = Column {
7147 name: "id".into(),
7148 ty: SqlType::BigInt,
7149 primary_key: true,
7150 nullable: false,
7151 fk_target: None,
7152 noform: false,
7153 privileged: false,
7154 private: false,
7155 secret: false,
7156 db_constraint: true,
7157 noedit: false,
7158 auto_user_add: false,
7159 auto_user: false,
7160 is_string_repr: false,
7161 max_length: 0,
7162 choices: vec![],
7163 choice_labels: vec![],
7164 default: String::new(),
7165 is_multichoice: false,
7166 unique: false,
7167 on_delete: crate::orm::FkAction::NoAction,
7168 on_update: crate::orm::FkAction::NoAction,
7169 // PK with index=true; the renderer should skip the
7170 // extra CREATE INDEX because the PK constraint
7171 // already covers it.
7172 index: true,
7173 auto_now_add: false,
7174 auto_now: false,
7175 trim: false,
7176 lowercase: false,
7177 case_insensitive: false,
7178 help: String::new(),
7179 example: String::new(),
7180 widget: None,
7181 supported_backends: Vec::new(),
7182 min: None,
7183 max: None,
7184 text_format: ::core::option::Option::None,
7185 slug_from: ::core::option::Option::None,
7186 };
7187 let slug = Column {
7188 name: "slug".into(),
7189 ty: SqlType::Text,
7190 primary_key: false,
7191 nullable: false,
7192 index: true,
7193 auto_now_add: false,
7194 auto_now: false,
7195 trim: false,
7196 lowercase: false,
7197 case_insensitive: false,
7198 help: String::new(),
7199 example: String::new(),
7200 widget: None,
7201 supported_backends: Vec::new(),
7202 ..id.clone()
7203 };
7204 let title = Column {
7205 name: "title".into(),
7206 ty: SqlType::Text,
7207 primary_key: false,
7208 nullable: false,
7209 index: false,
7210 auto_now_add: false,
7211 auto_now: false,
7212 trim: false,
7213 lowercase: false,
7214 case_insensitive: false,
7215 help: String::new(),
7216 example: String::new(),
7217 widget: None,
7218 supported_backends: Vec::new(),
7219 ..id.clone()
7220 };
7221 let op = Operation::CreateTable {
7222 table: "post".into(),
7223 columns: vec![id, slug, title],
7224 unique_together: Vec::new(),
7225 indexes: Vec::new(),
7226 };
7227
7228 for backend in ["sqlite", "postgres"] {
7229 let stmts = render_operation_for(&op, backend);
7230 assert!(
7231 stmts
7232 .iter()
7233 .any(|s| s.to_uppercase().contains("CREATE TABLE")),
7234 "{backend}: expected a CREATE TABLE; got: {stmts:?}",
7235 );
7236 let index_stmts: Vec<_> = stmts
7237 .iter()
7238 .filter(|s| s.to_uppercase().contains("CREATE INDEX"))
7239 .collect();
7240 assert_eq!(
7241 index_stmts.len(),
7242 1,
7243 "{backend}: expected exactly one CREATE INDEX (on `slug`); got {index_stmts:?}",
7244 );
7245 let ix = index_stmts[0];
7246 assert!(
7247 ix.contains("\"idx_post_slug\"") && ix.contains("(\"slug\")"),
7248 "{backend}: index should target post(slug); got: {ix}",
7249 );
7250 assert!(
7251 ix.to_uppercase().contains("IF NOT EXISTS"),
7252 "{backend}: should be idempotent via IF NOT EXISTS; got: {ix}",
7253 );
7254 }
7255 }
7256
7257 /// gaps3 #35: a `#[umbral(case_insensitive)]` text column renders
7258 /// per-backend — SQLite `COLLATE NOCASE`, Postgres `citext` with an
7259 /// idempotent `CREATE EXTENSION` emitted before the table.
7260 #[test]
7261 fn case_insensitive_column_renders_per_backend() {
7262 let id = Column {
7263 name: "id".into(),
7264 ty: SqlType::BigInt,
7265 primary_key: true,
7266 nullable: false,
7267 fk_target: None,
7268 noform: false,
7269 privileged: false,
7270 private: false,
7271 secret: false,
7272 db_constraint: true,
7273 noedit: false,
7274 auto_user_add: false,
7275 auto_user: false,
7276 is_string_repr: false,
7277 max_length: 0,
7278 choices: vec![],
7279 choice_labels: vec![],
7280 default: String::new(),
7281 is_multichoice: false,
7282 unique: false,
7283 on_delete: crate::orm::FkAction::NoAction,
7284 on_update: crate::orm::FkAction::NoAction,
7285 index: false,
7286 auto_now_add: false,
7287 auto_now: false,
7288 trim: false,
7289 lowercase: false,
7290 case_insensitive: false,
7291 help: String::new(),
7292 example: String::new(),
7293 widget: None,
7294 supported_backends: Vec::new(),
7295 min: None,
7296 max: None,
7297 text_format: None,
7298 slug_from: None,
7299 };
7300 let name = Column {
7301 name: "name".into(),
7302 ty: SqlType::Text,
7303 primary_key: false,
7304 unique: true,
7305 case_insensitive: true,
7306 ..id.clone()
7307 };
7308 let op = Operation::CreateTable {
7309 table: "handle".into(),
7310 columns: vec![id, name],
7311 unique_together: Vec::new(),
7312 indexes: Vec::new(),
7313 };
7314
7315 // SQLite: COLLATE NOCASE on the column; no extension.
7316 let sqlite = render_operation_for(&op, "sqlite");
7317 let create = sqlite
7318 .iter()
7319 .find(|s| s.to_uppercase().contains("CREATE TABLE"))
7320 .expect("a CREATE TABLE");
7321 assert!(
7322 create.to_uppercase().contains("COLLATE NOCASE"),
7323 "sqlite case_insensitive column must carry COLLATE NOCASE; got: {create}"
7324 );
7325 assert!(
7326 !sqlite
7327 .iter()
7328 .any(|s| s.to_uppercase().contains("EXTENSION")),
7329 "sqlite must not emit a citext extension; got: {sqlite:?}"
7330 );
7331
7332 // Postgres: citext type + CREATE EXTENSION IF NOT EXISTS citext, and the
7333 // extension statement precedes the CREATE TABLE.
7334 let pg = render_operation_for(&op, "postgres");
7335 let ext_idx = pg
7336 .iter()
7337 .position(|s| s.to_uppercase().contains("CREATE EXTENSION") && s.contains("citext"))
7338 .expect("a CREATE EXTENSION citext statement");
7339 let create_idx = pg
7340 .iter()
7341 .position(|s| s.to_uppercase().contains("CREATE TABLE"))
7342 .expect("a CREATE TABLE");
7343 assert!(
7344 ext_idx < create_idx,
7345 "the citext extension must be created before the table; got: {pg:?}"
7346 );
7347 assert!(
7348 pg[create_idx].to_lowercase().contains("citext"),
7349 "postgres case_insensitive column must render as citext; got: {}",
7350 pg[create_idx]
7351 );
7352 assert!(
7353 pg[ext_idx].to_uppercase().contains("IF NOT EXISTS"),
7354 "the extension create must be idempotent; got: {}",
7355 pg[ext_idx]
7356 );
7357 }
7358
7359 /// Regression: adding an `auto_now` / `auto_now_add` column to an
7360 /// existing populated table.
7361 ///
7362 /// - SQLite: a 2-statement sequence (nullable ADD + UPDATE
7363 /// backfill) since SQLite refuses non-constant defaults in
7364 /// ALTER. The column ends up nullable at the DB level;
7365 /// Rust still enforces non-null at the type level.
7366 /// - Postgres: a single ALTER with `DEFAULT now()` — Postgres
7367 /// allows the non-constant default and backfills inline.
7368 #[test]
7369 fn auto_now_add_column_renders_safe_backfill_per_backend() {
7370 for (label, auto_now, auto_now_add) in
7371 [("auto_now", true, false), ("auto_now_add", false, true)]
7372 {
7373 let col = Column {
7374 name: "updated_at".to_string(),
7375 ty: SqlType::Timestamptz,
7376 primary_key: false,
7377 nullable: false,
7378 fk_target: None,
7379 noform: false,
7380 privileged: false,
7381 private: false,
7382 secret: false,
7383 db_constraint: true,
7384 noedit: false,
7385 auto_user_add: false,
7386 auto_user: false,
7387 is_string_repr: false,
7388 max_length: 0,
7389 choices: Vec::new(),
7390 choice_labels: Vec::new(),
7391 default: String::new(),
7392 is_multichoice: false,
7393 unique: false,
7394 on_delete: crate::orm::FkAction::NoAction,
7395 on_update: crate::orm::FkAction::NoAction,
7396 index: false,
7397 auto_now_add,
7398 auto_now,
7399 trim: false,
7400 lowercase: false,
7401 case_insensitive: false,
7402 help: String::new(),
7403 example: String::new(),
7404 widget: None,
7405 supported_backends: Vec::new(),
7406 min: None,
7407 max: None,
7408 text_format: None,
7409 slug_from: None,
7410 };
7411
7412 // SQLite: the AddColumn op must produce TWO statements:
7413 // an ADD COLUMN nullable + an UPDATE backfill. The ADD
7414 // must NOT carry `NOT NULL` (otherwise SQLite rejects
7415 // it on the populated rows), and must NOT carry a
7416 // DEFAULT (otherwise SQLite rejects the non-constant).
7417 let op = Operation::AddColumn {
7418 table: "customer".to_string(),
7419 column: col.clone(),
7420 };
7421 let stmts = render_operation_sqlite(&op);
7422 assert_eq!(
7423 stmts.len(),
7424 2,
7425 "{label} SQLite: must emit ADD + UPDATE, got: {stmts:?}",
7426 );
7427 let add_sql = stmts[0].to_uppercase();
7428 assert!(
7429 add_sql.contains("ADD COLUMN"),
7430 "{label} SQLite: first stmt must be ADD COLUMN, got: {}",
7431 stmts[0],
7432 );
7433 assert!(
7434 !add_sql.contains("NOT NULL"),
7435 "{label} SQLite: ADD COLUMN must be nullable (NOT NULL = SQLite reject), got: {}",
7436 stmts[0],
7437 );
7438 assert!(
7439 !add_sql.contains("DEFAULT"),
7440 "{label} SQLite: ADD COLUMN must omit DEFAULT (non-constant = SQLite reject), got: {}",
7441 stmts[0],
7442 );
7443 let backfill_sql = &stmts[1];
7444 assert!(
7445 backfill_sql.contains("UPDATE") && backfill_sql.contains("datetime('now')"),
7446 "{label} SQLite: second stmt must be backfill UPDATE, got: {backfill_sql}",
7447 );
7448
7449 // Postgres: single ALTER with NOT NULL + DEFAULT now().
7450 let pstmts = render_operation_postgres(&op);
7451 assert_eq!(
7452 pstmts.len(),
7453 1,
7454 "{label} Postgres: single statement suffices, got: {pstmts:?}",
7455 );
7456 let p = &pstmts[0];
7457 assert!(
7458 p.to_lowercase().contains("default now()"),
7459 "{label} Postgres: expected DEFAULT now() in ALTER, got: {p}",
7460 );
7461 assert!(
7462 p.to_uppercase().contains("NOT NULL"),
7463 "{label} Postgres: keeps NOT NULL (Postgres allows non-constant defaults), got: {p}",
7464 );
7465 }
7466 }
7467
7468 /// Audit core-migrate #14 — raw DDL that interpolates
7469 /// developer-supplied identifiers must escape inner double quotes by
7470 /// doubling them (the quoting idiom used everywhere else), not strip
7471 /// or pass them through verbatim. A `"` in a table name previously
7472 /// produced malformed DDL in the multi-column index helper (ON-clause
7473 /// table was quote-stripped) and the M2M junction DDL (five raw
7474 /// interpolations).
7475 #[test]
7476 fn raw_ddl_escapes_quoted_identifiers() {
7477 // Multi-column index: the ON-clause table reference must carry
7478 // the doubled quote, not a stripped one.
7479 let idx = create_multi_index_stmt("we\"ird", &["a\"b".to_string(), "c".to_string()]);
7480 assert!(
7481 idx.contains("ON \"we\"\"ird\""),
7482 "multi-index ON clause must escape the quote (doubled); got: {idx}",
7483 );
7484 assert!(
7485 idx.contains("\"a\"\"b\""),
7486 "multi-index column list must escape the quote; got: {idx}",
7487 );
7488
7489 // M2M junction DDL: every interpolated identifier escapes its
7490 // inner quote. Check both backends.
7491 let op = Operation::CreateM2MTable {
7492 junction_table: "j\"t".to_string(),
7493 parent_table: "p\"t".to_string(),
7494 parent_col: "p\"c".to_string(),
7495 child_table: "c\"t".to_string(),
7496 child_col: "c\"c".to_string(),
7497 parent_ty: SqlType::BigInt,
7498 child_ty: SqlType::Text,
7499 };
7500 for backend in ["sqlite", "postgres"] {
7501 let sql = render_operation_for(&op, backend).join("\n");
7502 for (raw, escaped) in [
7503 ("j\"t", "\"j\"\"t\""),
7504 ("p\"t", "\"p\"\"t\""),
7505 ("p\"c", "\"p\"\"c\""),
7506 ("c\"t", "\"c\"\"t\""),
7507 ("c\"c", "\"c\"\"c\""),
7508 ] {
7509 assert!(
7510 sql.contains(escaped),
7511 "{backend}: identifier `{raw}` must render escaped as {escaped}; got: {sql}",
7512 );
7513 }
7514 }
7515 }
7516}
7517
7518/// A `#[derive(Model)]` type's self-registration (gaps3 #46).
7519///
7520/// The derive submits one of these into a link-time slice, so
7521/// `AppBuilder::auto_models()` can discover every model the binary links without
7522/// the app naming each one. It stores a *function* rather than a `ModelMeta`
7523/// because `ModelMeta::for_::<T>()` isn't const — the meta is built on demand at
7524/// boot.
7525pub struct ModelRegistration {
7526 /// Builds this model's meta.
7527 pub meta: fn() -> ModelMeta,
7528}
7529
7530inventory::collect!(ModelRegistration);
7531
7532/// Every model that self-registered via `#[derive(Model)]` in the linked binary.
7533pub fn link_registered_models() -> Vec<ModelMeta> {
7534 inventory::iter::<ModelRegistration>
7535 .into_iter()
7536 .map(|r| (r.meta)())
7537 .collect()
7538}