djogi 0.1.0-alpha.2

Model-first web framework for Rust — web-framework-agnostic core; Axum integration opt-in via the `axum` feature flag
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
//! Online-safety classification engine — Phase 7.5 T5.
//!
//! Walks a [`SchemaOperation`] (or a delta-worth of them) and assigns
//! each one an [`OnlineSafetyClassification`] verdict per the §7
//! classification table in
//! `docs/superpowers/plans/2026-04-23-phase7-5-live-migrations-and-protected-data-v3.md`.
//!
//! # Boundary contract (§6.5)
//!
//! - **PK-flip routing is exclusive.** When a delta carries
//!   [`SchemaOperation::PkTypeFlipGroup`] or
//!   [`SchemaOperation::PkTypeFlipMultiGroup`], that operation is
//!   already routed through Phase 7's
//!   [`crate::migrate::diff::Classification::PkTypeFlip`] cascade
//!   emitter family (`migrate::pk_flip`). The classifier short-circuits
//!   those entries — they appear in [`classify_delta`]'s output as
//!   skipped (filtered out) so live-plan callers never see them.
//! - **Logging-profile short-circuit.** Per §6.5 of the v3 plan, the
//!   classifier inspects [`ClassifyContext::logging_profile`] and the
//!   [`ClassifyContext::target_database`] field to decide whether to
//!   route through Phase 7.5 at all. Event-log databases never live-
//!   plan; crud-log databases under `light` / `balanced` never live-
//!   plan; only `strict_audit` crud-log + the application database
//!   reach the per-operation classifier.
//!
//! # Determinism
//!
//! Classification is pure: same inputs → same output, no `pg_catalog`
//! reads, no host-variable behaviour. Every dispatch arm cites the
//! §7 table row it maps to in a doc comment so the table stays
//! reviewable against this code.
//!
//! # Aggregation
//!
//! [`classify_delta`] performs the cross-operation aggregation §7
//! requires:
//!
//! - Per-table `AddForeignKey` counts — 4+ FK additions to a single
//!   table (configurable via [`ClassifyContext::multi_fk_threshold`])
//!   escalate every entry on that table to `ExpandContract`.
//! - Inbound FK counts on a `DropTable` — when 4+ existing FKs
//!   reference the table being dropped, the drop escalates to
//!   `ExpandContract` (multi-step DROP CONSTRAINT staging). Inbound
//!   FK counts must be supplied via [`ClassifyContext::inbound_fk_counts`]
//!   because the drop op alone does not carry the foreign-key graph;
//!   compose passes the count from the live snapshot.

use crate::descriptor::DefaultVolatility;
use crate::live_migrate::LoggingProfile;
use crate::migrate::diff::{ColumnChange, SchemaOperation};
use crate::migrate::pg_volatility::{Volatility, classify_default_expression};
use crate::migrate::schema::{
    ColumnSchema, IndexKindSchema, IndexSchema, IndexTargetSchema, OnlineSafetyClassification,
};
use std::collections::BTreeMap;

/// Ambient context the classifier consults that is not carried by a
/// single [`SchemaOperation`].
///
/// Constructed by the compose pipeline once per `(database, app)`
/// bucket and threaded into every classification call so the same
/// configuration drives every operation in the delta.
#[derive(Debug, Clone)]
pub struct ClassifyContext<'a> {
    /// Approximate row count of the operation's target table.
    /// `None` when the count is unknown — the classifier
    /// conservatively treats `None` as "above threshold" (slower
    /// path is safer).
    pub estimated_rows: Option<u64>,

    /// Threshold above which CHECK / NOT NULL / FK validation is
    /// staged via `NOT VALID` + separate `VALIDATE`. Default
    /// `100_000`; sourced from `Djogi.toml` `[live]
    /// validation_threshold_rows`.
    pub validation_threshold_rows: u64,

    /// Threshold for multi-FK staging — adding this many or more FKs
    /// to a single table in one delta escalates each addition to
    /// `ExpandContract`. Default `4`; sourced from `Djogi.toml`
    /// `[live] multi_fk_threshold`.
    pub multi_fk_threshold: u32,

    /// Logging profile in scope for the bucket being classified.
    /// Drives the §6.5 three-DB short-circuit:
    ///
    /// - Event-log database — never live-plans regardless of profile;
    ///   the classifier reports every operation as `OnlineSafe` so
    ///   compose routes the delta directly through Phase 7.
    /// - Crud-log database under [`LoggingProfile::Light`] /
    ///   [`LoggingProfile::Balanced`] — same direct route; brief
    ///   `AccessExclusiveLock` windows on crud-log mirror tables are
    ///   acceptable because the audit contract degrades gracefully.
    /// - Crud-log database under [`LoggingProfile::StrictAudit`] —
    ///   fail-closed semantics make audit-table locks block
    ///   application writes, so populated crud-log mirrors classify
    ///   the same way as the application database.
    /// - Application database — full classifier walk regardless of
    ///   profile.
    pub logging_profile: LoggingProfile,

    /// Which of the three databases the delta targets. Drives the
    /// §6.5 short-circuit alongside `logging_profile`.
    pub target_database: TargetDatabase,

    /// Inbound FK counts keyed by table name — used for the
    /// `DropTable` aggregation (4+ inbound FKs escalate the drop to
    /// `ExpandContract`). Populated by compose from the snapshot's
    /// FK graph; empty maps disable the escalation.
    pub inbound_fk_counts: &'a BTreeMap<String, u32>,

    /// Adopter override per `#[field(default_volatility = "stable")]`
    /// for known-safe UDFs that the static `pg_volatility.rs` table
    /// cannot classify. Keyed by `(table_name, column_name)`. When an
    /// entry is present for an `AddColumn` op, the classifier consults
    /// the override before falling through to the static volatility
    /// table — letting adopters fast-path columns whose default
    /// expression Djogi could not classify deterministically.
    ///
    /// Spec: §3 / §820 of the Phase 7.5 v3 plan. Populated by compose
    /// from `FieldDescriptor::default_volatility_override` (T3, PR 1).
    pub default_volatility_overrides: &'a BTreeMap<(String, String), DefaultVolatility>,
}

impl<'a> ClassifyContext<'a> {
    /// Reasonable defaults for testing / inline construction. Production
    /// callers populate every field from `Djogi.toml`.
    pub fn application_default(
        inbound_fk_counts: &'a BTreeMap<String, u32>,
        default_volatility_overrides: &'a BTreeMap<(String, String), DefaultVolatility>,
    ) -> Self {
        Self {
            estimated_rows: None,
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::Application,
            inbound_fk_counts,
            default_volatility_overrides,
        }
    }
}

/// Which of the three Djogi databases the delta is targeting. Phase 7
/// keeps three connection pools (application data, CRUD audit log,
/// event log) and the classifier's §6.5 short-circuit varies per pool.
///
/// `#[non_exhaustive]` so future targets (e.g. a separate vector-store
/// database) can be added without breaking downstream matches.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TargetDatabase {
    /// Adopter's application data — full classifier walk applies.
    Application,
    /// Per-model `_logs` mirror tables. Behaviour varies by
    /// [`LoggingProfile`] — see [`ClassifyContext::logging_profile`].
    CrudLog,
    /// `tracing`-driven event log. Best-effort under every built-in
    /// profile; never live-plans.
    EventLog,
}

/// Classify a single [`SchemaOperation`] against the §7 table.
///
/// **Pre-condition.** The caller has already filtered out
/// [`SchemaOperation::PkTypeFlipGroup`] / `PkTypeFlipMultiGroup`
/// operations — those are routed through Phase 7's `pk_flip`
/// emitter family and never reach this classifier per the §6.5
/// boundary contract. The function still has match arms for those
/// variants (returning [`OnlineSafetyClassification::OfflineOnly`]
/// so a misuse caller gets a refused-classification verdict — see
/// the dispatch arm), but production callers go through
/// [`classify_delta`] which performs the filtering as part of its
/// walk.
pub fn classify_operation(
    op: &SchemaOperation,
    ctx: &ClassifyContext<'_>,
) -> OnlineSafetyClassification {
    if is_pk_type_flip_operation(op) {
        return OnlineSafetyClassification::OfflineOnly;
    }

    // §6.5 short-circuit. Event-log target never live-plans; crud-log
    // under non-strict profiles never live-plans either. The classifier
    // returns `OnlineSafe` so the runner applies the operation
    // directly via Phase 7's regular path.
    if !classifier_applies(ctx) {
        return OnlineSafetyClassification::OnlineSafe;
    }

    match op {
        // §7: "Add nullable column" → OnlineSafe (no backfill, no
        // lock window beyond the catalog touch). Non-nullable columns
        // dispatch through default-expression analysis.
        SchemaOperation::AddColumn { table, column } => classify_add_column(table, column, ctx),

        // §7: "Drop column" → FastLockDestructiveGuarded (corrected
        // per Codex P1-01 — destroys data + invalidates dependents).
        SchemaOperation::DropColumn { .. } => {
            OnlineSafetyClassification::FastLockDestructiveGuarded
        }

        // §7: "Rename column (with #[field(renamed_from = ...)])" →
        // OnlineSafe (catalog-only).
        SchemaOperation::RenameColumn { .. } => OnlineSafetyClassification::OnlineSafe,

        // §7: column-type changes — heuristic walk.
        SchemaOperation::AlterColumn { change, .. } => classify_column_change(change, ctx),

        // §7: "Add FK" → OnlineSafe when below threshold (NOT VALID +
        // VALIDATE single statement); otherwise ExpandContract.
        // Multi-FK aggregation is handled by classify_delta — this
        // entry-point classifies a single FK without aggregation.
        SchemaOperation::AddForeignKey { .. } => classify_fk_addition(ctx),

        // §7: dropping an FK is a constraint removal — OnlineSafe
        // (catalog-only; no data loss in the FK column itself).
        SchemaOperation::DropForeignKey { .. } => OnlineSafetyClassification::OnlineSafe,

        // §7 (PR 7): "Add EXCLUDE constraint" — empty existing table
        // (estimated_rows == Some(0)) routes through OnlineSafe; the
        // ALTER TABLE inline form applies in a single transactional
        // segment. Populated tables (Some(n) where n > 0) AND unknown
        // row counts (None) classify OfflineOnly: Pg18 has no
        // `NOT VALID` for `EXCLUDE`, so two-phase staging is
        // structurally impossible — the constraint check runs under
        // AccessExclusiveLock against every existing row. The
        // unknown-row-count case takes the conservative offline path
        // because the classifier cannot prove the table is empty.
        SchemaOperation::AddExclusionConstraint { .. } => match ctx.estimated_rows {
            Some(0) => OnlineSafetyClassification::OnlineSafe,
            _ => OnlineSafetyClassification::OfflineOnly,
        },

        // Dropping an exclusion constraint is catalog-only — Postgres
        // releases the underlying GiST/B-tree index without scanning
        // rows. OnlineSafe.
        SchemaOperation::DropExclusionConstraint { .. } => OnlineSafetyClassification::OnlineSafe,

        // §7: "Add index" — concurrently=true → OnlineSafe; otherwise
        // ExpandContract. The `requires_out_of_transaction` flag on
        // IndexSchema mirrors the `concurrently = true` model knob.
        // Empty-table fast-path: estimated_rows == Some(0) routes
        // non-concurrent non-unique adds to OnlineSafe (the
        // AccessExclusiveLock is instant on a zero-row table).
        SchemaOperation::AddIndex(index) => classify_index_addition(index, ctx),

        // §7: "Drop index" — catalog-only; OnlineSafe regardless of
        // concurrent flag because Postgres' DROP INDEX is fast and
        // does not lock the table heavily. (DROP INDEX CONCURRENTLY
        // exists for replication-lag concerns but is not classified
        // distinctly here.)
        SchemaOperation::DropIndex(_) => OnlineSafetyClassification::OnlineSafe,

        // §7: "Add table" — pure additive; OnlineSafe.
        SchemaOperation::AddTable(_) => OnlineSafetyClassification::OnlineSafe,

        // §7: "Drop table" with 4+ inbound FKs → ExpandContract;
        // otherwise FastLockDestructiveGuarded. The aggregation walks
        // the bucket-level inbound counts, so single-table classification
        // returns the conservative "drop is destructive" verdict.
        SchemaOperation::DropTable(table) => classify_drop_table(table, ctx),

        // §7: "Rename table" → OnlineSafe.
        SchemaOperation::RenameTable { .. } => OnlineSafetyClassification::OnlineSafe,

        // §7: "Enum rewrite (add-value)" → OnlineSafe.
        SchemaOperation::AddEnum(_) | SchemaOperation::AddEnumVariant { .. } => {
            OnlineSafetyClassification::OnlineSafe
        }

        // §7: "Enum rewrite (rename / remove value)" → OfflineOnly.
        // The differ never emits a "DropEnumVariant" op (Postgres
        // has no such DDL); enum drops are handled below.
        SchemaOperation::DropEnum(_) => OnlineSafetyClassification::OfflineOnly,

        // App-level metadata changes — folder rename + ledger UPDATE,
        // no SQL DDL on the application schema.
        SchemaOperation::RenameApp { .. } | SchemaOperation::MoveModelBetweenApps { .. } => {
            OnlineSafetyClassification::OnlineSafe
        }

        // Phase 8.5 djogi#217 — `COMMENT ON TABLE <t> IS '<text>'` /
        // `IS NULL` is a catalog-only write against `pg_description`.
        // No row touch, no lock window beyond the brief catalog update.
        // OnlineSafe regardless of from/to direction.
        SchemaOperation::SetTableComment { .. } => OnlineSafetyClassification::OnlineSafe,

        // Phase 8.5 djogi#218 — table storage-parameter metadata
        // changes are catalog reloption updates; they do not rewrite
        // existing rows.
        SchemaOperation::SetStorageParams { .. } => OnlineSafetyClassification::OnlineSafe,

        // Phase 8.5 djogi#219 — `ALTER TABLE ... SET TABLESPACE`
        // rewrites the table's physical file and takes an ACCESS
        // EXCLUSIVE lock, so live planning must treat it as offline.
        SchemaOperation::SetTablespace { .. } => OnlineSafetyClassification::OfflineOnly,

        // PK-flip ops belong to the core-migration `pk_flip` cascade
        // emitter family — they must be filtered out by `classify_delta`
        // before reaching this dispatch per the §6.5 boundary contract.
        // A misuse caller bypassing the delta walk should get a
        // refused-classification verdict so the runner refuses to apply
        // rather than silently fast-applying a PK flip. `OfflineOnly`
        // is the safe-by-default verdict.
        SchemaOperation::PkTypeFlip { .. }
        | SchemaOperation::PkTypeFlipGroup(_)
        | SchemaOperation::PkTypeFlipMultiGroup(_) => OnlineSafetyClassification::OfflineOnly,

        // §7: "Opaque type transform" / unsupported variants →
        // OfflineOnly (operator must hand-edit).
        SchemaOperation::Unsupported { .. } => OnlineSafetyClassification::OfflineOnly,
    }
}

/// Walk a delta's operations, applying per-operation classification
/// plus cross-operation aggregation rules from §7:
///
/// - PK-flip groups are filtered out (routed through Phase 7 directly).
/// - 4+ FK additions to a single table escalate every addition on
///   that table to `ExpandContract`.
/// - 4+ inbound FK references on a `DropTable` escalate that drop to
///   `ExpandContract`.
///
/// Returns `(operation, classification)` pairs in input order. The
/// caller decides what to do with each verdict — the live-plan layer
/// keys off `ExpandContract`; the regular Phase 7 runner consumes the
/// other variants.
pub fn classify_delta(
    ops: &[SchemaOperation],
    ctx: &ClassifyContext<'_>,
) -> Vec<(SchemaOperation, OnlineSafetyClassification)> {
    if !classifier_applies(ctx) {
        let mut out: Vec<(SchemaOperation, OnlineSafetyClassification)> =
            Vec::with_capacity(ops.len());
        for op in ops {
            if is_pk_type_flip_operation(op) {
                continue;
            }
            out.push((op.clone(), OnlineSafetyClassification::OnlineSafe));
        }
        return out;
    }

    // Pre-pass — count FK additions per table for the multi-FK rule.
    let mut fk_addition_counts: BTreeMap<&str, u32> = BTreeMap::new();
    for op in ops {
        if let SchemaOperation::AddForeignKey { table, .. } = op {
            *fk_addition_counts.entry(table.as_str()).or_default() += 1;
        }
    }

    let mut out: Vec<(SchemaOperation, OnlineSafetyClassification)> = Vec::with_capacity(ops.len());
    for op in ops {
        // Skip PK-flip groups — Phase 7 territory. The standalone
        // `PkTypeFlip` variant survives only for unit-test fixtures
        // (production deltas always carry `PkTypeFlipGroup` after the
        // bucket-walk finalisation) — also filter it for safety.
        if is_pk_type_flip_operation(op) {
            continue;
        }

        let mut verdict = classify_operation(op, ctx);

        // Multi-FK escalation — when a single table receives `multi_fk_threshold`
        // or more FK additions in this delta, each addition lifts to
        // `ExpandContract` per §7.
        if let SchemaOperation::AddForeignKey { table, .. } = op
            && let Some(count) = fk_addition_counts.get(table.as_str())
            && *count >= ctx.multi_fk_threshold
        {
            verdict = OnlineSafetyClassification::ExpandContract;
        }

        if index_replacement_requires_refusal(op, ops) {
            verdict = OnlineSafetyClassification::OfflineOnly;
        }

        out.push((op.clone(), verdict));
    }
    out
}

fn is_pk_type_flip_operation(op: &SchemaOperation) -> bool {
    matches!(
        op,
        SchemaOperation::PkTypeFlip { .. }
            | SchemaOperation::PkTypeFlipGroup(_)
            | SchemaOperation::PkTypeFlipMultiGroup(_)
    )
}

/// `true` iff the classifier should run on operations targeting this
/// `(target_database, logging_profile)` pair. `false` triggers the
/// §6.5 short-circuit — every operation classifies as `OnlineSafe`
/// and routes through Phase 7 directly.
fn classifier_applies(ctx: &ClassifyContext<'_>) -> bool {
    match ctx.target_database {
        TargetDatabase::Application => true,
        TargetDatabase::CrudLog => matches!(ctx.logging_profile, LoggingProfile::StrictAudit),
        TargetDatabase::EventLog => false,
    }
}

/// §7: "Add nullable column" → OnlineSafe iff there is no default;
/// nullability is orthogonal to the volatility classification.
///
/// A nullable add with a volatile default (`gen_random_uuid()` /
/// `random()` / `clock_timestamp()`) still requires the 3-step
/// ExpandContract pattern: Pg18's catalog-only fast-path is gated on
/// the default being non-volatile, and Postgres evaluates the default
/// once-per-row at backfill time regardless of the column's NULL
/// permission. Both the nullable and non-nullable cases therefore route
/// through the same volatility/override pipeline.
///
/// Volatility resolution order (§820): adopter override per
/// [`ClassifyContext::default_volatility_overrides`] takes precedence
/// over the static `pg_volatility.rs` lookup, so known-safe UDFs
/// asserted via `#[field(default_volatility = "stable")]` reach the
/// Pg18 fast-path even when the static table would conservatively
/// classify the expression as VOLATILE.
fn classify_add_column(
    table: &str,
    column: &ColumnSchema,
    ctx: &ClassifyContext<'_>,
) -> OnlineSafetyClassification {
    // §7 (PR 7): "Add stored generated column" — empty existing table
    // (estimated_rows == Some(0)) routes through OnlineSafe; the
    // ALTER TABLE ADD COLUMN form applies in a single transactional
    // segment. Populated tables (Some(n) where n > 0) and unknown
    // row counts (None) classify OfflineOnly: Postgres rewrites every
    // row under AccessExclusiveLock to materialise the stored
    // expression. No `Pattern` exists for the populated case — see
    // [`crate::live_migrate::patterns::generated_column_refusal`].
    if column.generated.is_some() {
        return match ctx.estimated_rows {
            Some(0) => OnlineSafetyClassification::OnlineSafe,
            _ => OnlineSafetyClassification::OfflineOnly,
        };
    }
    let Some(default) = column.default_sql.as_deref() else {
        // No default at all. Nullable → catalog-only fast-path.
        // Non-nullable without a default → backfill required because
        // Postgres has nothing to populate the column with.
        if column.nullable {
            return OnlineSafetyClassification::OnlineSafe;
        }
        return OnlineSafetyClassification::ExpandContract;
    };
    // Default is present — same volatility/override pipeline applies
    // regardless of nullability. Adopter override wins over the static
    // table; T3 enforces that overrides only attach to fields with a
    // default expression, so the lookup is always meaningful when
    // present.
    if let Some(override_volatility) = ctx
        .default_volatility_overrides
        .get(&(table.to_string(), column.name.clone()))
    {
        return match override_volatility {
            DefaultVolatility::Immutable | DefaultVolatility::Stable => {
                OnlineSafetyClassification::OnlineSafe
            }
            DefaultVolatility::Volatile => OnlineSafetyClassification::ExpandContract,
        };
    }
    match classify_default_expression(default) {
        Volatility::Immutable | Volatility::Stable => OnlineSafetyClassification::OnlineSafe,
        Volatility::Volatile => OnlineSafetyClassification::ExpandContract,
    }
}

/// §7: column-type / nullability / default / check / unique / indexed
/// changes.
fn classify_column_change(
    change: &ColumnChange,
    ctx: &ClassifyContext<'_>,
) -> OnlineSafetyClassification {
    match change {
        // §7: "Add NOT NULL constraint to populated table" →
        // ExpandContract when above `validation_threshold_rows`;
        // single-statement OnlineSafe below threshold (Pg18
        // `CHECK (col IS NOT NULL) NOT VALID` + `VALIDATE` + `SET NOT
        // NULL` reduces to a direct `SET NOT NULL` on small tables).
        // The reverse direction (NOT NULL → NULL) is catalog-only.
        ColumnChange::SetNullable(now_nullable) => {
            if *now_nullable {
                OnlineSafetyClassification::OnlineSafe
            } else {
                classify_validation_against_threshold(ctx)
            }
        }

        // SET DEFAULT / DROP DEFAULT — catalog-only.
        ColumnChange::SetDefault(_) => OnlineSafetyClassification::OnlineSafe,

        // §7: "Change column type" — multiple sub-cases.
        //
        // djogi#220 — `using.is_some()` signals "this is a non-default
        // cast"; the live-plan shadow-column pattern can only emit a
        // plain SQL cast (`<col>::<to>`) and cannot replicate an
        // adopter-supplied expression in the backfill UPDATE. Route
        // such changes to `OfflineOnly` so the dispatcher never
        // receives an op whose adopter expression it would silently
        // drop. The dispatcher / pattern emitters retain a
        // belt-and-braces refusal as a defense-in-depth check (see
        // `dispatch_pattern` and the `replacement_column` /
        // `codec_transition` emitters).
        //
        // When `using.is_none()` the lock window is governed by the
        // cast pair alone and the existing pair-based dispatch
        // applies.
        ColumnChange::ChangeType { using: Some(_), .. } => OnlineSafetyClassification::OfflineOnly,
        ColumnChange::ChangeType {
            from,
            to,
            using: None,
        } => classify_type_change(from, to),

        // §7: "Add CHECK constraint to populated table" → ExpandContract
        // when above `validation_threshold_rows`; below threshold the
        // ADD CHECK validates inline as a single statement and stays
        // OnlineSafe. Pure DROP (`to = None`) is always catalog-only.
        // `from` carries the prior expression for non-lossy rollback
        // but does not change the online-safety classification — the
        // forward step's lock window is governed entirely by `to`.
        ColumnChange::SetCheck { to, .. } => {
            if to.is_some() {
                classify_validation_against_threshold(ctx)
            } else {
                OnlineSafetyClassification::OnlineSafe
            }
        }

        // §7: "Add unique constraint to populated table" → ExpandContract
        // (CREATE UNIQUE INDEX CONCURRENTLY + ADD CONSTRAINT USING
        // INDEX). Dropping a unique constraint is catalog-only.
        ColumnChange::SetUnique(new_unique) => {
            if *new_unique {
                OnlineSafetyClassification::ExpandContract
            } else {
                OnlineSafetyClassification::OnlineSafe
            }
        }

        // Implicit per-column index flag. Adding routes through index
        // classification (assume non-concurrent for the per-column
        // shortcut — operators reach for the explicit `IndexSpec` when
        // they need concurrent builds); dropping is catalog-only.
        ColumnChange::SetIndexed(now_indexed) => {
            if *now_indexed {
                OnlineSafetyClassification::ExpandContract
            } else {
                OnlineSafetyClassification::OnlineSafe
            }
        }

        // §7 (PR 7): "Change stored generated column expression on
        // populated table" → OfflineOnly. Postgres re-evaluates the
        // generation expression for every row under
        // AccessExclusiveLock, which is structurally the same lock
        // window that ExpandContract is meant to avoid — a shadow-
        // column pattern offers no relief because the row rewrite
        // still happens. See
        // [`crate::live_migrate::patterns::generated_column_refusal`]
        // for the no-`Pattern` rationale. Dropping the generation
        // (to = None) routes the same way; the catalog-only path is
        // not reachable for a generated column on a populated table.
        ColumnChange::SetGenerated { .. } => OnlineSafetyClassification::OfflineOnly,

        // Codex T22 BLOCK-3: identity-column transitions.
        // `ALTER COLUMN ADD GENERATED ... AS IDENTITY` is catalog-only
        // — Postgres allocates the sequence, no row rewrite. The
        // sequence's start value is set after MAX(c) for existing rows
        // automatically. Same for DROP IDENTITY (catalog-only) and
        // SET GENERATED kind change (catalog-only). All three route to
        // OnlineSafe.
        ColumnChange::SetIdentity { .. } => OnlineSafetyClassification::OnlineSafe,

        // Phase 8.5 djogi#217 — `COMMENT ON COLUMN <t>.<c> IS '<text>'`
        // / `IS NULL` is a catalog-only write against `pg_description`.
        // No row touch, no lock window beyond the brief catalog update.
        // OnlineSafe regardless of from/to direction.
        // Postgres docs: §"COMMENT" (no lock-window guidance because
        // `pg_description` updates are catalog-only).
        ColumnChange::SetComment { .. } => OnlineSafetyClassification::OnlineSafe,
    }
}

/// §7: "Change column type" routing.
///
/// - Identical types → OnlineSafe (no-op alter).
/// - Pg18 binary-coercible same storage (`varchar(n)` → `varchar(m)`
///   with m >= n; `text` ↔ `varchar(n)` for n large enough) →
///   OnlineSafe.
/// - Widening without rewrite (`int4` → `int8`, `int2` → `int4`) →
///   OnlineSafe.
/// - Known narrowing pairs (BIGINT → INT4, varchar(N) → varchar(M)
///   with M < N, TEXT → varchar(N), NUMERIC precision/scale loss) →
///   OfflineOnly. Narrowing risks truncation / overflow at the row
///   level; without an explicit `#[field(version, transform = ...)]`
///   signal the classifier cannot prove the conversion is lossless,
///   so it refuses the live path. When the transform field lands in a
///   later phase, this routing refines to ExpandContract when the
///   transform is present.
/// - Other / unknown type changes (ENUM rename, JSONB shape change,
///   foreign-type swaps) → ExpandContract via shadow-column pattern.
///   These cases require backfill and operator gates regardless of
///   direction.
fn classify_type_change(from: &str, to: &str) -> OnlineSafetyClassification {
    if from == to {
        return OnlineSafetyClassification::OnlineSafe;
    }
    if is_binary_coercible_widening(from, to) {
        return OnlineSafetyClassification::OnlineSafe;
    }
    if is_narrowing_or_truncating(from, to) {
        return OnlineSafetyClassification::OfflineOnly;
    }
    OnlineSafetyClassification::ExpandContract
}

/// `true` when `from → to` is a known narrowing / truncating pair.
///
/// Recognised cases per §7:
///
/// - Integer narrowing: BIGINT → INT4, BIGINT → SMALLINT, INT4 →
///   SMALLINT (overflow risk).
/// - varchar-length narrowing: `varchar(N)` → `varchar(M)` with
///   `M < N` (truncation risk).
/// - text → `varchar(N)` (truncation risk, regardless of N).
/// - NUMERIC narrowing: `numeric(p1, s1)` → `numeric(p2, s2)` with
///   `p2 < p1` or `s2 < s1` (precision / scale loss).
///
/// Pairs the classifier cannot recognise as narrowing fall through —
/// the caller then routes them via the regular ExpandContract path.
fn is_narrowing_or_truncating(from: &str, to: &str) -> bool {
    let f = from.trim().to_ascii_lowercase();
    let t = to.trim().to_ascii_lowercase();

    // Integer narrowing — canonicalise aliases first so cross-alias
    // pairs (e.g., `bigint -> int4`, `int8 -> integer`) are matched.
    if let (Some(fw), Some(tw)) = (canonical_int_width(&f), canonical_int_width(&t))
        && tw < fw
    {
        return true;
    }

    // varchar / char length narrowing — same kind, smaller length.
    if let Some((from_kind, from_len)) = parse_varchar(&f)
        && let Some((to_kind, to_len)) = parse_varchar(&t)
        && from_kind == to_kind
        && let (Some(fl), Some(tl)) = (from_len, to_len)
        && tl < fl
    {
        return true;
    }

    // text → varchar(N) — any length is potentially narrower than
    // unbounded text.
    if f == "text" && parse_varchar(&t).is_some() {
        return true;
    }

    // NUMERIC narrowing — precision, scale, or integer-digit room
    // loss. Postgres normalises `numeric(p)` to `numeric(p, 0)` so the
    // parser returns `(Some(p), Some(0))` for that form; bare
    // `numeric` returns `(None, None)`. An unbounded source classed
    // against a bounded destination is narrowing: the destination
    // imposes a ceiling that may reject existing rows. Bounded → bounded
    // narrowing compares precision, scale, and integer-digit room
    // (precision - scale) independently — `numeric(10) -> numeric(12,2)`
    // preserves all 10 integer digits and is widening, but
    // `numeric(10) -> numeric(10,2)` shrinks integer room to 8 and is
    // narrowing.
    if let Some(from) = parse_numeric_params(&f)
        && let Some(to) = parse_numeric_params(&t)
    {
        match (from, to) {
            (
                NumericTypmod::Bounded {
                    precision: fp,
                    scale: fs,
                },
                NumericTypmod::Bounded {
                    precision: tp,
                    scale: ts,
                },
            ) => {
                let from_int_digits = fp.saturating_sub(fs);
                let to_int_digits = tp.saturating_sub(ts);
                if tp < fp || ts < fs || to_int_digits < from_int_digits {
                    return true;
                }
            }
            (NumericTypmod::Unbounded, NumericTypmod::Bounded { .. }) => return true,
            _ => {}
        }
    }

    false
}

/// Canonical integer width in bits, with Postgres alias normalisation:
/// `bigint`/`int8` → 64, `integer`/`int`/`int4` → 32, `smallint`/`int2`
/// → 16. Returns `None` for non-integer SQL types.
fn canonical_int_width(name: &str) -> Option<u8> {
    match name {
        "bigint" | "int8" => Some(64),
        "integer" | "int" | "int4" => Some(32),
        "smallint" | "int2" => Some(16),
        _ => None,
    }
}

/// Postgres NUMERIC type modifier.
///
/// `numeric(p)` is normalised to `Bounded { precision: p, scale: 0 }`
/// per Postgres semantics — an omitted scale means scale-zero, not
/// "scale unknown". Bare `numeric` is `Unbounded`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NumericTypmod {
    Unbounded,
    Bounded { precision: u32, scale: u32 },
}

/// Parse `numeric(p)` / `numeric(p, s)` / bare `numeric` (and the
/// `decimal` alias). Returns `None` for non-numeric types.
fn parse_numeric_params(t: &str) -> Option<NumericTypmod> {
    let normalized = t.trim();
    let rest = normalized
        .strip_prefix("numeric")
        .or_else(|| normalized.strip_prefix("decimal"))?
        .trim_start();
    if rest.is_empty() {
        return Some(NumericTypmod::Unbounded);
    }
    let bytes = rest.as_bytes();
    if bytes.first() != Some(&b'(') || bytes.last() != Some(&b')') {
        return None;
    }
    let inner = rest[1..rest.len() - 1].trim();
    if inner.is_empty() {
        return Some(NumericTypmod::Unbounded);
    }
    let mut parts = inner.split(',');
    let p_str = parts.next()?.trim();
    let precision: u32 = p_str.parse().ok()?;
    // Postgres semantics: `numeric(p)` ≡ `numeric(p, 0)`.
    let scale: u32 = match parts.next() {
        Some(s_str) => s_str.trim().parse().ok()?,
        None => 0,
    };
    if parts.next().is_some() {
        return None;
    }
    Some(NumericTypmod::Bounded { precision, scale })
}

/// `true` when `from → to` is a Pg18 binary-coercible widening — no
/// table rewrite required.
///
/// Recognises the common cases the spec calls out: integer widening
/// (`int2` → `int4` / `int8`; `int4` → `int8`) and varchar-length
/// widening / text broadening. Other type pairs return `false` so the
/// caller takes the conservative ExpandContract path.
fn is_binary_coercible_widening(from: &str, to: &str) -> bool {
    let f = from.trim().to_ascii_lowercase();
    let t = to.trim().to_ascii_lowercase();

    // Integer widening — Pg18 stores `int2` / `int4` / `int8` with
    // increasing storage but the catalog rewrite is fast-path because
    // the tuple header carries the width.
    let is_widening_int = matches!(
        (f.as_str(), t.as_str()),
        ("smallint", "integer")
            | ("smallint", "bigint")
            | ("integer", "bigint")
            | ("int2", "int4")
            | ("int2", "int8")
            | ("int4", "int8")
    );
    if is_widening_int {
        return true;
    }

    // Varchar-length widening — `varchar(n)` → `varchar(m)` with m >=
    // n, and `varchar(_)` → `text`. We extract the parenthesised length
    // by manual byte scan.
    if let Some((from_kind, from_len)) = parse_varchar(&f)
        && let Some((to_kind, to_len)) = parse_varchar(&t)
        && from_kind == to_kind
        && let (Some(fl), Some(tl)) = (from_len, to_len)
        && tl >= fl
    {
        return true;
    }
    if parse_varchar(&f).is_some() && t == "text" {
        return true;
    }

    false
}

/// Parse a `varchar` / `character varying` / `char` / `character` type
/// string into `(kind, optional length)`. Returns `None` for non-
/// varchar-family types.
///
/// `kind` is the canonical name (`"varchar"` for `varchar` or
/// `character varying`; `"char"` for `char` or `character`). The
/// length is `None` when no parenthesised length is present.
fn parse_varchar(t: &str) -> Option<(&'static str, Option<u32>)> {
    let normalized = t.trim();
    let (kind, rest) = if let Some(rest) = normalized.strip_prefix("character varying") {
        ("varchar", rest.trim_start())
    } else if let Some(rest) = normalized.strip_prefix("varchar") {
        ("varchar", rest.trim_start())
    } else if let Some(rest) = normalized.strip_prefix("character") {
        ("char", rest.trim_start())
    } else if let Some(rest) = normalized.strip_prefix("char") {
        ("char", rest.trim_start())
    } else {
        return None;
    };
    if rest.is_empty() {
        return Some((kind, None));
    }
    // Expect `(<digits>)` — anything else is a different type that
    // happens to share a prefix.
    let bytes = rest.as_bytes();
    if bytes.first() != Some(&b'(') || bytes.last() != Some(&b')') {
        return None;
    }
    let inner = &rest[1..rest.len() - 1].trim();
    let len: u32 = inner.parse().ok()?;
    Some((kind, Some(len)))
}

/// §7: "Add FK on tables ≤ threshold rows" → OnlineSafe; above
/// threshold (or unknown row count) → ExpandContract.
///
/// Multi-FK aggregation (4+ FKs on one table) is layered on top by
/// [`classify_delta`].
fn classify_fk_addition(ctx: &ClassifyContext<'_>) -> OnlineSafetyClassification {
    classify_validation_against_threshold(ctx)
}

/// Shared decision for the §7 family of "add validating constraint to
/// populated table" rows — CHECK additions (line 814), NOT NULL
/// additions (line 815), and FK validation (line 816). Each routes
/// through the same `validation_threshold_rows` knob so adopters get a
/// single tunable on `Djogi.toml`.
///
/// Returns [`OnlineSafetyClassification::OnlineSafe`] iff
/// `estimated_rows` is known and at-or-below the threshold; otherwise
/// [`OnlineSafetyClassification::ExpandContract`]. Unknown row count
/// (`None`) takes the conservative above-threshold path — the slower
/// staged-validation plan is always safe; the catalog-only fast path is
/// only safe when the row count is provably small.
fn classify_validation_against_threshold(ctx: &ClassifyContext<'_>) -> OnlineSafetyClassification {
    match ctx.estimated_rows {
        Some(rows) if rows <= ctx.validation_threshold_rows => {
            OnlineSafetyClassification::OnlineSafe
        }
        _ => OnlineSafetyClassification::ExpandContract,
    }
}

/// §7: "Add index" routing.
///
/// Unique indexes (`UniqueConstraint` / `UniqueIndex`) ALWAYS route
/// through `ExpandContract` regardless of the `concurrently` flag.
/// The §7 rollout for "add unique constraint to populated table" is a
/// 2-step pattern — `CREATE UNIQUE INDEX CONCURRENTLY` builds the
/// index online, then `ALTER TABLE ... ADD CONSTRAINT ... USING INDEX`
/// promotes it. Concurrency is required for the build but not
/// sufficient on its own — the operator-driven 2-step gate is what
/// `ExpandContract` represents.
///
/// Non-unique indexes:
///
/// - `concurrently = true` → `OnlineSafe` (`CREATE INDEX CONCURRENTLY`
///   runs outside a transaction and does not block writes).
/// - `concurrently = false`, `estimated_rows == Some(0)` → `OnlineSafe`
///   (PR 7 empty-table fast-path: zero-row tables hold the
///   AccessExclusiveLock for an instant; the build is structurally
///   trivial).
/// - `concurrently = false`, populated or unknown → `ExpandContract`.
///   On a populated table the lock holds for the duration of the
///   build; unknown-row-count takes the conservative path.
///
/// Hash indexes without concurrent are refused at compose time (a
/// separate validation entry point handles the refusal — out of T5
/// scope).
fn classify_index_addition(
    index: &IndexSchema,
    ctx: &ClassifyContext<'_>,
) -> OnlineSafetyClassification {
    match index.kind {
        IndexKindSchema::UniqueConstraint | IndexKindSchema::UniqueIndex => {
            // Concurrency is required but not sufficient — the operator
            // still drives the 2-step build-then-promote rollout.
            OnlineSafetyClassification::ExpandContract
        }
        IndexKindSchema::NonUnique => {
            if index.requires_out_of_transaction {
                OnlineSafetyClassification::OnlineSafe
            } else if ctx.estimated_rows == Some(0) {
                // Empty-table fast-path: zero-row tables hold the
                // AccessExclusiveLock for an instant; the build is
                // structurally trivial. Mirrors the PR 7 routing for
                // EXCLUSION + stored-generated empty-table cases.
                OnlineSafetyClassification::OnlineSafe
            } else {
                OnlineSafetyClassification::ExpandContract
            }
        }
    }
}

/// §7: replacing an index is only online when both DROP and CREATE use
/// the out-of-transaction path. Otherwise the replacement is refused
/// because a live-plan handoff cannot make the blocking index build
/// safe after the drop/create pair has already been chosen.
fn index_replacement_requires_refusal(op: &SchemaOperation, ops: &[SchemaOperation]) -> bool {
    match op {
        SchemaOperation::AddIndex(add) => ops.iter().any(|other| {
            if let SchemaOperation::DropIndex(drop) = other {
                indexes_replace_each_other(add, drop)
                    && (!add.requires_out_of_transaction || !drop.requires_out_of_transaction)
            } else {
                false
            }
        }),
        SchemaOperation::DropIndex(drop) => ops.iter().any(|other| {
            if let SchemaOperation::AddIndex(add) = other {
                indexes_replace_each_other(add, drop)
                    && (!add.requires_out_of_transaction || !drop.requires_out_of_transaction)
            } else {
                false
            }
        }),
        _ => false,
    }
}

fn indexes_replace_each_other(add: &IndexSchema, drop: &IndexSchema) -> bool {
    add.table == drop.table && index_targets_overlap(&add.target, &drop.target)
}

fn index_targets_overlap(left: &IndexTargetSchema, right: &IndexTargetSchema) -> bool {
    match (left, right) {
        (IndexTargetSchema::Columns(left_cols), IndexTargetSchema::Columns(right_cols)) => {
            left_cols.iter().any(|left_col| {
                right_cols
                    .iter()
                    .any(|right_col| left_col.name == right_col.name)
            })
        }
        _ => false,
    }
}

/// §7: "Drop table with 4+ inbound FKs" → ExpandContract; otherwise
/// FastLockDestructiveGuarded.
fn classify_drop_table(table: &str, ctx: &ClassifyContext<'_>) -> OnlineSafetyClassification {
    let inbound = ctx.inbound_fk_counts.get(table).copied().unwrap_or(0);
    if inbound >= ctx.multi_fk_threshold {
        return OnlineSafetyClassification::ExpandContract;
    }
    OnlineSafetyClassification::FastLockDestructiveGuarded
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::migrate::diff::{ColumnChange, SchemaOperation};
    use crate::migrate::schema::{
        ColumnSchema, ExclusionConstraintSchema, ExclusionElementSchema, ForeignKeySchema,
        GeneratedColumnSchema, IndexColumnSchema, IndexKindSchema, IndexNullsOrderSchema,
        IndexOrderSchema, IndexSchema, IndexTargetSchema, IndexTypeSchema, OnDeleteSchema,
        PkKindSchema,
    };

    fn nullable_column(name: &str) -> ColumnSchema {
        ColumnSchema {
            check: None,
            comment: None,
            default_sql: None,
            foreign_key: None,
            generated: None,
            identity: None,
            index_type: None,
            indexed: false,
            max_length: None,
            name: name.to_string(),
            nullable: true,
            on_delete: None,
            outbox_exclude: false,
            rationale: None,
            relation_kind: None,
            renamed_from: None,
            sequence_within: None,
            sql_type: "TEXT".to_string(),
            unique: false,
            type_change_using: None,
        }
    }

    fn non_null_column(name: &str, default: Option<&str>) -> ColumnSchema {
        ColumnSchema {
            nullable: false,
            default_sql: default.map(|s| s.to_string()),
            ..nullable_column(name)
        }
    }

    fn nullable_column_with_default(name: &str, default: &str) -> ColumnSchema {
        ColumnSchema {
            default_sql: Some(default.to_string()),
            ..nullable_column(name)
        }
    }

    fn index(name: &str, table: &str, concurrently: bool) -> IndexSchema {
        IndexSchema {
            extension_dependency: None,
            include: Vec::new(),
            index_type: IndexTypeSchema::BTree,
            kind: IndexKindSchema::NonUnique,
            name: name.to_string(),
            nulls_not_distinct: false,
            predicate: None,
            requires_out_of_transaction: concurrently,
            table: table.to_string(),
            target: IndexTargetSchema::Columns(Vec::new()),
        }
    }

    fn unique_index(
        name: &str,
        table: &str,
        kind: IndexKindSchema,
        concurrently: bool,
    ) -> IndexSchema {
        IndexSchema {
            kind,
            ..index(name, table, concurrently)
        }
    }

    fn index_on_column(name: &str, table: &str, column: &str, concurrently: bool) -> IndexSchema {
        IndexSchema {
            target: IndexTargetSchema::Columns(vec![IndexColumnSchema {
                name: column.to_string(),
                nulls: IndexNullsOrderSchema::Default,
                opclass: None,
                order: IndexOrderSchema::Asc,
            }]),
            ..index(name, table, concurrently)
        }
    }

    fn fk_for(table: &str) -> ForeignKeySchema {
        ForeignKeySchema {
            deferrable: false,
            initially_deferred: false,
            on_delete: OnDeleteSchema::Restrict,
            ref_column: "id".to_string(),
            ref_table: table.to_string(),
        }
    }

    fn ctx_app(estimated: Option<u64>) -> (BTreeMap<String, u32>, ClassifyContext<'static>) {
        // SAFETY: leak the inbound + override maps for tests so the
        // lifetimes fit ClassifyContext<'static>. Tests only —
        // production code constructs a freshly borrowed context per
        // call.
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        (
            BTreeMap::new(),
            ClassifyContext {
                estimated_rows: estimated,
                validation_threshold_rows: 100_000,
                multi_fk_threshold: 4,
                logging_profile: LoggingProfile::Balanced,
                target_database: TargetDatabase::Application,
                inbound_fk_counts: inbound,
                default_volatility_overrides: overrides,
            },
        )
    }

    #[test]
    fn add_nullable_column_classifies_as_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: nullable_column("nickname"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_non_null_column_with_constant_default_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("score", Some("0")),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn ddl_metadata_catalog_writes_classify_online_safe() {
        let (_unused, ctx) = ctx_app(Some(10));
        let table_comment = SchemaOperation::SetTableComment {
            table: "users".to_string(),
            from: None,
            to: Some("Users table".to_string()),
        };
        let storage_params = SchemaOperation::SetStorageParams {
            table: "users".to_string(),
            from: None,
            to: Some("fillfactor=70".to_string()),
        };

        assert_eq!(
            classify_operation(&table_comment, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
        assert_eq!(
            classify_operation(&storage_params, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn set_tablespace_classifies_offline_only() {
        let (_unused, ctx) = ctx_app(Some(10));
        let op = SchemaOperation::SetTablespace {
            table: "users".to_string(),
            from: None,
            to: Some("fastspace".to_string()),
        };

        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn add_non_null_column_with_now_default_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("created_at", Some("now()")),
        };
        // `now()` is STABLE — Pg18 catalog-only fast-path applies.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_non_null_column_with_volatile_default_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("token", Some("gen_random_uuid()")),
        };
        // `gen_random_uuid()` is VOLATILE — 3-step pattern required.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_nullable_column_with_random_default_is_expand_contract() {
        // Spec-correctness: `ADD COLUMN <nullable> DEFAULT random()`
        // STILL requires the 3-step ExpandContract pattern. Pg18's
        // catalog-only fast-path is gated on the default being
        // non-volatile; the column's NULL permission does not change
        // the underlying volatility check. Pre-fix the classifier
        // returned OnlineSafe for any nullable add regardless of
        // default volatility.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: nullable_column_with_default("seed", "random()"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_nullable_column_with_gen_random_uuid_default_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: nullable_column_with_default("token", "gen_random_uuid()"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_nullable_column_with_clock_timestamp_default_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "events".to_string(),
            column: nullable_column_with_default("logged_at", "clock_timestamp()"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_nullable_column_with_stable_default_is_online_safe() {
        // Confirms the pipeline handles the non-volatile case for
        // nullable columns too — `now()` is STABLE, catalog-only
        // fast-path still applies.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "events".to_string(),
            column: nullable_column_with_default("logged_at", "now()"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_non_null_column_without_default_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("required", None),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn drop_column_is_fast_lock_destructive_guarded() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::DropColumn {
            table: "users".to_string(),
            column: "old".to_string(),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::FastLockDestructiveGuarded
        );
    }

    #[test]
    fn rename_column_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::RenameColumn {
            table: "users".to_string(),
            from: "name".to_string(),
            to: "full_name".to_string(),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn tighten_nullability_above_threshold_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(500_000));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email".to_string(),
            change: ColumnChange::SetNullable(false),
        };
        // Populated table above threshold — staged
        // `CHECK (col IS NOT NULL) NOT VALID` + `VALIDATE` + `SET NOT
        // NULL` per §815.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn tighten_nullability_below_threshold_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(50_000));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email".to_string(),
            change: ColumnChange::SetNullable(false),
        };
        // Small table — direct `SET NOT NULL` validates inline as a
        // single statement.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn tighten_nullability_unknown_rows_is_expand_contract() {
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email".to_string(),
            change: ColumnChange::SetNullable(false),
        };
        // Unknown row count → conservative staged path.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn relax_nullability_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email".to_string(),
            change: ColumnChange::SetNullable(true),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn integer_widening_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "id".to_string(),
            change: ColumnChange::ChangeType {
                from: "integer".to_string(),
                to: "bigint".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn varchar_widening_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "name".to_string(),
            change: ColumnChange::ChangeType {
                from: "varchar(64)".to_string(),
                to: "varchar(128)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn varchar_to_text_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "bio".to_string(),
            change: ColumnChange::ChangeType {
                from: "varchar(255)".to_string(),
                to: "text".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn text_to_varchar_is_offline_only() {
        // Narrowing direction — text is unbounded, varchar(N) imposes
        // a maximum length, so the conversion risks truncation.
        // Without an explicit transform signal the classifier refuses
        // the live path.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "bio".to_string(),
            change: ColumnChange::ChangeType {
                from: "text".to_string(),
                to: "varchar(255)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn bigint_to_int4_is_offline_only() {
        // Narrowing integer pair — overflow risk on rows with values
        // outside INT4 range. Routes to OfflineOnly until an explicit
        // transform signal lets the classifier prove the conversion
        // is lossless.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "metrics".to_string(),
            column: "count".to_string(),
            change: ColumnChange::ChangeType {
                from: "bigint".to_string(),
                to: "integer".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn int4_to_smallint_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "metrics".to_string(),
            column: "count".to_string(),
            change: ColumnChange::ChangeType {
                from: "integer".to_string(),
                to: "smallint".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn type_change_with_adopter_using_is_offline_only() {
        // djogi#220 — adopter-supplied `using` signals "this is a
        // non-default cast"; the live-plan shadow-column pattern can
        // only emit a plain SQL cast in its backfill and cannot
        // replicate an adopter expression. Route to OfflineOnly
        // regardless of the cast pair.
        //
        // INTEGER → BIGINT without `using` would classify OnlineSafe
        // (benign widening), so the `using.is_some()` arm is the only
        // thing producing OfflineOnly here.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "metrics".to_string(),
            column: "count".to_string(),
            change: ColumnChange::ChangeType {
                from: "integer".to_string(),
                to: "bigint".to_string(),
                using: Some("count::BIGINT".to_string()),
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
            "adopter `using` must force OfflineOnly regardless of cast pair",
        );
    }

    #[test]
    fn varchar_narrowing_is_offline_only() {
        // varchar(20) → varchar(10) — truncation risk for rows with
        // values longer than 10 bytes.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "tag".to_string(),
            change: ColumnChange::ChangeType {
                from: "varchar(20)".to_string(),
                to: "varchar(10)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn numeric_precision_loss_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "ledger".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "numeric(20, 4)".to_string(),
                to: "numeric(10, 4)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn numeric_scale_loss_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "ledger".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "numeric(20, 4)".to_string(),
                to: "numeric(20, 2)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn numeric_scale_addition_preserving_integer_digits_is_widening() {
        // Postgres: `numeric(10)` ≡ `numeric(10, 0)` (10 integer
        // digits, 0 fractional). `numeric(10, 0) -> numeric(12, 2)`
        // keeps the same 10 integer digits and adds 2 fractional —
        // strictly widening, must NOT classify as narrowing.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "ledger".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "numeric(10)".to_string(),
                to: "numeric(12, 2)".to_string(),
                using: None,
            },
        };
        // The unknown-type-change fallback returns ExpandContract; what
        // matters is that we do NOT misclassify as OfflineOnly.
        assert_ne!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn numeric_integer_digit_room_loss_is_offline_only() {
        // `numeric(10, 0) -> numeric(10, 2)` keeps the same 10 total
        // digits but redirects 2 to fractional, shrinking integer room
        // from 10 to 8. Existing rows with 10-digit integers no longer
        // fit — narrowing.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "ledger".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "numeric(10)".to_string(),
                to: "numeric(10, 2)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn bigint_to_int4_alias_is_offline_only() {
        // Cross-alias narrowing: source uses Postgres canonical name
        // (`bigint`), destination uses int8/int4-style alias. The
        // canonicalising helper must collapse both sides to the same
        // width-keyed lattice before comparing.
        let (_unused, ctx) = ctx_app(Some(0));
        for (from, to) in [
            ("bigint", "int4"),
            ("bigint", "int2"),
            ("int8", "integer"),
            ("int8", "smallint"),
            ("int4", "smallint"),
            ("integer", "int2"),
        ] {
            let op = SchemaOperation::AlterColumn {
                table: "metrics".to_string(),
                column: "count".to_string(),
                change: ColumnChange::ChangeType {
                    from: from.to_string(),
                    to: to.to_string(),
                    using: None,
                },
            };
            assert_eq!(
                classify_operation(&op, &ctx),
                OnlineSafetyClassification::OfflineOnly,
                "{from} -> {to}"
            );
        }
    }

    #[test]
    fn unbounded_numeric_to_bounded_is_offline_only() {
        // `numeric` (unbounded) → `numeric(10, 2)` introduces a
        // precision/scale ceiling that may reject existing rows whose
        // magnitude or scale exceeds the new bound.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "ledger".to_string(),
            column: "amount".to_string(),
            change: ColumnChange::ChangeType {
                from: "numeric".to_string(),
                to: "numeric(10, 2)".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn unknown_type_change_remains_expand_contract() {
        // ENUM rename / JSONB shape change / other unknown type pair
        // — not a known narrowing or widening, so the conservative
        // ExpandContract path applies.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "status".to_string(),
            change: ColumnChange::ChangeType {
                from: "user_status_v1".to_string(),
                to: "user_status_v2".to_string(),
                using: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_fk_below_threshold_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(50_000));
        let op = SchemaOperation::AddForeignKey {
            table: "posts".to_string(),
            column: "author_id".to_string(),
            fk: fk_for("authors"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_fk_above_threshold_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(500_000));
        let op = SchemaOperation::AddForeignKey {
            table: "posts".to_string(),
            column: "author_id".to_string(),
            fk: fk_for("authors"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_fk_unknown_rows_is_expand_contract() {
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AddForeignKey {
            table: "posts".to_string(),
            column: "author_id".to_string(),
            fk: fk_for("authors"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_concurrent_index_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddIndex(index("ix_a", "users", true));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_non_concurrent_index_on_populated_table_is_expand_contract() {
        // Populated table holds AccessExclusiveLock for the duration
        // of the build — escalate to ExpandContract.
        let (_unused, ctx) = ctx_app(Some(50_000));
        let op = SchemaOperation::AddIndex(index("ix_a", "users", false));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_non_concurrent_index_on_empty_table_is_online_safe() {
        // Empty-table fast-path (PR 7 round-2 finding): zero-row
        // tables hold the AccessExclusiveLock for an instant; the
        // build is structurally trivial.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddIndex(index("ix_a", "users", false));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn add_non_concurrent_index_with_unknown_row_count_is_expand_contract() {
        // None takes the conservative ExpandContract path — the
        // classifier cannot prove the table is empty.
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AddIndex(index("ix_a", "users", false));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_concurrent_unique_constraint_is_expand_contract() {
        // Per §7: adding a unique constraint to a populated table is a
        // 2-step rollout — CREATE UNIQUE INDEX CONCURRENTLY then
        // ALTER TABLE ... ADD CONSTRAINT ... USING INDEX. Concurrency
        // alone does NOT make the constraint addition OnlineSafe; the
        // operator-driven gate is what ExpandContract represents.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddIndex(unique_index(
            "ux_email",
            "users",
            IndexKindSchema::UniqueConstraint,
            true,
        ));
        let verdict = classify_operation(&op, &ctx);
        assert_ne!(
            verdict,
            OnlineSafetyClassification::OnlineSafe,
            "unique constraint must not short-circuit to OnlineSafe even when built concurrently"
        );
        assert_eq!(verdict, OnlineSafetyClassification::ExpandContract);
    }

    #[test]
    fn add_concurrent_unique_index_is_expand_contract() {
        // Same rule applies to UniqueIndex (partial unique / NULLS NOT
        // DISTINCT) — the build is online, the promotion is the gate.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddIndex(unique_index(
            "ux_email_partial",
            "users",
            IndexKindSchema::UniqueIndex,
            true,
        ));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_non_concurrent_unique_constraint_is_expand_contract() {
        // Non-concurrent unique build holds AccessExclusiveLock for the
        // duration plus needs the constraint promotion — both reasons
        // route through ExpandContract.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddIndex(unique_index(
            "ux_email",
            "users",
            IndexKindSchema::UniqueConstraint,
            false,
        ));
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn drop_table_few_inbound_fks_is_destructive_guarded() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::DropTable("legacy".to_string());
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::FastLockDestructiveGuarded
        );
    }

    #[test]
    fn drop_table_many_inbound_fks_is_expand_contract() {
        let mut inbound: BTreeMap<String, u32> = BTreeMap::new();
        inbound.insert("legacy".to_string(), 5);
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(inbound));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::Application,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::DropTable("legacy".to_string());
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn add_enum_variant_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddEnumVariant {
            enum_name: "status".to_string(),
            variant: "ARCHIVED".to_string(),
            anchor: None,
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn drop_enum_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::DropEnum("status".to_string());
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn unsupported_op_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::Unsupported {
            reason: "partition method change".to_string(),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn classifier_is_deterministic() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("token", Some("gen_random_uuid()")),
        };
        let first = classify_operation(&op, &ctx);
        let second = classify_operation(&op, &ctx);
        let third = classify_operation(&op, &ctx);
        assert_eq!(first, second);
        assert_eq!(second, third);
        assert_eq!(first, OnlineSafetyClassification::ExpandContract);
    }

    #[test]
    fn event_log_target_short_circuits_to_online_safe() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::StrictAudit,
            target_database: TargetDatabase::EventLog,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        // Without short-circuit this would be ExpandContract; the
        // §6.5 rule routes event-log targets directly to Phase 7.
        let op = SchemaOperation::AddColumn {
            table: "events".to_string(),
            column: non_null_column("required", None),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn event_log_delta_short_circuit_is_not_overridden_by_multi_fk_aggregation() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(50),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::StrictAudit,
            target_database: TargetDatabase::EventLog,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let ops: Vec<SchemaOperation> = (0..4)
            .map(|i| SchemaOperation::AddForeignKey {
                table: "events".to_string(),
                column: format!("ref_{i}"),
                fk: fk_for("authors"),
            })
            .collect();
        let out = classify_delta(&ops, &ctx);
        for (_op, verdict) in &out {
            assert_eq!(*verdict, OnlineSafetyClassification::OnlineSafe);
        }
    }

    #[test]
    fn crud_log_under_balanced_short_circuits() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::CrudLog,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::DropColumn {
            table: "users_log".to_string(),
            column: "old".to_string(),
        };
        // Non-strict crud-log: short-circuit reports OnlineSafe so
        // Phase 7 applies the drop directly (no live plan, no
        // FastLockDestructiveGuarded gate from this layer).
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn crud_log_under_strict_audit_runs_full_classifier() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::StrictAudit,
            target_database: TargetDatabase::CrudLog,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::DropColumn {
            table: "users_log".to_string(),
            column: "old".to_string(),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::FastLockDestructiveGuarded
        );
    }

    #[test]
    fn classify_delta_skips_pk_flip_groups() {
        use crate::migrate::diff::{PkFlipDirection, PkFlipJoinTableOption, PkTypeFlipGroup};
        use crate::migrate::schema::PkKindSchema;
        let (_unused, ctx) = ctx_app(Some(0));
        let group = PkTypeFlipGroup {
            parent_table: "users".to_string(),
            parent_from: PkKindSchema::HeerId,
            parent_to: PkKindSchema::HeerIdRecencyBiased,
            direction: PkFlipDirection::AscToDesc,
            children: Vec::new(),
            self_fk: None,
            join_tables: Vec::new(),
            cycles: Vec::new(),
            partitioned_parent: None,
            co_destructive: false,
            co_lossy: false,
            join_table_option: PkFlipJoinTableOption::OptionA,
        };
        let ops = vec![
            SchemaOperation::PkTypeFlipGroup(group),
            SchemaOperation::AddColumn {
                table: "users".to_string(),
                column: nullable_column("nickname"),
            },
        ];
        let out = classify_delta(&ops, &ctx);
        assert_eq!(out.len(), 1, "PkTypeFlipGroup must be filtered out");
        assert!(matches!(out[0].0, SchemaOperation::AddColumn { .. }));
        assert_eq!(out[0].1, OnlineSafetyClassification::OnlineSafe);
    }

    #[test]
    fn classify_delta_escalates_multi_fk_additions() {
        let (_unused, ctx) = ctx_app(Some(50)); // tiny table — single FK below threshold otherwise
        let ops: Vec<SchemaOperation> = (0..4)
            .map(|i| SchemaOperation::AddForeignKey {
                table: "posts".to_string(),
                column: format!("ref_{i}"),
                fk: fk_for("authors"),
            })
            .collect();
        let out = classify_delta(&ops, &ctx);
        assert_eq!(out.len(), 4);
        for (_op, verdict) in &out {
            // Each individual FK below threshold would be OnlineSafe;
            // the aggregate multi-FK rule lifts every entry to
            // ExpandContract.
            assert_eq!(*verdict, OnlineSafetyClassification::ExpandContract);
        }
    }

    #[test]
    fn set_check_below_threshold_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(50_000));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "age".to_string(),
            change: ColumnChange::SetCheck {
                from: None,
                to: Some("age >= 0".to_string()),
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn set_check_above_threshold_is_expand_contract() {
        let (_unused, ctx) = ctx_app(Some(200_000));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "age".to_string(),
            change: ColumnChange::SetCheck {
                from: None,
                to: Some("age >= 0".to_string()),
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn set_check_unknown_rows_is_expand_contract() {
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "age".to_string(),
            change: ColumnChange::SetCheck {
                from: None,
                to: Some("age >= 0".to_string()),
            },
        };
        // Unknown row count → conservative staged-validation path.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn set_check_drop_is_online_safe_regardless_of_prior() {
        // GPT-5.5 review redesign: SetCheck now carries `from` so
        // rollback restores the prior CHECK. The classifier still
        // routes purely on `to` — a pure DROP (to = None) is
        // catalog-only regardless of whether `from` is Some or None.
        let (_unused, ctx) = ctx_app(Some(200_000));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "age".to_string(),
            change: ColumnChange::SetCheck {
                from: Some("age >= 0".to_string()),
                to: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn default_volatility_override_stable_routes_to_online_safe() {
        // Build a context whose override map asserts the default is
        // STABLE. Without the override the static table classifies
        // `gen_random_uuid()` as VOLATILE → ExpandContract.
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let mut overrides_map: BTreeMap<(String, String), DefaultVolatility> = BTreeMap::new();
        overrides_map.insert(
            ("users".to_string(), "token".to_string()),
            DefaultVolatility::Stable,
        );
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(overrides_map));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::Application,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("token", Some("gen_random_uuid()")),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn default_volatility_override_immutable_routes_to_online_safe() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let mut overrides_map: BTreeMap<(String, String), DefaultVolatility> = BTreeMap::new();
        overrides_map.insert(
            ("users".to_string(), "token".to_string()),
            DefaultVolatility::Immutable,
        );
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(overrides_map));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::Application,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("token", Some("my_pure_udf()")),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe
        );
    }

    #[test]
    fn default_volatility_override_absent_falls_through_to_static_table() {
        // No entry in the override map → static `pg_volatility.rs`
        // table classifies `gen_random_uuid()` as VOLATILE →
        // ExpandContract.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: non_null_column("token", Some("gen_random_uuid()")),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::ExpandContract
        );
    }

    #[test]
    fn pk_type_flip_dispatch_returns_offline_only_to_refuse_misuse() {
        // A misuse caller bypassing classify_delta and dispatching a
        // PK-flip op directly through classify_operation must get a
        // refused-classification verdict — not OnlineSafe — so the
        // Phase 7 runner refuses to apply rather than silently
        // fast-applying. PK-flip routing is Phase 7's exclusive
        // territory.
        use crate::migrate::diff::{PkFlipDirection, PkFlipJoinTableOption, PkTypeFlipGroup};
        use crate::migrate::schema::PkKindSchema;
        let (_unused, ctx) = ctx_app(Some(0));
        let group = PkTypeFlipGroup {
            parent_table: "users".to_string(),
            parent_from: PkKindSchema::HeerId,
            parent_to: PkKindSchema::HeerIdRecencyBiased,
            direction: PkFlipDirection::AscToDesc,
            children: Vec::new(),
            self_fk: None,
            join_tables: Vec::new(),
            cycles: Vec::new(),
            partitioned_parent: None,
            co_destructive: false,
            co_lossy: false,
            join_table_option: PkFlipJoinTableOption::OptionA,
        };
        let op = SchemaOperation::PkTypeFlipGroup(group);
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn pk_type_flip_dispatch_refuses_even_when_target_short_circuits() {
        let inbound: &'static BTreeMap<String, u32> = Box::leak(Box::new(BTreeMap::new()));
        let overrides: &'static BTreeMap<(String, String), DefaultVolatility> =
            Box::leak(Box::new(BTreeMap::new()));
        let ctx = ClassifyContext {
            estimated_rows: Some(0),
            validation_threshold_rows: 100_000,
            multi_fk_threshold: 4,
            logging_profile: LoggingProfile::Balanced,
            target_database: TargetDatabase::EventLog,
            inbound_fk_counts: inbound,
            default_volatility_overrides: overrides,
        };
        let op = SchemaOperation::PkTypeFlip {
            table: "users".to_string(),
            from: PkKindSchema::HeerId,
            to: PkKindSchema::HeerIdRecencyBiased,
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly
        );
    }

    #[test]
    fn replacement_index_with_blocking_side_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(50));
        let ops = vec![
            SchemaOperation::DropIndex(index_on_column("ix_old", "users", "email", false)),
            SchemaOperation::AddIndex(index_on_column("ix_new", "users", "email", true)),
        ];
        let out = classify_delta(&ops, &ctx);
        assert_eq!(out.len(), 2);
        for (_op, verdict) in &out {
            assert_eq!(*verdict, OnlineSafetyClassification::OfflineOnly);
        }
    }

    #[test]
    fn classify_delta_does_not_escalate_below_multi_fk_threshold() {
        let (_unused, ctx) = ctx_app(Some(50));
        let ops: Vec<SchemaOperation> = (0..3)
            .map(|i| SchemaOperation::AddForeignKey {
                table: "posts".to_string(),
                column: format!("ref_{i}"),
                fk: fk_for("authors"),
            })
            .collect();
        let out = classify_delta(&ops, &ctx);
        for (_op, verdict) in &out {
            // 3 FKs on one table, threshold is 4 — each stays at the
            // per-op verdict (OnlineSafe for low-row tables).
            assert_eq!(*verdict, OnlineSafetyClassification::OnlineSafe);
        }
    }

    // ── Phase 7.5 PR 7: EXCLUSION + stored-generated classification ──

    fn exclusion(name: &str) -> ExclusionConstraintSchema {
        ExclusionConstraintSchema {
            deferrable: false,
            elements: vec![ExclusionElementSchema {
                expr: "room_id".to_string(),
                with_operator: "=".to_string(),
            }],
            extension_dependency: None,
            initially_deferred: false,
            name: name.to_string(),
            using: "gist".to_string(),
            where_clause: None,
        }
    }

    fn generated_column(name: &str, expression: &str) -> ColumnSchema {
        ColumnSchema {
            generated: Some(GeneratedColumnSchema {
                expression: expression.to_string(),
                stored: true,
            }),
            ..nullable_column(name)
        }
    }

    #[test]
    fn add_exclusion_constraint_on_empty_table_is_online_safe() {
        // Empty existing table (estimated_rows == Some(0)) →
        // OnlineSafe. The ALTER TABLE inline form applies in a single
        // transactional segment.
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddExclusionConstraint {
            table: "bookings".to_string(),
            exclusion: exclusion("no_overlap"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe,
        );
    }

    #[test]
    fn add_exclusion_constraint_on_populated_table_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(1_000_000));
        let op = SchemaOperation::AddExclusionConstraint {
            table: "bookings".to_string(),
            exclusion: exclusion("no_overlap"),
        };
        // Populated tables: Pg18 has no `NOT VALID` for `EXCLUDE`, so
        // the live-plan layer refuses; operator must hand-edit under
        // a maintenance window per the v3 plan.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }

    #[test]
    fn add_exclusion_constraint_with_unknown_row_count_is_offline_only() {
        // Unknown row count (None) takes the conservative OfflineOnly
        // path — the classifier cannot prove the table is empty.
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AddExclusionConstraint {
            table: "bookings".to_string(),
            exclusion: exclusion("no_overlap"),
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }

    #[test]
    fn drop_exclusion_constraint_classifies_as_online_safe() {
        let (_unused, ctx) = ctx_app(Some(1_000_000));
        let op = SchemaOperation::DropExclusionConstraint {
            table: "bookings".to_string(),
            name: "no_overlap".to_string(),
            exclusion: exclusion("no_overlap"),
        };
        // DROP CONSTRAINT releases the underlying GiST index; catalog-
        // only operation regardless of row count.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe,
        );
    }

    #[test]
    fn add_stored_generated_column_on_empty_table_is_online_safe() {
        let (_unused, ctx) = ctx_app(Some(0));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: generated_column("email_lower", "LOWER(email)"),
        };
        // Empty existing table: the rewrite is a no-op data-wise; the
        // ALTER TABLE ADD COLUMN form applies in a single
        // transactional segment.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OnlineSafe,
        );
    }

    #[test]
    fn add_stored_generated_column_on_populated_table_is_offline_only() {
        let (_unused, ctx) = ctx_app(Some(50_000));
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: generated_column("email_lower", "LOWER(email)"),
        };
        // Populated tables: Postgres rewrites every row under
        // AccessExclusiveLock to materialise the stored expression.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }

    #[test]
    fn add_stored_generated_column_with_unknown_row_count_is_offline_only() {
        let (_unused, ctx) = ctx_app(None);
        let op = SchemaOperation::AddColumn {
            table: "users".to_string(),
            column: generated_column("email_lower", "LOWER(email)"),
        };
        // Unknown row count → conservative offline path.
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }

    #[test]
    fn alter_column_set_generated_classifies_as_offline_only() {
        let (_unused, ctx) = ctx_app(Some(50));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email_lower".to_string(),
            change: ColumnChange::SetGenerated {
                from: None,
                to: Some(GeneratedColumnSchema {
                    expression: "LOWER(email)".to_string(),
                    stored: true,
                }),
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }

    #[test]
    fn alter_column_drop_generated_also_classifies_as_offline_only() {
        // Even removing the generated expression is OfflineOnly: Pg
        // re-evaluates the column's storage state under
        // AccessExclusiveLock. There is no online path; the operator
        // hand-edits a DROP+ADD COLUMN sequence.
        let (_unused, ctx) = ctx_app(Some(50));
        let op = SchemaOperation::AlterColumn {
            table: "users".to_string(),
            column: "email_lower".to_string(),
            change: ColumnChange::SetGenerated {
                from: Some(GeneratedColumnSchema {
                    expression: "LOWER(email)".to_string(),
                    stored: true,
                }),
                to: None,
            },
        };
        assert_eq!(
            classify_operation(&op, &ctx),
            OnlineSafetyClassification::OfflineOnly,
        );
    }
}