cadmus 0.1.4

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

use std::collections::{BTreeMap, BTreeSet};
use std::io::{BufRead, IsTerminal, Write};

use super::{Column, EntityTable, Operation, State, Table, Value};

/// What `makemigrations` cannot tell from the models alone.
pub(crate) trait Questions {
    /// Whether table `from` was renamed to `to` (rather than dropped and another created).
    fn renamed_table(&mut self, from: &str, to: &str) -> bool;
    /// Whether `table.from` was renamed to `to`.
    fn renamed_column(&mut self, table: &str, from: &str, to: &str) -> bool;
    /// The value rows already in `table` get for a new required column without a default.
    fn fill(&mut self, table: &str, column: &Column) -> Result<Value, String>;
}

/// Asks in the terminal when there is one; otherwise assumes nothing was renamed and refuses
/// to guess values.
pub(crate) struct Answers {
    interactive: bool,
}

impl Answers {
    pub(crate) fn new(no_input: bool) -> Self {
        Self {
            interactive: !no_input && std::io::stdin().is_terminal(),
        }
    }

    fn ask(&self, question: &str) -> Option<String> {
        if !self.interactive {
            return None;
        }
        print!("{question}");
        std::io::stdout().flush().ok()?;
        let mut line = String::new();
        std::io::stdin().lock().read_line(&mut line).ok()?;
        Some(line.trim().to_owned())
    }

    fn yes(&self, question: &str) -> bool {
        self.ask(&format!("{question} [y/N] "))
            .is_some_and(|a| matches!(a.to_lowercase().as_str(), "y" | "yes"))
    }
}

impl Questions for Answers {
    fn renamed_table(&mut self, from: &str, to: &str) -> bool {
        self.yes(&format!("Was the table `{from}` renamed to `{to}`?"))
    }

    fn renamed_column(&mut self, table: &str, from: &str, to: &str) -> bool {
        self.yes(&format!("Was `{table}.{from}` renamed to `{table}.{to}`?"))
    }

    fn fill(&mut self, table: &str, column: &Column) -> Result<Value, String> {
        let problem = format!(
            "`{table}.{}` is required and has no default, so rows already in `{table}` need a \
             value for it",
            column.name
        );
        if !self.interactive {
            return Err(format!(
                "{problem}. Give the field a default (`#[sea_orm(default_value = ...)]`), make \
                 it an `Option`, or run makemigrations in a terminal to type a value"
            ));
        }
        let typed = self
            .ask(&format!(
                "{problem}.\nType the value (text, a number, true or false), or press Enter to \
                 stop: "
            ))
            .unwrap_or_default();
        if typed.is_empty() {
            return Err("stopped: no value was given".into());
        }
        Ok(parse_value(&typed))
    }
}

/// Answers that only find out whether anything changed: nothing renamed, and a stand-in
/// value for new required columns.
pub(crate) struct Detect;

impl Questions for Detect {
    fn renamed_table(&mut self, _: &str, _: &str) -> bool {
        false
    }

    fn renamed_column(&mut self, _: &str, _: &str, _: &str) -> bool {
        false
    }

    fn fill(&mut self, _: &str, _: &Column) -> Result<Value, String> {
        Ok(Value::sql("NULL"))
    }
}

/// A value typed in the terminal: true/false, a number, or text (quotes optional).
fn parse_value(typed: &str) -> Value {
    match typed {
        "true" => return Value::Bool(true),
        "false" => return Value::Bool(false),
        _ => {}
    }
    if let Ok(n) = typed.parse::<i64>() {
        return Value::Int(n);
    }
    if let Ok(n) = typed.parse::<f64>() {
        return Value::Float(n);
    }
    let unquoted = typed
        .strip_prefix('"')
        .and_then(|t| t.strip_suffix('"'))
        .or_else(|| typed.strip_prefix('\'').and_then(|t| t.strip_suffix('\'')))
        .unwrap_or(typed);
    Value::Text(unquoted.to_owned())
}

fn same_columns(a: &Table, b: &Table) -> bool {
    a.columns.len() == b.columns.len()
        && a.columns.iter().all(|column| {
            b.find(&column.name)
                .is_some_and(|other| other.same_definition(column))
        })
}

/// The operations that turn `from` into the `target` tables, in a safe order: renames, then
/// dropped keys and indexes, new tables, new, changed and dropped columns, dropped tables,
/// and finally new indexes and keys.
pub(crate) fn diff(
    from: &State,
    target: &[EntityTable],
    questions: &mut dyn Questions,
) -> Result<Vec<Operation>, String> {
    let wanted: BTreeMap<&str, &EntityTable> =
        target.iter().map(|t| (t.table.name.as_str(), t)).collect();
    let mut state = from.clone();
    let mut operations = Vec::new();
    let mut emit = |state: &mut State, operation: Operation| -> Result<(), String> {
        state.apply(&operation)?;
        operations.push(operation);
        Ok(())
    };

    // Renamed tables: a dropped and a new table with the same columns.
    let mut removed: Vec<String> = state
        .tables
        .keys()
        .filter(|name| !wanted.contains_key(name.as_str()))
        .cloned()
        .collect();
    for (name, entity) in &wanted {
        if state.tables.contains_key(*name) {
            continue;
        }
        let found = removed.iter().position(|old| {
            same_columns(&state.tables[old], &entity.table) && questions.renamed_table(old, name)
        });
        if let Some(i) = found {
            let old = removed.remove(i);
            emit(
                &mut state,
                Operation::RenameTable {
                    from: old,
                    to: (*name).to_owned(),
                },
            )?;
        }
    }

    // Renamed columns: marked `renamed_from` on the model, or confirmed.
    for (name, entity) in &wanted {
        let Some(current) = state.tables.get(*name).cloned() else {
            continue;
        };
        let mut gone: Vec<&Column> = current
            .columns
            .iter()
            .filter(|c| entity.table.find(&c.name).is_none())
            .collect();
        for column in &entity.table.columns {
            if current.find(&column.name).is_some() {
                continue;
            }
            let hinted = entity
                .renamed_from
                .get(&column.name)
                .and_then(|old| gone.iter().position(|c| &c.name == old));
            let found = hinted.or_else(|| {
                gone.iter().position(|old| {
                    old.same_definition(column)
                        && questions.renamed_column(name, &old.name, &column.name)
                })
            });
            if let Some(i) = found {
                let old = gone.remove(i);
                emit(
                    &mut state,
                    Operation::RenameColumn {
                        table: (*name).to_owned(),
                        from: old.name.clone(),
                        to: column.name.clone(),
                    },
                )?;
            }
        }
    }

    let mut drop_keys = Vec::new();
    let mut drop_indexes = Vec::new();
    let mut add_columns = Vec::new();
    let mut alter_columns = Vec::new();
    let mut drop_columns = Vec::new();
    let mut add_indexes = Vec::new();
    let mut add_keys = Vec::new();

    for (name, entity) in &wanted {
        let Some(current) = state.tables.get(*name) else {
            continue;
        };
        let want = &entity.table;
        let table = (*name).to_owned();
        if current.primary_key != want.primary_key {
            return Err(format!(
                "the primary key of `{name}` changed; migrations cannot change a primary key \
                 over several columns, so write that migration by hand with `.sql(...)`"
            ));
        }
        for key in &current.foreign_keys {
            if !want.foreign_keys.iter().any(|k| k.same_definition(key)) {
                drop_keys.push(Operation::DropForeignKey {
                    table: table.clone(),
                    key: key.clone(),
                });
            }
        }
        for key in &want.foreign_keys {
            if !current.foreign_keys.iter().any(|k| k.same_definition(key)) {
                add_keys.push(Operation::AddForeignKey {
                    table: table.clone(),
                    key: key.clone(),
                });
            }
        }
        for index in &current.indexes {
            if !want.indexes.contains(index) {
                drop_indexes.push(Operation::DropIndex {
                    table: table.clone(),
                    index: index.clone(),
                });
            }
        }
        for index in &want.indexes {
            if !current.indexes.contains(index) {
                add_indexes.push(Operation::AddIndex {
                    table: table.clone(),
                    index: index.clone(),
                });
            }
        }
        for column in &want.columns {
            match current.find(&column.name) {
                Some(existing) if existing == column => {}
                Some(existing) => alter_columns.push(Operation::AlterColumn {
                    table: table.clone(),
                    from: existing.clone(),
                    to: column.clone(),
                }),
                None => {
                    let fill = if column.needs_fill() {
                        Some(questions.fill(name, column)?)
                    } else {
                        None
                    };
                    add_columns.push(Operation::AddColumn {
                        table: table.clone(),
                        column: column.clone(),
                        fill,
                    });
                }
            }
        }
        for column in &current.columns {
            if want.find(&column.name).is_none() {
                drop_columns.push(Operation::DropColumn {
                    table: table.clone(),
                    column: column.clone(),
                });
            }
        }
    }

    // New tables, each after the new tables it refers to. In a cycle, one table is created
    // without its keys to the others, which are added at the end.
    let mut pending: Vec<Table> = wanted
        .iter()
        .filter(|(name, _)| !state.tables.contains_key(**name))
        .map(|(_, entity)| entity.table.clone())
        .collect();
    let mut creates = Vec::new();
    let mut created: BTreeSet<String> = BTreeSet::new();
    while !pending.is_empty() {
        let waiting: BTreeSet<String> = pending.iter().map(|t| t.name.clone()).collect();
        let ready = pending.iter().position(|t| {
            t.foreign_keys
                .iter()
                .all(|k| k.to_table == t.name || !waiting.contains(&k.to_table))
        });
        let mut table = pending.remove(ready.unwrap_or(0));
        if ready.is_none() {
            let (later, now): (Vec<_>, Vec<_>) = table
                .foreign_keys
                .into_iter()
                .partition(|k| k.to_table != table.name && waiting.contains(&k.to_table));
            table.foreign_keys = now;
            for key in later {
                add_keys.push(Operation::AddForeignKey {
                    table: table.name.clone(),
                    key,
                });
            }
        }
        created.insert(table.name.clone());
        creates.push(Operation::CreateTable(table));
    }

    // Dropped tables, each before the dropped tables it refers to. In a cycle, keys are
    // dropped first.
    let mut doomed: Vec<String> = removed;
    let mut drop_order = Vec::new();
    while !doomed.is_empty() {
        let referenced = |name: &str, doomed: &[String]| {
            doomed.iter().any(|other| {
                other != name
                    && state.tables[other]
                        .foreign_keys
                        .iter()
                        .any(|k| k.to_table == name)
            })
        };
        let ready = doomed.iter().position(|name| !referenced(name, &doomed));
        let name = doomed.remove(ready.unwrap_or(0));
        if ready.is_none() {
            for other in &doomed {
                for key in &state.tables[other].foreign_keys {
                    if key.to_table == name {
                        drop_keys.push(Operation::DropForeignKey {
                            table: other.clone(),
                            key: key.clone(),
                        });
                    }
                }
            }
        }
        drop_order.push(name);
    }

    for operation in drop_keys
        .into_iter()
        .chain(drop_indexes)
        .chain(creates)
        .chain(add_columns)
        .chain(alter_columns)
        .chain(drop_columns)
    {
        emit(&mut state, operation)?;
    }
    for name in drop_order {
        let table = state.tables[&name].clone();
        emit(&mut state, Operation::DropTable(table))?;
    }
    for operation in add_indexes.into_iter().chain(add_keys) {
        emit(&mut state, operation)?;
    }
    Ok(operations)
}

/// One line per operation, for the terminal.
pub(crate) fn describe(operations: &[Operation]) -> Vec<String> {
    operations
        .iter()
        .map(|operation| match operation {
            Operation::CreateTable(t) => format!("+ table {}", t.name),
            Operation::DropTable(t) => format!("- table {} (its rows will be deleted)", t.name),
            Operation::RenameTable { from, to } => format!("> table {from} renamed to {to}"),
            Operation::AddColumn {
                table,
                column,
                fill,
            } => match fill {
                Some(value) => format!(
                    "+ column {table}.{} (existing rows get {})",
                    column.name,
                    super::codegen::value(value)
                ),
                None => format!("+ column {table}.{}", column.name),
            },
            Operation::DropColumn { table, column } => {
                format!(
                    "- column {table}.{} (its data will be deleted)",
                    column.name
                )
            }
            Operation::RenameColumn { table, from, to } => {
                format!("> column {table}.{from} renamed to {to}")
            }
            Operation::AlterColumn { table, to, .. } => format!("~ column {table}.{}", to.name),
            Operation::AddIndex { index, .. } => format!("+ index {}", index.name),
            Operation::DropIndex { index, .. } => format!("- index {}", index.name),
            Operation::AddForeignKey { key, .. } => format!("+ foreign key {}", key.name),
            Operation::DropForeignKey { key, .. } => format!("- foreign key {}", key.name),
            Operation::Sql { .. } => "! SQL".to_owned(),
            Operation::Run { .. } => "! Rust code".to_owned(),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::migrations::{ForeignKey, Type};

    struct Yes;

    impl Questions for Yes {
        fn renamed_table(&mut self, _: &str, _: &str) -> bool {
            true
        }
        fn renamed_column(&mut self, _: &str, _: &str, _: &str) -> bool {
            true
        }
        fn fill(&mut self, _: &str, _: &Column) -> Result<Value, String> {
            Ok(Value::Text("x".into()))
        }
    }

    fn entity(table: Table) -> EntityTable {
        EntityTable {
            table,
            renamed_from: BTreeMap::new(),
        }
    }

    fn id() -> Column {
        Column::new("id", Type::Integer)
            .primary_key()
            .auto_increment()
    }

    #[test]
    fn new_tables_come_after_the_tables_they_refer_to_and_cycles_are_split() {
        let a = Table::new("a")
            .column(id())
            .column(Column::new("b_id", Type::Integer).null())
            .foreign_key(ForeignKey::new("fk-a-b", ["b_id"], "b", ["id"]));
        let b = Table::new("b")
            .column(id())
            .column(Column::new("a_id", Type::Integer).null())
            .foreign_key(ForeignKey::new("fk-b-a", ["a_id"], "a", ["id"]));
        let c = Table::new("c")
            .column(id())
            .column(Column::new("d_id", Type::Integer))
            .foreign_key(ForeignKey::new("fk-c-d", ["d_id"], "d", ["id"]));
        let d = Table::new("d").column(id());
        let ops = diff(
            &State::default(),
            &[entity(a), entity(b), entity(c), entity(d)],
            &mut Yes,
        )
        .unwrap();
        let order: Vec<String> = ops
            .iter()
            .map(|op| match op {
                Operation::CreateTable(t) => format!("create {}", t.name),
                Operation::AddForeignKey { key, .. } => format!("key {}", key.name),
                other => format!("{other:?}"),
            })
            .collect();
        assert_eq!(
            order,
            ["create d", "create c", "create a", "create b", "key fk-a-b"]
        );
    }

    #[test]
    fn non_interactive_answers_assume_no_rename_and_refuse_to_fill() {
        let before = State::after(&[crate::migrations::Migration::new("0001").create_table(
            Table::new("t")
                .column(id())
                .column(Column::new("old", Type::Text)),
        )])
        .unwrap();
        let after = Table::new("t")
            .column(id())
            .column(Column::new("new", Type::Text));
        let error = diff(&before, &[entity(after.clone())], &mut Answers::new(true)).unwrap_err();
        assert!(error.contains("`t.new` is required"), "{error}");
        let ops = diff(&before, &[entity(after)], &mut Yes).unwrap();
        assert!(
            matches!(&ops[..], [Operation::RenameColumn { from, to, .. }] if from == "old" && to == "new")
        );
    }

    #[test]
    fn typed_values() {
        assert_eq!(parse_value("true"), Value::Bool(true));
        assert_eq!(parse_value("12"), Value::Int(12));
        assert_eq!(parse_value("1.5"), Value::Float(1.5));
        assert_eq!(parse_value("'draft'"), Value::Text("draft".into()));
        assert_eq!(parse_value("draft"), Value::Text("draft".into()));
    }
}