runique 3.0.2

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
//! `makemigration` command — generates SeaORM migration files from `ModelSchema` DSL.
use crate::migration::*;
use crate::utils::trad::{t, tf};
use anyhow::{Context, Result};
use chrono::Utc;
use std::fs;
use std::path::Path;

pub use crate::utils::*;

// ── public parse entry points ────────────────────────────────────────────────

/// Reads a snapshot/migration file from disk and parses it into a `ParsedSchema`.
pub fn parse_create_file(path: &str) -> Result<ParsedSchema> {
    let source: String =
        fs::read_to_string(path).with_context(|| format!("Cannot read file: {}", path))?;
    parse_seaorm_source(&source).with_context(|| format!("Cannot parse: {}", path))
}

/// An `extend!{}` snapshot: the extension columns, marked as recording lengths.
fn extend_snapshot_content(ext_schema: &ParsedSchema) -> String {
    format!(
        "{SNAPSHOT_LENGTHS_MARKER}\n{}",
        generate_create_file(ext_schema)
    )
}

fn records_lengths(path: &str) -> Result<bool> {
    Ok(fs::read_to_string(path)?.starts_with(SNAPSHOT_LENGTHS_MARKER))
}

/// Snapshots written before column lengths were recorded, for tables this
/// run doesn't otherwise touch: rewritten once with the model's lengths, so
/// later length changes are diffed instead of adopted forever. No migration.
fn snapshot_upgrades(
    entities_path: &str,
    schemas: &[ParsedSchema],
    main_changes: &[Changes],
    extend_planned: &[(ParsedSchema, Changes)],
    migrations_path: &str,
) -> Result<Vec<(String, String)>> {
    let mut upgrades = Vec::new();
    for schema in schemas {
        let path = snapshot_file_path(migrations_path, &schema.table_name);
        if Path::new(&path).exists()
            && !main_changes
                .iter()
                .any(|c| c.table_name == schema.table_name)
            && !records_lengths(&path)?
        {
            upgrades.push((path, generate_snapshot_file(schema)));
        }
    }
    for ext_schema in merge_extend_schemas(scan_extend_blocks(entities_path)?) {
        let path = extend_snapshot_file_path(migrations_path, &ext_schema.table_name);
        if Path::new(&path).exists()
            && !extend_planned
                .iter()
                .any(|(s, _)| s.table_name == ext_schema.table_name)
            && !records_lengths(&path)?
        {
            upgrades.push((path, extend_snapshot_content(&ext_schema)));
        }
    }
    Ok(upgrades)
}

/// The previous snapshot of a table, to diff `current` against. A snapshot
/// written before column lengths were recorded doesn't know them: it takes
/// `current`'s, so upgrading never produces a length migration on its own.
pub(crate) fn previous_snapshot(path: &str, current: &ParsedSchema) -> Result<ParsedSchema> {
    let mut previous = parse_create_file(path)?;
    if !records_lengths(path)? {
        for col in &mut previous.columns {
            if let Some(cur) = current.columns.iter().find(|c| c.name == col.name) {
                col.max_length = cur.max_length;
            }
        }
    }
    Ok(previous)
}

// ── scan ─────────────────────────────────────────────────────────────────────

/// Tables created by `EihwazUsersMigration` + `AdminTableMigration`, never
/// migrated from the app's entities.
const FRAMEWORK_TABLES: &[&str] = &[
    "eihwaz_users",
    "eihwaz_groupes",
    "eihwaz_groupes_droits",
    "eihwaz_users_groupes",
    "eihwaz_sessions",
    "eihwaz_reset_tokens",
];

/// Scans every `.rs` file in `entities_path` for `model!{}` schemas and
/// parses each one. A `belongs_to` target is resolved the way the `model!{}`
/// macro resolves it — the entity module of that name, i.e. the file
/// `<target>.rs`, or a framework table — and its FK then references that
/// model's real table and primary key; a target that matches nothing is an
/// error. Skips the framework's own tables (`eihwaz_*`).
pub fn scan_entities(entities_path: &str) -> Result<Vec<ParsedSchema>> {
    let mut schemas = Vec::new();
    // List fields' tables: their key to the owner is already resolved.
    let mut list_schemas: Vec<ParsedSchema> = Vec::new();
    // module name → (table, primary key column)
    let mut targets: std::collections::HashMap<String, (String, String)> = FRAMEWORK_TABLES
        .iter()
        .map(|t| (t.to_string(), (t.to_string(), "id".to_string())))
        .collect();
    let mut entries: Vec<_> = fs::read_dir(entities_path)
        .with_context(|| format!("Cannot read entities directory: {}", entities_path))?
        .collect::<std::io::Result<_>>()?;
    entries.sort_by_key(|e| e.path());

    for entry in entries {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("rs") {
            continue;
        }
        let Some(module) = path.file_stem().and_then(|n| n.to_str()) else {
            continue;
        };
        if module == "mod" {
            continue;
        }

        let source = fs::read_to_string(&path)
            .with_context(|| format!("Cannot read file: {}", path.display()))?;

        if let Some(ParsedModel { schema, lists, .. }) = parse_model_from_source(&source)
            .with_context(|| format!("Invalid model!{{}} in {}", path.display()))?
        {
            let pk = schema
                .primary_key
                .as_ref()
                .map_or_else(|| "id".to_string(), |pk| pk.name.clone());
            targets.insert(module.to_string(), (schema.table_name.clone(), pk));
            // Ignore tables provided by the framework (`EihwazUsersMigration` + `AdminTableMigration`)
            if FRAMEWORK_TABLES.contains(&schema.table_name.as_str()) {
                continue;
            }
            schemas.push(schema);
            list_schemas.extend(lists);
        }
    }

    for schema in &mut schemas {
        for fk in &mut schema.foreign_keys {
            let Some((table, pk)) = targets.get(&fk.to_table) else {
                anyhow::bail!(
                    "{}: `belongs_to: {} via {}` — no model `{}` ({}/{}.rs) and no framework table of that name",
                    schema.table_name,
                    fk.to_table,
                    fk.from_column,
                    fk.to_table,
                    entities_path,
                    fk.to_table
                );
            };
            fk.to_table = table.clone();
            fk.to_column = pk.clone();
        }
    }

    schemas.extend(list_schemas);
    let mut seen = std::collections::HashSet::new();
    for schema in &schemas {
        if !seen.insert(schema.table_name.as_str()) {
            anyhow::bail!(
                "two tables are named `{}` — a list field's table is `<owner table>_<field>`: rename the field or the other table",
                schema.table_name
            );
        }
    }

    Ok(schemas)
}

// ── lib.rs updater ───────────────────────────────────────────────────────────

/// `module_name` must be in SeaORM format: `m{timestamp}_create_{table}_table`
pub fn update_migration_lib(migrations_path: &str, module_name: &str) -> Result<()> {
    let lib = lib_path(migrations_path);
    let box_entry = format!("Box::new({}::Migration)", module_name);

    let mut state = if Path::new(&lib).exists() {
        parse_lib_state(&fs::read_to_string(&lib)?)
    } else {
        LibState::default()
    };

    // Runs whether `lib.rs` pre-existed or not: `sea-orm-cli migrate init` always
    // leaves this exact placeholder wired in (its `up()`/`down()` are both a bare
    // `todo!()`, which panics the first time any migration actually runs).
    strip_sea_orm_cli_placeholder_file(migrations_path);

    if state.mods.contains(&module_name.to_string()) {
        return Ok(());
    }
    state.mods.push(module_name.to_string());
    state
        .entries
        .retain(|e| !e.contains(SEA_ORM_CLI_PLACEHOLDER_MODULE));
    state.entries.push(box_entry);

    fs::write(&lib, render_lib(&state))?;
    Ok(())
}

// ── lib.rs canonical rewriting ───────────────────────────────────────────────
//
// `sea-orm-cli migrate init` is the documented first step to bootstrap the
// `migration` crate (Cargo.toml + main.rs) before ever running `makemigrations`
// — its own generated `lib.rs` uses a different shape than Runique's (single-line
// `vec![Box::new(X::Migration)]`, `pub use sea_orm_migration::prelude::*;`) that
// broke the old line-based `.replacen(...)` patching: the `vec![` search matched
// but inserted new entries *after* the whole (already-closed) vec statement
// instead of inside it, and framework migrations landed outside `Migrator::migrations()`
// entirely — syntactically invalid output. Parsing into a small in-memory model
// and always re-rendering canonically sidesteps every formatting assumption.

/// Literal placeholder module name `sea-orm-cli migrate init` always generates
/// (a fixed string, not templated with a timestamp — confirmed by direct testing).
const SEA_ORM_CLI_PLACEHOLDER_MODULE: &str = "m20220101_000001_create_table";

/// Parsed, formatting-independent view of `Migrator::migrations()`'s `lib.rs`.
#[derive(Default)]
struct LibState {
    uses_migrations_table: bool,
    mods: Vec<String>,
    entries: Vec<String>,
}

/// Finds the byte range of the `vec![ ... ]` inside `Migrator::migrations()`,
/// by bracket-depth balance from the first `vec![` — robust whether it's
/// Runique's own multi-line output or a single-line `vec![Box::new(...)]`.
fn find_vec_span(content: &str) -> Option<(usize, usize)> {
    let start = content.find("vec![")?;
    let open = start + "vec![".len() - 1;
    let mut depth = 0i32;
    for (i, ch) in content[open..].char_indices() {
        match ch {
            '[' => depth += 1,
            ']' => {
                depth -= 1;
                if depth == 0 {
                    return Some((open, open + i + 1));
                }
            }
            _ => {}
        }
    }
    None
}

/// Extracts each `Box::new(...)` entry inside a `vec![...]` span, in order —
/// works regardless of the original line layout (one entry per line, several
/// per line, or all on a single line).
fn extract_box_entries(vec_content: &str) -> Vec<String> {
    let mut entries = Vec::new();
    let mut rest = vec_content;
    while let Some(start) = rest.find("Box::new(") {
        let after = &rest[start + "Box::new(".len()..];
        let Some(end) = after.find(')') else { break };
        entries.push(format!("Box::new({})", &after[..end]));
        rest = &after[end + 1..];
    }
    entries
}

fn parse_lib_state(content: &str) -> LibState {
    let uses_migrations_table = content.contains("migrations_table");
    let mut mods = Vec::new();
    for line in content.lines() {
        if let Some(rest) = line.trim().strip_prefix("mod ")
            && let Some(name) = rest.strip_suffix(';')
            && name != SEA_ORM_CLI_PLACEHOLDER_MODULE
            && !mods.contains(&name.to_string())
        {
            mods.push(name.to_string());
        }
    }
    let entries = match find_vec_span(content) {
        Some((s, e)) => extract_box_entries(&content[s..e])
            .into_iter()
            .filter(|entry| !entry.contains(SEA_ORM_CLI_PLACEHOLDER_MODULE))
            .collect(),
        None => Vec::new(),
    };
    LibState {
        uses_migrations_table,
        mods,
        entries,
    }
}

fn render_lib(state: &LibState) -> String {
    let mut out = String::new();
    if state.uses_migrations_table {
        out.push_str("use runique::prelude::migrations_table;\n");
    }
    out.push_str("use sea_orm_migration::prelude::*;\n");
    for m in &state.mods {
        out.push_str(&format!("mod {};\n", m));
    }
    out.push('\n');
    out.push_str("pub struct Migrator;\n\n");
    out.push_str("#[async_trait::async_trait]\n");
    out.push_str("impl MigratorTrait for Migrator {\n");
    out.push_str("    fn migrations() -> Vec<Box<dyn MigrationTrait>> {\n        ");
    if state.entries.is_empty() {
        out.push_str("vec![]");
    } else {
        out.push_str("vec![\n");
        for e in &state.entries {
            out.push_str("            ");
            out.push_str(e);
            out.push_str(",\n");
        }
        out.push_str("        ]");
    }
    out.push_str("\n    }\n}\n");
    out
}

/// Deletes the `sea-orm-cli migrate init` placeholder migration file, if present.
fn strip_sea_orm_cli_placeholder_file(migrations_path: &str) {
    let placeholder_file = format!("{}/{}.rs", migrations_path, SEA_ORM_CLI_PLACEHOLDER_MODULE);
    if Path::new(&placeholder_file).exists() {
        let _ = fs::remove_file(&placeholder_file);
    }
}

// ── topological sort ─────────────────────────────────────────────────────────

/// Sorts `Changes` by FK dependency order: a new table comes after the new
/// tables it references, ties broken by name so the order never depends on a
/// hash map. A cycle of new tables is broken at its smallest name; the foreign
/// keys left pointing forward are added once their target exists (see
/// `build_main_plan`). Changes to existing tables follow, by name.
pub(crate) fn topological_sort_changes(
    changes: Vec<crate::migration::utils::types::Changes>,
) -> Vec<crate::migration::utils::types::Changes> {
    use std::collections::{BTreeMap, BTreeSet};

    let new_tables: BTreeSet<String> = changes
        .iter()
        .filter(|c| c.is_new_table)
        .map(|c| c.table_name.clone())
        .collect();

    // waits_for[A] = new tables A references (not yet placed)
    let mut waits_for: BTreeMap<String, BTreeSet<String>> = new_tables
        .iter()
        .map(|t| (t.clone(), BTreeSet::new()))
        .collect();
    for change in changes.iter().filter(|c| c.is_new_table) {
        for fk in &change.added_fks {
            if new_tables.contains(&fk.to_table) && fk.to_table != change.table_name {
                waits_for
                    .entry(change.table_name.clone())
                    .or_default()
                    .insert(fk.to_table.clone());
            }
        }
    }

    let mut sorted_names: Vec<String> = Vec::with_capacity(new_tables.len());
    while !waits_for.is_empty() {
        let next = waits_for
            .iter()
            .find(|(_, deps)| deps.is_empty())
            .or_else(|| waits_for.iter().next())
            .map(|(t, _)| t.clone())
            .expect("waits_for is not empty");
        waits_for.remove(&next);
        for deps in waits_for.values_mut() {
            deps.remove(&next);
        }
        sorted_names.push(next);
    }

    let mut by_name: BTreeMap<String, crate::migration::utils::types::Changes> = changes
        .into_iter()
        .map(|c| (c.table_name.clone(), c))
        .collect();
    let mut result: Vec<crate::migration::utils::types::Changes> =
        Vec::with_capacity(by_name.len());
    for name in sorted_names {
        if let Some(c) = by_name.remove(&name) {
            result.push(c);
        }
    }
    result.extend(by_name.into_values());
    result
}

// ── destructive change guard ─────────────────────────────────────────────────

/// Scans a set of changes for destructive operations (dropped columns, type
/// changes, nullable-to-required, dropped FKs, new `ON DELETE CASCADE` on an
/// existing table) and returns one human-readable warning per finding. An
/// empty result means the changes are safe to apply without `--force`.
pub fn collect_destructive_messages(all_changes: &[Changes]) -> Vec<String> {
    let dropped = all_changes.iter().flat_map(|c| {
        c.dropped_columns
            .iter()
            .map(|col| format!("  {}.{}: DROP COLUMN (data loss)", c.table_name, col.name))
    });

    let type_changes = all_changes.iter().flat_map(|c| {
        c.modified_columns
            .iter()
            // `col_type` alone misses becoming (or stopping being) an enum — an enum
            // column keeps col_type "String", the shape change lives in
            // `enum_string_values` instead. Without this, a string->enum column would
            // generate silently (no `--force` prompt) with only a code comment warning
            // the developer might never see.
            .filter(|(old, new)| {
                old.col_type != new.col_type
                    || old.enum_string_values.is_empty() != new.enum_string_values.is_empty()
            })
            .map(|(old, new)| {
                if old.col_type != new.col_type {
                    format!(
                        "  {}.{}: type {} -> {}",
                        c.table_name, old.name, old.col_type, new.col_type
                    )
                } else if new.enum_string_values.is_empty() {
                    format!(
                        "  {}.{}: enum -> {} (manual migration required)",
                        c.table_name, old.name, new.col_type
                    )
                } else {
                    format!(
                        "  {}.{}: {} -> enum (manual migration required)",
                        c.table_name, old.name, old.col_type
                    )
                }
            })
    });

    let nullable_to_required = all_changes.iter().flat_map(|c| {
        c.modified_columns
            .iter()
            .filter(|(old, new)| old.nullable && !new.nullable && old.col_type == new.col_type)
            .map(|(_, new)| {
                format!(
                    "  {}.{}: nullable -> not_null (requires a default or backfill)",
                    c.table_name, new.name
                )
            })
    });

    let length_shrinks = all_changes.iter().flat_map(|c| {
        c.modified_columns
            .iter()
            .filter(|(old, new)| {
                old.col_type == new.col_type && length_may_shrink(old.max_length, new.max_length)
            })
            .map(|(old, new)| {
                let len =
                    |l: Option<u32>| l.map_or_else(|| "default".to_string(), |n| n.to_string());
                format!(
                    "  {}.{}: length {} -> {} (longer values would be cut or refused)",
                    c.table_name,
                    new.name,
                    len(old.max_length),
                    len(new.max_length)
                )
            })
    });

    let dropped_fks = all_changes.iter().flat_map(|c| {
        c.dropped_fks.iter().map(|fk| {
            format!(
                "  {}.{}: DROP FOREIGN KEY -> {} (orphan records possible)",
                c.table_name, fk.from_column, fk.to_table
            )
        })
    });

    // Adding a CASCADE constraint to existing data can trigger mass deletes if a parent is removed.
    // Only a concern on existing tables: a table created in this same batch has no rows yet,
    // so a CASCADE FK on a brand-new table carries no data-loss risk (avoids a false positive
    // on first generation / from-scratch regeneration).
    let cascade_fks = all_changes
        .iter()
        .filter(|c| !c.is_new_table)
        .flat_map(|c| {
            c.added_fks
                .iter()
                .filter(|fk| fk.on_delete.to_uppercase() == "CASCADE")
                .map(|fk| {
                    format!(
                        "  {}.{}: ADD FOREIGN KEY -> {} ON DELETE CASCADE (existing rows may be deleted)",
                        c.table_name, fk.from_column, fk.to_table
                    )
                })
        });

    dropped
        .chain(type_changes)
        .chain(nullable_to_required)
        .chain(length_shrinks)
        .chain(dropped_fks)
        .chain(cascade_fks)
        .collect()
}

/// Whether going from `old` to `new` column length can drop data on some
/// engine. No length is unbounded on Postgres but `VARCHAR(255)` on MySQL, so
/// both directions out of "no length" are checked against each.
fn length_may_shrink(old: Option<u32>, new: Option<u32>) -> bool {
    match (old, new) {
        (Some(old), Some(new)) => new < old,
        (None, Some(_)) => true,
        (Some(old), None) => old > 255,
        (None, None) => false,
    }
}

/// Prints `messages` under the translated `header_key`, then bails with the translated
/// `bail_key` — the shared "list every violation, then stop" shape behind both
/// `check_destructive` (forceable) and `check_identifier_lengths` (never forceable, always
/// passes `force: false`). An empty `messages` or `force: true` is a silent no-op.
fn report_and_bail_if_any(
    messages: &[String],
    header_key: &str,
    bail_key: &str,
    force: bool,
) -> Result<()> {
    if messages.is_empty() || force {
        return Ok(());
    }

    eprintln!("\n{}", t(header_key));
    for msg in messages {
        eprintln!("{}", msg);
    }
    anyhow::bail!("{}", t(bail_key));
}

fn check_destructive(all_changes: &[Changes], force: bool) -> Result<()> {
    let blocking = collect_destructive_messages(all_changes);
    report_and_bail_if_any(
        &blocking,
        "makemigrations.destructive_detected",
        "makemigrations.destructive_require_force",
        force,
    )
}

// ── generated-identifier length guard ──────────────────────────────────────────

/// Scans a set of changes for FK constraint / index names that would exceed
/// MariaDB/MySQL's 64-character identifier limit (63 used here as the shared safe bound —
/// Postgres's own NAMEDATALEN-1 limit is one byte tighter). Checked once, upfront, over
/// the whole plan — before any migration file is generated — so the string-building
/// generators (`generators.rs`) and the DSL-to-schema conversion (`to_schema.rs`) stay
/// pure and infallible. There is no `--force` escape hatch here, unlike
/// `check_destructive`: Runique won't guess a shortened name for you (that would make it
/// unpredictable from the `model!{}` declaration alone), so a human has to rename
/// something regardless.
fn collect_long_identifier_messages(all_changes: &[Changes]) -> Vec<String> {
    const MAX_LEN: usize = 63;
    let mut messages = Vec::new();

    for change in all_changes {
        for fk in change.added_fks.iter().chain(change.dropped_fks.iter()) {
            let name = format!(
                "{}_{}_{}_fkey",
                change.table_name, fk.from_column, fk.to_table
            );
            if name.len() > MAX_LEN {
                messages.push(format!(
                    "  {name} ({len} characters, max {MAX_LEN}) — shorten the table name, '{col}', or the target table name",
                    len = name.len(),
                    col = fk.from_column,
                ));
            }
        }
        for idx in change
            .added_indexes
            .iter()
            .chain(change.dropped_indexes.iter())
        {
            if idx.name.len() > MAX_LEN {
                messages.push(format!(
                    "  {} ({} characters, max {MAX_LEN}) — shorten the table or column names in this index",
                    idx.name,
                    idx.name.len(),
                ));
            }
        }
    }
    messages
}

fn check_identifier_lengths(all_changes: &[Changes]) -> Result<()> {
    let long_ids = collect_long_identifier_messages(all_changes);
    report_and_bail_if_any(
        &long_ids,
        "makemigrations.long_identifier_detected",
        "makemigrations.long_identifier_rename_required",
        false,
    )
}

// ── run ──────────────────────────────────────────────────────────────────────
/// Builds the SeaORM module name for an ALTER migration on `table` (`m{timestamp}_alter_{table}_table`).
pub fn seaorm_alter_module_name(timestamp: &str, table: &str) -> String {
    format!("m{}_alter_{}_table", timestamp, table)
}

/// Builds the file path of the SeaORM ALTER migration for `table`.
pub fn seaorm_alter_file_path(migrations_path: &str, timestamp: &str, table: &str) -> String {
    format!(
        "{}/{}.rs",
        migrations_path,
        seaorm_alter_module_name(timestamp, table)
    )
}

/// Builds the SeaORM module name for an `extend!{}` migration on `table` (`m{timestamp}_extend_{table}_table`).
pub fn seaorm_extend_module_name(timestamp: &str, table: &str) -> String {
    format!("m{}_extend_{}_table", timestamp, table)
}

/// Builds the file path of the SeaORM `extend!{}` migration for `table`.
pub fn seaorm_extend_file_path(migrations_path: &str, timestamp: &str, table: &str) -> String {
    format!(
        "{}/m{}_extend_{}_table.rs",
        migrations_path, timestamp, table
    )
}

// ── scan extend blocks ────────────────────────────────────────────────────────

/// Scans all `.rs` files in the `entities_path` directory and collects
/// all found `extend!{}` blocks. Returns a flat list of `ParsedSchema`
/// (one per block — multiple blocks can target the same table).
pub fn scan_extend_blocks(entities_path: &str) -> Result<Vec<ParsedSchema>> {
    let mut schemas = Vec::new();
    let entries = fs::read_dir(entities_path)
        .with_context(|| format!("Cannot read entities directory: {}", entities_path))?;

    for entry in entries {
        let entry = entry?;
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("rs") {
            continue;
        }
        if path.file_name().and_then(|n| n.to_str()) == Some("mod.rs") {
            continue;
        }

        let source = fs::read_to_string(&path)
            .with_context(|| format!("Cannot read file: {}", path.display()))?;

        let blocks = parse_extend_blocks_from_source(&source)
            .with_context(|| format!("Invalid extend!{{}} in {}", path.display()))?;
        for schema in blocks {
            schemas.push(schema);
        }
    }
    Ok(schemas)
}

/// Merges `extend!{}` blocks targeting the same table into a single `ParsedSchema`.
/// Columns are concatenated in discovery order.
pub fn merge_extend_schemas(schemas: Vec<ParsedSchema>) -> Vec<ParsedSchema> {
    use std::collections::HashMap;
    let mut by_table: HashMap<String, Vec<ParsedColumn>> = HashMap::new();
    let mut order: Vec<String> = Vec::new();

    for schema in schemas {
        if !by_table.contains_key(&schema.table_name) {
            order.push(schema.table_name.clone());
            by_table.insert(schema.table_name.clone(), Vec::new());
        }
        by_table
            .get_mut(&schema.table_name)
            .unwrap()
            .extend(schema.columns);
    }

    order
        .into_iter()
        .map(|table_name| {
            let columns = by_table.remove(&table_name).unwrap_or_default();
            ParsedSchema {
                table_name,
                primary_key: None,
                columns,
                foreign_keys: Vec::new(),
                indexes: Vec::new(),
            }
        })
        .collect()
}
/// Entry point of the `makemigrations` command: scans models and `extend!{}`
/// blocks, diffs them against their last snapshot, blocks on destructive
/// changes unless `force` is set, then writes the full migration plan atomically.
pub fn run(entities_path: &str, migrations_path: &str, force: bool) -> Result<()> {
    let schemas = scan_entities(entities_path)?;

    fs::create_dir_all(snapshot_dir(migrations_path))?;

    // ── Plan everything up front — nothing is written until the full plan
    //    (main models + extend!{} blocks) is computed and validated.
    let mut main_changes = compute_main_changes(&schemas, migrations_path)?;
    let extend_planned = plan_extend_changes(entities_path, migrations_path)?;

    let upgrades = snapshot_upgrades(
        entities_path,
        &schemas,
        &main_changes,
        &extend_planned,
        migrations_path,
    )?;

    if main_changes.is_empty() && extend_planned.is_empty() {
        for (path, content) in &upgrades {
            fs::write(path, content)?;
        }
        // A project without a model of its own still needs the framework's
        // tables (accounts, sessions): wired into `lib.rs` even then, and the
        // `sea-orm-cli migrate init` placeholder dropped.
        ensure_admin_migration_positioned(migrations_path)?;
        if !upgrades.is_empty() {
            println!(
                "{}",
                tf("makemigrations.snapshots_upgraded", &[upgrades.len()])
            );
        }
        return Ok(());
    }

    // ── Single destructive guard over main + extend (honors --force) ──────
    let mut destructive_set: Vec<Changes> = main_changes.clone();
    destructive_set.extend(extend_planned.iter().map(|(_, c)| c.clone()));
    check_destructive(&destructive_set, force)?;
    check_identifier_lengths(&destructive_set)?;

    let timestamp = Utc::now().format("%Y%m%d_%H%M%S").to_string();

    // referenced tables created before those referencing them
    main_changes = topological_sort_changes(main_changes);

    // ── Build a single unified plan (no writing) ──────────────────────────
    let mut plan = Plan::default();
    build_main_plan(
        &mut plan,
        &main_changes,
        &schemas,
        migrations_path,
        &timestamp,
    );
    build_extend_plan(&mut plan, &extend_planned, migrations_path, &timestamp);
    plan.files.extend(upgrades);

    // ── One atomic commit: dirs → backups → write → lib.rs → admin positioning,
    //    with a single rollback covering all of it.
    let module_count = plan.lib_modules.len();
    commit_plan(&plan, migrations_path)?;

    println!("{}", tf("makemigrations.files_ready", &[module_count]));

    Ok(())
}

// ── unified plan ───────────────────────────────────────────────────────────────

/// A fully-computed migration plan: everything to create/write/register, no side effects.
/// Built before any IO so the destructive guard and a single atomic commit can run on it.
#[derive(Default)]
struct Plan {
    /// (path, content) files to write
    files: Vec<(String, String)>,
    /// directories to create before writing
    dirs: Vec<String>,
    /// migration modules to register in `lib.rs`, in order
    lib_modules: Vec<String>,
}

/// Computes the diff for every scanned model (no writing).
fn compute_main_changes(schemas: &[ParsedSchema], migrations_path: &str) -> Result<Vec<Changes>> {
    let mut all_changes: Vec<Changes> = Vec::new();
    for schema in schemas {
        let snap_path = snapshot_file_path(migrations_path, &schema.table_name);
        let changes = if Path::new(&snap_path).exists() {
            let previous = previous_snapshot(&snap_path, schema)?;
            diff_schemas(&previous, schema)
        } else {
            Changes {
                table_name: schema.table_name.clone(),
                added_columns: db_columns(schema).into_iter().cloned().collect(),
                dropped_columns: vec![],
                modified_columns: vec![],
                renamed_columns: vec![],
                added_fks: schema.foreign_keys.clone(),
                dropped_fks: vec![],
                added_indexes: schema.indexes.clone(),
                dropped_indexes: vec![],
                is_new_table: true,
                enum_renames: vec![],
                enum_value_adds: vec![],
                enum_value_drops: vec![],
            }
        };
        if !changes.is_empty() {
            all_changes.push(changes);
        }
    }
    Ok(all_changes)
}

/// For each new table, the foreign keys of an FK cycle: keys to a new table
/// created later in this batch (`forward`), and keys pointing to it from a
/// table created earlier (`closing`). `all_changes` is in creation order.
fn cycle_keys<'a>(
    all_changes: &[Changes],
    schemas: &'a [ParsedSchema],
) -> std::collections::HashMap<&'a str, CycleKeys<'a>> {
    use std::collections::{HashMap, HashSet};

    let new_tables: HashSet<&str> = all_changes
        .iter()
        .filter(|c| c.is_new_table)
        .map(|c| c.table_name.as_str())
        .collect();
    let mut created: HashSet<&str> = HashSet::new();
    let mut keys: HashMap<&'a str, CycleKeys<'a>> = HashMap::new();
    for change in all_changes.iter().filter(|c| c.is_new_table) {
        let Some(schema) = schemas.iter().find(|s| s.table_name == change.table_name) else {
            continue;
        };
        let table = schema.table_name.as_str();
        for fk in &schema.foreign_keys {
            let target = fk.to_table.as_str();
            if target != table && new_tables.contains(target) && !created.contains(target) {
                keys.entry(table).or_default().forward.push(fk);
                if let Some(target) = schemas.iter().find(|s| s.table_name == target) {
                    keys.entry(target.table_name.as_str())
                        .or_default()
                        .closing
                        .push((table, fk));
                }
            }
        }
        created.insert(table);
    }
    keys
}

/// Adds the main-model migration files/dirs/modules to the plan.
fn build_main_plan(
    plan: &mut Plan,
    all_changes: &[Changes],
    schemas: &[ParsedSchema],
    migrations_path: &str,
    timestamp: &str,
) {
    let cycle_keys = cycle_keys(all_changes, schemas);
    let no_cycle = CycleKeys::default();
    for change in all_changes {
        let schema = schemas
            .iter()
            .find(|s| s.table_name == change.table_name)
            .unwrap();

        // Snapshot — includes FK stmts so future diffs detect FK additions/removals.
        plan.files.push((
            snapshot_file_path(migrations_path, &change.table_name),
            generate_snapshot_file(schema),
        ));

        if change.is_new_table {
            let module_name = seaorm_create_module_name(timestamp, &change.table_name);
            let seaorm_path =
                seaorm_create_file_path(migrations_path, timestamp, &change.table_name);
            let keys = cycle_keys
                .get(change.table_name.as_str())
                .unwrap_or(&no_cycle);
            plan.files
                .push((seaorm_path, generate_create_file_in_cycle(schema, keys)));
            plan.lib_modules.push(module_name);
        } else {
            let module_name = seaorm_alter_module_name(timestamp, &change.table_name);
            let seaorm_path =
                seaorm_alter_file_path(migrations_path, timestamp, &change.table_name);
            plan.files.push((seaorm_path, generate_alter_file(change)));
            plan.lib_modules.push(module_name);
        }
    }
}

/// Adds the extend!{} migration files/dirs/modules to the plan.
fn build_extend_plan(
    plan: &mut Plan,
    planned: &[(ParsedSchema, Changes)],
    migrations_path: &str,
    timestamp: &str,
) {
    if planned.is_empty() {
        return;
    }
    plan.dirs.push(extend_snapshot_dir(migrations_path));

    for (ext_schema, changes) in planned {
        // Snapshot updated (without PK, just extension columns)
        plan.files.push((
            extend_snapshot_file_path(migrations_path, &ext_schema.table_name),
            extend_snapshot_content(ext_schema),
        ));

        let module_name = seaorm_extend_module_name(timestamp, &ext_schema.table_name);
        let seaorm_path =
            seaorm_extend_file_path(migrations_path, timestamp, &ext_schema.table_name);
        plan.files.push((seaorm_path, generate_alter_file(changes)));
        plan.lib_modules.push(module_name);
    }
}

/// Writes the whole plan atomically: create dirs, back up existing targets, write files,
/// register lib.rs modules, position AdminTableMigration — all under a single rollback.
fn commit_plan(plan: &Plan, migrations_path: &str) -> Result<()> {
    // Directory creation (idempotent)
    for dir in &plan.dirs {
        fs::create_dir_all(dir)?;
    }

    // lib.rs backup for rollback in case of partial error.
    let lib_file = lib_path(migrations_path);
    let lib_backup: Option<String> = if Path::new(&lib_file).exists() {
        Some(fs::read_to_string(&lib_file)?)
    } else {
        None
    };

    // Back up the previous content of any target that already exists (snapshots especially),
    // so the rollback restores it instead of deleting it — a deleted snapshot would make the
    // next run regenerate a full CREATE for an already-migrated table.
    let mut file_backups: StrMap = StrMap::new();
    for (path, _) in &plan.files {
        if Path::new(path).exists()
            && let Ok(prev) = fs::read_to_string(path)
        {
            file_backups.insert(path.clone(), prev);
        }
    }

    let mut written: Vec<String> = Vec::new();
    let write_result: Result<()> = (|| {
        for (path, content) in &plan.files {
            fs::write(path, content).with_context(|| format!("Failed to write: {}", path))?;
            written.push(path.clone());
        }
        for module_name in &plan.lib_modules {
            update_migration_lib(migrations_path, module_name)?;
        }
        // AdminTableMigration positioning rewrites lib.rs — kept inside the protected
        // scope so a failure here also triggers the rollback below.
        ensure_admin_migration_positioned(migrations_path)?;
        Ok(())
    })();

    if let Err(e) = write_result {
        eprintln!(
            "\n[makemigrations] Error: {}. Rollback generated files...",
            e
        );
        for path in &written {
            match file_backups.get(path) {
                Some(prev) => {
                    if let Err(re) = fs::write(path, prev) {
                        eprintln!("  warning: cannot restore {} : {}", path, re);
                    } else {
                        eprintln!("  restored: {}", path);
                    }
                }
                None => {
                    if let Err(re) = fs::remove_file(path) {
                        eprintln!("  warning: cannot delete {} : {}", path, re);
                    } else {
                        eprintln!("  deleted: {}", path);
                    }
                }
            }
        }
        match lib_backup {
            Some(content) => {
                let _ = fs::write(&lib_file, content);
                eprintln!("  lib.rs restored");
            }
            None => {
                let _ = fs::remove_file(&lib_file);
            }
        }
        return Err(e);
    }

    Ok(())
}

// ── AdminTableMigration positioning ───────────────────────────────────────

/// Puts the framework migrations (`eihwaz_users`, sessions, admin tables,
/// reset tokens) at the top of `lib.rs`, before the app's own.
///
/// - Adds `use runique::prelude::migrations_table;` if missing
/// - Drops app migrations that would recreate a framework table
/// - No effect when `lib.rs` doesn't exist yet
pub fn ensure_admin_migration_positioned(migrations_path: &str) -> Result<()> {
    let lib_file = lib_path(migrations_path);

    if !Path::new(&lib_file).exists() {
        return Ok(());
    }

    let content = fs::read_to_string(&lib_file)?;
    strip_sea_orm_cli_placeholder_file(migrations_path);
    let mut state = parse_lib_state(&content);
    state.uses_migrations_table = true;

    let admin_box = "Box::new(migrations_table::AdminTableMigration)".to_string();
    let sessions_box = "Box::new(migrations_table::EihwazSessionsMigration)".to_string();
    let reset_box = "Box::new(migrations_table::EihwazResetTokensMigration)".to_string();
    let users_box = "Box::new(migrations_table::EihwazUsersMigration)".to_string();

    // Tables created by `EihwazUsersMigration` + `EihwazSessionsMigration` + `AdminTableMigration` — to exclude from the vec
    const FRAMEWORK_TABLE_PATTERNS: &[&str] = &[
        "create_eihwaz_users_table",
        "create_eihwaz_groupes_table",
        "create_eihwaz_groupes_droits_table",
        "create_eihwaz_users_groupes_table",
        "create_eihwaz_sessions_table",
        "create_eihwaz_reset_tokens_table",
    ];

    // Remove existing framework entries/mods (we'll re-inject at the top)
    // and also drop app migrations duplicating framework tables.
    state
        .entries
        .retain(|e| e != &users_box && e != &sessions_box && e != &reset_box && e != &admin_box);
    state
        .mods
        .retain(|m| !FRAMEWORK_TABLE_PATTERNS.iter().any(|pat| m.contains(pat)));
    state.entries.splice(
        0..0,
        [
            users_box.clone(),
            sessions_box.clone(),
            admin_box.clone(),
            reset_box.clone(),
        ],
    );

    let result = render_lib(&state);
    if result != content {
        fs::write(&lib_file, &result)?;
    }

    Ok(())
}

// ── Extend pass (planning) ─────────────────────────────────────────────────────

/// Scans + merges `extend!{}` blocks and computes their diffs (no writing).
/// Returns the owned schema + change pair for each table that actually changed.
fn plan_extend_changes(
    entities_path: &str,
    migrations_path: &str,
) -> Result<Vec<(ParsedSchema, Changes)>> {
    let raw_extends = scan_extend_blocks(entities_path)?;
    if raw_extends.is_empty() {
        return Ok(Vec::new());
    }

    let extend_schemas = merge_extend_schemas(raw_extends);

    let mut planned: Vec<(ParsedSchema, Changes)> = Vec::new();
    for ext_schema in extend_schemas {
        let snap_path = extend_snapshot_file_path(migrations_path, &ext_schema.table_name);

        let changes = if Path::new(&snap_path).exists() {
            let previous = previous_snapshot(&snap_path, &ext_schema)?;
            diff_schemas(&previous, &ext_schema)
        } else {
            // First time — all columns are new (ADD COLUMN)
            Changes {
                table_name: ext_schema.table_name.clone(),
                added_columns: ext_schema.columns.clone(),
                dropped_columns: vec![],
                modified_columns: vec![],
                renamed_columns: vec![],
                added_fks: vec![],
                dropped_fks: vec![],
                added_indexes: vec![],
                dropped_indexes: vec![],
                is_new_table: false, // always false — the framework table already exists
                enum_renames: vec![],
                enum_value_adds: vec![],
                enum_value_drops: vec![],
            }
        };

        if changes.is_empty() {
            continue;
        }
        planned.push((ext_schema, changes));
    }

    Ok(planned)
}

/// Written from cargo-mutants survivors (2026-10-08): the private steps of
/// `run` — snapshot upgrades, lib.rs editing, ordering, guards.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::migration::utils::types::{ParsedColumn, ParsedFk};

    fn temp(tag: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!("rq_mk_{tag}_{}", uuid::Uuid::new_v4()));
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn schema(table: &str, fks: &[&str]) -> ParsedSchema {
        ParsedSchema {
            table_name: table.to_string(),
            primary_key: Some(ParsedColumn {
                name: "id".into(),
                col_type: "Integer".into(),
                ..ParsedColumn::default()
            }),
            columns: vec![ParsedColumn {
                name: "title".into(),
                col_type: "String".into(),
                max_length: Some(50),
                ..ParsedColumn::default()
            }],
            foreign_keys: fks
                .iter()
                .map(|to| ParsedFk {
                    from_column: format!("{to}_id"),
                    to_table: (*to).to_string(),
                    to_column: "id".into(),
                    on_delete: "Cascade".into(),
                    on_update: "NoAction".into(),
                })
                .collect(),
            indexes: vec![],
        }
    }

    fn new_table(s: &ParsedSchema) -> Changes {
        Changes {
            table_name: s.table_name.clone(),
            added_columns: s.columns.clone(),
            dropped_columns: vec![],
            modified_columns: vec![],
            renamed_columns: vec![],
            added_fks: s.foreign_keys.clone(),
            dropped_fks: vec![],
            added_indexes: vec![],
            dropped_indexes: vec![],
            is_new_table: true,
            enum_renames: vec![],
            enum_value_adds: vec![],
            enum_value_drops: vec![],
        }
    }

    #[test]
    fn an_extend_snapshot_is_marked_as_recording_lengths() {
        let content = extend_snapshot_content(&schema("eihwaz_users", &[]));
        assert!(content.starts_with(SNAPSHOT_LENGTHS_MARKER), "{content}");
        assert!(content.contains("create_table"), "{content}");
    }

    /// Only an old-format snapshot of a table this run doesn't touch is rewritten.
    #[test]
    fn only_untouched_old_snapshots_are_upgraded() {
        let (entities, migrations) = (temp("ent"), temp("mig"));
        let (e, m) = (entities.to_str().unwrap(), migrations.to_str().unwrap());
        fs::create_dir_all(snapshot_dir(m)).unwrap();
        let tables = [
            schema("old_untouched", &[]),
            schema("old_changed", &[]),
            schema("recorded", &[]),
        ];
        fs::write(snapshot_file_path(m, "old_untouched"), "// old").unwrap();
        fs::write(snapshot_file_path(m, "old_changed"), "// old").unwrap();
        fs::write(
            snapshot_file_path(m, "recorded"),
            generate_snapshot_file(&tables[2]),
        )
        .unwrap();
        // `extend!{}` side: an old snapshot of an extension nobody re-planned.
        fs::write(
            entities.join("ext.rs"),
            r#"extend! { table: "eihwaz_users", fields: { bio: text } }"#,
        )
        .unwrap();
        fs::create_dir_all(extend_snapshot_dir(m)).unwrap();
        fs::write(extend_snapshot_file_path(m, "eihwaz_users"), "// old").unwrap();

        let changed = [new_table(&tables[1])];
        let upgrades = snapshot_upgrades(e, &tables, &changed, &[], m).unwrap();
        let paths: Vec<&str> = upgrades.iter().map(|(p, _)| p.as_str()).collect();
        assert_eq!(
            paths,
            [
                snapshot_file_path(m, "old_untouched").as_str(),
                extend_snapshot_file_path(m, "eihwaz_users").as_str()
            ]
        );
        assert!(upgrades[1].1.starts_with(SNAPSHOT_LENGTHS_MARKER));

        // Re-planned extension: left to its own migration.
        let ext = merge_extend_schemas(scan_extend_blocks(e).unwrap()).remove(0);
        let planned = [(ext.clone(), new_table(&ext))];
        let upgrades = snapshot_upgrades(e, &tables, &changed, &planned, m).unwrap();
        assert_eq!(upgrades.len(), 1);
        let _ = (
            fs::remove_dir_all(&entities),
            fs::remove_dir_all(&migrations),
        );
    }

    /// The `vec![...]` of lib.rs is found by bracket depth and read entry by
    /// entry, whatever the layout.
    #[test]
    fn lib_entries_are_read_whatever_the_layout() {
        let content = "fn m() -> Vec<X> { vec![Box::new(a::Migration), Box::new(b::Migration)] }";
        let (s, e) = find_vec_span(content).unwrap();
        assert_eq!(
            &content[s..e],
            "[Box::new(a::Migration), Box::new(b::Migration)]"
        );
        assert_eq!(
            extract_box_entries(&content[s..e]),
            ["Box::new(a::Migration)", "Box::new(b::Migration)"]
        );
        assert_eq!(find_vec_span("no list here"), None);
    }

    /// No model of the project's own yet: the framework's migrations still
    /// land in the `lib.rs` that `sea-orm-cli migrate init` wrote, its
    /// placeholder gone — otherwise `migrate up` doesn't even compile.
    #[test]
    fn without_any_model_the_framework_migrations_are_wired_in() {
        let (entities, migrations) = (temp("ent_none"), temp("mig_none"));
        let (e, m) = (entities.to_str().unwrap(), migrations.to_str().unwrap());
        fs::write(
            migrations.join("lib.rs"),
            "pub use sea_orm_migration::prelude::*;\n\nmod m20220101_000001_create_table;\n\n\
             pub struct Migrator;\n\n#[async_trait::async_trait]\nimpl MigratorTrait for Migrator {\n    \
             fn migrations() -> Vec<Box<dyn MigrationTrait>> {\n        \
             vec![Box::new(m20220101_000001_create_table::Migration)]\n    }\n}\n",
        )
        .unwrap();
        fs::write(
            migrations.join("m20220101_000001_create_table.rs"),
            "// placeholder",
        )
        .unwrap();

        run(e, m, false).unwrap();
        let lib = fs::read_to_string(migrations.join("lib.rs")).unwrap();
        let placeholder_file = migrations.join("m20220101_000001_create_table.rs").exists();
        let _ = (
            fs::remove_dir_all(&entities),
            fs::remove_dir_all(&migrations),
        );

        assert!(
            lib.contains("migrations_table::EihwazUsersMigration"),
            "{lib}"
        );
        assert!(!lib.contains("m20220101_000001_create_table"), "{lib}");
        assert!(!placeholder_file);
    }

    #[test]
    fn the_sea_orm_cli_placeholder_is_removed() {
        let dir = temp("placeholder");
        let m = dir.to_str().unwrap();
        let file = format!("{m}/{SEA_ORM_CLI_PLACEHOLDER_MODULE}.rs");
        fs::write(&file, "// placeholder").unwrap();
        fs::write(dir.join("keep.rs"), "// mine").unwrap();
        strip_sea_orm_cli_placeholder_file(m);
        let (gone, kept) = (!Path::new(&file).exists(), dir.join("keep.rs").exists());
        let _ = fs::remove_dir_all(&dir);
        assert!(gone && kept);
    }

    /// A key to an existing table never holds a new table back.
    #[test]
    fn existing_targets_do_not_change_the_creation_order() {
        let zeta = schema("zeta", &["users"]);
        let alpha = schema("alpha", &["zeta"]);
        let sorted = topological_sort_changes(vec![new_table(&alpha), new_table(&zeta)]);
        let names: Vec<&str> = sorted.iter().map(|c| c.table_name.as_str()).collect();
        assert_eq!(names, ["zeta", "alpha"]);
    }

    #[test]
    fn length_shrinks_at_their_boundaries() {
        assert!(length_may_shrink(Some(100), Some(50)));
        assert!(!length_may_shrink(Some(50), Some(50)), "same length");
        assert!(!length_may_shrink(Some(50), Some(100)));
        assert!(length_may_shrink(Some(300), None), "MySQL's VARCHAR(255)");
        assert!(!length_may_shrink(Some(255), None));
        assert!(!length_may_shrink(Some(100), None));
    }

    /// 63 characters is the Postgres limit: 63 passes, 64 is refused.
    #[test]
    fn identifier_lengths_at_the_postgres_limit() {
        let mut change = new_table(&schema("t", &[]));
        let fk_to = |len: usize| ParsedFk {
            // "{table}_{from}_{to}_fkey" with table "t", from "c".
            to_table: "x".repeat(len - "t_c__fkey".len()),
            from_column: "c".into(),
            to_column: "id".into(),
            on_delete: "Cascade".into(),
            on_update: "NoAction".into(),
        };
        let index = |len: usize| crate::migration::utils::types::ParsedIndex {
            name: "i".repeat(len),
            columns: vec!["title".into()],
            unique: false,
        };
        change.added_fks = vec![fk_to(63)];
        change.added_indexes = vec![index(63)];
        assert!(collect_long_identifier_messages(std::slice::from_ref(&change)).is_empty());
        assert!(check_identifier_lengths(std::slice::from_ref(&change)).is_ok());
        change.added_fks = vec![fk_to(64)];
        change.added_indexes = vec![index(64)];
        assert_eq!(
            collect_long_identifier_messages(std::slice::from_ref(&change)).len(),
            2
        );
        assert!(check_identifier_lengths(&[change]).is_err());
    }

    /// In a cycle of new tables, the key to a table created later waits for
    /// it; a self key or a key to an earlier table doesn't.
    #[test]
    fn only_keys_to_later_new_tables_wait() {
        let schemas = [schema("a", &["b", "a"]), schema("b", &["a"])];
        let changes = [new_table(&schemas[0]), new_table(&schemas[1])];
        let keys = cycle_keys(&changes, &schemas);
        let forward: Vec<&str> = keys["a"]
            .forward
            .iter()
            .map(|f| f.to_table.as_str())
            .collect();
        assert_eq!(forward, ["b"], "self key and earlier table excluded");
        assert!(
            keys.get("b").is_none_or(|k| k.forward.is_empty()),
            "a is created before b"
        );
        assert_eq!(keys["b"].closing.len(), 1);
    }

    #[test]
    fn extend_blocks_are_planned_until_their_snapshot_matches() {
        let (entities, migrations) = (temp("ext_ent"), temp("ext_mig"));
        let (e, m) = (entities.to_str().unwrap(), migrations.to_str().unwrap());
        fs::write(
            entities.join("ext.rs"),
            r#"extend! { table: "eihwaz_users", fields: { bio: text } }"#,
        )
        .unwrap();
        let planned = plan_extend_changes(e, m).unwrap();
        assert_eq!(planned.len(), 1);
        assert_eq!(planned[0].1.added_columns[0].name, "bio");
        let _ = (
            fs::remove_dir_all(&entities),
            fs::remove_dir_all(&migrations),
        );
    }
}