oneiriq-surql 0.2.2

Code-first database toolkit for SurrealDB - schema definitions, migrations, query building, and typed CRUD (Rust port of oneiriq-surql). Published as the `oneiriq-surql` crate; imported as `use surql::...`.
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
//! Git hook utilities for schema drift detection.
//!
//! Port of `surql/migration/hooks.py`. Provides helpers for integrating
//! schema drift detection into git pre-commit hooks and CI/CD pipelines.
//! Drift is detected by diffing a code-side [`SchemaSnapshot`] against a
//! recorded (on-disk) snapshot; no database connection is required.
//!
//! ## Deviation from Python
//!
//! The Python implementation imports staged `.py` files via `importlib`
//! and uses file modification-time heuristics to detect drift. Rust cannot
//! execute arbitrary Python at runtime, so this port:
//!
//! * Takes two [`SchemaSnapshot`] values (code vs recorded) and compares
//!   them with [`crate::migration::diff::diff_schemas`], returning a
//!   structured [`DriftReport`].
//! * Exposes a higher-level [`check_schema_drift`] that derives the
//!   code-side snapshot from a [`SchemaRegistry`] and loads the recorded
//!   snapshot from the latest JSON file in a snapshots directory.
//! * Shells out to `git diff --cached --name-only --relative` via
//!   [`std::process::Command`] with no external dependency.
//! * Returns the pre-commit YAML snippet as a [`String`] (the caller is
//!   responsible for writing it to `.pre-commit-config.yaml`).
//!
//! ## Examples
//!
//! ```
//! use surql::migration::diff::SchemaSnapshot;
//! use surql::migration::hooks::check_schema_drift_from_snapshots;
//! use surql::schema::table::table_schema;
//!
//! let code = SchemaSnapshot {
//!     tables: vec![table_schema("user")],
//!     edges: vec![],
//! };
//! let recorded = SchemaSnapshot::new();
//! let report = check_schema_drift_from_snapshots(&code, &recorded);
//! assert!(report.drift_detected);
//! ```

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicBool, Ordering};

use serde::{Deserialize, Serialize};

use crate::error::{Result, SurqlError};
use crate::migration::diff::{diff_schemas, SchemaSnapshot};
use crate::migration::models::{DiffOperation, SchemaDiff};
use crate::migration::versioning::{
    create_snapshot, list_snapshots, store_snapshot, VersionedSnapshot,
};
use crate::schema::registry::SchemaRegistry;

/// Severity of a single drift issue.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DriftSeverity {
    /// Additive change (e.g. new table, new field, new index).
    Info,
    /// Non-destructive modification (e.g. field type change).
    Warning,
    /// Destructive change (e.g. dropped table or field).
    Critical,
}

impl DriftSeverity {
    /// Render the severity as a lowercase string.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Info => "info",
            Self::Warning => "warning",
            Self::Critical => "critical",
        }
    }
}

impl std::fmt::Display for DriftSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Classify a [`DiffOperation`] as a [`DriftSeverity`].
#[must_use]
pub fn severity_for_operation(op: DiffOperation) -> DriftSeverity {
    match op {
        DiffOperation::AddTable
        | DiffOperation::AddField
        | DiffOperation::AddIndex
        | DiffOperation::AddEvent => DriftSeverity::Info,
        DiffOperation::ModifyField
        | DiffOperation::ModifyPermissions
        | DiffOperation::DropEvent => DriftSeverity::Warning,
        DiffOperation::DropTable | DiffOperation::DropField | DiffOperation::DropIndex => {
            DriftSeverity::Critical
        }
    }
}

/// A single drift issue derived from one [`SchemaDiff`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DriftIssue {
    /// Severity of this issue.
    pub severity: DriftSeverity,
    /// The underlying diff operation.
    pub operation: DiffOperation,
    /// Table affected by the change.
    pub table: String,
    /// Field affected, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub field: Option<String>,
    /// Human-readable description.
    pub description: String,
}

impl DriftIssue {
    /// Construct an issue from a [`SchemaDiff`] using [`severity_for_operation`].
    #[must_use]
    pub fn from_diff(diff: &SchemaDiff) -> Self {
        Self {
            severity: severity_for_operation(diff.operation),
            operation: diff.operation,
            table: diff.table.clone(),
            field: diff.field.clone(),
            description: diff.description.clone(),
        }
    }
}

/// Structured drift report returned by the `check_schema_drift*` helpers.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DriftReport {
    /// `true` if any drift issues were detected.
    pub drift_detected: bool,
    /// One issue per underlying [`SchemaDiff`].
    pub issues: Vec<DriftIssue>,
    /// Suggested `surql` CLI invocation to create a migration, or [`None`]
    /// if no drift was detected.
    pub suggested_migration: Option<String>,
}

impl DriftReport {
    /// Build an empty (no-drift) report.
    #[must_use]
    pub fn empty() -> Self {
        Self::default()
    }

    /// Construct a report from a slice of [`SchemaDiff`] entries.
    #[must_use]
    pub fn from_diffs(diffs: &[SchemaDiff]) -> Self {
        if diffs.is_empty() {
            return Self::empty();
        }
        let issues: Vec<DriftIssue> = diffs.iter().map(DriftIssue::from_diff).collect();
        let suggested =
            Some("surql schema generate -s <schema-file> -m '<description>'".to_string());
        Self {
            drift_detected: true,
            issues,
            suggested_migration: suggested,
        }
    }

    /// Count issues at [`DriftSeverity::Critical`].
    #[must_use]
    pub fn critical_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == DriftSeverity::Critical)
            .count()
    }

    /// Render the report as a human-readable multi-line summary.
    #[must_use]
    pub fn to_summary(&self) -> String {
        if !self.drift_detected {
            return "No schema drift detected.".to_string();
        }
        let mut lines: Vec<String> = Vec::with_capacity(self.issues.len() + 2);
        lines.push(format!(
            "Schema drift detected ({} issue{}):",
            self.issues.len(),
            if self.issues.len() == 1 { "" } else { "s" }
        ));
        for issue in &self.issues {
            let field_part = issue
                .field
                .as_ref()
                .map_or(String::new(), |f| format!(".{f}"));
            lines.push(format!(
                "  [{severity}] {op:?} {table}{field}: {desc}",
                severity = issue.severity,
                op = issue.operation,
                table = issue.table,
                field = field_part,
                desc = issue.description,
            ));
        }
        if let Some(cmd) = &self.suggested_migration {
            lines.push(format!("Suggested: {cmd}"));
        }
        lines.join("\n")
    }
}

/// Compute a [`DriftReport`] from a pair of [`SchemaSnapshot`]s.
///
/// Delegates to [`diff_schemas`] and wraps every returned [`SchemaDiff`]
/// in a [`DriftIssue`]. Returns an empty report when the snapshots are
/// structurally identical.
#[must_use]
pub fn check_schema_drift_from_snapshots(
    code: &SchemaSnapshot,
    recorded: &SchemaSnapshot,
) -> DriftReport {
    let diffs = diff_schemas(code, recorded);
    DriftReport::from_diffs(&diffs)
}

/// Compute a [`DriftReport`] by comparing a code-side [`SchemaRegistry`]
/// against the latest snapshot stored under `snapshots_dir`.
///
/// If `snapshots_dir` is [`None`] or contains no snapshots, the recorded
/// snapshot is treated as empty. This mirrors the Python behaviour of
/// returning "all tables are new" drift when no migrations have been
/// generated yet.
///
/// The `_migrations_dir` parameter is accepted for signature-parity with
/// the Python implementation; the Rust port derives the recorded snapshot
/// solely from the versioned snapshot files in `snapshots_dir`.
///
/// # Errors
///
/// Returns [`SurqlError::MigrationHistory`] when `snapshots_dir` exists
/// but cannot be enumerated, or [`SurqlError::Io`] when a snapshot file
/// cannot be read.
pub fn check_schema_drift(
    registry: &SchemaRegistry,
    snapshots_dir: Option<&Path>,
    _migrations_dir: Option<&Path>,
) -> Result<DriftReport> {
    let code_snapshot = registry_to_snapshot(registry);
    let recorded_snapshot = match snapshots_dir {
        Some(dir) if dir.exists() => {
            latest_snapshot(dir)?.map_or_else(SchemaSnapshot::new, |s| versioned_to_snapshot(&s))
        }
        _ => SchemaSnapshot::new(),
    };
    Ok(check_schema_drift_from_snapshots(
        &code_snapshot,
        &recorded_snapshot,
    ))
}

/// Convert a [`SchemaRegistry`] into a [`SchemaSnapshot`].
#[must_use]
pub fn registry_to_snapshot(registry: &SchemaRegistry) -> SchemaSnapshot {
    SchemaSnapshot {
        tables: registry.tables().into_values().collect(),
        edges: registry.edges().into_values().collect(),
    }
}

/// Convert a [`VersionedSnapshot`] into a [`SchemaSnapshot`].
#[must_use]
pub fn versioned_to_snapshot(snapshot: &VersionedSnapshot) -> SchemaSnapshot {
    SchemaSnapshot {
        tables: snapshot.tables.values().cloned().collect(),
        edges: snapshot.edges.values().cloned().collect(),
    }
}

fn latest_snapshot(dir: &Path) -> Result<Option<VersionedSnapshot>> {
    let mut snaps = list_snapshots(dir)?;
    if snaps.is_empty() {
        return Ok(None);
    }
    // `list_snapshots` sorts ascending by version; take the last.
    Ok(snaps.pop())
}

// ---------------------------------------------------------------------------
// Staged file discovery (via `git diff --cached`)
// ---------------------------------------------------------------------------

/// Default predicate used by [`get_staged_schema_files`]: accepts paths
/// whose final extension is `.rs` or `.surql`.
#[must_use]
pub fn default_schema_filter(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|e| e.to_str()),
        Some("rs" | "surql")
    )
}

/// Return the list of files currently staged in git under `schema_dir`.
///
/// Runs `git diff --cached --name-only --diff-filter=ACMR --relative`
/// with `schema_dir` as the current working directory. The `--relative`
/// flag makes git scope output to `schema_dir` and emit paths relative
/// to it, which matches the filtered view the caller wants.
///
/// The `filter` closure decides which of those relative paths to include.
/// If `schema_dir` does not exist, an empty vector is returned.
///
/// # Errors
///
/// Returns [`SurqlError::Io`] if the `git` binary cannot be invoked at
/// the process level. A non-zero exit from `git` is not treated as an
/// error: an empty list is returned instead (matching the Python
/// behaviour of "no repo = no staged files").
pub fn get_staged_schema_files<F>(schema_dir: &Path, filter: F) -> Result<Vec<PathBuf>>
where
    F: Fn(&Path) -> bool,
{
    if !schema_dir.exists() {
        return Ok(Vec::new());
    }

    let cwd = if schema_dir.is_file() {
        schema_dir.parent().unwrap_or(schema_dir)
    } else {
        schema_dir
    };

    let output = Command::new("git")
        .args([
            "diff",
            "--cached",
            "--name-only",
            "--diff-filter=ACMR",
            "--relative",
        ])
        .current_dir(cwd)
        .output()
        .map_err(|e| SurqlError::Io {
            reason: format!("failed to invoke git: {e}"),
        })?;

    if !output.status.success() {
        // Not a git repo, or some other failure; mirror Python and return
        // an empty list rather than surfacing a hard error.
        return Ok(Vec::new());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);

    let mut staged: Vec<PathBuf> = Vec::new();
    for line in stdout.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let path = PathBuf::from(trimmed);
        if !filter(&path) {
            continue;
        }
        staged.push(path);
    }

    Ok(staged)
}

// ---------------------------------------------------------------------------
// Pre-commit config snippet
// ---------------------------------------------------------------------------

/// Render a `.pre-commit-config.yaml` snippet that wires the `surql`
/// schema-check CLI into a pre-commit hook.
///
/// The returned string is a valid YAML document; the caller is
/// responsible for writing it to disk or merging it into an existing
/// config.
///
/// ## Examples
///
/// ```
/// use surql::migration::hooks::generate_precommit_config;
///
/// let yaml = generate_precommit_config("schemas/", true);
/// assert!(yaml.starts_with("repos:"));
/// assert!(yaml.contains("surql-schema-check"));
/// ```
#[must_use]
pub fn generate_precommit_config(schema_path: &str, fail_on_drift: bool) -> String {
    let fail_flag = if fail_on_drift {
        " --fail-on-drift"
    } else {
        ""
    };
    format!(
        "repos:\n  - repo: local\n    hooks:\n      - id: surql-schema-check\n        name: Check schema migrations\n        entry: surql schema check --schema {schema_path}{fail_flag}\n        language: system\n        pass_filenames: false\n"
    )
}

// ---------------------------------------------------------------------------
// Auto-snapshot hooks (parity with `surql/migration/hooks.py`)
// ---------------------------------------------------------------------------

/// Global toggle for automatic post-migration snapshots.
///
/// Mirrors the Python `AUTO_SNAPSHOT_ENABLED` module-level boolean. The
/// toggle lives in the always-on [`hooks`](self) module so it can be
/// read from both client-gated (history/executor) and pure (watcher,
/// squash) call sites.
static AUTO_SNAPSHOT_ENABLED: AtomicBool = AtomicBool::new(false);

/// Enable automatic schema snapshots after successful migrations.
///
/// Subsequent calls to [`create_snapshot_on_migration`] will take a
/// snapshot; callers that honour the flag (e.g. the client-gated
/// migration executor) will start taking snapshots on apply.
pub fn enable_auto_snapshots() {
    AUTO_SNAPSHOT_ENABLED.store(true, Ordering::Relaxed);
}

/// Disable automatic schema snapshots.
pub fn disable_auto_snapshots() {
    AUTO_SNAPSHOT_ENABLED.store(false, Ordering::Relaxed);
}

/// `true` when automatic snapshots are enabled.
#[must_use]
pub fn is_auto_snapshot_enabled() -> bool {
    AUTO_SNAPSHOT_ENABLED.load(Ordering::Relaxed)
}

/// Callback run immediately before the snapshot is taken; receives the
/// migration version that triggered the snapshot.
pub type PreSnapshotHook<'a> = Box<dyn FnOnce(&str) + 'a>;
/// Callback run after the snapshot has been stored; receives a reference
/// to the stored [`VersionedSnapshot`].
pub type PostSnapshotHook<'a> = Box<dyn FnOnce(&VersionedSnapshot) + 'a>;

/// Hook invoked around [`create_snapshot_on_migration`].
///
/// The `pre` hook runs before the snapshot is created; the `post` hook
/// runs after a successful store with the resulting [`VersionedSnapshot`].
/// Either hook may be [`None`]. Hooks are `FnOnce` so they can capture
/// state by move.
pub struct SnapshotHooks<'a> {
    /// Callback run immediately before creating the snapshot. Receives
    /// the migration version that triggered the snapshot.
    pub pre: Option<PreSnapshotHook<'a>>,
    /// Callback run after the snapshot has been stored. Receives a
    /// reference to the stored [`VersionedSnapshot`].
    pub post: Option<PostSnapshotHook<'a>>,
}

impl<'a> SnapshotHooks<'a> {
    /// Construct a hook pair with no pre- or post-callback.
    #[must_use]
    pub fn none() -> Self {
        Self {
            pre: None,
            post: None,
        }
    }

    /// Attach a pre-snapshot callback.
    #[must_use]
    pub fn pre<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&str) + 'a,
    {
        self.pre = Some(Box::new(f));
        self
    }

    /// Attach a post-snapshot callback.
    #[must_use]
    pub fn post<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&VersionedSnapshot) + 'a,
    {
        self.post = Some(Box::new(f));
        self
    }
}

impl Default for SnapshotHooks<'_> {
    fn default() -> Self {
        Self::none()
    }
}

/// Create and persist a schema snapshot on behalf of a just-applied
/// migration.
///
/// Honours [`is_auto_snapshot_enabled`]: when the flag is `false` the
/// function is a no-op and returns `Ok(None)`. When enabled it captures
/// the current [`SchemaRegistry`] state via
/// [`create_snapshot`] and persists it to `snapshots_dir` via
/// [`store_snapshot`].
///
/// `migration_count` is stored on the snapshot for later inspection and
/// matches the Python signature.
///
/// `hooks.pre` runs before the snapshot is created; `hooks.post` runs
/// after a successful store. Hooks are best-effort: they must not
/// panic, and their execution is not reported through the returned
/// `Result` (errors are swallowed by the hook closure itself).
///
/// # Errors
///
/// Returns [`SurqlError::Validation`] if `version` is empty (surfaced
/// from [`create_snapshot`]), or [`SurqlError::Io`] /
/// [`SurqlError::Serialization`] if the snapshot cannot be written.
pub fn create_snapshot_on_migration(
    registry: &SchemaRegistry,
    snapshots_dir: &Path,
    version: &str,
    migration_count: u64,
    hooks: SnapshotHooks<'_>,
) -> Result<Option<VersionedSnapshot>> {
    if !is_auto_snapshot_enabled() {
        return Ok(None);
    }

    if let Some(pre) = hooks.pre {
        pre(version);
    }

    let mut snapshot = create_snapshot(registry, version, format!("auto: {version}"))?;
    snapshot.migration_count = migration_count;
    store_snapshot(&snapshot, snapshots_dir)?;

    if let Some(post) = hooks.post {
        post(&snapshot);
    }

    Ok(Some(snapshot))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::migration::models::{DiffOperation, SchemaDiff};
    use crate::migration::versioning::{store_snapshot, VersionedSnapshot};
    use crate::schema::registry::SchemaRegistry;
    use crate::schema::table::table_schema;
    use std::collections::BTreeMap;
    use std::fs;
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::{SystemTime, UNIX_EPOCH};

    static TEST_DIR_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn unique_temp_dir(tag: &str) -> PathBuf {
        let nanos: u128 = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_or(0, |d| d.as_nanos());
        let n = TEST_DIR_COUNTER.fetch_add(1, Ordering::Relaxed);
        let pid = std::process::id();
        let dir = std::env::temp_dir().join(format!("surql-hooks-{tag}-{pid}-{nanos}-{n}"));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    /// Spin up an ephemeral git repository in `dir`. Returns `true` on
    /// success. If `git` is not available, returns `false` so individual
    /// tests can skip gracefully.
    fn init_git_repo(dir: &Path) -> bool {
        let Ok(status) = Command::new("git")
            .args(["init", "-q"])
            .current_dir(dir)
            .status()
        else {
            return false;
        };
        if !status.success() {
            return false;
        }
        let _ = Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(dir)
            .status();
        let _ = Command::new("git")
            .args(["config", "user.name", "surql-test"])
            .current_dir(dir)
            .status();
        true
    }

    fn git_add(dir: &Path, relpath: &str) -> bool {
        Command::new("git")
            .args(["add", "--", relpath])
            .current_dir(dir)
            .status()
            .is_ok_and(|s| s.success())
    }

    fn make_diff(op: DiffOperation, table: &str, field: Option<&str>, desc: &str) -> SchemaDiff {
        SchemaDiff {
            operation: op,
            table: table.to_string(),
            field: field.map(ToString::to_string),
            index: None,
            event: None,
            description: desc.to_string(),
            forward_sql: String::new(),
            backward_sql: String::new(),
            details: BTreeMap::new(),
        }
    }

    // --- DriftSeverity ------------------------------------------------------

    #[test]
    fn severity_as_str_round_trip() {
        assert_eq!(DriftSeverity::Info.as_str(), "info");
        assert_eq!(DriftSeverity::Warning.as_str(), "warning");
        assert_eq!(DriftSeverity::Critical.as_str(), "critical");
    }

    #[test]
    fn severity_display_matches_as_str() {
        assert_eq!(format!("{}", DriftSeverity::Info), "info");
        assert_eq!(format!("{}", DriftSeverity::Critical), "critical");
    }

    #[test]
    fn severity_for_add_is_info() {
        assert_eq!(
            severity_for_operation(DiffOperation::AddTable),
            DriftSeverity::Info
        );
        assert_eq!(
            severity_for_operation(DiffOperation::AddField),
            DriftSeverity::Info
        );
        assert_eq!(
            severity_for_operation(DiffOperation::AddIndex),
            DriftSeverity::Info
        );
    }

    #[test]
    fn severity_for_modify_is_warning() {
        assert_eq!(
            severity_for_operation(DiffOperation::ModifyField),
            DriftSeverity::Warning
        );
        assert_eq!(
            severity_for_operation(DiffOperation::ModifyPermissions),
            DriftSeverity::Warning
        );
    }

    #[test]
    fn severity_for_drop_is_critical() {
        assert_eq!(
            severity_for_operation(DiffOperation::DropTable),
            DriftSeverity::Critical
        );
        assert_eq!(
            severity_for_operation(DiffOperation::DropField),
            DriftSeverity::Critical
        );
        assert_eq!(
            severity_for_operation(DiffOperation::DropIndex),
            DriftSeverity::Critical
        );
    }

    // --- DriftIssue / DriftReport ------------------------------------------

    #[test]
    fn issue_from_diff_carries_fields() {
        let diff = make_diff(
            DiffOperation::AddField,
            "user",
            Some("email"),
            "add email field",
        );
        let issue = DriftIssue::from_diff(&diff);
        assert_eq!(issue.severity, DriftSeverity::Info);
        assert_eq!(issue.operation, DiffOperation::AddField);
        assert_eq!(issue.table, "user");
        assert_eq!(issue.field.as_deref(), Some("email"));
        assert_eq!(issue.description, "add email field");
    }

    #[test]
    fn report_empty_has_no_drift() {
        let r = DriftReport::empty();
        assert!(!r.drift_detected);
        assert!(r.issues.is_empty());
        assert!(r.suggested_migration.is_none());
    }

    #[test]
    fn report_from_empty_diffs_is_empty() {
        let r = DriftReport::from_diffs(&[]);
        assert_eq!(r, DriftReport::empty());
    }

    #[test]
    fn report_from_diffs_populates_issues() {
        let diffs = vec![
            make_diff(DiffOperation::AddTable, "user", None, "create user"),
            make_diff(DiffOperation::DropTable, "stale", None, "drop stale"),
        ];
        let r = DriftReport::from_diffs(&diffs);
        assert!(r.drift_detected);
        assert_eq!(r.issues.len(), 2);
        assert!(r.suggested_migration.is_some());
        assert_eq!(r.critical_count(), 1);
    }

    #[test]
    fn report_summary_no_drift() {
        assert!(DriftReport::empty()
            .to_summary()
            .contains("No schema drift"));
    }

    #[test]
    fn report_summary_mentions_each_issue() {
        let diffs = vec![make_diff(
            DiffOperation::AddField,
            "user",
            Some("email"),
            "add email",
        )];
        let summary = DriftReport::from_diffs(&diffs).to_summary();
        assert!(summary.contains("Schema drift detected"));
        assert!(summary.contains("user.email"));
        assert!(summary.contains("AddField"));
        assert!(summary.contains("add email"));
        assert!(summary.contains("Suggested:"));
    }

    #[test]
    fn report_summary_singular_vs_plural() {
        let one =
            DriftReport::from_diffs(&[make_diff(DiffOperation::AddTable, "user", None, "add")]);
        assert!(one.to_summary().contains("1 issue)"));

        let two = DriftReport::from_diffs(&[
            make_diff(DiffOperation::AddTable, "a", None, "a"),
            make_diff(DiffOperation::AddTable, "b", None, "b"),
        ]);
        assert!(two.to_summary().contains("2 issues)"));
    }

    // --- check_schema_drift_from_snapshots ---------------------------------

    #[test]
    fn drift_from_snapshots_no_change_is_clean() {
        let snap = SchemaSnapshot {
            tables: vec![table_schema("user")],
            edges: vec![],
        };
        let report = check_schema_drift_from_snapshots(&snap, &snap);
        assert!(!report.drift_detected);
        assert!(report.issues.is_empty());
    }

    #[test]
    fn drift_from_snapshots_detects_new_table() {
        let code = SchemaSnapshot {
            tables: vec![table_schema("user")],
            edges: vec![],
        };
        let recorded = SchemaSnapshot::new();
        let report = check_schema_drift_from_snapshots(&code, &recorded);
        assert!(report.drift_detected);
        assert!(!report.issues.is_empty());
        assert!(report
            .issues
            .iter()
            .any(|i| i.operation == DiffOperation::AddTable && i.table == "user"));
    }

    #[test]
    fn drift_from_snapshots_detects_dropped_table() {
        let code = SchemaSnapshot::new();
        let recorded = SchemaSnapshot {
            tables: vec![table_schema("old")],
            edges: vec![],
        };
        let report = check_schema_drift_from_snapshots(&code, &recorded);
        assert!(report.drift_detected);
        assert!(report
            .issues
            .iter()
            .any(|i| i.operation == DiffOperation::DropTable && i.table == "old"));
        assert!(report.critical_count() >= 1);
    }

    #[test]
    fn drift_report_serde_round_trip() {
        let diffs = vec![make_diff(
            DiffOperation::AddTable,
            "user",
            None,
            "create user",
        )];
        let report = DriftReport::from_diffs(&diffs);
        let json = serde_json::to_string(&report).expect("serialise");
        let parsed: DriftReport = serde_json::from_str(&json).expect("deserialise");
        assert_eq!(parsed, report);
    }

    // --- check_schema_drift (with snapshots dir) ---------------------------

    #[test]
    fn check_drift_with_no_snapshots_dir_treats_recorded_as_empty() {
        let registry = SchemaRegistry::new();
        registry.register_table(table_schema("user"));
        let report =
            check_schema_drift(&registry, None, None).expect("check_schema_drift succeeds");
        assert!(report.drift_detected);
    }

    #[test]
    fn check_drift_with_empty_snapshots_dir_treats_recorded_as_empty() {
        let registry = SchemaRegistry::new();
        registry.register_table(table_schema("user"));
        let dir = unique_temp_dir("empty-snaps");
        let report =
            check_schema_drift(&registry, Some(&dir), None).expect("check_schema_drift succeeds");
        assert!(report.drift_detected);
    }

    #[test]
    fn check_drift_with_nonexistent_snapshots_dir_is_ok() {
        let registry = SchemaRegistry::new();
        let missing = std::env::temp_dir().join("surql-hooks-does-not-exist-xyz-123");
        let report = check_schema_drift(&registry, Some(&missing), None)
            .expect("check_schema_drift succeeds");
        assert!(!report.drift_detected);
    }

    #[test]
    fn check_drift_matching_snapshot_has_no_drift() {
        let registry = SchemaRegistry::new();
        registry.register_table(table_schema("user"));

        let dir = unique_temp_dir("match-snap");
        let mut tables = BTreeMap::new();
        tables.insert("user".to_string(), table_schema("user"));
        let snap = VersionedSnapshot {
            version: "20260101_000000".to_string(),
            timestamp: chrono::Utc::now(),
            description: "baseline".to_string(),
            tables,
            edges: BTreeMap::new(),
            accesses: BTreeMap::new(),
            checksum: String::new(),
            migration_count: 0,
        };
        store_snapshot(&snap, &dir).expect("store snapshot");

        let report =
            check_schema_drift(&registry, Some(&dir), None).expect("check_schema_drift succeeds");
        assert!(!report.drift_detected, "report: {report:?}");
    }

    #[test]
    fn check_drift_uses_latest_snapshot() {
        let registry = SchemaRegistry::new();
        registry.register_table(table_schema("user"));
        registry.register_table(table_schema("post"));

        let dir = unique_temp_dir("latest-snap");

        // Older snapshot only has `user`.
        let mut older_tables = BTreeMap::new();
        older_tables.insert("user".to_string(), table_schema("user"));
        let older = VersionedSnapshot {
            version: "20260101_000000".to_string(),
            timestamp: chrono::Utc::now(),
            description: "older".to_string(),
            tables: older_tables,
            edges: BTreeMap::new(),
            accesses: BTreeMap::new(),
            checksum: String::new(),
            migration_count: 0,
        };
        store_snapshot(&older, &dir).expect("store older");

        // Newer snapshot has both; makes registry match exactly.
        let mut newer_tables = BTreeMap::new();
        newer_tables.insert("user".to_string(), table_schema("user"));
        newer_tables.insert("post".to_string(), table_schema("post"));
        let newer = VersionedSnapshot {
            version: "20260301_000000".to_string(),
            timestamp: chrono::Utc::now(),
            description: "newer".to_string(),
            tables: newer_tables,
            edges: BTreeMap::new(),
            accesses: BTreeMap::new(),
            checksum: String::new(),
            migration_count: 0,
        };
        store_snapshot(&newer, &dir).expect("store newer");

        let report =
            check_schema_drift(&registry, Some(&dir), None).expect("check_schema_drift succeeds");
        assert!(!report.drift_detected, "report: {report:?}");
    }

    #[test]
    fn registry_to_snapshot_collects_all_tables() {
        let registry = SchemaRegistry::new();
        registry.register_table(table_schema("user"));
        registry.register_table(table_schema("post"));
        let snap = registry_to_snapshot(&registry);
        assert_eq!(snap.tables.len(), 2);
        let names: Vec<&str> = snap.tables.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"user"));
        assert!(names.contains(&"post"));
    }

    #[test]
    fn versioned_to_snapshot_preserves_tables() {
        let mut tables = BTreeMap::new();
        tables.insert("user".to_string(), table_schema("user"));
        let snap = VersionedSnapshot {
            version: "v1".to_string(),
            timestamp: chrono::Utc::now(),
            description: String::new(),
            tables,
            edges: BTreeMap::new(),
            accesses: BTreeMap::new(),
            checksum: String::new(),
            migration_count: 0,
        };
        let schema = versioned_to_snapshot(&snap);
        assert_eq!(schema.tables.len(), 1);
        assert_eq!(schema.tables[0].name, "user");
    }

    // --- default_schema_filter ---------------------------------------------

    #[test]
    fn default_filter_accepts_rs() {
        assert!(default_schema_filter(&PathBuf::from("src/schemas/user.rs")));
    }

    #[test]
    fn default_filter_accepts_surql() {
        assert!(default_schema_filter(&PathBuf::from(
            "migrations/20260101_000000_init.surql"
        )));
    }

    #[test]
    fn default_filter_rejects_non_schema() {
        assert!(!default_schema_filter(&PathBuf::from("README.md")));
        assert!(!default_schema_filter(&PathBuf::from("src/main.py")));
        assert!(!default_schema_filter(&PathBuf::from("Cargo.toml")));
    }

    // --- get_staged_schema_files: fake-git fixtures ------------------------

    #[test]
    fn staged_returns_empty_when_dir_missing() {
        let missing = std::env::temp_dir().join("surql-hooks-never-exists-xyz");
        let files = get_staged_schema_files(&missing, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert!(files.is_empty());
    }

    #[test]
    fn staged_returns_empty_outside_git_repo() {
        let dir = unique_temp_dir("no-git");
        // No `git init` here; call should gracefully return empty.
        let files = get_staged_schema_files(&dir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert!(files.is_empty());
    }

    #[test]
    fn staged_returns_empty_when_nothing_staged() {
        let dir = unique_temp_dir("empty-stage");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        // Create an untracked file; don't stage it.
        fs::write(dir.join("untracked.surql"), "-- @up\nSELECT 1;\n").unwrap();
        let files = get_staged_schema_files(&dir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert!(files.is_empty());
    }

    #[test]
    fn staged_detects_newly_staged_schema_file() {
        let dir = unique_temp_dir("stage-one");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        let schema_subdir = dir.join("schemas");
        fs::create_dir_all(&schema_subdir).unwrap();
        let file = schema_subdir.join("user.surql");
        fs::write(&file, "-- schema\n").unwrap();
        assert!(git_add(&dir, "schemas/user.surql"));

        let files = get_staged_schema_files(&schema_subdir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(files.len(), 1);
        assert!(files[0].to_string_lossy().ends_with("user.surql"));
    }

    #[test]
    fn staged_filters_by_custom_predicate() {
        let dir = unique_temp_dir("stage-filter");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        let schema_subdir = dir.join("schemas");
        fs::create_dir_all(&schema_subdir).unwrap();
        fs::write(schema_subdir.join("user.surql"), "-- surql\n").unwrap();
        fs::write(schema_subdir.join("README.md"), "docs\n").unwrap();
        assert!(git_add(&dir, "schemas/user.surql"));
        assert!(git_add(&dir, "schemas/README.md"));

        // Custom filter: only accept `.md` files.
        let md_only = |p: &Path| p.extension().and_then(|e| e.to_str()) == Some("md");
        let md_files = get_staged_schema_files(&schema_subdir, md_only)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(md_files.len(), 1);
        assert!(md_files[0].to_string_lossy().ends_with("README.md"));

        // Default filter only accepts the surql file.
        let rs_surql_only = get_staged_schema_files(&schema_subdir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(rs_surql_only.len(), 1);
        assert!(rs_surql_only[0].to_string_lossy().ends_with("user.surql"));
    }

    #[test]
    fn staged_excludes_files_outside_schema_dir() {
        let dir = unique_temp_dir("stage-outside");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        let schema_subdir = dir.join("schemas");
        let other_subdir = dir.join("other");
        fs::create_dir_all(&schema_subdir).unwrap();
        fs::create_dir_all(&other_subdir).unwrap();
        fs::write(schema_subdir.join("keep.surql"), "x").unwrap();
        fs::write(other_subdir.join("skip.surql"), "x").unwrap();
        assert!(git_add(&dir, "schemas/keep.surql"));
        assert!(git_add(&dir, "other/skip.surql"));

        let files = get_staged_schema_files(&schema_subdir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(files.len(), 1);
        assert!(files[0].to_string_lossy().ends_with("keep.surql"));
    }

    #[test]
    fn staged_handles_multiple_files() {
        let dir = unique_temp_dir("stage-multi");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        let schema_subdir = dir.join("schemas");
        fs::create_dir_all(&schema_subdir).unwrap();
        fs::write(schema_subdir.join("a.surql"), "x").unwrap();
        fs::write(schema_subdir.join("b.surql"), "x").unwrap();
        fs::write(schema_subdir.join("c.rs"), "x").unwrap();
        assert!(git_add(&dir, "schemas/a.surql"));
        assert!(git_add(&dir, "schemas/b.surql"));
        assert!(git_add(&dir, "schemas/c.rs"));

        let files = get_staged_schema_files(&schema_subdir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(files.len(), 3);
    }

    #[test]
    fn staged_accepts_schema_dir_pointing_to_repo_root() {
        let dir = unique_temp_dir("stage-root");
        if !init_git_repo(&dir) {
            eprintln!("skipping: git not available");
            return;
        }
        fs::write(dir.join("init.surql"), "x").unwrap();
        assert!(git_add(&dir, "init.surql"));
        let files = get_staged_schema_files(&dir, default_schema_filter)
            .expect("get_staged_schema_files succeeds");
        assert_eq!(files.len(), 1);
        assert!(files[0].to_string_lossy().ends_with("init.surql"));
    }

    // --- generate_precommit_config -----------------------------------------

    #[test]
    fn precommit_config_starts_with_repos() {
        let yaml = generate_precommit_config("schemas/", true);
        assert!(yaml.starts_with("repos:"));
    }

    #[test]
    fn precommit_config_contains_hook_id() {
        let yaml = generate_precommit_config("schemas/", true);
        assert!(yaml.contains("id: surql-schema-check"));
    }

    #[test]
    fn precommit_config_embeds_schema_path() {
        let yaml = generate_precommit_config("custom/schemas/", true);
        assert!(yaml.contains("--schema custom/schemas/"));
    }

    #[test]
    fn precommit_config_toggles_fail_on_drift() {
        let with_flag = generate_precommit_config("schemas/", true);
        assert!(with_flag.contains("--fail-on-drift"));

        let without_flag = generate_precommit_config("schemas/", false);
        assert!(!without_flag.contains("--fail-on-drift"));
    }

    #[test]
    fn precommit_config_has_expected_yaml_keys() {
        // Rather than pulling in a YAML parser, verify the structural
        // invariants the snippet is contractually required to hold: a
        // single top-level `repos:` key, exactly one repo entry, and one
        // hook entry with a name / entry / language / pass_filenames.
        let yaml = generate_precommit_config("schemas/", true);
        assert_eq!(
            yaml.matches("\nrepos:").count() + usize::from(yaml.starts_with("repos:")),
            1
        );
        assert_eq!(yaml.matches("- repo: local").count(), 1);
        assert_eq!(yaml.matches("- id: surql-schema-check").count(), 1);
        assert!(yaml.contains("name: Check schema migrations"));
        assert!(yaml.contains("entry: surql schema check"));
        assert!(yaml.contains("language: system"));
        assert!(yaml.contains("pass_filenames: false"));
    }

    #[test]
    fn precommit_config_is_nonempty() {
        let yaml = generate_precommit_config("schemas/", true);
        assert!(!yaml.is_empty());
        assert!(yaml.len() > 100);
    }

    // --- auto-snapshot hooks -----------------------------------------------

    // NOTE: these tests mutate the global AUTO_SNAPSHOT_ENABLED toggle.
    // They are serialised via AUTO_SNAPSHOT_TEST_LOCK because cargo test
    // runs tests in parallel by default and a shared atomic toggle
    // cannot be partitioned per-thread.

    static AUTO_SNAPSHOT_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn with_flag_lock<R>(f: impl FnOnce() -> R) -> R {
        let guard = AUTO_SNAPSHOT_TEST_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let out = f();
        drop(guard);
        out
    }

    #[test]
    fn enable_disable_is_enabled_roundtrip() {
        with_flag_lock(|| {
            disable_auto_snapshots();
            assert!(!is_auto_snapshot_enabled());
            enable_auto_snapshots();
            assert!(is_auto_snapshot_enabled());
            disable_auto_snapshots();
            assert!(!is_auto_snapshot_enabled());
        });
    }

    #[test]
    fn create_snapshot_on_migration_no_op_when_disabled() {
        with_flag_lock(|| {
            disable_auto_snapshots();
            let registry = SchemaRegistry::new();
            registry.register_table(table_schema("user"));
            let dir = unique_temp_dir("auto-off");
            let hooks = SnapshotHooks::none();
            let out = create_snapshot_on_migration(&registry, &dir, "20260101_000000", 0, hooks)
                .expect("hook runs");
            assert!(out.is_none());
            let list = std::fs::read_dir(&dir).unwrap();
            assert_eq!(list.count(), 0);
        });
    }

    #[test]
    fn create_snapshot_on_migration_writes_file_when_enabled() {
        with_flag_lock(|| {
            enable_auto_snapshots();
            let registry = SchemaRegistry::new();
            registry.register_table(table_schema("user"));
            let dir = unique_temp_dir("auto-on");
            let snap = create_snapshot_on_migration(
                &registry,
                &dir,
                "20260101_000000",
                7,
                SnapshotHooks::none(),
            )
            .expect("hook runs")
            .expect("snapshot present");
            disable_auto_snapshots();
            assert_eq!(snap.migration_count, 7);
            let files: Vec<_> = std::fs::read_dir(&dir).unwrap().collect();
            assert_eq!(files.len(), 1);
        });
    }

    #[test]
    fn create_snapshot_on_migration_runs_pre_and_post_hooks() {
        with_flag_lock(|| {
            enable_auto_snapshots();
            let registry = SchemaRegistry::new();
            registry.register_table(table_schema("user"));
            let dir = unique_temp_dir("auto-hooks");

            let pre_cell = std::sync::Arc::new(std::sync::Mutex::new(Option::<String>::None));
            let post_cell = std::sync::Arc::new(std::sync::Mutex::new(Option::<String>::None));

            let pre_cell_cb = std::sync::Arc::clone(&pre_cell);
            let post_cell_cb = std::sync::Arc::clone(&post_cell);
            let hooks = SnapshotHooks::none()
                .pre(move |v: &str| {
                    *pre_cell_cb.lock().unwrap() = Some(v.to_string());
                })
                .post(move |s: &VersionedSnapshot| {
                    *post_cell_cb.lock().unwrap() = Some(s.version.clone());
                });

            let snap = create_snapshot_on_migration(&registry, &dir, "20260109_120000", 3, hooks)
                .expect("hook runs")
                .expect("snapshot present");
            disable_auto_snapshots();

            assert_eq!(pre_cell.lock().unwrap().as_deref(), Some("20260109_120000"));
            assert_eq!(
                post_cell.lock().unwrap().as_deref(),
                Some(snap.version.as_str())
            );
        });
    }

    #[test]
    fn create_snapshot_on_migration_surfaces_validation_error_on_empty_version() {
        with_flag_lock(|| {
            enable_auto_snapshots();
            let registry = SchemaRegistry::new();
            let dir = unique_temp_dir("auto-empty");
            let err = create_snapshot_on_migration(&registry, &dir, "", 0, SnapshotHooks::none())
                .expect_err("must reject empty version");
            disable_auto_snapshots();
            assert!(matches!(err, SurqlError::Validation { .. }));
        });
    }
}