autumn-web 0.6.0

An opinionated, convention-over-configuration web framework for Rust
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
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
//! Database connection pool and extractor.
//!
//! This module provides async Postgres connectivity via `diesel-async` with
//! the `deadpool` connection pool. The pool is created at startup by
//! [`AppBuilder::run`](crate::app::AppBuilder::run) and stored in
//! [`crate::state::AppState`].
//!
//! When no `database.primary_url` or legacy `database.url` is configured,
//! [`create_pool`] returns `Ok(None)` and the application runs without a
//! database -- useful for static-site or API-gateway use cases.
//!
//! # The [`Db`] extractor
//!
//! Declare `db: Db` in your handler signature to get a pooled connection.
//! The connection is automatically returned to the pool when `Db` is dropped
//! at the end of the request.
//!
//! ```rust,no_run
//! use autumn_web::prelude::*;
//!
//! #[get("/hello")]
//! async fn hello(db: Db) -> AutumnResult<String> {
//!     // Use `db` with Diesel queries...
//!     Ok("hello from db".to_string())
//! }
//! ```

use axum::extract::FromRequestParts;
use diesel;
// Named by the default Postgres pool builder/TLS connector and by the
// Postgres migration connection (`MigrationConnection`), which stays compiled
// under the `sqlite` feature (diesel's `postgres` backend is still in the graph
// via `db`), so this import is used on both builds.
use diesel_async::AsyncPgConnection;
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::pooled_connection::deadpool::Pool;
use futures::FutureExt as _;
use std::any::Any;
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tracing::Instrument as _;

use crate::config::DatabaseConfig;
use crate::error::AutumnError;

/// The database connection type the runtime is built against.
///
/// Defaults to Postgres (`AsyncPgConnection`). Enabling the crate's `sqlite`
/// feature flips it to a `SQLite` connection. Threading this alias (instead of a
/// hard-coded `AsyncPgConnection`) through the connection-typed surface — the
/// pool, the pooled connection, the transaction/query helpers, and the
/// `#[repository]`/`#[model]` generated code — is what lets the same codebase
/// target either backend.
///
/// FEATURE-UNIFICATION HAZARD: the `sqlite` feature must ONLY be enabled by an
/// end application (or an explicit `--features sqlite` build). If any workspace
/// crate or dev-dependency enables it, cargo feature unification flips this
/// alias for every consumer in the graph and breaks the Postgres default. See
/// the doc comment above `sqlite = []` in `autumn/Cargo.toml`.
#[cfg(not(feature = "sqlite"))]
pub type RuntimeConnection = diesel_async::AsyncPgConnection;
/// See [`RuntimeConnection`] (Postgres variant) for the full contract. Under
/// the `sqlite` feature the runtime is built against a `SQLite` connection.
#[cfg(feature = "sqlite")]
pub type RuntimeConnection =
    diesel_async::sync_connection_wrapper::SyncConnectionWrapper<diesel::SqliteConnection>;

/// The diesel query backend the runtime is built against — the companion of
/// [`RuntimeConnection`].
///
/// Defaults to Postgres (`diesel::pg::Pg`); the `sqlite` feature flips it to
/// `diesel::sqlite::Sqlite`. Generated `#[repository]`/`#[model]` CRUD names
/// this alias (as `::autumn_web::RuntimeBackend`) wherever a diesel
/// `QueryFragment<_>` / `SelectableHelper<_>` bound must resolve to the *active*
/// backend rather than a hard-coded `Pg`. Threading it (instead of `Pg`) through
/// the always-emitted upsert set-clause and boxed-query types is what lets the
/// same generated code type-check on either backend. Genuinely Postgres-only
/// query fragments (advisory-lock upserts, FTS `searchable`) keep an explicit
/// `Pg` bound — they are not portable and are cfg-gated off under `sqlite`.
#[cfg(not(feature = "sqlite"))]
pub type RuntimeBackend = diesel::pg::Pg;
/// See [`RuntimeBackend`] (Postgres variant). Under the `sqlite` feature the
/// runtime query backend is `diesel::sqlite::Sqlite`.
#[cfg(feature = "sqlite")]
pub type RuntimeBackend = diesel::sqlite::Sqlite;

// ── After-commit callback infrastructure ─────────────────────────────────────

/// A boxed async callback registered for post-transaction execution.
///
/// Stored in [`AFTER_COMMIT_REGISTRY`] during an active [`Db::tx`] block.
/// The registry is drained and each callback is awaited after the transaction
/// commits successfully. On rollback or panic the callbacks are dropped
/// without being called.
pub type CommitCallback = Box<
    dyn FnOnce() -> Pin<Box<dyn Future<Output = crate::AutumnResult<()>> + Send + 'static>>
        + Send
        + 'static,
>;

tokio::task_local! {
    /// Task-local registry used by [`Db::tx`] to accumulate after-commit
    /// callbacks. Only set while the [`Db::tx`] future is being polled;
    /// absent outside a transaction block.
    pub static AFTER_COMMIT_REGISTRY: Arc<Mutex<Vec<CommitCallback>>>;
}

/// Per-request accumulator for database query timing, used by the
/// `Server-Timing` middleware to surface `db;dur=…;desc="N queries"`.
///
/// Populated by the [`RequestQueryTimer`] connection instrumentation, which
/// [`Db::checkout`] installs only on connections checked out while this
/// task-local is scoped (i.e. while the `ServerTimingLayer` has scoped the
/// request — see [`request_db_timing_active`]). The timer only *records* when
/// the task-local is scoped; a stale one left on a reused connection has an
/// `on_start` that is a cheap bool-probe no-op formatting and recording nothing. It
/// fires on every diesel-async query (including the raw `.load()`/`.execute()`
/// calls generated repositories run). The connection instrumentation is the
/// sole writer during a request — helpers like [`run_instrumented`]
/// deliberately do *not* record here, to avoid double-counting the same
/// statement. When the middleware is disabled the scope is absent, so opted-out
/// requests pay only that per-query bool probe (no `DebugQuery` formatting, no
/// allocation).
#[derive(Default, Debug)]
pub(crate) struct RequestDbTimings {
    /// Total elapsed time across all DB queries for this request, in
    /// microseconds. Microseconds keep the header format (`f64` ms rounded
    /// to three decimals) consistent with the access-log clock without an
    /// f64 atomic.
    pub(crate) total_us: AtomicU64,
    /// Number of instrumented DB queries this request performed.
    pub(crate) query_count: std::sync::atomic::AtomicUsize,
}

tokio::task_local! {
    /// Task-local accumulator populated by DB instrumentation and read by
    /// the `Server-Timing` middleware. See [`RequestDbTimings`].
    pub(crate) static REQUEST_DB_TIMINGS: Arc<RequestDbTimings>;
}

tokio::task_local! {
    /// Task-local sink for request-wide SQL query capture, independent of the
    /// `Server-Timing` timing accumulator ([`REQUEST_DB_TIMINGS`]).
    ///
    /// Scoped by the test harness (`RequestBuilder::send`) around the whole
    /// request so every instrumented statement is retained as a
    /// [`crate::inspector::QueryRecord`] for `TestResponse` query-count / N+1
    /// assertions. Kept as a separate task-local lane — not folded into
    /// `RequestDbTimings` — so query capture is unaffected by how the
    /// `Server-Timing` middleware scopes (and nests) its per-scope timing
    /// accumulators. Absent in production, where the capture lane is never
    /// scoped and nothing is retained.
    #[cfg(feature = "db")]
    pub(crate) static REQUEST_QUERY_CAPTURE: Arc<Mutex<Vec<crate::inspector::QueryRecord>>>;
}

/// Strip the trailing `-- binds: [...]` annotation that diesel's `DebugQuery`
/// `Display` appends to a query's SQL text.
///
/// diesel-async feeds each real query into the `StartQuery` instrumentation
/// event as a `DebugQuery` (see `diesel::debug_query` /
/// `AsyncPgConnection::with_prepared_statement`), whose `Display` renders
/// `"{sql} -- binds: {binds:?}"` — the format lives in
/// `diesel::query_builder::debug_query::display`
/// (`write!(f, "{query} -- binds: {debug_binds:?}")`). So the SQL text we
/// materialise from `query.to_string()` is e.g.
/// `SELECT * FROM books WHERE author_id = $1 -- binds: [1]`.
///
/// The per-row executions of an N+1 pattern share the same parameterised
/// statement (`… WHERE author_id = $1`) and differ only in their bind values
/// (`-- binds: [1]`, `-- binds: [2]`, …). If the capture path retained that
/// annotation, [`crate::inspector::normalize_sql`] (which only collapses
/// whitespace and lower-cases) would treat each per-row execution as a distinct
/// template, so [`crate::inspector::detect_n_plus_one`] would never see the
/// repetition and `assert_no_n_plus_one()` would miss the N+1 it exists to
/// catch. Truncating at the `-- binds` marker leaves the parameterised
/// statement — `$N` placeholders intact — so repeated per-row queries collapse
/// to one template.
///
/// Robust to input without the marker (transaction-control SQL diesel runs via
/// `batch_execute`, or synthetic test input): returns the input unchanged. Uses
/// the last occurrence, since diesel always appends the annotation after the
/// full statement.
#[cfg(feature = "db")]
fn strip_bind_annotation(sql: &str) -> &str {
    sql.rfind("-- binds:")
        .map_or(sql, |idx| sql[..idx].trim_end())
}

/// Record one instrumented DB query into the current request's
/// [`REQUEST_DB_TIMINGS`], if any. No-op when the task-local is unset —
/// which is the case whenever the `Server-Timing` middleware is disabled
/// or the query runs outside a request (e.g. background job).
pub(crate) fn record_request_db_query(elapsed: Duration, sql: Option<&str>) {
    // Lane 1: the `Server-Timing` timing accumulator (count + cumulative time).
    let _ = REQUEST_DB_TIMINGS.try_with(|t| {
        let micros = u64::try_from(elapsed.as_micros()).unwrap_or(u64::MAX);
        t.total_us.fetch_add(micros, Ordering::Relaxed);
        t.query_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    });

    // Lane 2: request-wide SQL capture, independent of the timing lane. Only
    // active when a test scoped `REQUEST_QUERY_CAPTURE`; a no-op otherwise
    // (production, background jobs). The two `try_with` calls are independent —
    // either lane may be active without the other — and no lock is held across
    // an await.
    #[cfg(feature = "db")]
    if let Some(sql) = sql {
        let _ = REQUEST_QUERY_CAPTURE.try_with(|sink| {
            if let Ok(mut list) = sink.lock() {
                list.push(crate::inspector::QueryRecord {
                    // Store the parameterised statement WITHOUT diesel's per-row
                    // `-- binds: [...]` annotation (the `$N` placeholders stay), so
                    // repeated per-row queries normalise to the same template and
                    // `detect_n_plus_one` can see the repetition. See
                    // [`strip_bind_annotation`].
                    sql: strip_bind_annotation(sql).to_owned(),
                    params: Vec::new(),
                    elapsed_ms: u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX),
                    location: String::new(),
                });
            }
        });
    }
}

/// Whether the current task is inside a [`REQUEST_DB_TIMINGS`] scope — i.e.
/// whether the `ServerTimingLayer` (only active when `[observability]
/// server_timing` is enabled) has wrapped this request's handler future.
///
/// Cheap: probes the task-local without cloning the `Arc`, allocating, or
/// formatting anything. Probed by [`RequestQueryTimer::on_start`] on every
/// query: when no scope is active (the production default, `server_timing`
/// unset), the timer records nothing and skips the `DebugQuery` formatting, so
/// an always-installed timer costs opted-out requests only this bool probe per
/// query — no per-statement allocation.
#[cfg(feature = "db")]
pub(crate) fn request_db_timing_active() -> bool {
    REQUEST_DB_TIMINGS.try_with(|_| ()).is_ok()
}

/// Whether the current task is inside a [`REQUEST_QUERY_CAPTURE`] scope — i.e.
/// whether the test harness (`RequestBuilder::send`) has scoped a capture sink
/// around this request.
///
/// Mirrors [`request_db_timing_active`]: a cheap task-local probe that neither
/// clones the `Arc` nor allocates. [`Db::checkout`] installs the
/// [`RequestQueryTimer`] instrumentation when EITHER this lane or the timing
/// lane is active, so query capture works even when `server_timing` is off (the
/// common test case) and no `REQUEST_DB_TIMINGS` scope exists.
#[cfg(feature = "db")]
pub(crate) fn request_query_capture_active() -> bool {
    REQUEST_QUERY_CAPTURE.try_with(|_| ()).is_ok()
}

/// diesel-async [`Instrumentation`](diesel::connection::Instrumentation)
/// that feeds every executed statement into the per-request
/// [`REQUEST_DB_TIMINGS`] accumulator for the `Server-Timing` `db` metric.
///
/// Installed on a pooled connection at [`Db::checkout`] only when a
/// [`REQUEST_DB_TIMINGS`] scope is active (see [`request_db_timing_active`]),
/// i.e. only for requests the `ServerTimingLayer` is measuring — so opted-out
/// requests never install it and pay no per-query overhead. Because generated
/// repositories run raw `diesel-async` `.load()`/`.execute()` (they do not go
/// through [`run_instrumented`]), the connection-level instrumentation is the
/// only thing that observes those queries. It brackets each statement between
/// the `StartQuery`/`FinishQuery` events the connection emits and records the
/// elapsed wall time via [`record_request_db_query`], which is a no-op when no
/// request has scoped the task-local (background jobs, or the middleware being
/// disabled) — so it never panics off-request.
///
/// Only genuine application statements are timed. The dedicated
/// connection-establish, prepared-statement-cache, and
/// `BeginTransaction`/`CommitTransaction`/`RollbackTransaction` events are
/// ignored outright. Crucially, diesel-async runs the transaction-control SQL
/// itself (`BEGIN`, `COMMIT`, `ROLLBACK`, and the `SAVEPOINT`/`RELEASE`
/// variants for nested transactions) through `batch_execute`, which emits a
/// `StartQuery`/`FinishQuery` pair with that SQL — exactly like a real query.
/// Left unfiltered, a transaction wrapping a single `SELECT` would report
/// `desc="3 queries"` (BEGIN + SELECT + COMMIT) and fold begin/commit latency
/// into `db;dur`. Likewise [`Db::checkout`] issues a `SET statement_timeout`
/// housekeeping statement on every checkout, which would otherwise add a bogus
/// `+1 query` to every request before any app SQL runs. So at `StartQuery` we
/// inspect the statement text and skip timing when it is a housekeeping /
/// transaction-control command (see
/// [`RequestQueryTimer::is_uncounted_statement`]). Queries on a single
/// connection are strictly sequential (`&mut conn`), so a single
/// `Option<Instant>` slot is sufficient — there is no overlap between a start
/// and its finish, and skipping a start leaves the slot empty so the matching
/// finish is a no-op.
///
/// The timer is installed at [`Db::checkout`] (before the housekeeping `SET`)
/// **only when a `REQUEST_DB_TIMINGS` scope is active** — i.e. only for requests
/// the `ServerTimingLayer` is measuring. This gate matters because
/// `set_instrumentation` wholesale replaces the connection's instrumentation:
/// installing unconditionally would clobber any global default an application
/// registered via `diesel::connection::set_default_instrumentation` (query
/// logging, tracing, metrics), even when `server_timing` is disabled. Gating on
/// the scope leaves an opted-out app's instrumentation fully intact. When the
/// scope *is* active, installing a fresh timer per checkout also clears any
/// stale timer a pooled connection carried from a prior timed request. A stale
/// timer left on a connection later reused by an opted-out request is a cheap
/// no-op: `on_start` probes [`request_db_timing_active`] before doing any work,
/// so it never formats SQL or allocates off-scope.
///
/// Autumn does not currently *compose* with an app-registered
/// `set_default_instrumentation` — while `server_timing` is enabled its timer
/// replaces the app's on measured checkouts. This is a documented limitation
/// (`server_timing` is a dev/off-by-default feature); apps needing both should
/// keep it disabled in that environment.
#[cfg(feature = "db")]
#[derive(Default, Debug)]
pub(crate) struct RequestQueryTimer {
    /// The currently in-flight statement (start instant + SQL text), if any.
    pending: Option<PendingQuery>,
}

/// A statement whose `StartQuery` has fired but whose `FinishQuery` has not.
///
/// Holds the start instant (for latency) and the materialised SQL text (so a
/// capturing accumulator can retain it). Queries on a single connection are
/// strictly sequential, so a single slot suffices.
#[cfg(feature = "db")]
#[derive(Debug)]
struct PendingQuery {
    started_at: std::time::Instant,
    sql: String,
}

#[cfg(feature = "db")]
impl RequestQueryTimer {
    /// Whether `sql` is a statement that must **not** be counted as an
    /// application query in the `Server-Timing` `db` metric. Two kinds reach
    /// the connection instrumentation but are not application work:
    ///
    /// * **Transaction control** that diesel-async runs via `batch_execute`
    ///   (which emits a `StartQuery`/`FinishQuery` pair just like a real
    ///   query): `BEGIN`, `COMMIT`, `ROLLBACK`, the `SAVEPOINT`/`RELEASE`
    ///   nested-transaction variants, and the two-word `START TRANSACTION` form.
    /// * **Session/config housekeeping** — any leading-token `SET`. This covers
    ///   the `SET statement_timeout` that [`Db::checkout`] issues on every
    ///   checkout before handing the connection to the request, as well as
    ///   `SET TRANSACTION`. None of these are application queries.
    ///
    /// Matches a case-insensitive leading token in
    /// `{BEGIN, COMMIT, ROLLBACK, SAVEPOINT, RELEASE, SET}`, plus the two-word
    /// `START TRANSACTION` form. See the type-level docs.
    fn is_uncounted_statement(sql: &str) -> bool {
        let mut tokens = sql.split_whitespace();
        let Some(first) = tokens.next() else {
            return false;
        };
        match first.to_ascii_uppercase().as_str() {
            "BEGIN" | "COMMIT" | "ROLLBACK" | "SAVEPOINT" | "RELEASE" | "SET" => true,
            "START" => tokens
                .next()
                .is_some_and(|second| second.eq_ignore_ascii_case("TRANSACTION")),
            _ => false,
        }
    }

    /// Record the start of a statement.
    ///
    /// Probes both [`request_db_timing_active`] and
    /// [`request_query_capture_active`] first: when NEITHER the timing
    /// accumulator nor the capture sink is scoped (the opted-out / off-request
    /// path) the timer does nothing and — crucially — never invokes `sql`, so
    /// the caller's `DebugQuery` `to_string()` allocation is skipped entirely.
    /// This is what makes it safe to leave a `RequestQueryTimer` installed on a
    /// pooled connection that a later opted-out request reuses: the stale timer
    /// is a cheap bool-probe no-op, not a per-query allocator.
    ///
    /// When either lane *is* active, the SQL text is materialised and inspected:
    /// housekeeping / transaction-control statements (see
    /// [`Self::is_uncounted_statement`], e.g. the checkout `SET
    /// statement_timeout`) leave the slot empty so they are excluded from the
    /// accumulator; genuine application statements record their start instant.
    /// Clearing the slot keeps the matching `FinishQuery` a no-op.
    ///
    /// `sql` is a closure so the (allocating) formatting is deferred until we
    /// know the statement will be counted. Extracted from the event handler so
    /// the start/finish accounting is unit-testable without constructing a
    /// (non-exhaustive, unstable-to-build) `InstrumentationEvent`.
    fn on_start(&mut self, now: std::time::Instant, sql: impl FnOnce() -> String) {
        // Probe BOTH lanes: the timing accumulator (`server_timing`) and the
        // query-capture sink (test harness). Either being active means the
        // upcoming statement must be observed. When neither is scoped (the
        // opted-out / off-request path) the timer does nothing and never
        // invokes `sql`, so the caller's `DebugQuery` `to_string()` allocation
        // is skipped — keeping a stale installed timer a cheap bool-probe no-op.
        if !request_db_timing_active() && !request_query_capture_active() {
            self.pending = None;
            return;
        }
        // Materialise the SQL once: it is needed both for the housekeeping
        // filter and (when a test opted into capture) to retain the statement
        // text at `on_finish`.
        let sql = sql();
        self.pending = if Self::is_uncounted_statement(&sql) {
            None
        } else {
            Some(PendingQuery {
                started_at: now,
                sql,
            })
        };
    }

    /// Record the completion of the in-flight statement, accumulating its
    /// elapsed time into the per-request accumulator. A `FinishQuery` without
    /// a matching `StartQuery` is ignored.
    fn on_finish(&mut self, now: std::time::Instant) {
        if let Some(p) = self.pending.take() {
            record_request_db_query(now.saturating_duration_since(p.started_at), Some(&p.sql));
        }
    }
}

#[cfg(feature = "db")]
impl diesel::connection::Instrumentation for RequestQueryTimer {
    fn on_connection_event(&mut self, event: diesel::connection::InstrumentationEvent<'_>) {
        use diesel::connection::InstrumentationEvent;
        match event {
            InstrumentationEvent::StartQuery { query, .. } => {
                // `query` is an opaque `&dyn DebugQuery`; its `Display` impl
                // renders the SQL text, which we inspect to skip housekeeping /
                // transaction-control statements (see the type-level docs). The
                // `to_string()` is deferred behind a closure so an installed but
                // opted-out timer never pays the allocation — see `on_start`.
                self.on_start(std::time::Instant::now(), || query.to_string());
            }
            InstrumentationEvent::FinishQuery { .. } => self.on_finish(std::time::Instant::now()),
            // Ignore connection-establish, prepared-statement cache, and the
            // dedicated Begin/Commit/RollbackTransaction events — see the
            // type-level docs.
            _ => {}
        }
    }
}

/// Total count of after-commit callback errors since process start.
///
/// Incremented each time a callback registered via [`register_after_commit`]
/// or [`Db::tx`] returns an error **after** the transaction has already
/// committed. The underlying transaction is unaffected; this counter surfaces
/// failures for alerting and dashboards.
///
/// Exposed by the `/actuator/health` endpoint as the top-level
/// `autumn_after_commit_failures_total` field.
pub static AFTER_COMMIT_FAILURES_TOTAL: AtomicU64 = AtomicU64::new(0);

pub(crate) fn record_after_commit_failure() -> u64 {
    AFTER_COMMIT_FAILURES_TOTAL.fetch_add(1, Ordering::Relaxed) + 1
}

/// Total number of transaction retries triggered by a transient serialization
/// failure (`40001`) or deadlock (`40P01`) since process start.
///
/// Incremented by [`Db::tx_with`] each time a retryable transaction error is
/// re-run under a stronger isolation level. Surfaces contention on the existing
/// metrics surface (`autumn_tx_retries_total`).
pub static TX_RETRIES_TOTAL: AtomicU64 = AtomicU64::new(0);

/// Total number of transactions that exhausted their retry budget without
/// succeeding, since process start. Exposed as `autumn_tx_retry_exhausted_total`.
pub static TX_RETRY_EXHAUSTED_TOTAL: AtomicU64 = AtomicU64::new(0);

// The transaction serialization-failure retry loop is Postgres-only (SQLite's
// `tx_with` runs a single plain transaction), so these retry-metric helpers are
// unused in a `--features sqlite` library build. They are still exercised by the
// crate's unit tests, so keep them compiled and only silence the dead-code
// warning under the feature.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
pub(crate) fn record_tx_retry() -> u64 {
    TX_RETRIES_TOTAL.fetch_add(1, Ordering::Relaxed) + 1
}

#[cfg_attr(feature = "sqlite", allow(dead_code))]
pub(crate) fn record_tx_retry_exhausted() -> u64 {
    TX_RETRY_EXHAUSTED_TOTAL.fetch_add(1, Ordering::Relaxed) + 1
}

/// Guidance appended to the nested-`tx` rejection, naming the supported
/// alternative for a same-connection nested transaction.
const NESTED_TX_MESSAGE: &str = "Nested Db::tx calls are not supported; use \
    autumn_web::db::savepoint(conn, ..) inside the closure for a same-connection savepoint";

pub(crate) fn reject_ambient_after_commit_registry_for_tx() -> Result<(), AutumnError> {
    if AFTER_COMMIT_REGISTRY.try_with(|_| ()).is_ok() {
        return Err(AutumnError::bad_request_msg(NESTED_TX_MESSAGE));
    }
    Ok(())
}

pub(crate) fn spawn_committed_after_commit_callbacks(
    callbacks: Vec<CommitCallback>,
) -> Option<tokio::task::JoinHandle<()>> {
    if callbacks.is_empty() {
        return None;
    }

    Some(tokio::task::spawn(async move {
        for cb in callbacks {
            let result = match std::panic::catch_unwind(AssertUnwindSafe(cb)) {
                Ok(callback) => AssertUnwindSafe(callback).catch_unwind().await,
                Err(panic) => Err(panic),
            };

            match result {
                Ok(Ok(())) => {}
                Ok(Err(e)) => {
                    let failures_total = record_after_commit_failure();
                    tracing::error!(
                        autumn.after_commit.failures_total = failures_total,
                        "after_commit callback failed (tx already committed): {e}"
                    );
                }
                Err(panic) => {
                    let failures_total = record_after_commit_failure();
                    let panic = after_commit_panic_message(&*panic);
                    tracing::error!(
                        autumn.after_commit.failures_total = failures_total,
                        "after_commit callback panicked (tx already committed): {panic}"
                    );
                }
            }
        }
    }))
}

fn after_commit_panic_message(payload: &(dyn Any + Send)) -> String {
    match (
        payload.downcast_ref::<&'static str>(),
        payload.downcast_ref::<String>(),
    ) {
        (Some(message), _) => (*message).to_owned(),
        (_, Some(message)) => message.clone(),
        (None, None) => "non-string panic payload".to_owned(),
    }
}

/// Register a callback to run after the current database transaction commits.
///
/// If called inside a [`Db::tx`] block, the callback is deferred until the
/// transaction commits successfully. On rollback the callback is dropped
/// without being called.
///
/// The deferred callback is process-local work spawned after commit. It avoids
/// side effects for rolled-back transactions, but it is not a crash-safe
/// delivery mechanism. For side effects that must survive process exit, write a
/// durable outbox or queue row inside the same database transaction and use
/// this callback only as an optional wake-up hint.
///
/// If called **outside** any active transaction, the callback runs immediately
/// (eager execution) with a `debug`-level log note.
///
/// # Panics
///
/// Panics if the internal registry mutex is poisoned (only possible if a
/// previous thread holding the lock panicked, which should not occur in normal
/// operation).
///
/// # Example
///
/// ```rust,ignore
/// db.tx(move |conn| {
///     scoped_boxed(async move {
///         diesel::insert_into(users::table).values(&new_user).execute(conn).await?;
///         autumn_web::db::register_after_commit(|| async {
///             welcome_email_job.enqueue("user_id", user_id).await
///         }).await;
///         Ok(())
///     })
/// }).await?;
/// ```
pub async fn register_after_commit<F, Fut>(f: F)
where
    F: FnOnce() -> Fut + Send + 'static,
    Fut: Future<Output = crate::AutumnResult<()>> + Send + 'static,
{
    let mut f_opt = Some(f);
    AFTER_COMMIT_REGISTRY
        .try_with(|registry| {
            let f = f_opt.take().expect("closure only entered once");
            let boxed: CommitCallback = Box::new(move || Box::pin(f()));
            registry.lock().expect("registry lock").push(boxed);
        })
        .ok();

    // If still Some, the task-local wasn't set — we're outside a tx; run eagerly.
    if let Some(f) = f_opt {
        tracing::debug!("register_after_commit: no active transaction; running callback eagerly");
        if let Err(e) = f().await {
            let failures_total = record_after_commit_failure();
            tracing::error!(
                autumn.after_commit.failures_total = failures_total,
                "register_after_commit eager callback failed: {e}"
            );
        }
    }
}

/// Trait to abstract the state requirement for the `Db` extractor.
/// This breaks the circular dependency between the database extractor
/// and the central `AppState`.
pub trait DbState {
    /// Returns the database connection pool, if configured.
    fn pool(&self) -> Option<&Pool<RuntimeConnection>>;

    /// Returns the metrics collector, if configured.
    fn metrics(&self) -> Option<&crate::middleware::MetricsCollector> {
        None
    }

    /// Returns the read/replica connection pool, if configured.
    fn replica_pool(&self) -> Option<&Pool<RuntimeConnection>> {
        None
    }

    /// Returns the pool used for read-only work.
    ///
    /// Defaults to the replica role when present, otherwise the primary role.
    fn read_pool(&self) -> Option<&Pool<RuntimeConnection>> {
        self.replica_pool().or_else(|| self.pool())
    }

    /// Returns the configured shard set, when `[[database.shards]]`
    /// entries exist. Defaults to `None` so unsharded states need no
    /// changes.
    fn shards(&self) -> Option<&crate::sharding::ShardSet> {
        None
    }

    /// Returns any registered database connection checkout interceptors.
    fn db_interceptors(
        &self,
    ) -> Vec<std::sync::Arc<dyn crate::interceptor::DbConnectionInterceptor>> {
        Vec::new()
    }
    /// Returns the global statement timeout, if configured.
    fn statement_timeout(&self) -> Option<std::time::Duration> {
        None
    }

    /// Returns the slow query threshold.
    fn slow_query_threshold(&self) -> std::time::Duration {
        std::time::Duration::from_millis(500)
    }
}

// ── SQL telemetry helpers ─────────────────────────────────────────────────────

/// Scrub a SQL string to remove literal parameter values.
///
/// Replaces values with `?` placeholders to prevent PII leakage in
/// slow-query logs while still surfacing the query shape for performance
/// analysis.
///
/// Rules:
/// - Single-quoted string literals `'...'` → `'?'`
/// - Unquoted integer/float literals → `?`
/// - Postgres `$N` positional parameters are left untouched
///
/// # Examples
///
/// ```
/// use autumn_web::db::scrub_sql;
///
/// assert_eq!(scrub_sql("SELECT * FROM users WHERE name = 'Alice'"),
///            "SELECT * FROM users WHERE name = '?'");
/// assert_eq!(scrub_sql("SELECT * FROM orders WHERE id = 42"),
///            "SELECT * FROM orders WHERE id = ?");
/// assert_eq!(scrub_sql("SELECT * FROM t WHERE x = $1"),
///            "SELECT * FROM t WHERE x = $1");
/// ```
/// Consumes the body of an E-string escape literal and its closing `'`.
///
/// Called after the opening `'` has already been consumed. Handles
/// `\'` backslash-escaped quotes so they do not prematurely close the string.
fn consume_estring_body(chars: &mut std::iter::Peekable<std::str::Chars<'_>>) {
    loop {
        match chars.next() {
            None => break,
            Some('\'') => {
                if chars.peek() == Some(&'\'') {
                    chars.next(); // consume the doubled quote
                } else {
                    break;
                }
            }
            Some('\\') => {
                chars.next(); // skip the character after the backslash
            }
            Some(_) => {}
        }
    }
}

/// Consumes the body of a dollar-quoted string and its closing `$tag$`.
///
/// Called after the opening `$tag$` delimiter has already been consumed.
/// Uses a simple sliding-window match — sufficient for valid SQL.
fn consume_dollar_quoted_body(chars: &mut std::iter::Peekable<std::str::Chars<'_>>, tag: &str) {
    let closing: Vec<char> = format!("${tag}$").chars().collect();
    let clen = closing.len();
    let mut match_count = 0usize;
    for sc in chars.by_ref() {
        if sc == closing[match_count] {
            match_count += 1;
            if match_count == clen {
                break; // Found the closing delimiter.
            }
        } else {
            match_count = 0;
            // The current char may start a new partial match.
            if sc == closing[0] {
                match_count = 1;
            }
        }
    }
}

/// Returns true for every char that can legally precede a bare numeric
/// literal in SQL — whitespace, comparison, arithmetic, and structural chars.
#[inline]
const fn is_separator(c: char) -> bool {
    matches!(
        c,
        ' ' | '\t' | '\n'          // whitespace
        | '=' | '<' | '>'          // comparison
        | '!' | '+' | '-'          // arithmetic / negation (signed literals)
        | '*' | '/' | '%'          // arithmetic operators
        | '(' | ',' // structure
    )
}

#[must_use]
pub fn scrub_sql(sql: &str) -> String {
    let mut out = String::with_capacity(sql.len());
    // Tracks whether the last character written was a separator, so a digit
    // at the current position starts a standalone literal rather than being
    // part of an identifier like `table1` or `col2`.
    let mut prev_is_sep = true; // treat start-of-input as a separator boundary

    let mut chars = sql.chars().peekable();

    while let Some(c) = chars.next() {
        // ── E-string literal  E'...' / e'...'  (backslash-escape aware) ──
        // Must be checked before the single-quote handler so we consume the
        // `E` prefix and don't leave it in the fingerprint.
        if (c == 'E' || c == 'e') && chars.peek() == Some(&'\'') {
            chars.next(); // consume the opening '
            out.push_str("'?'");
            prev_is_sep = false;
            consume_estring_body(&mut chars);
            continue;
        }

        // ── Single-quoted string literal ─────────────────────────────────
        if c == '\'' {
            out.push_str("'?'");
            prev_is_sep = false;
            loop {
                match chars.next() {
                    None => break,
                    Some('\'') => {
                        if chars.peek() == Some(&'\'') {
                            // Escaped quote ('') — consume both, stay inside string
                            chars.next();
                        } else {
                            // Closing quote
                            break;
                        }
                    }
                    Some(_) => {}
                }
            }
            continue;
        }

        // ── Dollar sign: positional parameter or dollar-quoted string ─────
        if c == '$' {
            let next_ch = chars.peek().copied();

            // Positional parameter $N — pass through verbatim.
            if next_ch.is_some_and(|nc| nc.is_ascii_digit()) {
                out.push('$');
                prev_is_sep = false;
                while chars.peek().is_some_and(char::is_ascii_digit) {
                    if let Some(d) = chars.next() {
                        out.push(d);
                    }
                }
                continue;
            }

            // Dollar-quoted string: $$ (anonymous) or $tag$ (tagged).
            // Collect the optional tag, looking for the second `$`.
            let mut tag = String::new();
            let mut found_closing_dollar = false;

            if next_ch == Some('$') {
                // Anonymous $$: consume the second `$`.
                chars.next();
                found_closing_dollar = true;
            } else if next_ch.is_some_and(|nc| nc.is_alphabetic() || nc == '_') {
                // Accumulate tag chars until we hit `$` or a non-identifier char.
                while let Some(&tc) = chars.peek() {
                    if tc == '$' {
                        chars.next(); // consume the closing `$` of the opening tag
                        found_closing_dollar = true;
                        break;
                    } else if tc.is_alphanumeric() || tc == '_' {
                        tag.push(tc);
                        chars.next();
                    } else {
                        // Not a valid tag character — not a dollar-quoted string.
                        break;
                    }
                }
            }

            if found_closing_dollar {
                out.push_str("'?'");
                prev_is_sep = false;
                consume_dollar_quoted_body(&mut chars, &tag);
            } else {
                // Not a recognisable dollar form — emit $ and any partial tag.
                out.push('$');
                out.push_str(&tag);
                prev_is_sep = false;
            }
            continue;
        }

        // ── Unquoted numeric literal ──────────────────────────────────────
        // Only scrub when preceded by a separator to avoid stomping on
        // identifiers like `table1`, `col2`, or `alias99`.
        let is_leading_dot =
            c == '.' && prev_is_sep && chars.peek().is_some_and(char::is_ascii_digit);
        if (c.is_ascii_digit() && prev_is_sep) || is_leading_dot {
            out.push('?');
            if is_leading_dot {
                chars.next(); // consume the leading dot
            }
            // Consume integer/decimal digits, underscores, and dots.
            while chars
                .peek()
                .is_some_and(|d| d.is_ascii_digit() || *d == '.' || *d == '_')
            {
                chars.next();
            }
            // Consume optional scientific-notation exponent: e/E [+/-] <digits>.
            if chars.peek().is_some_and(|e| *e == 'e' || *e == 'E') {
                chars.next(); // consume 'e'/'E'
                if chars.peek().is_some_and(|s| *s == '+' || *s == '-') {
                    chars.next(); // consume optional sign
                }
                while chars.peek().is_some_and(char::is_ascii_digit) {
                    chars.next();
                }
            }
            prev_is_sep = false;
            continue;
        }

        // ── Regular character ─────────────────────────────────────────────
        out.push(c);
        prev_is_sep = is_separator(c);
    }

    out
}

/// Instrument a database query: time it, log slow queries with a scrubbed SQL
/// fingerprint, record metrics, and map Postgres `57014` (statement timeout)
/// to [`AutumnError::query_timeout`].
///
/// # Parameters
/// - `sql`: The raw SQL string for slow-query fingerprinting (scrubbed before logging).
/// - `route_key`: Label string used for metrics, e.g. `"GET /users"`.
/// - `slow_threshold`: Queries taking longer than this emit a `WARN` log.
/// - `metrics`: The [`crate::middleware::MetricsCollector`] to record into.
/// - `query`: The async closure that actually executes the query.
///
/// # Returns
/// The result of `query()`, with Postgres `57014` mapped to
/// [`AutumnError::query_timeout`].
///
/// # Errors
/// Returns [`AutumnError`] from the underlying query, or [`AutumnError::query_timeout`]
/// when Postgres cancels the statement due to `statement_timeout`.
pub async fn run_instrumented<F, Fut, T>(
    sql: &str,
    route_key: &str,
    slow_threshold: std::time::Duration,
    metrics: &crate::middleware::metrics::MetricsCollector,
    query: F,
) -> Result<T, AutumnError>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = Result<T, diesel::result::Error>>,
{
    let start = std::time::Instant::now();
    let result = query().await;
    let elapsed = start.elapsed();
    let elapsed_ms = u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX);

    // Record metrics regardless of success/failure
    let verb = sql.split_whitespace().next().unwrap_or("?");
    let metric_key = format!("{route_key} {verb}");
    metrics.record_db_query(&metric_key, elapsed_ms);
    // NOTE: deliberately *not* recorded into the per-request Server-Timing
    // accumulator. The connection-level `RequestQueryTimer` instrumentation
    // (installed at `Db::checkout`) already brackets every executed statement
    // — including any query run through this helper — so recording here too
    // would double-count it in `db;dur` and `desc="N queries"`.

    // Log slow queries with scrubbed SQL
    if elapsed >= slow_threshold {
        let fingerprint = scrub_sql(sql);
        tracing::warn!(
            route = %route_key,
            sql = %fingerprint,
            duration_ms = elapsed_ms,
            "slow database query"
        );
    }

    // Map result — translate Postgres 57014 to query_timeout
    result.map_err(|db_err| {
        if is_query_canceled(&db_err) {
            tracing::warn!(
                route = %route_key,
                duration_ms = elapsed_ms,
                "database query cancelled: statement_timeout exceeded"
            );
            AutumnError::query_timeout(format!(
                "Database query timed out after {elapsed_ms}ms (statement_timeout exceeded)"
            ))
        } else {
            AutumnError::from(db_err)
        }
    })
}

/// Walk `err`'s source chain looking for a `tokio_postgres` SQLSTATE matching
/// `predicate`, downcasting each link to [`tokio_postgres::Error`] and
/// [`tokio_postgres::error::DbError`] in turn. Shared by [`is_query_canceled`]
/// and [`is_retryable_txn_error`] — both need the same downcast-through-the-
/// chain strategy, only with a different SQLSTATE to look for.
fn source_chain_has_sqlstate(
    err: &(dyn std::error::Error + 'static),
    predicate: impl Fn(&tokio_postgres::error::SqlState) -> bool,
) -> bool {
    let mut source: Option<&(dyn std::error::Error + 'static)> = Some(err);
    while let Some(e) = source {
        if e.downcast_ref::<tokio_postgres::Error>()
            .and_then(tokio_postgres::Error::code)
            .is_some_and(&predicate)
        {
            return true;
        }
        if e.downcast_ref::<tokio_postgres::error::DbError>()
            .is_some_and(|db_err| predicate(db_err.code()))
        {
            return true;
        }
        source = e.source();
    }
    false
}

/// Check whether a Diesel error wraps a Postgres `57014` `query_canceled` error.
///
/// Prefers downcasting through the source chain to find a
/// [`tokio_postgres::Error`] and checking its SQL state code directly,
/// which is more robust than string-matching error messages.
fn is_query_canceled(err: &diesel::result::Error) -> bool {
    // Robust string-matching first to catch wrapped/unwrapped representations
    let err_str = err.to_string().to_lowercase();
    if err_str.contains("57014")
        || err_str.contains("query_canceled")
        || err_str.contains("canceling statement due to statement timeout")
        || err_str.contains("statement timeout")
        || err_str.contains("query canceled")
    {
        return true;
    }

    source_chain_has_sqlstate(err, |state| {
        *state == tokio_postgres::error::SqlState::QUERY_CANCELED
    })
}

/// Error type for pool creation failures.
///
/// Returned by [`create_pool`] (and the other topology builders) when a pool
/// cannot be constructed. Historically this was a bare alias for deadpool's
/// `BuildError`; it now also carries the boot-time refusal emitted for a
/// recognized-but-not-yet-wired backend (`SQLite`, issue #1614), so callers fail
/// fast at pool construction with an actionable message instead of at the first
/// query. The [`Build`](PoolError::Build) variant delegates its `Display` to the
/// underlying `BuildError`, so the Postgres path's error text is unchanged.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PoolError {
    /// The underlying deadpool builder failed (e.g., timeouts configured
    /// without a runtime, or an invalid max-size configuration).
    #[error(transparent)]
    Build(#[from] diesel_async::pooled_connection::deadpool::BuildError),

    /// A database backend that Autumn recognizes but whose runtime pool is not
    /// available in this build was configured. See
    /// [`DatabaseBackend`](crate::config::DatabaseBackend).
    #[error("{0}")]
    UnsupportedBackend(String),
}

/// Primary plus optional read-replica database pools.
#[derive(Clone)]
pub struct DatabaseTopology {
    primary: Pool<RuntimeConnection>,
    replica: Option<Pool<RuntimeConnection>>,
    /// Connection URL to target with startup migrations, when the provider
    /// resolved one at runtime that the static config doesn't carry (e.g. the
    /// managed-Postgres provider whose socket URL is only known after boot).
    /// Scoping it to the topology keeps it per-app instead of a process global.
    migration_url: Option<String>,
}

impl DatabaseTopology {
    /// Build a topology from explicit primary and optional replica pools.
    ///
    /// This is useful for custom [`DatabasePoolProvider`] implementations that
    /// need to create or decorate both roles themselves.
    #[must_use]
    pub const fn from_pools(
        primary: Pool<RuntimeConnection>,
        replica: Option<Pool<RuntimeConnection>>,
    ) -> Self {
        Self {
            primary,
            replica,
            migration_url: None,
        }
    }

    /// Build a topology from a primary pool only.
    #[must_use]
    pub const fn primary_only(primary: Pool<RuntimeConnection>) -> Self {
        Self {
            primary,
            replica: None,
            migration_url: None,
        }
    }

    /// Attach a runtime-resolved migration URL (see [`Self::migration_url`]).
    ///
    /// Providers whose primary URL isn't present in the static config — such as
    /// the managed-Postgres provider — call this so startup migrations target
    /// the pool that was actually built, without publishing the URL to a
    /// process-global shared across every app instance.
    #[must_use]
    pub fn with_migration_url(mut self, url: Option<String>) -> Self {
        self.migration_url = url;
        self
    }

    /// The runtime-resolved migration URL, if the provider supplied one.
    #[must_use]
    pub fn migration_url(&self) -> Option<&str> {
        self.migration_url.as_deref()
    }

    /// Primary/write role pool.
    #[must_use]
    pub const fn primary(&self) -> &Pool<RuntimeConnection> {
        &self.primary
    }

    /// Optional read/replica role pool.
    #[must_use]
    pub const fn replica(&self) -> Option<&Pool<RuntimeConnection>> {
        self.replica.as_ref()
    }

    /// Pool used for read-only work.
    #[must_use]
    pub fn read(&self) -> &Pool<RuntimeConnection> {
        self.replica.as_ref().unwrap_or(&self.primary)
    }
}

fn build_pool(
    url: &str,
    pool_size: usize,
    connect_timeout_secs: u64,
) -> Result<Pool<RuntimeConnection>, PoolError> {
    // Under the `sqlite` feature `RuntimeConnection` is a SQLite connection, so
    // the pool must be built over `SyncConnectionWrapper<SqliteConnection>`
    // rather than the Postgres manager below. Route to the dedicated SQLite
    // builder (PR2, issue #1614). This block is the whole function body under
    // the feature (the Postgres arms below are cfg'd out), so it is the tail
    // expression — no `return` needed.
    #[cfg(feature = "sqlite")]
    {
        build_sqlite_pool(url, pool_size, connect_timeout_secs)
    }

    // Default (Postgres) build. A SQLite target is recognized here but its
    // runtime pool only exists in a `--features sqlite` build — the pool below
    // is a Postgres pool (`Pool<AsyncPgConnection>`). Refuse at pool-build
    // (boot) time with an actionable message so a SQLite misconfiguration fails
    // fast rather than reaching a confusing first-query failure. A Postgres
    // target skips this branch entirely.
    #[cfg(not(feature = "sqlite"))]
    if crate::config::DatabaseBackend::detect(url) == Some(crate::config::DatabaseBackend::Sqlite) {
        return Err(PoolError::UnsupportedBackend(format!(
            "SQLite is a recognized database backend but its runtime pool is only available in \
             a build of autumn-web compiled with `--features sqlite`; this is a default \
             (Postgres) build (target: {url:?})"
        )));
    }

    #[cfg(not(feature = "sqlite"))]
    {
        let timeout = Duration::from_secs(connect_timeout_secs);
        // When the URL's `sslmode` asks for TLS, plug a rustls-backed connector
        // into the pool via a custom setup callback — diesel-async's default
        // establish path hardcodes `NoTls`, which cannot satisfy
        // `sslmode=require` at all. `sslmode` absent/`disable`/`prefer` keeps the
        // default (NoTls) path, so existing configurations behave exactly as
        // before. See [`tls`] for the full posture table.
        let manager = match tls::TlsPosture::from_database_url(url) {
            tls::TlsPosture::Off => AsyncDieselConnectionManager::<AsyncPgConnection>::new(url),
            posture => {
                let mut config =
                    diesel_async::pooled_connection::ManagerConfig::<AsyncPgConnection>::default();
                config.custom_setup = tls::setup_callback(posture);
                AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_config(url, config)
            }
        };
        Ok(Pool::builder(manager)
            .max_size(pool_size.max(1))
            .wait_timeout(Some(timeout))
            .create_timeout(Some(timeout))
            .runtime(deadpool::Runtime::Tokio1)
            .build()?)
    }
}

/// Normalize a configured `SQLite` target into the filename token diesel's
/// `SqliteConnection` understands.
///
/// diesel's `SQLite` backend passes the string straight to `sqlite3_open`, which
/// understands a filesystem path, a `file:` URI, or the special `:memory:`
/// token — but **not** a `sqlite:` URL scheme. Strip the recognized `SQLite` URL
/// spellings down to that: `sqlite::memory:`, `sqlite://:memory:`, and an empty
/// `sqlite://` all become an in-memory database; `sqlite:///path` /
/// `sqlite://path` / `sqlite:path` reduce to their path; a `file:` URI or a
/// bare path passes through unchanged.
#[cfg(feature = "sqlite")]
fn normalize_sqlite_target(url: &str) -> String {
    if url.starts_with("file:") {
        return url.to_owned();
    }
    let rest = url
        .strip_prefix("sqlite://")
        .or_else(|| url.strip_prefix("sqlite:"))
        .unwrap_or(url);
    if rest.is_empty() || rest == ":memory:" {
        return String::from(":memory:");
    }
    rest.to_owned()
}

/// Whether a normalized `SQLite` target names a **private** in-memory database
/// (each such connection is its own private database, so the pool must be
/// single-slot to stay consistent — see [`build_sqlite_pool`]).
///
/// Covers every in-memory spelling `SQLite` accepts through this pool: the bare
/// `:memory:` token, the `file:` URI form `file::memory:` (with or without a
/// query string — addresses Codex P1: the multi-slot default would otherwise
/// hand out connections that each see a different, empty in-memory database),
/// and any `file:` URI that asks for `mode=memory`.
///
/// A **shared-cache** in-memory database (`cache=shared`) is the deliberate
/// exception: it IS shareable across the pool's connections within one process,
/// so it must NOT be forced single-slot and returns `false` here.
#[cfg(feature = "sqlite")]
fn sqlite_target_is_memory(target: &str) -> bool {
    if target.contains("cache=shared") {
        return false;
    }
    target == ":memory:"
        || target == "file::memory:"
        || target.starts_with("file::memory:?")
        || target.contains("mode=memory")
}

/// Whether a `SQLite` database URL (any accepted spelling) resolves to **any**
/// in-memory target — the private spellings (`sqlite::memory:` / `:memory:` /
/// `file::memory:`) AND the shared-cache in-memory form
/// (`file::memory:?cache=shared`, `file:app?mode=memory&cache=shared`).
///
/// This is deliberately broader than [`sqlite_target_is_memory`] (the pool
/// *sizing* predicate): it does NOT exempt `cache=shared`. It is the predicate
/// the startup-migration reject uses, because **no** in-memory target — private
/// or shared-cache — can retain a registered migration for the runtime pool. The
/// migration runs on a transient synchronous connection; `SQLite` destroys a
/// shared in-memory database the moment its *last* connection closes, and the
/// runtime deadpool is created lazily (it may not have checked out a connection
/// yet), so the pool's first checkout opens a fresh, empty in-memory database and
/// every DB-backed request then 500s with "no such table". Only a **file-backed**
/// database survives the migration connection closing, so that is the sole
/// supported remedy.
///
/// Pool *sizing* deliberately keeps using [`sqlite_target_is_memory`] instead:
/// a shared-cache in-memory database IS shareable across the pool's connections
/// within one live process, so it must NOT be forced single-slot. The two
/// predicates answer different questions ("share across the pool?" vs. "survive
/// the migration connection closing?") and must not be conflated.
#[cfg(feature = "sqlite")]
pub(crate) fn sqlite_target_is_any_in_memory(url: &str) -> bool {
    let target = normalize_sqlite_target(url);
    target == ":memory:"
        || target == "file::memory:"
        || target.starts_with("file::memory:?")
        || target.contains("mode=memory")
}

/// Whether a normalized `SQLite` target names a **read-only** database via its
/// URI query string (`mode=ro`, or `immutable=1`/`immutable=true`).
///
/// A read-only target rejects any write, so the per-connection setup batch must
/// skip the write-affecting pragmas (`journal_mode = WAL`) that would otherwise
/// fail with "attempt to write a readonly database" and take the whole pool 503
/// (see [`build_sqlite_pool`]). The query string is parsed key-by-key
/// (case-insensitive keys and values) rather than substring-matched, so an
/// unrelated value that merely contains `ro` never trips it.
///
/// A plain file path, an in-memory target (`mode=memory`, which is not
/// read-only), and a `cache=shared` target all return `false`.
#[cfg(feature = "sqlite")]
fn sqlite_target_is_read_only(target: &str) -> bool {
    let Some((_, query)) = target.split_once('?') else {
        return false;
    };
    for pair in query.split('&') {
        let (key, value) = pair.split_once('=').unwrap_or((pair, ""));
        let (key, value) = (key.trim(), value.trim());
        if key.eq_ignore_ascii_case("mode") && value.eq_ignore_ascii_case("ro") {
            return true;
        }
        if key.eq_ignore_ascii_case("immutable")
            && (value == "1" || value.eq_ignore_ascii_case("true"))
        {
            return true;
        }
    }
    false
}

/// Build a deadpool pool over `SyncConnectionWrapper<SqliteConnection>` for a
/// `SQLite` target (issue #1614, PR2).
///
/// `SyncConnectionWrapper` runs synchronous diesel `SqliteConnection` calls on
/// Tokio's blocking pool, so this integrates with the async runtime like the
/// Postgres path. Pool sizing is deliberately conservative: `SQLite` is
/// single-writer, so a large pool only multiplies `SQLITE_BUSY` contention, and
/// an in-memory database is **private per connection** — a multi-slot pool over
/// `:memory:` would hand out connections that each see a different, empty
/// database (so migrations applied on one would be invisible on another).
/// In-memory targets are therefore forced to a single slot; file targets
/// respect the configured size (still small by convention).
#[cfg(feature = "sqlite")]
fn build_sqlite_pool(
    url: &str,
    pool_size: usize,
    connect_timeout_secs: u64,
) -> Result<Pool<RuntimeConnection>, PoolError> {
    // Under the `sqlite` feature the runtime targets SQLite. A Postgres URL here
    // is a misconfiguration — refuse with an actionable message rather than
    // trying to open a file literally named "postgres://…".
    if crate::config::DatabaseBackend::detect(url) == Some(crate::config::DatabaseBackend::Postgres)
    {
        return Err(PoolError::UnsupportedBackend(format!(
            "this build of autumn-web targets SQLite (compiled with `--features sqlite`) but the \
             configured database URL is a Postgres target; configure a `sqlite:` URL instead \
             (target: {url:?})"
        )));
    }

    let timeout = Duration::from_secs(connect_timeout_secs);
    let target = normalize_sqlite_target(url);
    let max_size = if sqlite_target_is_memory(&target) {
        1
    } else {
        pool_size.max(1)
    };
    // SQLite starts every connection with `foreign_keys` OFF, so a bare manager
    // would hand out pooled connections that silently ignore `REFERENCES`
    // constraints — orphan rows and referential-integrity violations become
    // possible for the whole app (addresses Codex P1). It also uses a default
    // busy handler that returns `SQLITE_BUSY` *immediately* when another pooled
    // connection holds the single writer lock, so ordinary overlapping writes on
    // a >1 slot file pool fail as 5xx instead of waiting briefly for the lock to
    // clear (addresses Codex P1). Install both pragmas — plus a deliberate WAL
    // journal mode with `synchronous = NORMAL` for better write concurrency —
    // during EVERY pooled connection's setup, mirroring how the sync store
    // configures its own SQLite connection (see `crate::sync::store`:
    // `busy_timeout = 5000`, `journal_mode = WAL`, `synchronous = NORMAL`,
    // `foreign_keys = ON`). `busy_timeout` is set FIRST so everything after it
    // (and every later query) queues on the timeout instead of failing on a
    // held lock; `journal_mode = WAL` is a harmless no-op for a pure `:memory:`
    // database. A `custom_setup` callback on the manager runs once per
    // newly-created connection, which is exactly the per-connection hook we need
    // (the same mechanism the Postgres path uses to install TLS).
    //
    // A **read-only** URI target (`mode=ro` / `immutable`, e.g.
    // `sqlite://file:/srv/reference.db?mode=ro`) is the exception: `journal_mode
    // = WAL` writes to the database (it rewrites the file header and creates the
    // `-wal`/`-shm` sidecars), so it fails with "attempt to write a readonly
    // database" — `custom_setup` would propagate that as a connection-setup
    // error and the pool could not service even read-only queries (`Db` routes
    // 503). For such targets we install only the non-writing per-connection
    // pragmas (`busy_timeout`, `foreign_keys`) and skip the write-affecting
    // ones, so a read-only pool builds and serves reads. In-memory targets are
    // NOT read-only and keep the full batch.
    let mut config = diesel_async::pooled_connection::ManagerConfig::<RuntimeConnection>::default();
    config.custom_setup = Box::new(|url: &str| {
        use diesel_async::{AsyncConnection as _, SimpleAsyncConnection as _};
        let url = url.to_owned();
        async move {
            let mut conn = RuntimeConnection::establish(&url).await?;
            let pragmas = if sqlite_target_is_read_only(&url) {
                // Non-writing pragmas only — WAL + synchronous would write and
                // fail on a read-only database.
                "PRAGMA busy_timeout = 5000; \
                 PRAGMA foreign_keys = ON;"
            } else {
                "PRAGMA busy_timeout = 5000; \
                 PRAGMA journal_mode = WAL; \
                 PRAGMA synchronous = NORMAL; \
                 PRAGMA foreign_keys = ON;"
            };
            conn.batch_execute(pragmas)
                .await
                .map_err(diesel::ConnectionError::CouldntSetupConfiguration)?;
            // #1910 FTS5 capability probe. Searchable repositories emit FTS5
            // virtual tables + `bm25()` ranking, and the `AddSearch` migration's
            // `CREATE VIRTUAL TABLE ... USING fts5(...)` is the hard stop that
            // fails loudly at boot if the linked SQLite lacks FTS5. Probe it here
            // (create + drop a throwaway FTS5 table in the always-writable `temp`
            // database — harmless on read-only main targets) so the failure is a
            // clear, actionable diagnostic naming FTS5 and the fix, instead of a
            // bare "no such module: fts5" surfacing from a migration. There is NO
            // silent fallback to LIKE — full-text search requires FTS5.
            if let Err(e) = conn
                .batch_execute(
                    "CREATE VIRTUAL TABLE temp.__autumn_fts5_probe USING fts5(x); \
                     DROP TABLE temp.__autumn_fts5_probe;",
                )
                .await
            {
                return Err(diesel::ConnectionError::CouldntSetupConfiguration(
                    diesel::result::Error::QueryBuilderError(
                        format!(
                            "SQLite FTS5 is not available in the linked SQLite library, but \
                             autumn-web full-text search (searchable repositories / the \
                             `--search` scaffold, issue #1910) requires it. Build with the \
                             bundled, FTS5-enabled SQLite by enabling autumn-web's `sqlite` \
                             feature (it turns on `libsqlite3-sys/bundled`, whose amalgamation \
                             defines SQLITE_ENABLE_FTS5). Underlying probe error: {e}"
                        )
                        .into(),
                    ),
                ));
            }
            Ok(conn)
        }
        .boxed()
    });
    let manager =
        AsyncDieselConnectionManager::<RuntimeConnection>::new_with_config(target, config);
    Ok(Pool::builder(manager)
        .max_size(max_size)
        .wait_timeout(Some(timeout))
        .create_timeout(Some(timeout))
        .runtime(deadpool::Runtime::Tokio1)
        .build()?)
}

/// Create a connection pool from the database configuration.
///
/// Returns `Ok(None)` if no primary database URL is configured
/// (`database.primary_url` and the legacy `database.url` are absent or `null`
/// in `autumn.toml`).
///
/// # Errors
///
/// Returns [`PoolError`] if the pool cannot be built (e.g., invalid
/// max-size configuration).
pub fn create_pool(config: &DatabaseConfig) -> Result<Option<Pool<RuntimeConnection>>, PoolError> {
    let Some(url) = config.effective_primary_url() else {
        return Ok(None);
    };

    #[cfg(feature = "sqlite")]
    reject_sqlite_statement_timeout(config.statement_timeout)?;

    let pool = build_pool(
        url,
        config.effective_primary_pool_size(),
        config.connect_timeout_secs,
    )?;

    Ok(Some(pool))
}

/// Reject a configured `database.statement_timeout` under the `SQLite` backend.
///
/// `SQLite` cannot enforce a per-statement wall-clock timeout through the async
/// connection wrapper: diesel's `SqliteConnection` exposes no
/// interrupt/progress-handler hook (nor the raw `sqlite3` handle) through
/// `SyncConnectionWrapper`, so a runaway query cannot be aborted mid-flight.
/// Rather than silently ignore a correctness guarantee we cannot honor, this
/// fails the boot fast with an actionable message (fail-closed, issue #1996).
///
/// A `None` or zero timeout (the default) asks for no guarantee and boots
/// cleanly, so ordinary `SQLite` apps are unaffected — only an operator who
/// explicitly configured a timeout we cannot meet is stopped. This is the single
/// choke point for every pool-construction entry point: the built-in
/// `create_pool`, `create_topology`, and `create_shard_topology` factories all
/// call it before the pool is built, and `setup_database` calls it once more at
/// the pool-provider dispatch boundary so a custom
/// [`DatabasePoolProvider`](crate::db::DatabasePoolProvider) — whose
/// `create_topology`/`create_shard_topology` need not route through those
/// factories — cannot bypass the guard. No configured timeout can reach a live
/// `SQLite` pool. `busy_timeout` still bounds lock waits; a real per-statement
/// timeout is tracked by #1996/#1910.
///
/// # Errors
///
/// Returns [`PoolError::UnsupportedBackend`] when `statement_timeout` is `Some`
/// and non-zero.
#[cfg(feature = "sqlite")]
pub(crate) fn reject_sqlite_statement_timeout(
    statement_timeout: Option<Duration>,
) -> Result<(), PoolError> {
    let Some(timeout) = statement_timeout.filter(|t| !t.is_zero()) else {
        return Ok(());
    };
    Err(PoolError::UnsupportedBackend(format!(
        "SQLite backend cannot enforce database.statement_timeout ({}ms): diesel's \
         SqliteConnection exposes no interrupt/progress-handler hook through the async \
         connection wrapper, so a runaway query cannot be aborted. Unset \
         database.statement_timeout for the SQLite backend (busy_timeout already bounds \
         lock waits), or run on Postgres. Tracking issue: #1996/#1910.",
        timeout.as_millis()
    )))
}

/// Reject a `SQLite` `replica_url` that cannot act as a real read replica for the
/// given `primary_url`.
///
/// Under the `sqlite` feature a configured replica cannot replicate: `SQLite` has
/// no primary/replica replication in this pool architecture. Two single-slot
/// `:memory:` pools are two *private* empty databases, and a distinct replica
/// *file* has nothing replicating into it, so read-routed queries would hit an
/// empty replica after writes and schema setup went to the primary. Reject an
/// in-memory or distinct-file replica with an actionable boot error (addresses
/// Codex P2). A replica that normalizes to the SAME file as the primary is the
/// same database — harmless — so it is allowed. `normalize_sqlite_target` is
/// reused so "same file" matches how the pool actually opens the file, and
/// `sqlite_target_is_memory` so pool sizing and this guard agree on what
/// "in-memory" means. Shared by the control-database and per-shard topologies so
/// the two rejection rules cannot drift.
#[cfg(feature = "sqlite")]
fn reject_unusable_sqlite_replica(primary_url: &str, replica_url: &str) -> Result<(), PoolError> {
    let replica_target = normalize_sqlite_target(replica_url);
    let primary_target = normalize_sqlite_target(primary_url);
    if sqlite_target_is_memory(&replica_target) || replica_target != primary_target {
        return Err(PoolError::UnsupportedBackend(format!(
            "SQLite does not support a separate read replica: replica_url {replica_url:?} \
             is in-memory or differs from primary_url {primary_url:?}. Configure only a \
             primary, or point the replica at the same database file as the primary."
        )));
    }
    Ok(())
}

/// Create primary and optional replica pools from the database configuration.
///
/// Returns `Ok(None)` when neither `database.primary_url` nor the legacy
/// `database.url` compatibility field is configured.
///
/// # Errors
///
/// Returns [`PoolError`] if either configured role cannot be built.
pub fn create_topology(config: &DatabaseConfig) -> Result<Option<DatabaseTopology>, PoolError> {
    let Some(primary_url) = config.effective_primary_url() else {
        return Ok(None);
    };

    #[cfg(feature = "sqlite")]
    reject_sqlite_statement_timeout(config.statement_timeout)?;

    let primary = build_pool(
        primary_url,
        config.effective_primary_pool_size(),
        config.connect_timeout_secs,
    )?;

    #[cfg(feature = "sqlite")]
    if let Some(replica_url) = config.replica_url.as_deref() {
        reject_unusable_sqlite_replica(primary_url, replica_url)?;
    }

    let replica = config
        .replica_url
        .as_deref()
        .map(|url| {
            build_pool(
                url,
                config.effective_replica_pool_size(),
                config.connect_timeout_secs,
            )
        })
        .transpose()?;

    Ok(Some(DatabaseTopology::from_pools(primary, replica)))
}

/// Create one shard's primary and optional replica pools, applying the
/// shard's pool-size and timeout fallbacks to the `[database]` defaults.
///
/// # Errors
///
/// Returns [`PoolError`] if either configured role cannot be built.
pub fn create_shard_topology(
    shard: &crate::config::ShardConfig,
    defaults: &DatabaseConfig,
) -> Result<DatabaseTopology, PoolError> {
    // A SQLite `[[database.shards]]` deployment reaches this via the ungated
    // `sharding::create_shard_set` → `create_shard_topology` path (the
    // transactional test-harness variant `create_shard_set_transactional` is
    // Postgres-only), so the same fail-closed guard applies per shard. The shard
    // inherits the `[database]` `statement_timeout`.
    #[cfg(feature = "sqlite")]
    reject_sqlite_statement_timeout(defaults.statement_timeout)?;

    let primary = build_pool(
        &shard.primary_url,
        shard.effective_primary_pool_size(defaults),
        defaults.connect_timeout_secs,
    )?;

    // A per-shard `replica_url` is subject to the same SQLite-replica rule as the
    // control database: without it a SQLite `[[database.shards]]` entry with a
    // distinct or in-memory replica would boot and route reads to an unrelated,
    // empty database. Reuse the exact same helper so the two rejections cannot
    // drift (addresses Codex P2).
    #[cfg(feature = "sqlite")]
    if let Some(replica_url) = shard.replica_url.as_deref() {
        reject_unusable_sqlite_replica(&shard.primary_url, replica_url)?;
    }

    let replica = shard
        .replica_url
        .as_deref()
        .map(|url| {
            build_pool(
                url,
                shard.effective_replica_pool_size(defaults),
                defaults.connect_timeout_secs,
            )
        })
        .transpose()?;

    Ok(DatabaseTopology::from_pools(primary, replica))
}

// ── Transaction options, retry, and isolation (issue #1202) ──────────────────

/// Postgres transaction isolation level, requested per call via [`TxOptions`].
///
/// `ReadCommitted` is Postgres' default and Autumn's default; the stronger
/// levels are opt-in on [`Db::tx_with`]. See the transactions guide for what
/// each level buys and costs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IsolationLevel {
    /// Postgres default. Each statement sees rows committed before it began.
    #[default]
    ReadCommitted,
    /// A snapshot fixed at the first query; no non-repeatable or phantom reads.
    RepeatableRead,
    /// Full serializability (SSI). Transactions behave as if run one at a time;
    /// conflicts surface as `40001` and are retried by [`Db::tx_with`].
    Serializable,
}

impl IsolationLevel {
    /// The SQL fragment used in tracing (`db.isolation`).
    const fn as_str(self) -> &'static str {
        match self {
            Self::ReadCommitted => "read_committed",
            Self::RepeatableRead => "repeatable_read",
            Self::Serializable => "serializable",
        }
    }
}

/// Default number of attempts (including the first) for the retrying isolation
/// levels. Chosen to match the jobs system's default retry budget.
const DEFAULT_TX_MAX_ATTEMPTS: u32 = 5;

/// Options for [`Db::tx_with`]: isolation level, access mode, and the automatic
/// retry policy for transient serialization failures.
///
/// [`TxOptions::default`] is byte-for-byte equivalent to today's [`Db::tx`]:
/// READ COMMITTED, read-write, a single attempt, and no retry.
///
/// ```rust
/// use autumn_web::db::{TxOptions, IsolationLevel};
///
/// let opts = TxOptions::serializable().read_only().max_attempts(8);
/// assert_eq!(opts.isolation, IsolationLevel::Serializable);
/// assert!(opts.read_only);
/// assert_eq!(opts.max_attempts, 8);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct TxOptions {
    /// Requested isolation level.
    pub isolation: IsolationLevel,
    /// Run the transaction `READ ONLY`.
    pub read_only: bool,
    /// Add `DEFERRABLE` (only meaningful for `SERIALIZABLE READ ONLY`).
    pub deferrable: bool,
    /// Total attempts including the first. `1` disables retry.
    pub max_attempts: u32,
    /// Backoff before the first retry; doubles each subsequent retry.
    pub initial_backoff: Duration,
    /// Upper bound on any single backoff delay.
    pub max_backoff: Duration,
}

impl Default for TxOptions {
    fn default() -> Self {
        Self {
            isolation: IsolationLevel::ReadCommitted,
            read_only: false,
            deferrable: false,
            max_attempts: 1,
            initial_backoff: Duration::from_millis(5),
            max_backoff: Duration::from_millis(500),
        }
    }
}

impl TxOptions {
    /// READ COMMITTED, no retry — identical to [`Db::tx`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// READ COMMITTED (the default), no retry.
    #[must_use]
    pub fn read_committed() -> Self {
        Self {
            isolation: IsolationLevel::ReadCommitted,
            ..Self::default()
        }
    }

    /// REPEATABLE READ with automatic retry (`serialization_failure` can occur
    /// at this level too).
    #[must_use]
    pub fn repeatable_read() -> Self {
        Self {
            isolation: IsolationLevel::RepeatableRead,
            max_attempts: DEFAULT_TX_MAX_ATTEMPTS,
            ..Self::default()
        }
    }

    /// SERIALIZABLE with automatic retry. This is the correctness-critical
    /// default: conflicts surface as `40001` and are retried transparently.
    #[must_use]
    pub fn serializable() -> Self {
        Self {
            isolation: IsolationLevel::Serializable,
            max_attempts: DEFAULT_TX_MAX_ATTEMPTS,
            ..Self::default()
        }
    }

    /// Set the isolation level, keeping the other options.
    #[must_use]
    pub const fn isolation(mut self, level: IsolationLevel) -> Self {
        self.isolation = level;
        self
    }

    /// Run the transaction `READ ONLY`.
    #[must_use]
    pub const fn read_only(mut self) -> Self {
        self.read_only = true;
        self
    }

    /// Add `DEFERRABLE` (for `SERIALIZABLE READ ONLY`).
    #[must_use]
    pub const fn deferrable(mut self) -> Self {
        self.deferrable = true;
        self
    }

    /// Set the maximum number of attempts (clamped to at least 1). A value of
    /// `1` disables retry.
    #[must_use]
    pub const fn max_attempts(mut self, attempts: u32) -> Self {
        self.max_attempts = if attempts < 1 { 1 } else { attempts };
        self
    }

    /// `max_attempts`, clamped to at least 1.
    ///
    /// The `max_attempts` field is public (struct-literal update syntax is a
    /// supported way to build a `TxOptions`), so a value of `0` can reach the
    /// struct without going through [`TxOptions::max_attempts`]'s clamp. The
    /// retry loop calls this instead of reading the field directly, so `0`
    /// always behaves like `1` (run the closure once, no retry) rather than
    /// skipping the closure entirely.
    // Only the Postgres retry loop consults this; unused in a `--features
    // sqlite` library build (still unit-tested, so keep it compiled).
    #[cfg_attr(feature = "sqlite", allow(dead_code))]
    const fn effective_max_attempts(&self) -> u32 {
        if self.max_attempts < 1 {
            1
        } else {
            self.max_attempts
        }
    }

    /// Set the backoff before the first retry.
    #[must_use]
    pub const fn initial_backoff(mut self, delay: Duration) -> Self {
        self.initial_backoff = delay;
        self
    }

    /// Set the ceiling on any single backoff delay.
    #[must_use]
    pub const fn max_backoff(mut self, delay: Duration) -> Self {
        self.max_backoff = delay;
        self
    }
}

/// Whether the retry loop should re-run the closure or return the error.
// The whole serialization-failure retry machinery is Postgres-only (see
// `Db::tx_with`); these items are unused in a `--features sqlite` library build
// but remain unit-tested, so keep them compiled and silence dead-code under the
// feature.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RetryDecision {
    Retry,
    Stop,
}

/// Decide whether a just-failed attempt should be retried.
///
/// `attempt` is the 1-indexed number of the attempt that just failed. A retry
/// happens only when the error is retryable **and** attempts remain.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
const fn retry_decision(attempt: u32, max_attempts: u32, retryable: bool) -> RetryDecision {
    if retryable && attempt < max_attempts {
        RetryDecision::Retry
    } else {
        RetryDecision::Stop
    }
}

/// Deterministic capped exponential backoff base: `initial * 2^(attempt-1)`,
/// saturating on overflow and capped at `max`.
///
/// Mirrors the jobs system's backoff shape (`pg_retry_delay_ms`) plus the cap
/// idiom from the migrate startup loop. Kept jitter-free so it is unit-testable.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
fn retry_backoff_base(initial: Duration, max: Duration, attempt: u32) -> Duration {
    // Cap the shift so `2^shift` never overflows and huge attempts saturate.
    let shift = attempt.saturating_sub(1).min(31);
    let multiplier = 1u32 << shift;
    initial.checked_mul(multiplier).unwrap_or(max).min(max)
}

/// [`retry_backoff_base`] with +/-20% jitter applied, never exceeding `max`.
///
/// Jitter decorrelates a thundering herd of transactions all retrying after the
/// same conflict. Delegates to [`crate::cache::jittered_ttl`] (rather than
/// re-implementing the RNG/fallback logic) so there's one jitter
/// implementation in the crate, including its `getrandom`-failure fallback to
/// `SystemTime` entropy instead of silently degrading to no jitter.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
fn retry_backoff_delay(initial: Duration, max: Duration, attempt: u32) -> Duration {
    let base = retry_backoff_base(initial, max, attempt);
    crate::cache::jittered_ttl(base, 0.2).min(max)
}

/// Whether `err` wraps a Postgres serialization failure (`40001`) or deadlock
/// (`40P01`) — the two transient errors that are safe to retry by re-running
/// the whole transaction.
///
/// Classification is structural and authoritative, never based on scanning an
/// arbitrary error's displayed message: a `diesel::result::Error` anywhere in
/// `err`'s source chain (not just the top-level wrapped error — a custom `E`
/// that wraps a diesel error via `#[source]`/`#[from]` is still found) is
/// classified by its `DatabaseErrorKind`. `diesel-async`'s Postgres backend
/// maps `40001` to `SerializationFailure` directly from the real SQLSTATE (see
/// `diesel_async::pg::error_helper::from_tokio_postgres_error`) — fully
/// reliable, no message inspection involved. Any *other* kind (e.g.
/// `UniqueViolation`) is trusted as non-retryable outright — never
/// string-matched, so a constraint name or key value that happens to contain
/// `"40001"` cannot misclassify it.
///
/// `40P01` (deadlock) has no dedicated `DatabaseErrorKind` and always surfaces
/// as `Unknown`. Worse: the wrapper type diesel-async uses to carry Postgres's
/// error fields for an `Unknown`-kind error (`PostgresDbErrorWrapper`, private
/// to diesel-async) implements only `DatabaseErrorInformation`, not
/// `std::error::Error` — so [`source_chain_has_sqlstate`]'s downcast walk can
/// **never** reach the real SQLSTATE for it; there is no structural path to a
/// deadlock's code at all through this crate boundary. The only signal
/// available is the message, so this checks it — but with an **exact**
/// match, not a substring: Postgres's deadlock detector always raises the
/// primary message as precisely `"deadlock detected"` with nothing else (the
/// context lives in DETAIL/HINT, not here), so an app's own `RAISE EXCEPTION`
/// would have to reproduce that exact string as its *entire* message to
/// collide — unlike a substring check, which a longer business message
/// merely mentioning the phrase would already trip.
///
/// When no `diesel::result::Error` is found anywhere in the chain, this checks
/// for a raw `tokio_postgres` SQLSTATE directly (a custom `E` that bypasses
/// diesel). If neither is found, the error is **not** retried — there is
/// deliberately no generic message-substring fallback beyond the one exact
/// match above: retrying based on bare text risks misclassifying an unrelated
/// domain/validation error as a transient conflict, silently re-running a
/// non-idempotent closure and delaying the response.
#[cfg_attr(feature = "sqlite", allow(dead_code))]
fn is_retryable_txn_error(err: &AutumnError) -> bool {
    use tokio_postgres::error::SqlState;

    fn is_retryable_sqlstate(state: &SqlState) -> bool {
        *state == SqlState::T_R_SERIALIZATION_FAILURE || *state == SqlState::T_R_DEADLOCK_DETECTED
    }

    if let Some(diesel_err) = err.downcast_chain_ref::<diesel::result::Error>() {
        return match diesel_err {
            // Fast path: diesel-async maps 40001 to SerializationFailure from
            // the real SQLSTATE. Fully structural, no message involved.
            diesel::result::Error::DatabaseError(
                diesel::result::DatabaseErrorKind::SerializationFailure,
                _,
            ) => true,
            // 40P01 (deadlock) has no dedicated kind and surfaces as Unknown.
            // The chain walk is kept for forward-compatibility (a future
            // diesel/diesel-async release, or another backend, could start
            // exposing the real SQLSTATE through the source chain) but is
            // currently unreachable for diesel-async's own Postgres backend
            // — see the doc comment above. The message check that follows is
            // therefore the only thing that actually fires deadlock retries
            // today, hence the exact (not substring) match.
            diesel::result::Error::DatabaseError(
                diesel::result::DatabaseErrorKind::Unknown,
                info,
            ) => {
                source_chain_has_sqlstate(diesel_err, is_retryable_sqlstate)
                    || info
                        .message()
                        .trim()
                        .eq_ignore_ascii_case("deadlock detected")
            }
            // Any other kind (UniqueViolation, NotNullViolation, ...) is
            // authoritatively non-retryable — no string matching against its
            // message/constraint text.
            _ => source_chain_has_sqlstate(diesel_err, is_retryable_sqlstate),
        };
    }

    // No `diesel::result::Error` anywhere in the chain — check for a raw
    // `tokio_postgres` SQLSTATE directly. No text-based fallback beyond this;
    // see the doc comment above for why.
    err.downcast_chain_ref::<tokio_postgres::Error>()
        .and_then(tokio_postgres::Error::code)
        .is_some_and(is_retryable_sqlstate)
        || err
            .downcast_chain_ref::<tokio_postgres::error::DbError>()
            .is_some_and(|db| is_retryable_sqlstate(db.code()))
}

/// Run `f` inside a transaction on `conn`, adapting the `ScopedBoxFuture`
/// callback shape used throughout Autumn's generated code to the
/// `AsyncFnOnce` callback [`diesel_async::AsyncConnection::transaction`]
/// expects since diesel-async 0.9.
///
/// This is a runtime support function for code generated by Autumn proc
/// macros. It is semver-exempt; do not call it directly.
///
/// # Errors
///
/// Returns the error from `f`, or a `diesel::result::Error` from starting,
/// committing, or rolling back the transaction.
#[doc(hidden)]
pub async fn scoped_transaction<'a, T, E, C, F>(conn: &'a mut C, f: F) -> Result<T, E>
where
    C: diesel_async::AsyncConnection + Send,
    T: Send + 'a,
    E: From<diesel::result::Error> + Send + 'a,
    F: for<'r> FnOnce(&'r mut C) -> scoped_futures::ScopedBoxFuture<'a, 'r, Result<T, E>>
        + Send
        + 'a,
{
    // Mirrors the default body of `TransactionManager::transaction` in
    // diesel-async 0.9, but drives the boxed callback future directly instead
    // of going through the `AsyncFnOnce` bounds (which reject the boxed
    // `ScopedBoxFuture` callback shape).
    use diesel_async::TransactionManager as _;

    C::TransactionManager::begin_transaction(conn).await?;
    match f(&mut *conn).await {
        Ok(value) => {
            C::TransactionManager::commit_transaction(conn).await?;
            Ok(value)
        }
        Err(user_error) => match C::TransactionManager::rollback_transaction(conn).await {
            // A broken transaction manager means the rollback error is a
            // consequence of the original error; surface the original.
            Ok(()) | Err(diesel::result::Error::BrokenTransactionManager) => Err(user_error),
            Err(rollback_error) => Err(rollback_error.into()),
        },
    }
}

/// Run a write read-modify-write closure inside a transaction that takes the
/// SQLite write lock up front (`BEGIN IMMEDIATE`).
///
/// On Postgres this delegates to [`scoped_transaction`] unchanged. On SQLite it
/// begins the transaction with `BEGIN IMMEDIATE` before running `f`, so a
/// concurrent writer queues on the connection's `busy_timeout` instead of
/// failing its deferred read→write snapshot upgrade with `SQLITE_BUSY_SNAPSHOT`
/// (which bypasses the busy handler). It is the transaction primitive for
/// generated write-RMW paths (`with_lock`, `update`, `delete_by_id`,
/// `find_or_create_by`); read-only transactions, [`Db::tx`], and [`savepoint`]
/// deliberately stay on the deferred [`scoped_transaction`] so read-only user
/// transactions keep their read concurrency.
///
/// This is a runtime support function for code generated by Autumn proc macros.
/// It is semver-exempt; do not call it directly.
///
/// # SQLite nesting parity
///
/// The `BEGIN IMMEDIATE` is issued **through** diesel's `AnsiTransactionManager`
/// (`begin_transaction_sql`) rather than as a raw statement, so the manager's
/// depth counter is synchronized (0 → 1). A nested [`savepoint`] or a
/// `TransactionManager`-driven `.transaction()` inside `f` therefore emits a
/// `SAVEPOINT` (matching Postgres) instead of a raw `BEGIN` that would fail with
/// "cannot start a transaction within a transaction". Commit and rollback also
/// route through the manager, so its depth/status bookkeeping stays correct. If
/// a `COMMIT` fails, the manager leaves the connection marked in-transaction,
/// so deadpool sees a broken transaction manager and discards the connection
/// rather than recycling it with an open write transaction.
///
/// # Errors
///
/// Returns the error from `f`, or a `diesel::result::Error` from starting or
/// committing the transaction. On SQLite a panic inside `f` rolls the
/// transaction back (through the transaction manager) before resuming the
/// unwind, so the pooled connection is never recycled with an open write
/// transaction.
#[doc(hidden)]
pub async fn scoped_immediate_transaction<'a, T, E, F>(
    conn: &'a mut RuntimeConnection,
    f: F,
) -> Result<T, E>
where
    T: Send + 'a,
    E: From<diesel::result::Error> + Send + 'a,
    F: for<'r> FnOnce(
            &'r mut RuntimeConnection,
        ) -> scoped_futures::ScopedBoxFuture<'a, 'r, Result<T, E>>
        + Send
        + 'a,
{
    crate::backend_select! {
        pg => { scoped_transaction(conn, f).await },
        sqlite => {{
            // Begin the immediate transaction THROUGH the transaction manager so
            // its depth counter is kept in sync (depth 0 → 1). Reaching the inner
            // `SqliteConnection` for `begin_transaction_sql` requires the concrete
            // `SyncConnectionWrapper<SqliteConnection>` (`spawn_blocking`), which
            // is why this helper takes `&mut RuntimeConnection` rather than a
            // generic connection.
            use diesel::connection::{AnsiTransactionManager, TransactionManager};

            // Take the write lock up front. A concurrent writer queues here on
            // `busy_timeout` instead of failing a deferred snapshot upgrade. With
            // the depth counter at 1, a nested transaction/savepoint inside `f`
            // becomes a SAVEPOINT (Postgres parity) instead of a raw nested BEGIN.
            conn.spawn_blocking(|inner| {
                AnsiTransactionManager::begin_transaction_sql(inner, "BEGIN IMMEDIATE")
            })
            .await
            .map_err(E::from)?;

            // `catch_unwind` is mandatory: a panic that unwound without a rollback
            // would leave deadpool free to recycle this connection with an open,
            // uncommitted write transaction.
            let outcome = AssertUnwindSafe(f(&mut *conn)).catch_unwind().await;
            match outcome {
                Ok(Ok(value)) => {
                    // Commit through the manager. At depth 1 it runs `COMMIT`. If
                    // that fails (e.g. a deferred-FK violation) the manager leaves
                    // the connection marked in-transaction, so its
                    // `is_broken_transaction_manager` reports broken and deadpool
                    // discards the connection on return rather than recycling it
                    // with an open write transaction — the pool never hands back a
                    // dirty connection.
                    conn.spawn_blocking(|inner| {
                        <AnsiTransactionManager as TransactionManager<
                            diesel::SqliteConnection,
                        >>::commit_transaction(inner)
                    })
                    .await
                    .map_err(E::from)?;
                    Ok(value)
                }
                Ok(Err(user_error)) => {
                    if let Err(e) = conn
                        .spawn_blocking(|inner| {
                            <AnsiTransactionManager as TransactionManager<
                                diesel::SqliteConnection,
                            >>::rollback_transaction(inner)
                        })
                        .await
                    {
                        tracing::warn!(
                            "failed to roll back immediate transaction after error: {e}"
                        );
                    }
                    Err(user_error)
                }
                Err(panic) => {
                    if let Err(e) = conn
                        .spawn_blocking(|inner| {
                            <AnsiTransactionManager as TransactionManager<
                                diesel::SqliteConnection,
                            >>::rollback_transaction(inner)
                        })
                        .await
                    {
                        tracing::error!(
                            "failed to roll back immediate transaction during panic: {e}"
                        );
                    }
                    std::panic::resume_unwind(panic);
                }
            }
        }},
    }
}

/// Run `f` inside a Postgres `SAVEPOINT` on a connection already inside a
/// transaction.
///
/// Pass the `conn` handed to a [`Db::tx`] / [`Db::tx_with`] closure. The
/// savepoint is released when `f` returns `Ok`, or rolled back (`ROLLBACK TO
/// SAVEPOINT`) when it returns `Err` — leaving the surrounding transaction
/// intact.
///
/// This is the supported way to get a nested, partially-rollbackable unit of
/// work: `Db::tx` itself cannot be re-entered on the same connection (its
/// closure receives `&mut PooledConnection`, not `&mut Db`), so a same-connection
/// savepoint can only live here, inside the closure.
///
/// # Caveat
///
/// After-commit callbacks registered inside the savepoint via
/// [`register_after_commit`] fire when the **outer** transaction commits,
/// regardless of whether this savepoint rolled back — the callback registry is
/// transaction-scoped, not savepoint-scoped.
///
/// # Errors
///
/// Returns the error from `f`, or a `diesel::result::Error` if the savepoint
/// itself cannot be established or released.
pub async fn savepoint<'a, C, T, E, F>(conn: &'a mut C, f: F) -> Result<T, E>
where
    // Generic over the connection so it works with the `&mut PooledConnection`
    // handed to a `tx` closure and the `&mut AsyncPgConnection` handed to a
    // `tx_with` closure alike.
    C: diesel_async::AsyncConnection + Send,
    T: Send + 'a,
    E: From<diesel::result::Error> + Send + 'a,
    F: for<'r> FnOnce(&'r mut C) -> scoped_futures::ScopedBoxFuture<'a, 'r, Result<T, E>>
        + Send
        + 'a,
{
    // `scoped_transaction` drives `C::TransactionManager` exactly like
    // `conn.transaction` does. diesel-async issues SAVEPOINT (not BEGIN)
    // because `conn` is already in a transaction, and RELEASE / ROLLBACK TO on
    // Ok / Err respectively.
    scoped_transaction(conn, f).await
}

// ── Db extractor ─────────────────────────────────────────────

/// Connection type managed by the deadpool pool.
pub type PooledConnection = diesel_async::pooled_connection::deadpool::Object<RuntimeConnection>;

struct TxDepthGuard<'a> {
    depth: &'a mut usize,
    poisoned: &'a mut bool,
    disarmed: bool,
}

impl Drop for TxDepthGuard<'_> {
    fn drop(&mut self) {
        *self.depth -= 1;
        if !self.disarmed {
            *self.poisoned = true;
        }
    }
}

/// Async database connection extractor.
///
/// Declare `db: Db` in a handler signature to get a pooled connection to
/// Postgres. The connection is returned to the pool when `Db` is dropped
/// at the end of the request.
///
/// `Db` implements [`Deref`](std::ops::Deref) and
/// [`DerefMut`](std::ops::DerefMut) to
/// `diesel_async::AsyncPgConnection`, so you can use it directly with
/// Diesel query methods.
///
/// If no database is configured (i.e., `database.primary_url` and legacy
/// `database.url` are absent),
/// requests that use `Db` will receive a `503 Service Unavailable`
/// response.
///
/// # Examples
///
/// ```rust,no_run
/// use autumn_web::prelude::*;
///
/// #[get("/ping-db")]
/// async fn ping_db(db: Db) -> AutumnResult<&'static str> {
///     // `db` dereferences to AsyncPgConnection
///     Ok("database is reachable")
/// }
/// ```
/// Extension/extractor struct for route-level statement timeout override.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StatementTimeout(pub std::time::Duration);

pub struct Db {
    conn: PooledConnection,
    /// Span covering the full checkout-to-release window. Dropped when
    /// `Db` is dropped at the end of the request, so span duration
    /// reflects real connection hold time rather than just `pool.get()`
    /// latency. Exposed via [`Db::span`] so handlers can attach
    /// per-query spans as children with
    /// [`tracing::Instrument::instrument`].
    span: tracing::Span,
    tx_depth: usize,
    tx_poisoned: bool,
    route_key: Option<String>,
    metrics: Option<crate::middleware::MetricsCollector>,
    slow_query_threshold: std::time::Duration,
    start_time: std::time::Instant,
    is_test_tx: bool,
}

impl Db {
    /// Connection-scoped span. Instrument a query future with this to
    /// emit a child span tagged under the connection checkout window.
    ///
    /// ```rust,no_run
    /// use autumn_web::prelude::*;
    /// use tracing::Instrument as _;
    ///
    /// # async fn example(mut db: Db) -> AutumnResult<()> {
    /// let span = db.span().clone();
    /// // run a Diesel query here, e.g. users::table.load(&mut *db)
    /// async {
    ///     // ... diesel_async query ...
    ///     Ok::<_, AutumnError>(())
    /// }
    /// .instrument(span)
    /// .await
    /// # }
    /// ```
    #[must_use]
    pub const fn span(&self) -> &tracing::Span {
        &self.span
    }

    /// Run an async closure inside a database transaction at the default
    /// isolation level (READ COMMITTED).
    ///
    /// Commits when the closure returns `Ok(_)`, rolls back when it returns
    /// `Err(_)`. For a stronger isolation level and/or automatic
    /// serialization-failure retry, use [`Db::tx_with`].
    ///
    /// # Errors
    ///
    /// Returns [`AutumnError`] when:
    ///
    /// - the underlying transaction returns an error,
    /// - the closure returns an error that converts into `AutumnError`,
    /// - this `Db` is already inside a transaction,
    /// - this `Db` has been poisoned by a previously cancelled/dropped
    ///   transaction future.
    ///
    /// # Panics
    ///
    /// Panics if the internal after-commit registry mutex is poisoned (only
    /// possible if a previous thread holding the lock panicked).
    pub async fn tx<'a, T, E, F>(&'a mut self, f: F) -> Result<T, crate::error::AutumnError>
    where
        T: Send + 'a,
        E: From<diesel::result::Error> + Send + Sync + 'a,
        crate::error::AutumnError: From<E>,
        F: for<'r> FnOnce(
                &'r mut PooledConnection,
            ) -> scoped_futures::ScopedBoxFuture<'a, 'r, Result<T, E>>
            + Send
            + 'a,
    {
        if self.tx_poisoned {
            return Err(crate::error::AutumnError::service_unavailable_msg(
                "Database connection is in an invalid transaction state",
            ));
        }
        if self.tx_depth > 0 {
            return Err(crate::error::AutumnError::bad_request_msg(
                NESTED_TX_MESSAGE,
            ));
        }
        reject_ambient_after_commit_registry_for_tx()?;
        self.tx_depth += 1;
        let mut guard = TxDepthGuard {
            depth: &mut self.tx_depth,
            poisoned: &mut self.tx_poisoned,
            disarmed: false,
        };

        // Each tx gets its own callback registry shared with the task-local so
        // that code running inside the closure (jobs, mailer, hooks) can push
        // callbacks without having access to `Db` directly. The `Arc` lets us
        // read the registry after the `scope` future completes.
        let registry: Arc<Mutex<Vec<CommitCallback>>> = Arc::new(Mutex::new(Vec::new()));

        // NOTE: `tx` keeps its own body rather than delegating to `tx_with`
        // because the two hand out different connection types. `transaction()`
        // runs on the pooled `Object` (closure sees `&mut PooledConnection`),
        // whereas `tx_with` must use `build_transaction()` — only available on
        // `AsyncPgConnection` — so its closure sees `&mut AsyncPgConnection`.
        // `scoped_transaction` adapts the public `ScopedBoxFuture` callback
        // shape to the transaction API diesel-async 0.9 expects.
        let result = AFTER_COMMIT_REGISTRY
            .scope(
                registry.clone(),
                scoped_transaction::<T, E, _, _>(&mut self.conn, f),
            )
            .await
            .map_err(Into::into);

        guard.disarmed = true;

        // On commit: spawn the registered callbacks outside the transaction
        // connection, but await them sequentially inside that task so callback
        // dependencies observe registration order.
        // Errors are counted and logged; they do NOT affect the committed tx.
        // In transactional tests (outer transaction is rolled back), we suppress
        // spawning these callbacks to prevent observing uncommitted side effects.
        if result.is_ok() {
            let callbacks: Vec<CommitCallback> = {
                let mut reg = registry.lock().expect("registry lock");
                std::mem::take(&mut *reg)
            };

            if !callbacks.is_empty() && !self.is_test_tx {
                let _ = spawn_committed_after_commit_callbacks(callbacks);
            }
        }

        result
    }

    /// Run an async closure inside a database transaction with explicit
    /// [`TxOptions`] — isolation level, read-only/deferrable mode, and automatic
    /// retry of transient serialization failures (`40001`) and deadlocks
    /// (`40P01`).
    ///
    /// Commits when the closure returns `Ok(_)`, rolls back when it returns
    /// `Err(_)`. On a retryable failure with attempts remaining, the whole
    /// closure is re-run after a capped exponential backoff with jitter.
    ///
    /// # The closure must be re-runnable
    ///
    /// **Because the closure can run more than once, it must be free of
    /// side effects that are not themselves transactional (or must be
    /// idempotent).** Database work is rolled back between attempts, and
    /// after-commit callbacks from failed attempts are discarded — but any
    /// non-database side effect in the closure body (logging aside — external
    /// API calls, channel sends, in-memory mutation) will re-execute on each
    /// retry. Keep such effects out of the closure, or gate them on the final
    /// success.
    ///
    /// # Observability
    ///
    /// The transaction runs under a `db.transaction` span carrying
    /// `db.isolation` and the final `db.tx.attempts` count. Each retry also
    /// increments [`TX_RETRIES_TOTAL`]; an exhausted retry budget increments
    /// [`TX_RETRY_EXHAUSTED_TOTAL`].
    ///
    /// # Errors
    ///
    /// Returns [`AutumnError`] under the same conditions as [`Db::tx`]. A
    /// non-retryable error is returned immediately; when the retry budget is
    /// exhausted, the **final** underlying error is returned (never swallowed).
    ///
    /// Under the `sqlite` feature there is no transaction builder that can
    /// enforce `READ ONLY`, so a request carrying [`TxOptions::read_only`]
    /// returns an unsupported-options error **before the closure runs** rather
    /// than silently executing a writable transaction (which would let writes
    /// commit under a read-only contract). A normal read-write transaction is
    /// unaffected. The Postgres path enforces read-only via the transaction
    /// builder and never rejects.
    ///
    /// # Panics
    ///
    /// Panics if the internal after-commit registry mutex is poisoned (only
    /// possible if a previous thread holding the lock panicked).
    #[allow(clippy::too_many_lines)]
    // The Postgres path re-borrows `f` per retry attempt (`&mut f`); the SQLite
    // path consumes it once, so `mut` is unused there.
    #[cfg_attr(feature = "sqlite", allow(unused_mut))]
    pub async fn tx_with<'a, T, E, F>(
        &'a mut self,
        opts: TxOptions,
        mut f: F,
    ) -> Result<T, crate::error::AutumnError>
    where
        T: Send + 'a,
        E: From<diesel::result::Error> + Send + Sync + 'a,
        crate::error::AutumnError: From<E>,
        // The closure receives `&mut RuntimeConnection` (not `&mut PooledConnection`
        // as `tx` does): on Postgres, isolation levels require `build_transaction()`,
        // which is inherent on `AsyncPgConnection` (the default `RuntimeConnection`).
        // Diesel query methods work on either backend. Under the `sqlite` feature
        // `RuntimeConnection` is a SQLite connection and the isolation/retry path
        // below degrades to a single plain transaction.
        F: for<'r> FnMut(
                &'r mut RuntimeConnection,
            ) -> scoped_futures::ScopedBoxFuture<'a, 'r, Result<T, E>>
            + Send
            + 'a,
    {
        if self.tx_poisoned {
            return Err(crate::error::AutumnError::service_unavailable_msg(
                "Database connection is in an invalid transaction state",
            ));
        }
        if self.tx_depth > 0 {
            return Err(crate::error::AutumnError::bad_request_msg(
                NESTED_TX_MESSAGE,
            ));
        }
        reject_ambient_after_commit_registry_for_tx()?;

        // Under the SQLite runtime there is no transaction builder that can
        // enforce `READ ONLY` semantics — the `sqlite` arm below runs a single
        // plain, writable transaction. Silently honoring a caller's
        // `TxOptions::read_only()` by running a writable transaction anyway would
        // let writes succeed and commit under a contract that promised none — a
        // safety regression for any code relying on a read-only transaction to
        // prevent mutation. So reject the request up front, BEFORE the closure
        // can run, rather than pretending to honor it. (Real `query_only`
        // enforcement is avoided deliberately: deadpool's `custom_setup` runs on
        // CREATE only, so a leaked `PRAGMA query_only = ON` would poison a pooled
        // connection for its lifetime.) The Postgres path enforces read-only via
        // the transaction builder's `read_only()` and is unaffected.
        #[cfg(feature = "sqlite")]
        if opts.read_only {
            return Err(crate::error::AutumnError::bad_request_msg(
                "SQLite runtime does not support read-only transactions \
                 (TxOptions::read_only); this build cannot enforce read-only \
                 semantics on SQLite. Remove the read_only option or run on \
                 Postgres.",
            ));
        }

        self.tx_depth += 1;
        let mut guard = TxDepthGuard {
            depth: &mut self.tx_depth,
            poisoned: &mut self.tx_poisoned,
            disarmed: false,
        };

        let span = tracing::info_span!(
            "db.transaction",
            db.system = "postgresql",
            db.isolation = opts.isolation.as_str(),
            db.tx.attempts = tracing::field::Empty,
        );

        if self.is_test_tx {
            // Under a transactional `TestApp` the connection is already inside
            // the test harness's outer transaction (`begin_test_transaction`),
            // so issuing a literal `BEGIN`/`SET TRANSACTION ISOLATION LEVEL`
            // via `build_transaction()` here would be invalid — Postgres
            // rejects `SET TRANSACTION ISOLATION LEVEL` inside a
            // subtransaction, and the retry loop's per-attempt `&mut f`
            // re-borrow doesn't type-check against the plain `transaction()`
            // method's lifetime shape (unlike `build_transaction().run()`, its
            // bound is not scoped to a single call). So: nest via `SAVEPOINT`
            // instead, exactly like `Db::tx`, running the closure exactly
            // once — the requested isolation/read-only/deferrable/retry
            // options are inherited from (or meaningless nested inside) the
            // outer test transaction, so there is nothing to retry against a
            // single test-harness connection.
            let registry: Arc<Mutex<Vec<CommitCallback>>> = Arc::new(Mutex::new(Vec::new()));
            let conn: &mut RuntimeConnection = &mut self.conn;
            let result = AFTER_COMMIT_REGISTRY
                .scope(registry, scoped_transaction::<T, E, _, _>(conn, f))
                .instrument(span.clone())
                .await
                .map_err(Into::into);

            span.record("db.tx.attempts", 1u32);
            guard.disarmed = true;
            // `is_test_tx` always suppresses after-commit spawning (matching
            // `Db::tx`), so the registry is simply dropped without draining.
            return result;
        }

        // SQLite has no `SET TRANSACTION ISOLATION LEVEL` / read-only /
        // deferrable transaction builder and no serialization-failure retry
        // semantics, so run the closure once inside a single plain transaction.
        // The requested isolation level, read-only, deferrable, and retry
        // options are Postgres-only and are ignored here (documented).
        // After-commit callbacks still fire on commit, exactly as on the
        // Postgres path.
        #[cfg(feature = "sqlite")]
        {
            let registry: Arc<Mutex<Vec<CommitCallback>>> = Arc::new(Mutex::new(Vec::new()));
            let conn: &mut RuntimeConnection = &mut self.conn;
            let result: Result<T, E> = AFTER_COMMIT_REGISTRY
                .scope(registry.clone(), scoped_transaction::<T, E, _, _>(conn, f))
                .instrument(span.clone())
                .await;
            span.record("db.tx.attempts", 1u32);
            guard.disarmed = true;
            // This block is the whole remaining function body under the feature
            // (the Postgres retry loop below is cfg'd out), so the `match` is the
            // tail expression — no `return` needed.
            match result {
                Ok(value) => {
                    let callbacks: Vec<CommitCallback> = {
                        let mut reg = registry.lock().expect("registry lock");
                        std::mem::take(&mut *reg)
                    };
                    if !callbacks.is_empty() {
                        let _ = spawn_committed_after_commit_callbacks(callbacks);
                    }
                    Ok(value)
                }
                Err(user_error) => Err(crate::error::AutumnError::from(user_error)),
            }
        }

        #[cfg(not(feature = "sqlite"))]
        {
            let max_attempts = opts.effective_max_attempts();
            let mut attempt: u32 = 0;

            let outcome: Result<T, crate::error::AutumnError> = loop {
                attempt += 1;

                // A fresh registry per attempt: a rolled-back attempt's after-commit
                // callbacks are discarded simply by dropping this `Arc` undrained.
                let registry: Arc<Mutex<Vec<CommitCallback>>> = Arc::new(Mutex::new(Vec::new()));

                let attempt_result: Result<T, E> = {
                    // *** LOAD-BEARING ORDER: borrow `f` BEFORE `build_transaction()`.
                    // diesel-async's `TransactionBuilder::run` bounds the closure by
                    // the connection-borrow lifetime; `&mut f`'s region must start
                    // earlier than that borrow or it fails to compile. Do not reorder.
                    let f_ref = &mut f;

                    let mut builder = self.conn.build_transaction();
                    builder = match opts.isolation {
                        IsolationLevel::ReadCommitted => builder.read_committed(),
                        IsolationLevel::RepeatableRead => builder.repeatable_read(),
                        IsolationLevel::Serializable => builder.serializable(),
                    };
                    if opts.read_only {
                        builder = builder.read_only();
                    }
                    if opts.deferrable {
                        builder = builder.deferrable();
                    }

                    AFTER_COMMIT_REGISTRY
                        .scope(
                            registry.clone(),
                            builder.run::<T, E, _>(async move |conn| f_ref(conn).await),
                        )
                        .instrument(span.clone())
                        .await
                };

                match attempt_result {
                    Ok(value) => {
                        // Commit path: drain THIS attempt's callbacks and spawn
                        // them. (The `is_test_tx` case already returned above, so
                        // spawning here is always live.)
                        let callbacks: Vec<CommitCallback> = {
                            let mut reg = registry.lock().expect("registry lock");
                            std::mem::take(&mut *reg)
                        };
                        if !callbacks.is_empty() {
                            let _ = spawn_committed_after_commit_callbacks(callbacks);
                        }
                        break Ok(value);
                    }
                    Err(e) => {
                        // Convert to AutumnError first, then classify on it.
                        let ae = crate::error::AutumnError::from(e);
                        let retryable = is_retryable_txn_error(&ae);
                        match retry_decision(attempt, max_attempts, retryable) {
                            RetryDecision::Retry => {
                                let retries_total = record_tx_retry();
                                let delay = retry_backoff_delay(
                                    opts.initial_backoff,
                                    opts.max_backoff,
                                    attempt,
                                );
                                tracing::debug!(
                                    parent: &span,
                                    attempt,
                                    max_attempts,
                                    delay_ms = u64::try_from(delay.as_millis()).unwrap_or(u64::MAX),
                                    autumn.tx.retries_total = retries_total,
                                    "retrying transaction after serialization/deadlock failure"
                                );
                                // `registry` drops here → rolled-back attempt's
                                // after-commit callbacks are discarded; the loop
                                // then re-runs the closure.
                                tokio::time::sleep(delay).await;
                            }
                            RetryDecision::Stop => {
                                if retryable {
                                    let exhausted_total = record_tx_retry_exhausted();
                                    tracing::warn!(
                                        parent: &span,
                                        attempt,
                                        max_attempts,
                                        autumn.tx.retry_exhausted_total = exhausted_total,
                                        "transaction retry budget exhausted; returning final error"
                                    );
                                }
                                break Err(ae);
                            }
                        }
                    }
                }
            };

            span.record("db.tx.attempts", attempt);
            guard.disarmed = true;
            outcome
        }
    }
}

impl std::ops::Deref for Db {
    type Target = RuntimeConnection;
    fn deref(&self) -> &Self::Target {
        assert!(
            !self.tx_poisoned,
            "Db connection is poisoned due to a cancelled/dropped transaction"
        );
        &self.conn
    }
}

impl std::ops::DerefMut for Db {
    fn deref_mut(&mut self) -> &mut Self::Target {
        assert!(
            !self.tx_poisoned,
            "Db connection is poisoned due to a cancelled/dropped transaction"
        );
        &mut self.conn
    }
}

/// Everything required to check out and instrument a pooled connection.
///
/// Shared by the plain [`Db`] extractor and shard-routed checkouts so that
/// every connection — regardless of which pool it came from — gets the same
/// span, interceptor, statement-timeout, and slow-query treatment.
pub(crate) struct DbCheckoutParams<'a> {
    /// Pool to check the connection out of.
    pub pool: &'a Pool<RuntimeConnection>,
    /// Role label surfaced to [`DbConnectionInterceptor`]s, e.g. `"primary"`
    /// or `"shard:<name>:primary"`.
    pub pool_name: &'a str,
    /// Shard name recorded on the `db.connection` span, when routed.
    pub shard: Option<&'a str>,
    /// Resolved statement timeout (route override already merged with the
    /// global config). `None` disables the timeout (`SET statement_timeout = 0`).
    pub statement_timeout: Option<std::time::Duration>,
    /// `"METHOD /matched/path"` key used for per-route DB metrics.
    pub route_key: Option<String>,
    pub metrics: Option<crate::middleware::MetricsCollector>,
    pub slow_query_threshold: std::time::Duration,
    pub interceptors: Vec<std::sync::Arc<dyn crate::interceptor::DbConnectionInterceptor>>,
}

impl Db {
    /// Check a connection out of `params.pool` with full instrumentation.
    ///
    /// This is the single code path behind the [`Db`] extractor and all
    /// shard-routed checkouts: span creation, checkout interceptors,
    /// `SET statement_timeout`, and the metrics captured for the
    /// slow-query warning on `Drop`.
    pub(crate) async fn checkout(params: DbCheckoutParams<'_>) -> Result<Self, AutumnError> {
        // `SET statement_timeout` (and its i32-cap arithmetic below) is a
        // Postgres session GUC. Under the `sqlite` feature the runtime backend
        // is entirely SQLite (the `RuntimeConnection` alias flips wholesale —
        // see `build_sqlite_pool`), which rejects the statement and would turn
        // every `Db`-using route into a 503. The const, the `RunQueryDsl`
        // import, the timeout arithmetic, and the `SET` itself are therefore all
        // gated off on the SQLite build; the Postgres path is byte-identical.
        #[cfg(not(feature = "sqlite"))]
        const PG_TIMEOUT_MAX_MS: u64 = i32::MAX as u64;
        #[cfg(not(feature = "sqlite"))]
        use diesel_async::RunQueryDsl as _;

        // Span covers the full time the connection is held — from
        // checkout through the end of the request — rather than just
        // `pool.get()`. Dropping `Db` closes the span, so span duration
        // reflects real connection hold time and `db.system=postgresql`
        // propagates to any query futures handlers instrument with
        // `db.span()`.
        let span = tracing::info_span!(
            "db.connection",
            otel.kind = "client",
            db.system = "postgresql",
            db.shard = tracing::field::Empty,
        );
        if let Some(shard) = params.shard {
            span.record("db.shard", shard);
        }

        let pool = params.pool;
        let mut checkout_future: std::pin::Pin<
            Box<
                dyn std::future::Future<Output = Result<PooledConnection, AutumnError>> + Send + '_,
            >,
        > = Box::pin(async move {
            pool.get().await.map_err(|e| {
                tracing::error!("Failed to acquire database connection: {e}");
                AutumnError::service_unavailable_msg(e.to_string())
            })
        });
        for interceptor in &params.interceptors {
            let ctx = crate::interceptor::DbCheckoutContext {
                pool_name: params.pool_name.to_string(),
            };
            checkout_future = interceptor.intercept_checkout(ctx, checkout_future);
        }

        let mut conn = checkout_future.instrument(span.clone()).await?;

        // `statement_timeout` is a Postgres session GUC; it is intentionally
        // unused on the SQLite backend (see the gating note below), so consume
        // it here to keep the shared `DbCheckoutParams` field from reading as
        // dead code under `--features sqlite`.
        #[cfg(feature = "sqlite")]
        let _ = params.statement_timeout;

        // Postgres statement_timeout is a signed 32-bit integer (milliseconds).
        // Cap at i32::MAX to avoid a confusing 503 for very large configured values.
        #[cfg(not(feature = "sqlite"))]
        let timeout_ms = params.statement_timeout.map_or(0u64, |d| {
            u64::try_from(d.as_millis())
                .unwrap_or(PG_TIMEOUT_MAX_MS)
                .min(PG_TIMEOUT_MAX_MS)
        });

        // Install a fresh per-request query timer, but ONLY when a query
        // observer is active — EITHER a `REQUEST_DB_TIMINGS` scope (the
        // `ServerTimingLayer`, enabled by `[observability] server_timing`) OR a
        // `REQUEST_QUERY_CAPTURE` scope (the test harness capturing the SQL
        // list, which runs with `server_timing` off). The timer feeds both
        // lanes via `record_request_db_query`. Installed BEFORE the `SET
        // statement_timeout` housekeeping statement below.
        //
        // `set_instrumentation` WHOLESALE REPLACES the connection's
        // instrumentation, so it must not run unconditionally: an application
        // that registered a global default via
        // `diesel::connection::set_default_instrumentation` (query logging,
        // tracing, metrics) would have it silently clobbered on the first
        // checkout and never restored — even when `server_timing` is disabled.
        // Gating on `request_db_timing_active() || request_query_capture_active()`
        // preserves the app's instrumentation whenever neither lane is scoped
        // (no `server_timing`, no test capture — the production default), and
        // only overwrites it for the duration a query observer is active.
        //
        // Installing a *fresh* timer on every observed checkout also clears any
        // stale `RequestQueryTimer` a pooled connection carried from a prior
        // request (diesel-async's deadpool manager never resets instrumentation
        // on recycle), so a stale timer can never record the upcoming
        // housekeeping `SET` (or later app queries) into a *different* request's
        // accumulator. Installing BEFORE the `SET` (which
        // `is_uncounted_statement` classifies as housekeeping) keeps that
        // statement out of the `Server-Timing` `db` count regardless. Any stale
        // timer left on a connection later reused by an opted-out request is a
        // cheap no-op: `on_start` probes both lanes before formatting or
        // recording anything, so it never allocates off-scope.
        #[cfg(feature = "db")]
        {
            use diesel_async::AsyncConnection as _;
            if request_db_timing_active() || request_query_capture_active() {
                conn.set_instrumentation(RequestQueryTimer::default());
            }
        }

        // Postgres-only per-checkout initialization; see the gating note above.
        // SQLite builds skip it entirely (it would 503 every `Db`-using route).
        #[cfg(not(feature = "sqlite"))]
        diesel::sql_query(format!("SET statement_timeout = {timeout_ms}"))
            .execute(&mut conn)
            .await
            .map_err(|e| {
                tracing::error!("Failed to set database statement_timeout to {timeout_ms}ms: {e}");
                AutumnError::service_unavailable_msg(format!("Database initialization error: {e}"))
            })?;

        let start_time = std::time::Instant::now();
        let is_test_tx = params
            .interceptors
            .iter()
            .any(|i| i.is_transactional_test());

        Ok(Self {
            conn,
            span,
            tx_depth: 0,
            tx_poisoned: false,
            route_key: params.route_key,
            metrics: params.metrics,
            slow_query_threshold: params.slow_query_threshold,
            start_time,
            is_test_tx,
        })
    }

    /// Check a plain, uninstrumented connection out of `pool` for use in tests.
    ///
    /// Unlike the request extractor, this applies no interceptors, statement
    /// timeout, or route metrics — it exists so integration tests can drive
    /// [`Db::tx`] / [`Db::tx_with`] directly against a [`crate::test::TestDb`]
    /// pool without spinning up a full `TestApp`.
    ///
    /// # Errors
    ///
    /// Returns [`AutumnError`] if a connection cannot be acquired from `pool`.
    #[cfg(feature = "test-support")]
    pub async fn connect_for_test(pool: &Pool<RuntimeConnection>) -> Result<Self, AutumnError> {
        Self::checkout(DbCheckoutParams {
            pool,
            pool_name: "test",
            shard: None,
            statement_timeout: None,
            route_key: None,
            metrics: None,
            slow_query_threshold: std::time::Duration::from_millis(500),
            interceptors: Vec::new(),
        })
        .await
    }
}

/// Request-derived context shared by every `Db`-producing extractor.
///
/// Captures the route-override statement timeout, the matched-path metrics
/// key, and the state-held instrumentation handles so shard-routed
/// checkouts behave identically to the plain [`Db`] extractor.
#[derive(Clone)]
pub(crate) struct RequestDbContext {
    pub statement_timeout: Option<std::time::Duration>,
    pub route_key: Option<String>,
    pub metrics: Option<crate::middleware::MetricsCollector>,
    pub slow_query_threshold: std::time::Duration,
    pub interceptors: Vec<std::sync::Arc<dyn crate::interceptor::DbConnectionInterceptor>>,
}

impl RequestDbContext {
    pub(crate) fn from_parts<S: DbState>(parts: &axum::http::request::Parts, state: &S) -> Self {
        let timeout_override = parts.extensions.get::<StatementTimeout>().copied();
        let matched_path = parts
            .extensions
            .get::<axum::extract::MatchedPath>()
            .map_or_else(|| parts.uri.path(), axum::extract::MatchedPath::as_str);
        Self {
            statement_timeout: timeout_override
                .map(|t| t.0)
                .or_else(|| state.statement_timeout()),
            route_key: Some(format!("{} {}", parts.method, matched_path)),
            metrics: state.metrics().cloned(),
            slow_query_threshold: state.slow_query_threshold(),
            interceptors: state.db_interceptors(),
        }
    }
}

impl<S> FromRequestParts<S> for Db
where
    S: DbState + Send + Sync,
{
    type Rejection = AutumnError;

    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        state: &S,
    ) -> Result<Self, Self::Rejection> {
        let pool = state
            .pool()
            .ok_or_else(|| AutumnError::service_unavailable_msg("Database not configured"))?;
        let ctx = RequestDbContext::from_parts(parts, state);

        let result = Self::checkout(DbCheckoutParams {
            pool,
            pool_name: "primary",
            shard: None,
            statement_timeout: ctx.statement_timeout,
            route_key: ctx.route_key,
            metrics: ctx.metrics,
            slow_query_threshold: ctx.slow_query_threshold,
            interceptors: ctx.interceptors,
        })
        .await;
        // Notify the RYWW task-local that a primary connection was checked out.
        // No-op when read_your_writes = "off" (task-local absent).
        if result.is_ok() {
            crate::read_your_writes::mark_write();
        }
        result
    }
}

impl Drop for Db {
    fn drop(&mut self) {
        if let (Some(route_key), Some(metrics)) = (&self.route_key, &self.metrics) {
            let elapsed = self.start_time.elapsed();
            let elapsed_ms = u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX);

            // Record DB query metric
            let metric_key = format!("{route_key} SELECT");
            metrics.record_db_query(&metric_key, elapsed_ms);
            // NOTE: deliberately *not* recorded into the Server-Timing
            // per-request accumulator. `elapsed` here is the whole
            // connection checkout-to-release window (see `start_time` /
            // the `span` doc above), not a single query's wall time. Every
            // request that extracts `Db` would otherwise add its entire
            // connection-hold time as one bogus "query", inflating both
            // `db;dur` and the `desc="N queries"` count (and double-counting
            // against real queries recorded by `run_instrumented`). The
            // accumulator must reflect only genuine instrumented queries.

            // Log slow query if it exceeds the threshold
            if elapsed >= self.slow_query_threshold {
                tracing::warn!(
                    route = %route_key,
                    sql = "SELECT ?",
                    duration_ms = elapsed_ms,
                    "slow database query"
                );
            }
        }
    }
}

// ----------------------------------------------------------------------------
// DatabasePoolProvider — tier-1 boot-time replaceable pool factory
// ----------------------------------------------------------------------------

/// Pluggable boot-time database pool factory.
///
/// Replace the default `deadpool + diesel-async` factory with a custom
/// strategy (custom metrics wrapper, circuit breaker, separate pools per
/// shard, etc.) by implementing this trait and installing it on the
/// [`AppBuilder`](crate::app::AppBuilder) via
/// [`with_pool_provider`](crate::app::AppBuilder::with_pool_provider).
///
/// The trait abstracts the *factory*, not the pool *type* — the return type is
/// fixed at `Pool<AsyncPgConnection>` for now. Swapping to a different backend
/// (e.g. `MySQL`, `SQLite`) would require generic `Pool<C>` propagation through
/// `Db` / `DbState` / `AppState` and is intentionally out of scope.
///
/// Providers that only implement [`DatabasePoolProvider::create_pool`] still
/// participate in primary/replica topology: the default
/// [`DatabasePoolProvider::create_topology`] uses the custom primary pool and
/// builds the configured replica role with Autumn's deadpool factory. Override
/// `create_topology` when both roles need custom construction.
///
/// # Example
///
/// ```rust,no_run
/// use autumn_web::config::DatabaseConfig;
/// use autumn_web::db::{DatabasePoolProvider, PoolError};
/// use diesel_async::AsyncPgConnection;
/// use diesel_async::pooled_connection::deadpool::Pool;
///
/// pub struct MetricsPoolProvider;
///
/// impl DatabasePoolProvider for MetricsPoolProvider {
///     async fn create_pool(
///         &self,
///         config: &DatabaseConfig,
///     ) -> Result<Option<Pool<AsyncPgConnection>>, PoolError> {
///         // Wrap the default pool with custom metrics, then return it.
///         autumn_web::db::create_pool(config)
///     }
/// }
/// ```
pub trait DatabasePoolProvider: Send + Sync + 'static {
    /// Create a connection pool from the resolved [`DatabaseConfig`].
    ///
    /// Returning `Ok(None)` signals that the application should run without a
    /// database — useful for static-site / API-gateway use cases or for
    /// disabling the DB in test contexts.
    fn create_pool(
        &self,
        config: &DatabaseConfig,
    ) -> impl std::future::Future<Output = Result<Option<Pool<RuntimeConnection>>, PoolError>> + Send;

    /// Create primary and optional replica pools from the resolved
    /// [`DatabaseConfig`].
    ///
    /// The default implementation preserves the provider's custom primary pool
    /// and builds a replica pool when `database.replica_url` is configured.
    ///
    /// # Errors
    ///
    /// Returns [`PoolError`] if either configured role cannot be built.
    fn create_topology(
        &self,
        config: &DatabaseConfig,
    ) -> impl std::future::Future<Output = Result<Option<DatabaseTopology>, PoolError>> + Send {
        async move {
            let Some(primary) = self.create_pool(config).await? else {
                return Ok(None);
            };

            // A custom provider that overrides only `create_pool` still gets its
            // replica built by this default here, so the SQLite-replica rule must
            // be enforced on this path too -- otherwise a provider configuring a
            // distinct/in-memory `database.replica_url` would boot two unrelated
            // SQLite databases and route reads to an empty/stale replica. Reuse
            // the same helper `create_topology`/`create_shard_topology` use so the
            // rejection rule cannot drift (addresses Codex P2). The primary URL is
            // whatever `effective_primary_url` resolves; a provider returning a
            // pool for a `None` primary URL has no URL to compare, so skip then.
            #[cfg(feature = "sqlite")]
            if let (Some(primary_url), Some(replica_url)) = (
                config.effective_primary_url(),
                config.replica_url.as_deref(),
            ) {
                reject_unusable_sqlite_replica(primary_url, replica_url)?;
            }

            let replica = config
                .replica_url
                .as_deref()
                .map(|url| {
                    build_pool(
                        url,
                        config.effective_replica_pool_size(),
                        config.connect_timeout_secs,
                    )
                })
                .transpose()?;

            Ok(Some(DatabaseTopology::from_pools(primary, replica)))
        }
    }

    /// Create one shard's [`DatabaseTopology`] from its
    /// `[[database.shards]]` entry.
    ///
    /// The default implementation uses Autumn's deadpool factory for both
    /// roles. Override to decorate per-shard pools (metrics wrappers,
    /// circuit breakers) the same way `create_pool` decorates the control
    /// role.
    ///
    /// # Errors
    ///
    /// Returns [`PoolError`] if either configured role cannot be built.
    fn create_shard_topology(
        &self,
        shard: &crate::config::ShardConfig,
        defaults: &DatabaseConfig,
    ) -> impl std::future::Future<Output = Result<DatabaseTopology, PoolError>> + Send {
        async move { create_shard_topology(shard, defaults) }
    }
}

/// Default [`DatabasePoolProvider`] — the `deadpool + diesel-async` factory.
///
/// Delegates to the free function [`create_pool`]. This is the provider used
/// when no override is installed via
/// [`with_pool_provider`](crate::app::AppBuilder::with_pool_provider).
#[derive(Debug, Default, Clone, Copy)]
pub struct DieselDeadpoolPoolProvider;

impl DieselDeadpoolPoolProvider {
    /// Construct a new default provider.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl DatabasePoolProvider for DieselDeadpoolPoolProvider {
    async fn create_pool(
        &self,
        config: &DatabaseConfig,
    ) -> Result<Option<Pool<RuntimeConnection>>, PoolError> {
        create_pool(config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::DatabaseConfig;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
    use std::time::Duration;

    // ── RequestQueryTimer / Server-Timing db accumulator tests ───

    /// The connection instrumentation records one query per completed
    /// `StartQuery`/`FinishQuery` pair into the request accumulator, summing
    /// elapsed time. Drives the extracted `on_start`/`on_finish` accounting
    /// (the `InstrumentationEvent` itself is non-exhaustive and its
    /// constructors are gated behind an unstable diesel feature, so it cannot
    /// be built in a test). Deterministic: derives finish instants from the
    /// start via `Instant + Duration`, no sleeps.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_query_timer_accumulates_finished_queries() {
        let timings = Arc::new(RequestDbTimings::default());
        REQUEST_DB_TIMINGS
            .scope(Arc::clone(&timings), async {
                let mut timer = RequestQueryTimer::default();

                // Query 1: 1200µs.
                let t0 = std::time::Instant::now();
                timer.on_start(t0, || "SELECT 1".to_string());
                timer.on_finish(t0 + Duration::from_micros(1_200));

                // Query 2: 800µs.
                let t1 = std::time::Instant::now();
                timer.on_start(t1, || "UPDATE users SET name = $1".to_string());
                timer.on_finish(t1 + Duration::from_micros(800));

                // A stray FinishQuery with no matching StartQuery is ignored.
                timer.on_finish(std::time::Instant::now());
            })
            .await;

        assert_eq!(
            timings.query_count.load(Ordering::Relaxed),
            2,
            "only completed StartQuery/FinishQuery pairs are counted"
        );
        assert_eq!(
            timings.total_us.load(Ordering::Relaxed),
            2_000,
            "elapsed times accumulate (1200µs + 800µs)"
        );
    }

    /// Outside a `REQUEST_DB_TIMINGS` scope the timer records nothing and does
    /// not panic — the off-request / middleware-disabled path.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_query_timer_is_noop_off_request() {
        let mut timer = RequestQueryTimer::default();
        let t0 = std::time::Instant::now();
        timer.on_start(t0, || "SELECT 1".to_string());
        // Must not panic even though no task-local accumulator is scoped.
        timer.on_finish(t0 + Duration::from_micros(500));
    }

    /// Approach-(b) guarantee: when no `REQUEST_DB_TIMINGS` scope is active,
    /// `on_start` must be a cheap no-op — it must not invoke the (allocating)
    /// SQL-formatting closure, and must leave no in-flight statement. This is
    /// what makes it safe to leave a `RequestQueryTimer` installed on a pooled
    /// connection that a later opted-out request reuses.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_query_timer_on_start_is_cheap_noop_off_request() {
        let mut timer = RequestQueryTimer::default();
        let invoked = std::cell::Cell::new(false);
        let t0 = std::time::Instant::now();
        timer.on_start(t0, || {
            invoked.set(true);
            "SELECT 1".to_string()
        });
        assert!(
            !invoked.get(),
            "off-request, on_start must not format the SQL (no allocation)"
        );
        // No in-flight statement was recorded, so on_finish is a no-op.
        timer.on_finish(t0 + Duration::from_micros(500));
    }

    /// Regression for the stale-timer / housekeeping-`SET` bug: a pooled
    /// connection reused across requests keeps a `RequestQueryTimer` installed,
    /// and `Db::checkout` runs a `SET statement_timeout` before handing the
    /// connection over. That housekeeping `SET` must NOT be counted, or every
    /// `Server-Timing` request would report a bogus `+1 query` before any app
    /// SQL. After the `SET`, the first real `SELECT` increments the count to
    /// exactly 1 and only its latency accumulates.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_query_timer_excludes_checkout_statement_timeout_set() {
        let timings = Arc::new(RequestDbTimings::default());
        REQUEST_DB_TIMINGS
            .scope(Arc::clone(&timings), async {
                // A timer as it would be freshly installed at checkout.
                let mut timer = RequestQueryTimer::default();

                // Checkout housekeeping `SET` — must not be counted.
                let t0 = std::time::Instant::now();
                timer.on_start(t0, || "SET statement_timeout = 5000".to_string());
                timer.on_finish(t0 + Duration::from_millis(3));

                assert_eq!(
                    timings.query_count.load(Ordering::Relaxed),
                    0,
                    "the checkout SET statement_timeout must not be counted"
                );
                assert_eq!(
                    timings.total_us.load(Ordering::Relaxed),
                    0,
                    "the checkout SET contributes no latency to db;dur"
                );

                // First real application query: 600µs.
                let t1 = std::time::Instant::now();
                timer.on_start(t1, || "SELECT * FROM users".to_string());
                timer.on_finish(t1 + Duration::from_micros(600));

                assert_eq!(
                    timings.query_count.load(Ordering::Relaxed),
                    1,
                    "the real SELECT is the first counted query"
                );
                assert_eq!(
                    timings.total_us.load(Ordering::Relaxed),
                    600,
                    "only the SELECT's 600µs accumulates; the SET is excluded"
                );
            })
            .await;
    }

    /// `request_db_timing_active` — the predicate `RequestQueryTimer::on_start`
    /// probes to decide whether to record — must report `false` outside a
    /// `REQUEST_DB_TIMINGS` scope (the production / `server_timing` disabled
    /// default) and `true` inside one (the `ServerTimingLayer`-scoped request).
    /// This predicate gates two things: whether `Db::checkout` installs the
    /// timer at all (skipped off-scope so an app's own instrumentation is not
    /// clobbered), and whether a stale timer left on a reused connection does
    /// any work — with the probe `false`, `on_start` is a cheap no-op.
    ///
    /// NOTE: the real `Db::checkout` path needs a live Postgres connection,
    /// which the sandbox cannot provide, so this exercises the predicate
    /// directly.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_db_timing_active_reflects_scope() {
        assert!(
            !request_db_timing_active(),
            "no REQUEST_DB_TIMINGS scope active outside the ServerTimingLayer: \
             the timer records nothing"
        );

        let timings = Arc::new(RequestDbTimings::default());
        REQUEST_DB_TIMINGS
            .scope(Arc::clone(&timings), async {
                assert!(
                    request_db_timing_active(),
                    "inside a REQUEST_DB_TIMINGS scope the probe is active: \
                     the timer records queries"
                );
            })
            .await;

        // Scope has ended; the gate reads false again.
        assert!(
            !request_db_timing_active(),
            "the gate is false again once the scope is dropped"
        );
    }

    /// A transaction wraps its inner statements in `BEGIN`/`COMMIT`, which
    /// diesel-async runs through `batch_execute` — emitting a
    /// `StartQuery`/`FinishQuery` pair with that transaction-control SQL, just
    /// like a real query. The timer must skip those so a transaction with one
    /// real `SELECT` reports `desc="1 query"`, not `"3 queries"`, and excludes
    /// begin/commit latency from `db;dur`.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn request_query_timer_excludes_transaction_control_statements() {
        let timings = Arc::new(RequestDbTimings::default());
        REQUEST_DB_TIMINGS
            .scope(Arc::clone(&timings), async {
                let mut timer = RequestQueryTimer::default();

                // BEGIN — must not be counted (10_000µs, would dominate if it
                // leaked into the total).
                let t0 = std::time::Instant::now();
                timer.on_start(t0, || "BEGIN".to_string());
                timer.on_finish(t0 + Duration::from_millis(10));

                // The single real query: 700µs.
                let t1 = std::time::Instant::now();
                timer.on_start(t1, || "SELECT * FROM users".to_string());
                timer.on_finish(t1 + Duration::from_micros(700));

                // COMMIT — must not be counted.
                let t2 = std::time::Instant::now();
                timer.on_start(t2, || "COMMIT".to_string());
                timer.on_finish(t2 + Duration::from_millis(10));
            })
            .await;

        assert_eq!(
            timings.query_count.load(Ordering::Relaxed),
            1,
            "only the real SELECT is counted; BEGIN/COMMIT are excluded"
        );
        assert_eq!(
            timings.total_us.load(Ordering::Relaxed),
            700,
            "only the SELECT's 700µs accumulates; begin/commit latency is excluded"
        );
    }

    /// The checkout install gate: `Db::checkout` calls `set_instrumentation`
    /// (which WHOLESALE REPLACES the connection's instrumentation — clobbering
    /// any app-registered `diesel::connection::set_default_instrumentation`)
    /// ONLY when `request_db_timing_active()` is true. Outside a
    /// `REQUEST_DB_TIMINGS` scope (the `server_timing`-disabled production
    /// default) the gate is false, so the guarded install is skipped and a
    /// pre-existing sentinel instrumentation is left intact. Inside a scope the
    /// timer is installed.
    ///
    /// NOTE: the real `Db::checkout` path needs a live Postgres connection the
    /// sandbox cannot provide, so this models the connection's single
    /// instrumentation slot and drives the exact gate expression from
    /// `Db::checkout`.
    #[cfg(feature = "db")]
    #[tokio::test]
    async fn checkout_installs_timer_only_when_scope_active() {
        // Models the connection's single instrumentation slot. "app" stands for
        // an application-registered default (`set_default_instrumentation`);
        // "timer" is autumn's `RequestQueryTimer`. Mirrors the guarded block in
        // `Db::checkout`: `if request_db_timing_active() { install timer }`.
        fn run_checkout_gate(slot: &mut &'static str) {
            if request_db_timing_active() {
                *slot = "timer";
            }
        }

        // Off-scope (server_timing disabled / production default): the gate is
        // false, so the app's instrumentation must survive untouched.
        assert!(!request_db_timing_active());
        let mut slot = "app";
        run_checkout_gate(&mut slot);
        assert_eq!(
            slot, "app",
            "off-scope checkout must NOT clobber the app's instrumentation"
        );

        // Inside a scope (server_timing enabled, handler wrapped by the layer):
        // the gate is true, so autumn installs its RequestQueryTimer.
        let timings = Arc::new(RequestDbTimings::default());
        REQUEST_DB_TIMINGS
            .scope(Arc::clone(&timings), async {
                let mut slot = "app";
                run_checkout_gate(&mut slot);
                assert_eq!(
                    slot, "timer",
                    "inside a REQUEST_DB_TIMINGS scope autumn installs its timer"
                );
            })
            .await;
    }

    /// Unit coverage for the uncounted-statement classifier used by the timer:
    /// leading-token match is case-insensitive and covers savepoint/release, the
    /// two-word `START TRANSACTION` form, and any leading `SET` (both
    /// `SET TRANSACTION` and the checkout `SET statement_timeout` housekeeping),
    /// while real application queries are counted. `UPDATE ... SET ...` is
    /// counted because `SET` is not its leading token.
    #[cfg(feature = "db")]
    #[test]
    fn is_uncounted_statement_classifies_statements() {
        for sql in [
            "BEGIN",
            "begin",
            "  COMMIT",
            "ROLLBACK",
            "ROLLBACK TO SAVEPOINT diesel_savepoint_0",
            "SAVEPOINT diesel_savepoint_1",
            "RELEASE SAVEPOINT diesel_savepoint_0",
            "start transaction",
            "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE",
            "SET statement_timeout = 5000",
            "set statement_timeout = 0",
        ] {
            assert!(
                RequestQueryTimer::is_uncounted_statement(sql),
                "{sql:?} should be treated as an uncounted housekeeping/tx statement"
            );
        }

        for sql in [
            "SELECT 1",
            "UPDATE users SET name = $1",
            "INSERT INTO t VALUES (1)",
            "DELETE FROM t WHERE id = 1",
            "",
        ] {
            assert!(
                !RequestQueryTimer::is_uncounted_statement(sql),
                "{sql:?} should be treated as a countable query"
            );
        }
    }

    /// Finding-1 regression: the capture path stores the parameterised
    /// statement WITHOUT diesel's trailing `-- binds: [...]` annotation, so the
    /// per-row executions of an N+1 pattern (which differ only in their bind
    /// values) collapse to a single template and `detect_n_plus_one` catches
    /// them. Without the strip, each `-- binds: [N]` would normalise to a
    /// distinct template and the N+1 would go undetected. Locks the fix with no
    /// live database.
    #[cfg(feature = "db")]
    #[test]
    fn strip_bind_annotation_lets_detect_n_plus_one_see_per_row_repetition() {
        use crate::inspector::{QueryRecord, detect_n_plus_one};

        // Construct QueryRecords exactly as `record_request_db_query` does:
        // the captured `sql` is diesel's `DebugQuery` `Display` output run
        // through `strip_bind_annotation`.
        fn record(diesel_display: &str) -> QueryRecord {
            QueryRecord {
                sql: strip_bind_annotation(diesel_display).to_owned(),
                params: Vec::new(),
                elapsed_ms: 1,
                location: String::new(),
            }
        }

        // The `-- binds:` marker (and the leading whitespace before it) is
        // stripped; the `$N` placeholder is preserved.
        assert_eq!(
            strip_bind_annotation("SELECT * FROM books WHERE author_id = $1 -- binds: [1]"),
            "SELECT * FROM books WHERE author_id = $1"
        );
        // Zero-bind annotation is stripped too.
        assert_eq!(
            strip_bind_annotation("SELECT * FROM authors -- binds: []"),
            "SELECT * FROM authors"
        );
        // No marker (transaction-control / synthetic input) → unchanged.
        assert_eq!(strip_bind_annotation("SELECT 1"), "SELECT 1");
        assert_eq!(strip_bind_annotation("BEGIN"), "BEGIN");

        // A classic N+1: one parent query, then the same per-row child query
        // executed once per row with a different bind value each time.
        let n_plus_one = vec![
            record("SELECT * FROM authors -- binds: []"),
            record("SELECT * FROM books WHERE author_id = $1 -- binds: [1]"),
            record("SELECT * FROM books WHERE author_id = $1 -- binds: [2]"),
            record("SELECT * FROM books WHERE author_id = $1 -- binds: [3]"),
        ];
        let warning = detect_n_plus_one(&n_plus_one, 3)
            .expect("three identical per-row templates should trip the N+1 detector");
        assert_eq!(
            warning.count, 3,
            "all three per-row executions collapse to a single template"
        );
        assert_eq!(
            warning.sql_template, "select * from books where author_id = $1",
            "the reported template is the parameterised statement, bind-free"
        );

        // Distinct query templates must NOT trip the detector, even at the
        // same total query count.
        let distinct = vec![
            record("SELECT * FROM authors -- binds: []"),
            record("SELECT * FROM books WHERE id = $1 -- binds: [1]"),
            record("UPDATE users SET name = $1 WHERE id = $2 -- binds: [\"x\", 2]"),
        ];
        assert!(
            detect_n_plus_one(&distinct, 3).is_none(),
            "three distinct query templates must not be reported as N+1"
        );
    }

    // ── after_commit tests ───────────────────────────────────────

    #[tokio::test]
    async fn register_after_commit_outside_tx_runs_eagerly() {
        // When called outside a db.tx block, the callback should run immediately.
        let counter = Arc::new(AtomicUsize::new(0));
        let c = counter.clone();
        register_after_commit(move || async move {
            c.fetch_add(1, Ordering::SeqCst);
            Ok(())
        })
        .await;
        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn register_after_commit_eager_failure_increments_failure_counter() {
        let before = AFTER_COMMIT_FAILURES_TOTAL.load(Ordering::Relaxed);

        register_after_commit(|| async {
            Err(crate::AutumnError::internal_server_error_msg(
                "deliberate eager after-commit failure",
            ))
        })
        .await;

        let after = AFTER_COMMIT_FAILURES_TOTAL.load(Ordering::Relaxed);
        assert!(
            after > before,
            "eager after_commit failures should be counted for recovery signals"
        );
    }

    #[tokio::test]
    async fn register_after_commit_inside_scope_defers_until_drained() {
        // Inside a task-local scope (simulating Db::tx), callbacks are deferred.
        let counter = Arc::new(AtomicUsize::new(0));
        let c = counter.clone();

        let registry = Arc::new(std::sync::Mutex::new(Vec::<CommitCallback>::new()));

        // Simulate being inside a db.tx by setting the task-local
        AFTER_COMMIT_REGISTRY
            .scope(registry.clone(), async {
                register_after_commit(move || async move {
                    c.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                })
                .await;
            })
            .await;

        // Callback must NOT have run yet
        assert_eq!(counter.load(Ordering::SeqCst), 0);

        // Drain and run the callbacks (simulating post-commit)
        let callbacks: Vec<CommitCallback> = {
            let mut reg = registry.lock().unwrap();
            std::mem::take(&mut *reg)
        };
        for cb in callbacks {
            cb().await.unwrap();
        }

        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn register_after_commit_on_rollback_callbacks_dropped() {
        // Callbacks registered inside a tx scope that is NOT drained are dropped.
        let counter = Arc::new(AtomicUsize::new(0));
        let c = counter.clone();

        let registry = Arc::new(std::sync::Mutex::new(Vec::<CommitCallback>::new()));

        AFTER_COMMIT_REGISTRY
            .scope(registry.clone(), async {
                register_after_commit(move || async move {
                    c.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                })
                .await;
            })
            .await;

        // Simulate rollback: drop the callbacks without running them
        drop(registry);

        assert_eq!(counter.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn register_after_commit_callbacks_run_in_registration_order() {
        let order = Arc::new(std::sync::Mutex::new(Vec::<u32>::new()));
        let registry = Arc::new(std::sync::Mutex::new(Vec::<CommitCallback>::new()));

        let o1 = order.clone();
        let o2 = order.clone();
        let o3 = order.clone();

        AFTER_COMMIT_REGISTRY
            .scope(registry.clone(), async {
                register_after_commit(move || async move {
                    o1.lock().unwrap().push(1);
                    Ok(())
                })
                .await;
                register_after_commit(move || async move {
                    o2.lock().unwrap().push(2);
                    Ok(())
                })
                .await;
                register_after_commit(move || async move {
                    o3.lock().unwrap().push(3);
                    Ok(())
                })
                .await;
            })
            .await;

        let callbacks: Vec<CommitCallback> = {
            let mut reg = registry.lock().unwrap();
            std::mem::take(&mut *reg)
        };
        for cb in callbacks {
            cb().await.unwrap();
        }

        assert_eq!(*order.lock().unwrap(), vec![1, 2, 3]);
    }

    #[tokio::test]
    async fn production_after_commit_drain_preserves_registration_order() {
        let order = Arc::new(std::sync::Mutex::new(Vec::<u32>::new()));
        let (release_first, wait_first) = tokio::sync::oneshot::channel::<()>();

        let first_order = order.clone();
        let second_order = order.clone();
        let callbacks: Vec<CommitCallback> = vec![
            Box::new(move || {
                Box::pin(async move {
                    wait_first
                        .await
                        .expect("test should release first callback");
                    first_order.lock().unwrap().push(1);
                    Ok(())
                })
            }),
            Box::new(move || {
                Box::pin(async move {
                    second_order.lock().unwrap().push(2);
                    Ok(())
                })
            }),
        ];

        let drain = spawn_committed_after_commit_callbacks(callbacks)
            .expect("non-empty callback list should spawn a drain task");
        tokio::task::yield_now().await;

        assert_eq!(
            *order.lock().unwrap(),
            Vec::<u32>::new(),
            "later callbacks must wait for earlier callbacks to finish"
        );

        release_first
            .send(())
            .expect("first callback receiver alive");
        drain.await.expect("drain task should not panic");

        assert_eq!(*order.lock().unwrap(), vec![1, 2]);
    }

    #[tokio::test]
    async fn production_after_commit_drain_isolates_panicking_callbacks() {
        let before = AFTER_COMMIT_FAILURES_TOTAL.load(Ordering::Relaxed);
        let ran_later = Arc::new(AtomicU64::new(0));
        let later = ran_later.clone();

        let callbacks: Vec<CommitCallback> = vec![
            Box::new(|| Box::pin(async { panic!("deliberate after_commit panic") })),
            Box::new(move || {
                Box::pin(async move {
                    later.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                })
            }),
        ];

        let drain = spawn_committed_after_commit_callbacks(callbacks)
            .expect("non-empty callback list should spawn a drain task");
        drain.await.expect("panicking callback should be isolated");

        assert_eq!(
            ran_later.load(Ordering::SeqCst),
            1,
            "later callbacks must still run after an earlier callback panics"
        );
        let after = AFTER_COMMIT_FAILURES_TOTAL.load(Ordering::Relaxed);
        assert!(
            after > before,
            "panicking after_commit callbacks must increment the failure counter"
        );
    }

    #[tokio::test]
    async fn db_tx_rejects_ambient_after_commit_registry() {
        let registry = Arc::new(std::sync::Mutex::new(Vec::<CommitCallback>::new()));

        let err = AFTER_COMMIT_REGISTRY
            .scope(registry, async {
                reject_ambient_after_commit_registry_for_tx().expect_err(
                    "starting Db::tx inside an ambient transaction registry should fail",
                )
            })
            .await;

        assert!(
            err.to_string().contains("Nested Db::tx calls"),
            "unexpected nested transaction error: {err}"
        );
    }

    #[tokio::test]
    async fn register_after_commit_callback_error_is_swallowed() {
        // A failing callback is logged but doesn't panic or propagate.
        let registry = Arc::new(std::sync::Mutex::new(Vec::<CommitCallback>::new()));

        AFTER_COMMIT_REGISTRY
            .scope(registry.clone(), async {
                register_after_commit(|| async {
                    Err(crate::AutumnError::internal_server_error_msg(
                        "deliberate error",
                    ))
                })
                .await;
            })
            .await;

        let callbacks: Vec<CommitCallback> = {
            let mut reg = registry.lock().unwrap();
            std::mem::take(&mut *reg)
        };
        // Running a failing callback should not panic
        for cb in callbacks {
            let _ = cb().await;
        }
    }

    // ── Pool provider trait tests ────────────────────────────────

    /// No-op provider for tests — always returns `Ok(None)` regardless of the
    /// supplied config. Verifies the trait actually overrides the default
    /// (which would otherwise build a pool from the URL).
    struct NoOpPoolProvider;

    impl DatabasePoolProvider for NoOpPoolProvider {
        async fn create_pool(
            &self,
            _config: &DatabaseConfig,
        ) -> Result<Option<Pool<crate::db::RuntimeConnection>>, PoolError> {
            Ok(None)
        }
    }

    #[tokio::test]
    async fn pool_provider_trait_returns_supplied_pool() {
        // Even with a configured URL, the no-op provider returns None — proving
        // the trait can replace the default factory's behaviour.
        let config = DatabaseConfig {
            url: Some("postgres://localhost/ignored".to_owned()),
            ..Default::default()
        };
        let provider = NoOpPoolProvider;
        let pool = provider
            .create_pool(&config)
            .await
            .expect("no-op provider should succeed");
        assert!(
            pool.is_none(),
            "no-op provider must override default behaviour"
        );
    }

    #[tokio::test]
    async fn default_pool_provider_matches_free_function() {
        let config = DatabaseConfig::default();
        let via_provider = DieselDeadpoolPoolProvider::new()
            .create_pool(&config)
            .await
            .expect("default provider should succeed");
        let via_function = create_pool(&config).expect("free fn should succeed");
        assert_eq!(via_provider.is_none(), via_function.is_none());
    }

    // ── Pool creation tests ──────────────────────────────────────

    #[tokio::test]
    async fn default_pool_provider_respects_url_config() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            ..Default::default()
        };
        let provider = DieselDeadpoolPoolProvider::new();
        let pool = provider
            .create_pool(&config)
            .await
            .expect("default provider should succeed");
        assert!(
            pool.is_some(),
            "default provider should return Some when url is provided"
        );
    }

    #[test]
    fn create_pool_with_no_url_returns_none() {
        let config = DatabaseConfig::default();
        let pool = create_pool(&config).expect("should not fail with no URL");
        assert!(pool.is_none());
    }

    #[test]
    fn create_pool_with_url_returns_some() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            ..Default::default()
        };
        let pool = create_pool(&config).expect("should build pool from valid config");
        assert!(pool.is_some());
    }

    // In the default (Postgres) build a SQLite target has no runtime pool, so
    // pool construction must refuse at boot with an actionable message pointing
    // at the `--features sqlite` build — never reaching a first-query failure or
    // panic. Under `--features sqlite` the same target instead builds a real
    // pool (see the `sqlite_boot_serve` integration test), so this refusal
    // contract only applies to the default build.
    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn create_pool_with_sqlite_url_fails_fast() {
        let config = DatabaseConfig {
            url: Some("sqlite:///var/lib/app.db".into()),
            ..Default::default()
        };
        // The Ok variant (`Option<Pool>`) is not `Debug`, so match rather than
        // use `expect_err`.
        let Err(err) = create_pool(&config) else {
            panic!("sqlite target must refuse at pool build");
        };
        assert!(
            matches!(err, PoolError::UnsupportedBackend(_)),
            "expected UnsupportedBackend, got: {err:?}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("SQLite") && msg.contains("--features sqlite"),
            "message must be actionable and name the sqlite build, got: {msg}"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn create_topology_with_sqlite_url_fails_fast() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let Err(err) = create_topology(&config) else {
            panic!("sqlite target must refuse at topology build");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
    }

    // Under `--features sqlite` the same targets that refuse above instead build
    // a real pool/topology over `SyncConnectionWrapper<SqliteConnection>`.
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_pool_with_sqlite_url_builds_under_feature() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let pool = create_pool(&config).expect("sqlite pool builds under the feature");
        assert!(pool.is_some(), "a configured sqlite url yields a pool");
    }

    // A Postgres URL in a sqlite build is a misconfiguration and must refuse.
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_pool_with_postgres_url_refuses_under_sqlite_feature() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            ..Default::default()
        };
        let Err(err) = create_pool(&config) else {
            panic!("a Postgres url must refuse under the sqlite feature");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
    }

    // A SQLite runtime has no primary/replica replication in this pool
    // architecture, so a separate replica pool would serve reads from an empty
    // database. `create_topology` must reject an in-memory or distinct-file
    // replica with an actionable boot error (addresses Codex P2).
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_topology_rejects_in_memory_sqlite_replica() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/app.db".into()),
            replica_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let Err(err) = create_topology(&config) else {
            panic!("an in-memory sqlite replica must be rejected");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
        let msg = err.to_string();
        assert!(
            msg.contains("separate read replica") && msg.contains("primary"),
            "message must be actionable: {msg}"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn create_topology_rejects_distinct_file_sqlite_replica() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/primary.db".into()),
            replica_url: Some("sqlite:///var/lib/replica.db".into()),
            ..Default::default()
        };
        let Err(err) = create_topology(&config) else {
            panic!("a distinct-file sqlite replica must be rejected");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
    }

    // A replica that normalizes to the SAME file as the primary is the same
    // database — harmless — so it is allowed and the topology builds.
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_topology_allows_same_file_sqlite_replica() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/app.db".into()),
            replica_url: Some("sqlite:///var/lib/app.db".into()),
            ..Default::default()
        };
        let topology = create_topology(&config)
            .expect("same-file sqlite replica must be allowed")
            .expect("a configured primary yields a topology");
        assert!(
            topology.replica().is_some(),
            "same-file replica pool should be built"
        );
    }

    // The no-replica happy path still builds a topology fine under the feature.
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_topology_sqlite_primary_only_builds() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let topology = create_topology(&config)
            .expect("sqlite primary-only topology builds")
            .expect("a configured primary yields a topology");
        assert!(topology.replica().is_none(), "no replica configured");
    }

    // A custom provider that overrides ONLY `create_pool` still has its replica
    // built by the trait DEFAULT `create_topology`. That default path must apply
    // the same SQLite-replica rule as the free `create_topology` /
    // `create_shard_topology`, or a provider could boot a distinct/in-memory
    // replica and route reads to an unrelated, empty database (addresses Codex
    // P2). This minimal provider delegates `create_pool` to the default factory
    // and leaves `create_topology` as the trait default — exactly the path under
    // test.
    #[cfg(feature = "sqlite")]
    struct PrimaryOnlyProvider;

    #[cfg(feature = "sqlite")]
    impl DatabasePoolProvider for PrimaryOnlyProvider {
        async fn create_pool(
            &self,
            config: &DatabaseConfig,
        ) -> Result<Option<Pool<RuntimeConnection>>, PoolError> {
            create_pool(config)
        }
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn default_provider_topology_rejects_distinct_file_sqlite_replica() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/primary.db".into()),
            replica_url: Some("sqlite:///var/lib/replica.db".into()),
            ..Default::default()
        };
        let Err(err) = PrimaryOnlyProvider.create_topology(&config).await else {
            panic!("the default-topology path must reject a distinct-file sqlite replica");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
        let msg = err.to_string();
        assert!(
            msg.contains("separate read replica") && msg.contains("primary"),
            "message must be actionable: {msg}"
        );
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn default_provider_topology_rejects_in_memory_sqlite_replica() {
        let config = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/app.db".into()),
            replica_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let Err(err) = PrimaryOnlyProvider.create_topology(&config).await else {
            panic!("an in-memory sqlite replica must be rejected by the default topology");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
    }

    // A same-file replica is the same database (harmless) and still builds
    // through the default topology; a primary-only config builds with no replica.
    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn default_provider_topology_allows_same_file_and_primary_only() {
        let same_file = DatabaseConfig {
            primary_url: Some("sqlite:///var/lib/app.db".into()),
            replica_url: Some("sqlite:///var/lib/app.db".into()),
            ..Default::default()
        };
        let topology = PrimaryOnlyProvider
            .create_topology(&same_file)
            .await
            .expect("same-file replica must be allowed by the default topology")
            .expect("a configured primary yields a topology");
        assert!(
            topology.replica().is_some(),
            "same-file replica pool should be built"
        );

        let primary_only = DatabaseConfig {
            primary_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let topology = PrimaryOnlyProvider
            .create_topology(&primary_only)
            .await
            .expect("primary-only default topology builds")
            .expect("a configured primary yields a topology");
        assert!(topology.replica().is_none(), "no replica configured");
    }

    // The per-shard replica is subject to the SAME SQLite-replica rule as the
    // control database (addresses Codex P2): an in-memory or distinct-file shard
    // replica would route reads to an unrelated, empty database, so it must be
    // rejected; a shard with only a primary (or a same-file replica) must build.
    #[cfg(feature = "sqlite")]
    #[test]
    fn create_shard_topology_rejects_in_memory_sqlite_replica() {
        let defaults = DatabaseConfig::default();
        let shard = crate::config::ShardConfig {
            name: "shard0".into(),
            primary_url: "sqlite:///var/lib/shard0.db".into(),
            replica_url: Some("sqlite::memory:".into()),
            ..Default::default()
        };
        let Err(err) = create_shard_topology(&shard, &defaults) else {
            panic!("an in-memory sqlite shard replica must be rejected");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
        let msg = err.to_string();
        assert!(
            msg.contains("separate read replica") && msg.contains("primary"),
            "message must be actionable: {msg}"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn create_shard_topology_rejects_distinct_file_sqlite_replica() {
        let defaults = DatabaseConfig::default();
        let shard = crate::config::ShardConfig {
            name: "shard0".into(),
            primary_url: "sqlite:///var/lib/shard0-primary.db".into(),
            replica_url: Some("sqlite:///var/lib/shard0-replica.db".into()),
            ..Default::default()
        };
        let Err(err) = create_shard_topology(&shard, &defaults) else {
            panic!("a distinct-file sqlite shard replica must be rejected");
        };
        assert!(matches!(err, PoolError::UnsupportedBackend(_)), "{err:?}");
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn create_shard_topology_allows_same_file_sqlite_replica() {
        let defaults = DatabaseConfig::default();
        let shard = crate::config::ShardConfig {
            name: "shard0".into(),
            primary_url: "sqlite:///var/lib/shard0.db".into(),
            replica_url: Some("sqlite:///var/lib/shard0.db".into()),
            ..Default::default()
        };
        let topology = create_shard_topology(&shard, &defaults)
            .expect("same-file sqlite shard replica must be allowed");
        assert!(
            topology.replica().is_some(),
            "same-file shard replica pool should be built"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn create_shard_topology_sqlite_primary_only_builds() {
        let defaults = DatabaseConfig::default();
        let shard = crate::config::ShardConfig {
            name: "shard0".into(),
            primary_url: "sqlite::memory:".into(),
            ..Default::default()
        };
        let topology = create_shard_topology(&shard, &defaults)
            .expect("sqlite primary-only shard topology builds");
        assert!(topology.replica().is_none(), "no shard replica configured");
    }

    // `file::memory:` (and its query-string form) is a private in-memory target
    // and must be forced single-slot; a shared-cache in-memory DB is shareable
    // and must NOT be (addresses Codex P1).
    #[cfg(feature = "sqlite")]
    #[test]
    fn sqlite_target_is_memory_covers_file_memory() {
        assert!(sqlite_target_is_memory(":memory:"));
        assert!(sqlite_target_is_memory("file::memory:"));
        assert!(sqlite_target_is_memory("file::memory:?foo=bar"));
        assert!(sqlite_target_is_memory("file:app?mode=memory"));
        // Shared-cache in-memory databases are shareable across connections.
        assert!(!sqlite_target_is_memory("file::memory:?cache=shared"));
        assert!(!sqlite_target_is_memory(
            "file:app?mode=memory&cache=shared"
        ));
        // Plain file targets are not in-memory.
        assert!(!sqlite_target_is_memory("/var/lib/app.db"));
    }

    // `sqlite_target_is_any_in_memory` is the broader predicate the
    // startup-migration reject uses: it classifies EVERY in-memory spelling —
    // private AND shared-cache — as in-memory, because none of them survive the
    // transient migration connection closing to reach the runtime pool (issue
    // #1614 follow-up). It must diverge from `sqlite_target_is_memory` (pool
    // sizing) precisely on `cache=shared`, which sizing keeps NOT-in-memory so a
    // shared-cache DB is never forced single-slot.
    #[cfg(feature = "sqlite")]
    #[test]
    fn sqlite_target_is_any_in_memory_covers_shared_cache() {
        // Private in-memory spellings — in-memory for BOTH predicates.
        assert!(sqlite_target_is_any_in_memory(":memory:"));
        assert!(sqlite_target_is_any_in_memory("sqlite::memory:"));
        assert!(sqlite_target_is_any_in_memory("sqlite://:memory:"));
        assert!(sqlite_target_is_any_in_memory("sqlite://"));
        assert!(sqlite_target_is_any_in_memory("file::memory:"));
        assert!(sqlite_target_is_any_in_memory("file::memory:?foo=bar"));
        // Shared-cache in-memory: `any_in_memory` → true (rejected for
        // migrations), but pool sizing's `sqlite_target_is_memory` → false (NOT
        // forced single-slot). This divergence is the whole point.
        assert!(sqlite_target_is_any_in_memory("file::memory:?cache=shared"));
        assert!(sqlite_target_is_any_in_memory(
            "file:app?mode=memory&cache=shared"
        ));
        assert!(!sqlite_target_is_memory("file::memory:?cache=shared"));
        assert!(!sqlite_target_is_memory(
            "file:app?mode=memory&cache=shared"
        ));
        // File-backed targets are never in-memory under either predicate.
        assert!(!sqlite_target_is_any_in_memory("sqlite:///var/lib/app.db"));
        assert!(!sqlite_target_is_any_in_memory("/var/lib/app.db"));
        assert!(!sqlite_target_is_any_in_memory("file:/var/lib/app.db"));
    }

    // The transient SQLite migration connection must carry `PRAGMA busy_timeout`
    // (mirroring the runtime pool's per-connection setup) so a concurrent
    // migrator or a briefly-held write lock WAITS instead of failing immediately
    // with SQLITE_BUSY and aborting `auto_migrate_sqlite` (Codex P1). Proof: a
    // fresh migration connection reports the configured non-zero timeout.
    #[cfg(feature = "sqlite")]
    #[test]
    fn sqlite_migration_connection_sets_busy_timeout() {
        use diesel::RunQueryDsl as _;

        #[derive(diesel::QueryableByName)]
        struct BusyTimeout {
            #[diesel(sql_type = diesel::sql_types::Integer)]
            timeout: i32,
        }

        // A tempfile-backed target (not `:memory:`) so the connection maps to a
        // real database the pragma read reflects.
        let tmp = tempfile::TempDir::new().expect("temp dir");
        let db_path = tmp.path().join("migration.db");
        let url = format!("sqlite://{}", db_path.display());

        let mut conn = super::establish_sqlite_migration_connection(&url)
            .expect("establish sqlite migration connection");
        let rows: Vec<BusyTimeout> = diesel::sql_query("PRAGMA busy_timeout")
            .load(&mut conn)
            .expect("read busy_timeout pragma");
        let timeout = rows
            .into_iter()
            .next()
            .expect("busy_timeout pragma returns a row")
            .timeout;
        assert_eq!(
            timeout, 5000,
            "the migration connection must carry the configured busy_timeout (ms) \
             so concurrent/locked migrators wait rather than hitting SQLITE_BUSY"
        );
    }

    // A read-only URI target (`mode=ro` / `immutable`) must be detected so the
    // per-connection setup batch skips the write-affecting `journal_mode = WAL`
    // pragma, which otherwise fails with "attempt to write a readonly database"
    // and 503s the whole pool (Codex P2).
    #[cfg(feature = "sqlite")]
    #[test]
    fn sqlite_target_is_read_only_detects_ro_and_immutable() {
        assert!(sqlite_target_is_read_only("file:/srv/ref.db?mode=ro"));
        // Case-insensitive key/value.
        assert!(sqlite_target_is_read_only("file:/srv/ref.db?MODE=RO"));
        assert!(sqlite_target_is_read_only("file:/srv/ref.db?immutable=1"));
        assert!(sqlite_target_is_read_only(
            "file:/srv/ref.db?immutable=true"
        ));
        assert!(sqlite_target_is_read_only(
            "file:/srv/ref.db?immutable=TRUE"
        ));
        // Read-only marker alongside other params.
        assert!(sqlite_target_is_read_only(
            "file:/srv/ref.db?cache=shared&mode=ro"
        ));

        // A plain file, an in-memory target, and a writable/shared-cache target
        // are NOT read-only.
        assert!(!sqlite_target_is_read_only("/var/lib/app.db"));
        assert!(!sqlite_target_is_read_only(":memory:"));
        assert!(!sqlite_target_is_read_only("file::memory:?cache=shared"));
        assert!(!sqlite_target_is_read_only("file:app?mode=memory"));
        assert!(!sqlite_target_is_read_only("file:/srv/ref.db?mode=rwc"));
        assert!(!sqlite_target_is_read_only("file:/srv/ref.db?immutable=0"));
        // A value that merely CONTAINS `ro` must not trip the substring trap.
        assert!(!sqlite_target_is_read_only("file:/srv/ro-data.db"));
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn file_memory_sqlite_pool_is_single_slot() {
        let config = DatabaseConfig {
            primary_url: Some("file::memory:".into()),
            pool_size: 5,
            ..Default::default()
        };
        let pool = create_pool(&config)
            .expect("file::memory: sqlite pool builds")
            .expect("a configured url yields a pool");
        assert_eq!(
            pool.status().max_size,
            1,
            "a private in-memory target must be forced single-slot"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn shared_cache_in_memory_sqlite_pool_is_not_forced_single_slot() {
        let config = DatabaseConfig {
            primary_url: Some("file::memory:?cache=shared".into()),
            pool_size: 5,
            ..Default::default()
        };
        let pool = create_pool(&config)
            .expect("shared-cache in-memory sqlite pool builds")
            .expect("a configured url yields a pool");
        assert_eq!(
            pool.status().max_size,
            5,
            "a shared-cache in-memory target must respect the configured size"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn normalize_sqlite_target_strips_schemes() {
        assert_eq!(normalize_sqlite_target("sqlite::memory:"), ":memory:");
        assert_eq!(normalize_sqlite_target("sqlite://:memory:"), ":memory:");
        assert_eq!(normalize_sqlite_target("sqlite://"), ":memory:");
        assert_eq!(
            normalize_sqlite_target("sqlite:///var/lib/app.db"),
            "/var/lib/app.db"
        );
        assert_eq!(normalize_sqlite_target("sqlite:app.db"), "app.db");
        assert_eq!(
            normalize_sqlite_target("file:/var/lib/app.db"),
            "file:/var/lib/app.db"
        );
    }

    #[test]
    fn pool_respects_max_size() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            pool_size: 5,
            ..Default::default()
        };
        let pool = create_pool(&config)
            .expect("should build pool")
            .expect("should be Some");
        assert_eq!(pool.status().max_size, 5);
    }

    #[test]
    fn pool_clamps_size_to_one_if_zero() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            pool_size: 0,
            ..Default::default()
        };
        let pool = create_pool(&config)
            .expect("should build pool")
            .expect("should be Some");
        assert_eq!(
            pool.status().max_size,
            1,
            "Pool size should be clamped to 1"
        );
    }

    // ── Db extractor tests ───────────────────────────────────────

    #[test]
    fn database_topology_builds_primary_and_replica_pools() {
        let config = DatabaseConfig {
            primary_url: Some("postgres://localhost/primary".into()),
            replica_url: Some("postgres://localhost/replica".into()),
            primary_pool_size: Some(6),
            replica_pool_size: Some(2),
            ..Default::default()
        };

        let topology = create_topology(&config)
            .expect("topology should build")
            .expect("topology should be configured");

        assert_eq!(topology.primary().status().max_size, 6);
        assert_eq!(
            topology.replica().expect("replica pool").status().max_size,
            2
        );
        assert_eq!(topology.read().status().max_size, 2);
    }

    #[test]
    fn database_topology_single_url_builds_only_primary_pool() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/single".into()),
            pool_size: 5,
            ..Default::default()
        };

        let topology = create_topology(&config)
            .expect("topology should build")
            .expect("topology should be configured");

        assert_eq!(topology.primary().status().max_size, 5);
        assert!(topology.replica().is_none());
        assert_eq!(topology.read().status().max_size, 5);
    }

    #[test]
    fn config_runtime_drift_pool_applies_connect_timeout_to_wait_and_create() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/test".into()),
            connect_timeout_secs: 7,
            ..Default::default()
        };
        let pool = create_pool(&config)
            .expect("should build pool")
            .expect("should be Some");

        let timeouts = pool.timeouts();
        assert_eq!(timeouts.wait, Some(Duration::from_secs(7)));
        assert_eq!(timeouts.create, Some(Duration::from_secs(7)));
    }

    #[derive(Clone)]
    struct TestDbState;

    impl DbState for TestDbState {
        fn pool(&self) -> Option<&Pool<crate::db::RuntimeConnection>> {
            None
        }
    }

    #[derive(Clone)]
    struct TestReadState {
        primary: Pool<crate::db::RuntimeConnection>,
    }

    impl DbState for TestReadState {
        fn pool(&self) -> Option<&Pool<crate::db::RuntimeConnection>> {
            Some(&self.primary)
        }
    }

    #[test]
    fn database_topology_read_pool_falls_back_to_primary() {
        let config = DatabaseConfig {
            url: Some("postgres://localhost/read-fallback".into()),
            pool_size: 3,
            ..Default::default()
        };
        let primary = create_pool(&config).unwrap().unwrap();
        let state = TestReadState { primary };

        assert_eq!(state.read_pool().expect("read pool").status().max_size, 3);
    }

    #[tokio::test]
    async fn db_extractor_rejects_when_no_pool() {
        use axum::Router;
        use axum::body::Body;
        use axum::http::{Request, StatusCode};
        use axum::routing::get;
        use tower::ServiceExt;

        async fn handler(_db: Db) -> &'static str {
            "ok"
        }

        let app = Router::new()
            .route("/", get(handler))
            .with_state(TestDbState);

        let response = app
            .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[tokio::test]
    async fn database_topology_primary_only_has_no_replica() {
        let config = DatabaseConfig {
            primary_url: Some("postgres://user:pass@localhost/db".to_string()),
            ..DatabaseConfig::default()
        };
        let topology = create_topology(&config).unwrap().unwrap();

        let primary = topology.primary().clone();

        let new_topology = DatabaseTopology::primary_only(primary);
        assert!(
            new_topology.replica().is_none(),
            "primary_only must set replica to None"
        );
    }

    #[tokio::test]
    async fn database_topology_from_pools_retains_replica() {
        let config = DatabaseConfig {
            primary_url: Some("postgres://user:pass@localhost/db".to_string()),
            replica_url: Some("postgres://user:pass@localhost/db_replica".to_string()),
            ..DatabaseConfig::default()
        };
        let topology = create_topology(&config).unwrap().unwrap();

        let primary = topology.primary().clone();
        let replica = topology.replica().cloned();

        let new_topology = DatabaseTopology::from_pools(primary, replica);
        assert!(
            new_topology.replica().is_some(),
            "from_pools must preserve the replica pool"
        );
    }

    // ── scrub_sql tests ───────────────────────────────────────────────────────

    #[test]
    fn scrub_sql_strips_string_literals() {
        assert_eq!(
            super::scrub_sql("SELECT * FROM users WHERE name = 'Alice'"),
            "SELECT * FROM users WHERE name = '?'"
        );
    }

    #[test]
    fn scrub_sql_strips_numeric_literals() {
        assert_eq!(
            super::scrub_sql("SELECT * FROM orders WHERE id = 42"),
            "SELECT * FROM orders WHERE id = ?"
        );
    }

    #[test]
    fn scrub_sql_preserves_pg_positional_params() {
        assert_eq!(
            super::scrub_sql("SELECT * FROM t WHERE x = $1 AND y = $2"),
            "SELECT * FROM t WHERE x = $1 AND y = $2"
        );
    }

    #[test]
    fn scrub_sql_does_not_stomp_identifiers() {
        // "table1" should not be replaced because it's not preceded by a separator
        assert_eq!(
            super::scrub_sql("SELECT * FROM table1 WHERE active = true"),
            "SELECT * FROM table1 WHERE active = true"
        );
    }

    #[test]
    fn scrub_sql_multiple_literals_in_one_query() {
        assert_eq!(
            super::scrub_sql("INSERT INTO users (name, age) VALUES ('Bob', 30)"),
            "INSERT INTO users (name, age) VALUES ('?', ?)"
        );
    }

    #[test]
    fn scrub_sql_handles_escaped_single_quotes() {
        assert_eq!(
            super::scrub_sql("SELECT * FROM t WHERE s = 'it''s a test'"),
            "SELECT * FROM t WHERE s = '?'"
        );
    }

    #[test]
    fn scrub_sql_empty_string() {
        assert_eq!(super::scrub_sql(""), "");
    }

    // ── Bug fixes: exponent suffix, dollar-quoted strings, E-string escapes ──

    #[test]
    fn scrub_sql_scientific_notation_integer_exponent() {
        // 1e6 should be fully redacted to ? (the "e6" part is the exponent)
        assert_eq!(
            super::scrub_sql("SELECT * FROM t WHERE n = 1e6"),
            "SELECT * FROM t WHERE n = ?"
        );
    }

    #[test]
    fn scrub_sql_scientific_notation_float_exponent() {
        // 2.5E-4 should be fully redacted: digit + decimal + E + sign + digit(s)
        assert_eq!(
            super::scrub_sql("SELECT * FROM t WHERE n = 2.5E-4"),
            "SELECT * FROM t WHERE n = ?"
        );
    }

    #[test]
    fn scrub_sql_scientific_notation_uppercase_positive_exponent() {
        // 3E+10 — uppercase E with explicit + sign
        assert_eq!(
            super::scrub_sql("SELECT * FROM t WHERE n = 3E+10"),
            "SELECT * FROM t WHERE n = ?"
        );
    }

    #[test]
    fn scrub_sql_dollar_quoted_anonymous() {
        // $$...$$ dollar-quoted string: content must be fully redacted
        assert_eq!(super::scrub_sql("SELECT $$secret value$$"), "SELECT '?'");
    }

    #[test]
    fn scrub_sql_dollar_quoted_with_tag() {
        // $tag$...$tag$ — tagged dollar-quoted string
        assert_eq!(
            super::scrub_sql("SELECT $body$hello world$body$"),
            "SELECT '?'"
        );
    }

    #[test]
    fn scrub_sql_dollar_quoted_does_not_affect_positional_params() {
        // $1, $2 positional params must still pass through unmodified
        assert_eq!(
            super::scrub_sql("SELECT $1, $2 FROM $$secret$$ WHERE id = $3"),
            "SELECT $1, $2 FROM '?' WHERE id = $3"
        );
    }

    #[test]
    fn scrub_sql_estring_backslash_escaped_quote() {
        // E'it\'s secret' — backslash-escaped quote inside E'' string
        assert_eq!(
            super::scrub_sql(r"SELECT E'it\'s secret' FROM t"),
            "SELECT '?' FROM t"
        );
    }

    #[test]
    fn scrub_sql_estring_uppercase() {
        // Uppercase E prefix variant E'...'
        assert_eq!(
            super::scrub_sql("SELECT E'hello world' FROM t"),
            "SELECT '?' FROM t"
        );
    }

    #[test]
    fn scrub_sql_estring_multiple_backslash_escapes() {
        // Multiple backslash sequences inside one E'' literal
        assert_eq!(
            super::scrub_sql(r"SELECT E'line1\nline2' FROM t"),
            "SELECT '?' FROM t"
        );
    }

    #[test]
    fn scrub_sql_leading_dot_numeric_literals() {
        assert_eq!(super::scrub_sql("SELECT .5"), "SELECT ?");
        assert_eq!(super::scrub_sql("SELECT .25 + .75"), "SELECT ? + ?");
        assert_eq!(super::scrub_sql("SELECT t.col"), "SELECT t.col");
        assert_eq!(
            super::scrub_sql("SELECT schema.table.col"),
            "SELECT schema.table.col"
        );
    }

    #[test]
    fn scrub_sql_estring_doubled_quote_escape() {
        assert_eq!(
            super::scrub_sql("SELECT E'it''s secret' FROM t"),
            "SELECT '?' FROM t"
        );
    }

    #[test]
    fn scrub_sql_numeric_literal_underscore_grouping() {
        assert_eq!(super::scrub_sql("SELECT 5_432_000"), "SELECT ?");
        assert_eq!(super::scrub_sql("SELECT 1_000.5_0"), "SELECT ?");
        assert_eq!(super::scrub_sql("SELECT col_5_val"), "SELECT col_5_val");
        assert_eq!(super::scrub_sql("SELECT col_5"), "SELECT col_5");
    }

    // ── Transaction isolation + retry (issue #1202) ─────────────────

    #[derive(Debug)]
    struct FakeDbErrorInfo {
        message: &'static str,
    }

    impl diesel::result::DatabaseErrorInformation for FakeDbErrorInfo {
        fn message(&self) -> &str {
            self.message
        }
        fn details(&self) -> Option<&str> {
            None
        }
        fn hint(&self) -> Option<&str> {
            None
        }
        fn table_name(&self) -> Option<&str> {
            None
        }
        fn column_name(&self) -> Option<&str> {
            None
        }
        fn constraint_name(&self) -> Option<&str> {
            None
        }
        fn statement_position(&self) -> Option<i32> {
            None
        }
    }

    fn diesel_db_error(
        kind: diesel::result::DatabaseErrorKind,
        message: &'static str,
    ) -> diesel::result::Error {
        diesel::result::Error::DatabaseError(kind, Box::new(FakeDbErrorInfo { message }))
    }

    // ── TxOptions builder ────────────────────────────────────────

    #[test]
    fn tx_options_default_is_read_committed_no_retry() {
        let opts = TxOptions::default();
        assert_eq!(opts.isolation, IsolationLevel::ReadCommitted);
        assert!(!opts.read_only);
        assert!(!opts.deferrable);
        assert_eq!(
            opts.max_attempts, 1,
            "default must behave exactly like today's tx(): one attempt, no retry"
        );
    }

    #[test]
    fn tx_options_serializable_enables_retry() {
        let opts = TxOptions::serializable();
        assert_eq!(opts.isolation, IsolationLevel::Serializable);
        assert!(
            opts.max_attempts > 1,
            "serializable() should default to retrying since retry is the point"
        );
    }

    #[test]
    fn tx_options_repeatable_read_enables_retry() {
        let opts = TxOptions::repeatable_read();
        assert_eq!(opts.isolation, IsolationLevel::RepeatableRead);
        assert!(opts.max_attempts > 1);
    }

    #[test]
    fn tx_options_builder_chains() {
        let opts = TxOptions::serializable()
            .read_only()
            .deferrable()
            .max_attempts(9)
            .initial_backoff(Duration::from_millis(3))
            .max_backoff(Duration::from_millis(30));
        assert!(opts.read_only);
        assert!(opts.deferrable);
        assert_eq!(opts.max_attempts, 9);
        assert_eq!(opts.initial_backoff, Duration::from_millis(3));
        assert_eq!(opts.max_backoff, Duration::from_millis(30));
    }

    #[test]
    fn tx_options_max_attempts_clamps_to_at_least_one() {
        // A zero here must never produce a loop that skips the closure entirely.
        assert_eq!(TxOptions::default().max_attempts(0).max_attempts, 1);
    }

    #[test]
    fn tx_options_effective_max_attempts_clamps_struct_literal_bypass() {
        // `max_attempts` is a public field, so struct-literal update syntax can
        // set it to 0 directly, bypassing the `max_attempts()` builder's clamp.
        // The retry loop must still treat this as "run once", not "never run".
        let opts = TxOptions {
            max_attempts: 0,
            ..TxOptions::default()
        };
        assert_eq!(opts.max_attempts, 0, "the raw field is not itself clamped");
        assert_eq!(
            opts.effective_max_attempts(),
            1,
            "the retry loop's accessor must clamp a struct-literal 0 to 1"
        );
    }

    // ── Backoff ──────────────────────────────────────────────────

    #[test]
    fn retry_backoff_base_doubles_per_attempt() {
        let initial = Duration::from_millis(5);
        let max = Duration::from_secs(60);
        assert_eq!(
            retry_backoff_base(initial, max, 1),
            Duration::from_millis(5)
        );
        assert_eq!(
            retry_backoff_base(initial, max, 2),
            Duration::from_millis(10)
        );
        assert_eq!(
            retry_backoff_base(initial, max, 3),
            Duration::from_millis(20)
        );
        assert_eq!(
            retry_backoff_base(initial, max, 4),
            Duration::from_millis(40)
        );
    }

    #[test]
    fn retry_backoff_base_caps_at_max() {
        let initial = Duration::from_millis(5);
        let max = Duration::from_millis(50);
        // 5,10,20,40 then capped at 50 for all higher attempts.
        assert_eq!(
            retry_backoff_base(initial, max, 5),
            Duration::from_millis(50)
        );
        assert_eq!(retry_backoff_base(initial, max, 40), max);
        // Huge attempt must not panic on shift overflow.
        assert_eq!(retry_backoff_base(initial, max, u32::MAX), max);
    }

    #[test]
    fn retry_backoff_delay_stays_within_jitter_bounds_and_cap() {
        let initial = Duration::from_millis(100);
        let max = Duration::from_secs(10);
        for attempt in 1..=6u32 {
            let base = retry_backoff_base(initial, max, attempt);
            for _ in 0..64 {
                let d = retry_backoff_delay(initial, max, attempt);
                // Jitter is +/-20% of base, never exceeding the cap.
                assert!(
                    d >= base.mul_f64(0.8) && d <= base.mul_f64(1.2),
                    "delay {d:?} out of jitter bounds for base {base:?}"
                );
                assert!(d <= max, "delay {d:?} exceeded cap {max:?}");
            }
        }
    }

    // ── Retryable-error classification ───────────────────────────

    #[test]
    fn serialization_failure_is_retryable() {
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::SerializationFailure,
            "could not serialize access due to read/write dependencies among transactions",
        ));
        assert!(is_retryable_txn_error(&err));
    }

    #[test]
    fn deadlock_is_retryable() {
        // Postgres 40P01 is not mapped to a dedicated DatabaseErrorKind, so it
        // is caught via an exact match against Postgres's own invariant
        // primary message for this condition.
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::Unknown,
            "deadlock detected",
        ));
        assert!(is_retryable_txn_error(&err));
    }

    #[test]
    fn deadlock_message_is_matched_case_and_whitespace_insensitively() {
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::Unknown,
            "  Deadlock Detected  ",
        ));
        assert!(is_retryable_txn_error(&err));
    }

    #[test]
    fn unknown_kind_message_merely_mentioning_deadlock_is_not_retryable() {
        // Regression (PR #1581 review, round 2): an `Unknown`-kind Postgres
        // error from application SQL (e.g. `RAISE EXCEPTION`) whose message
        // merely *mentions* "deadlock detected" or "40001" as part of a
        // longer business message must not be retried -- only an exact match
        // against Postgres's own invariant primary message counts.
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::Unknown,
            "order 40001 could not be processed: deadlock detected in workflow",
        ));
        assert!(!is_retryable_txn_error(&err));
    }

    #[test]
    fn unknown_kind_40001_text_alone_is_not_retryable() {
        // A bare "40001" in an Unknown-kind message is no longer treated as a
        // retry signal -- genuine 40001s are already reliably classified via
        // the DatabaseErrorKind::SerializationFailure fast path (diesel-async
        // maps the real SQLSTATE), so this text pattern is dead weight that
        // only added false-positive risk.
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::Unknown,
            "ERROR: 40001: could not serialize access",
        ));
        assert!(!is_retryable_txn_error(&err));
    }

    #[test]
    fn unique_violation_is_not_retryable() {
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::UniqueViolation,
            "duplicate key value violates unique constraint",
        ));
        assert!(!is_retryable_txn_error(&err));
    }

    #[test]
    fn unique_violation_with_magic_substring_in_message_is_not_retryable() {
        // Regression: a non-retryable `DatabaseErrorKind` must never be
        // misclassified just because its message/constraint text happens to
        // contain a magic substring like "40001" or "deadlock detected".
        let err: AutumnError = AutumnError::internal_server_error(diesel_db_error(
            diesel::result::DatabaseErrorKind::UniqueViolation,
            "duplicate key value violates unique constraint \"orders_40001_key\"",
        ));
        assert!(!is_retryable_txn_error(&err));
    }

    #[test]
    fn plain_internal_error_is_not_retryable() {
        let err = AutumnError::internal_server_error_msg("something unrelated broke");
        assert!(!is_retryable_txn_error(&err));
    }

    #[test]
    fn domain_error_with_magic_substring_and_no_db_error_is_not_retryable() {
        // Regression (PR #1581 review): a plain domain/validation error — no
        // diesel::result::Error or tokio_postgres error anywhere in its
        // chain — must never be retried just because its message happens to
        // contain a magic substring like "40001" or "deadlock detected".
        // Retry requires a structured signal, never bare text.
        let err = AutumnError::bad_request_msg(
            "order 40001 could not be processed: deadlock detected in workflow",
        );
        assert!(!is_retryable_txn_error(&err));
    }

    #[derive(Debug, thiserror::Error)]
    #[error("app error")]
    struct WrappedDbError(#[source] diesel::result::Error);

    #[test]
    fn serialization_failure_wrapped_in_custom_error_is_retryable() {
        // A custom `E` (e.g. an app error enum with a `#[from] diesel::result::Error`
        // variant) is not itself a `diesel::result::Error`, so it isn't found by
        // a top-level-only downcast. `downcast_chain_ref` walks `source()` to
        // find it, so wrapped diesel errors are still classified structurally
        // — no need to fall back to message scanning for this common case.
        let err: AutumnError = AutumnError::internal_server_error(WrappedDbError(diesel_db_error(
            diesel::result::DatabaseErrorKind::SerializationFailure,
            "could not serialize access due to read/write dependencies among transactions",
        )));
        assert!(is_retryable_txn_error(&err));
    }

    #[test]
    fn unique_violation_wrapped_in_custom_error_is_not_retryable() {
        let err: AutumnError = AutumnError::internal_server_error(WrappedDbError(diesel_db_error(
            diesel::result::DatabaseErrorKind::UniqueViolation,
            "duplicate key value violates unique constraint",
        )));
        assert!(!is_retryable_txn_error(&err));
    }

    // ── Attempt accounting ───────────────────────────────────────

    #[test]
    fn retry_decision_retries_until_attempts_exhausted() {
        // A retryable error keeps retrying while attempts remain, then stops.
        let max = 3;
        assert_eq!(retry_decision(1, max, true), RetryDecision::Retry);
        assert_eq!(retry_decision(2, max, true), RetryDecision::Retry);
        assert_eq!(
            retry_decision(3, max, true),
            RetryDecision::Stop,
            "the final attempt must stop even when the error is retryable (exhausted)"
        );
    }

    #[test]
    fn retry_decision_stops_immediately_on_non_retryable() {
        assert_eq!(retry_decision(1, 5, false), RetryDecision::Stop);
    }

    #[test]
    fn retry_decision_single_attempt_never_retries() {
        // max_attempts == 1 (the default) must behave exactly like today's tx().
        assert_eq!(retry_decision(1, 1, true), RetryDecision::Stop);
    }
}

// ── Postgres TLS (sslmode) support ───────────────────────────────────────────

/// TLS support for the Postgres pool, driven by `sslmode` in the database URL.
///
/// diesel-async's default `AsyncPgConnection::establish` hardcodes
/// `tokio_postgres::NoTls`, which makes `sslmode=require` fail on every
/// connection with "no TLS implementation configured". This module plugs a
/// rustls-backed connector into the pool via
/// [`diesel_async::pooled_connection::ManagerConfig::custom_setup`] when the
/// URL asks for TLS. Both URL (`postgres://…?sslmode=require`) and
/// keyword/value (`host=… sslmode=require`) connection strings are
/// recognized. The synchronous migration/startup-wait path shares the same
/// connector through [`super::establish_migration_connection`] — the
/// bundled libpq has no SSL support, so the native `PgConnection` path
/// cannot reach TLS-only servers at all.
///
/// | `sslmode`                     | behavior |
/// | ----------------------------- | -------- |
/// | absent, `disable`, `prefer`   | unchanged: the default `NoTls` path (plaintext), exactly as before this module existed |
/// | `require`                     | TLS. The connection is encrypted and the handshake signatures are verified, but the server's certificate **chain/identity is not** — matching what `libpq`/`psql` do for `require` (self-signed and private-CA servers work) |
/// | `verify-full`                 | TLS with full chain **and** hostname verification against the Mozilla root store (`webpki-roots`), plus any `sslrootcert=<PEM file>` from the URL |
/// | `verify-ca`                   | rejected with guidance: chain-only verification is not implemented; use `verify-full` (stricter) or `require` |
///
/// `require` combined with `sslrootcert` is also rejected rather than
/// silently ignoring the CA file: `libpq` documents that combination as
/// upgrading to certificate verification, so dropping the file would
/// silently weaken what the operator asked for.
mod tls {
    use std::sync::Arc;

    use diesel::{ConnectionError, ConnectionResult};
    use diesel_async::pooled_connection::SetupCallback;
    use diesel_async::{AsyncConnection as _, AsyncPgConnection};
    use futures::FutureExt as _;
    use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
    use rustls::crypto::CryptoProvider;
    use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
    use rustls::{DigitallySignedStruct, SignatureScheme};
    use tokio_postgres_rustls::MakeRustlsConnect;

    /// TLS posture derived from the connection string's `sslmode` (and
    /// `sslrootcert`). See the [module docs](self) for the full table.
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub(super) enum TlsPosture {
        /// No TLS machinery: keep diesel-async's default `NoTls` setup path.
        Off,
        /// Encrypt without verifying the server certificate chain
        /// (`libpq` parity for `sslmode=require`).
        Require,
        /// Encrypt and fully verify the certificate chain + hostname.
        VerifyFull {
            /// Optional `sslrootcert` PEM file to trust in addition to the
            /// Mozilla root store.
            root_cert: Option<String>,
        },
        /// A recognized-but-unsupported combination; every connection attempt
        /// fails loudly with this reason instead of silently downgrading.
        Unsupported { reason: String },
    }

    impl TlsPosture {
        /// Classify a database URL / keyword-value connection string.
        pub(super) fn from_database_url(database_url: &str) -> Self {
            let params = ssl_params(database_url);
            // Last occurrence wins, matching libpq/tokio-postgres semantics.
            let get = |key: &str| {
                params
                    .iter()
                    .rev()
                    .find(|(k, _)| k == key)
                    .map(|(_, v)| v.as_str())
            };
            let root_cert = get("sslrootcert").map(str::to_owned);
            match get("sslmode") {
                Some("require") => {
                    if root_cert.is_some() {
                        Self::Unsupported {
                            reason: "sslmode=require with sslrootcert is not supported: \
                                     PostgreSQL treats that combination as requiring \
                                     certificate verification against the CA file, which \
                                     this pool implements only for sslmode=verify-full. \
                                     Use sslmode=verify-full to verify the certificate, or \
                                     sslmode=require without sslrootcert to encrypt without \
                                     verifying it."
                                .to_owned(),
                        }
                    } else {
                        Self::Require
                    }
                }
                Some("verify-full") => Self::VerifyFull { root_cert },
                Some("verify-ca") => Self::Unsupported {
                    reason: "sslmode=verify-ca is not supported: chain-only verification \
                             (without hostname checking) is not implemented. Use \
                             sslmode=verify-full (stricter) or sslmode=require (encrypts \
                             without verifying the certificate)."
                        .to_owned(),
                },
                // Absent, `disable`, `prefer`, or anything unrecognized:
                // preserve the pre-TLS behavior exactly (including the error
                // tokio-postgres itself raises for invalid values).
                _ => Self::Off,
            }
        }
    }

    /// Build the pool's custom connection-setup callback for a TLS posture.
    // Only the default (Postgres) `build_pool` arm installs this custom TLS
    // setup; unused in a `--features sqlite` build (SQLite has no TLS transport).
    #[cfg_attr(feature = "sqlite", allow(dead_code))]
    pub(super) fn setup_callback(posture: TlsPosture) -> SetupCallback<AsyncPgConnection> {
        Box::new(move |url: &str| {
            let posture = posture.clone();
            let url = url.to_owned();
            async move { establish(&url, posture).await }.boxed()
        })
    }

    /// Establish an [`AsyncPgConnection`] honoring `posture` — the single
    /// connect path shared by the pool's setup callback and the synchronous
    /// migration/wait-check wrapper
    /// ([`super::establish_migration_connection`]).
    pub(super) async fn establish(
        url: &str,
        posture: TlsPosture,
    ) -> ConnectionResult<AsyncPgConnection> {
        match posture {
            // Defensive: `build_pool` never installs the callback for
            // `Off`, but fall back to the stock path if it ever does.
            TlsPosture::Off => AsyncPgConnection::establish(url).await,
            TlsPosture::Require => connect_with(url, relaxed_connector()?).await,
            TlsPosture::VerifyFull { root_cert } => {
                // tokio-postgres's own connection-string parser
                // rejects `verify-*` and `sslrootcert`, so hand it a
                // sanitized string; the real verification lives in
                // the rustls config.
                let sanitized = sanitize_for_verify_full(url);
                connect_with(&sanitized, verifying_connector(root_cert.as_deref())?).await
            }
            TlsPosture::Unsupported { reason } => {
                Err(ConnectionError::InvalidConnectionUrl(reason))
            }
        }
    }

    async fn connect_with(
        url: &str,
        tls: MakeRustlsConnect,
    ) -> ConnectionResult<AsyncPgConnection> {
        let (client, connection) = tokio_postgres::connect(url, tls).await.map_err(|e| {
            // tokio_postgres's Display gives only the error kind ("error
            // performing TLS handshake"); append the source so operators see
            // the actionable cause ("server does not support TLS", a
            // certificate verification failure, …).
            let msg = std::error::Error::source(&e)
                .map_or_else(|| e.to_string(), |source| format!("{e}: {source}"));
            ConnectionError::BadConnection(msg)
        })?;
        AsyncPgConnection::try_from_client_and_connection(client, connection).await
    }

    /// Encrypt-only connector for `sslmode=require`: handshake signatures are
    /// verified but the certificate chain/identity is not, mirroring
    /// `libpq`/`psql` behavior for `require` (which self-signed and
    /// private-CA deployments rely on).
    fn relaxed_connector() -> Result<MakeRustlsConnect, ConnectionError> {
        let provider = Arc::new(rustls::crypto::ring::default_provider());
        let config = rustls::ClientConfig::builder_with_provider(provider.clone())
            .with_safe_default_protocol_versions()
            .map_err(|e| {
                ConnectionError::BadConnection(format!("failed to build TLS config: {e}"))
            })?
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(NoServerCertVerification(
                (*provider).clone(),
            )))
            .with_no_client_auth();
        Ok(MakeRustlsConnect::new(config))
    }

    /// Fully verifying connector for `sslmode=verify-full`: certificate chain
    /// and hostname are checked against the Mozilla root store plus any
    /// `sslrootcert` PEM file from the connection string. `webpki-roots` is
    /// used (rather than the platform store) so behavior is deterministic on
    /// every target, including mobile, where no native store is reachable.
    fn verifying_connector(root_cert: Option<&str>) -> Result<MakeRustlsConnect, ConnectionError> {
        let mut roots = rustls::RootCertStore::empty();
        roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
        if let Some(path) = root_cert {
            use rustls_pki_types::pem::PemObject as _;
            let certs = CertificateDer::pem_file_iter(path).map_err(|e| {
                ConnectionError::BadConnection(format!("failed to read sslrootcert {path}: {e}"))
            })?;
            for cert in certs {
                let cert = cert.map_err(|e| {
                    ConnectionError::BadConnection(format!(
                        "failed to parse sslrootcert {path}: {e}"
                    ))
                })?;
                roots.add(cert).map_err(|e| {
                    ConnectionError::BadConnection(format!(
                        "failed to trust sslrootcert {path}: {e}"
                    ))
                })?;
            }
        }
        let provider = Arc::new(rustls::crypto::ring::default_provider());
        let config = rustls::ClientConfig::builder_with_provider(provider)
            .with_safe_default_protocol_versions()
            .map_err(|e| {
                ConnectionError::BadConnection(format!("failed to build TLS config: {e}"))
            })?
            .with_root_certificates(roots)
            .with_no_client_auth();
        Ok(MakeRustlsConnect::new(config))
    }

    /// Whether tokio-postgres would parse this connection string as a URL —
    /// see [`crate::pg_conn_str::is_url`]. Getting this wrong would send
    /// keyword strings whose values embed URL-like tokens
    /// (`password=https://…`) down the URL path, where `url::Url::parse`
    /// fails and the TLS posture silently falls back to [`TlsPosture::Off`].
    /// Shared with config validation so the two never disagree about which
    /// strings are reachable.
    use crate::pg_conn_str::is_url as is_url_connection_string;
    /// Parse a libpq-style `key = value` connection string, mirroring
    /// tokio-postgres's (private) parser — see
    /// [`crate::pg_conn_str::keyword_value_pairs`]. Naive whitespace
    /// splitting would miss `sslmode` in quoted/spaced strings and silently
    /// downgrade the TLS posture to [`TlsPosture::Off`].
    use crate::pg_conn_str::keyword_value_pairs;

    /// Extract query/keyword parameters relevant to TLS from either a
    /// `postgres://…` URL or a `key=value …` connection string.
    fn ssl_params(database_url: &str) -> Vec<(String, String)> {
        if is_url_connection_string(database_url) {
            url::Url::parse(database_url)
                .map(|u| {
                    u.query_pairs()
                        .map(|(k, v)| (k.into_owned(), v.into_owned()))
                        .collect()
                })
                .unwrap_or_default()
        } else {
            keyword_value_pairs(database_url).unwrap_or_default()
        }
    }

    /// Serialize one value of a keyword/value connection string, quoting it
    /// whenever it would not survive re-parsing as a bare token.
    fn quote_keyword_value(value: &str) -> String {
        if !value.is_empty()
            && !value.contains(|c: char| c.is_whitespace() || c == '\'' || c == '\\')
        {
            return value.to_owned();
        }
        let mut quoted = String::with_capacity(value.len() + 2);
        quoted.push('\'');
        for c in value.chars() {
            if c == '\'' || c == '\\' {
                quoted.push('\\');
            }
            quoted.push(c);
        }
        quoted.push('\'');
        quoted
    }

    /// Rewrite a `verify-full` connection string into one tokio-postgres can
    /// parse: `sslmode` downgraded to `require` (the handshake decision), and
    /// `sslrootcert` removed (consumed by [`verifying_connector`] instead).
    fn sanitize_for_verify_full(database_url: &str) -> String {
        if is_url_connection_string(database_url) {
            let Ok(mut parsed) = url::Url::parse(database_url) else {
                return database_url.to_owned();
            };
            // Rewrite ONLY the sslmode/sslrootcert components, preserving
            // every other raw query component byte-for-byte: decoding and
            // re-serializing through query_pairs()/append_pair() would
            // form-encode spaces as `+`, which libpq/tokio-postgres do not
            // accept in Postgres URI parameters —
            // `options=-c%20search_path%3Dtenant` would be mangled into
            // `options=-c+search_path=tenant`. (The startup-wait splice in
            // migrate.rs avoids those APIs for the same reason.)
            let raw = parsed
                .query()
                .unwrap_or("")
                .split('&')
                .filter(|component| !component.is_empty())
                .filter_map(|component| {
                    let key = component.split('=').next().unwrap_or(component);
                    match key {
                        "sslmode" => Some("sslmode=require"),
                        "sslrootcert" => None,
                        _ => Some(component),
                    }
                })
                .collect::<Vec<_>>()
                .join("&");
            if raw.is_empty() {
                parsed.set_query(None);
            } else {
                parsed.set_query(Some(&raw));
            }
            parsed.to_string()
        } else {
            let Some(pairs) = keyword_value_pairs(database_url) else {
                // Malformed: pass through so tokio-postgres reports its own
                // parse error instead of us inventing a different string.
                return database_url.to_owned();
            };
            pairs
                .into_iter()
                .filter_map(|(k, v)| match k.as_str() {
                    "sslmode" => Some("sslmode=require".to_owned()),
                    "sslrootcert" => None,
                    _ => Some(format!("{k}={}", quote_keyword_value(&v))),
                })
                .collect::<Vec<_>>()
                .join(" ")
        }
    }

    /// Accepts any server certificate without validating its chain or
    /// hostname, while still cryptographically verifying the TLS handshake
    /// signatures (the connection is genuinely encrypted to *some* holder of
    /// the presented key — "no identity check", not "no security"). This is
    /// exactly `libpq`/`psql`'s posture for `sslmode=require`; identity
    /// verification is `sslmode=verify-full`'s job.
    #[derive(Debug)]
    struct NoServerCertVerification(CryptoProvider);

    impl ServerCertVerifier for NoServerCertVerification {
        fn verify_server_cert(
            &self,
            _end_entity: &CertificateDer<'_>,
            _intermediates: &[CertificateDer<'_>],
            _server_name: &ServerName<'_>,
            _ocsp_response: &[u8],
            _now: UnixTime,
        ) -> Result<ServerCertVerified, rustls::Error> {
            Ok(ServerCertVerified::assertion())
        }

        fn verify_tls12_signature(
            &self,
            message: &[u8],
            cert: &CertificateDer<'_>,
            dss: &DigitallySignedStruct,
        ) -> Result<HandshakeSignatureValid, rustls::Error> {
            rustls::crypto::verify_tls12_signature(
                message,
                cert,
                dss,
                &self.0.signature_verification_algorithms,
            )
        }

        fn verify_tls13_signature(
            &self,
            message: &[u8],
            cert: &CertificateDer<'_>,
            dss: &DigitallySignedStruct,
        ) -> Result<HandshakeSignatureValid, rustls::Error> {
            rustls::crypto::verify_tls13_signature(
                message,
                cert,
                dss,
                &self.0.signature_verification_algorithms,
            )
        }

        fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
            self.0.signature_verification_algorithms.supported_schemes()
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn absent_disable_and_prefer_keep_the_default_notls_path() {
            for url in [
                "postgres://user:pass@db.example.com:5432/app",
                "postgres://user:pass@db.example.com:5432/app?sslmode=disable",
                "postgres://user:pass@db.example.com:5432/app?sslmode=prefer",
                "host=db.example.com user=user",
                "host=db.example.com user=user sslmode=disable",
            ] {
                assert_eq!(
                    TlsPosture::from_database_url(url),
                    TlsPosture::Off,
                    "{url} must keep the default NoTls path"
                );
            }
        }

        #[test]
        fn unrecognized_sslmode_values_keep_the_default_path_and_its_errors() {
            // tokio-postgres itself rejects these at connect time; the pool
            // must not mask that with a different TLS decision.
            assert_eq!(
                TlsPosture::from_database_url("postgres://u@h/db?sslmode=allow"),
                TlsPosture::Off
            );
            assert_eq!(
                TlsPosture::from_database_url("postgres://u@h/db?sslmode=bogus"),
                TlsPosture::Off
            );
        }

        #[test]
        fn require_selects_the_relaxed_tls_connector() {
            assert_eq!(
                TlsPosture::from_database_url(
                    "postgres://user:pass@db.example.com:5432/app?sslmode=require"
                ),
                TlsPosture::Require
            );
            assert_eq!(
                TlsPosture::from_database_url("host=db.example.com sslmode=require"),
                TlsPosture::Require
            );
        }

        #[test]
        fn verify_full_selects_the_verifying_connector_with_optional_root() {
            assert_eq!(
                TlsPosture::from_database_url("postgres://u@h/db?sslmode=verify-full"),
                TlsPosture::VerifyFull { root_cert: None }
            );
            assert_eq!(
                TlsPosture::from_database_url(
                    "postgres://u@h/db?sslmode=verify-full&sslrootcert=/etc/ca.pem"
                ),
                TlsPosture::VerifyFull {
                    root_cert: Some("/etc/ca.pem".to_owned())
                }
            );
        }

        #[test]
        fn keyword_strings_with_spaces_around_equals_are_parsed() {
            // Regression: whitespace-splitting missed `sslmode` here and
            // silently downgraded the posture to Off (plaintext).
            assert_eq!(
                TlsPosture::from_database_url("host=db user=u sslmode = require"),
                TlsPosture::Require
            );
            assert_eq!(
                TlsPosture::from_database_url("sslmode = require"),
                TlsPosture::Require
            );
            assert_eq!(
                TlsPosture::from_database_url("host = db sslmode\t=\nverify-full user=u"),
                TlsPosture::VerifyFull { root_cert: None }
            );
        }

        #[test]
        fn keyword_strings_with_quoted_values_are_parsed() {
            assert_eq!(
                TlsPosture::from_database_url("sslmode='verify-full'"),
                TlsPosture::VerifyFull { root_cert: None }
            );
            assert_eq!(
                TlsPosture::from_database_url(
                    "host=db sslmode='verify-full' sslrootcert='/path with space/ca.pem'"
                ),
                TlsPosture::VerifyFull {
                    root_cert: Some("/path with space/ca.pem".to_owned())
                }
            );
            // Backslash escapes work inside and outside quotes, as in
            // tokio-postgres/libpq.
            assert_eq!(
                TlsPosture::from_database_url(r"sslmode=verify-full sslrootcert='/pki/it\'s.pem'"),
                TlsPosture::VerifyFull {
                    root_cert: Some("/pki/it's.pem".to_owned())
                }
            );
            assert_eq!(
                TlsPosture::from_database_url(r"sslmode=verify-full sslrootcert=/pki/my\ ca.pem"),
                TlsPosture::VerifyFull {
                    root_cert: Some("/pki/my ca.pem".to_owned())
                }
            );
        }

        #[test]
        fn malformed_keyword_strings_keep_the_default_path_and_its_errors() {
            // tokio-postgres rejects these at connect time; the posture must
            // not guess a different TLS decision for them.
            for url in [
                "host=db sslmode",            // missing `=` and value
                "host=db sslmode=",           // empty unquoted value
                "host=db sslmode='require",   // unterminated quote
                r"host=db sslmode='require\", // EOF right after escape
            ] {
                assert_eq!(
                    TlsPosture::from_database_url(url),
                    TlsPosture::Off,
                    "{url:?} must keep the default NoTls path"
                );
            }
        }

        #[test]
        fn keyword_strings_with_url_like_values_stay_on_the_keyword_path() {
            // Regression: `contains("://")` sent this whole string to
            // url::Url::parse, which failed, so sslmode=require silently
            // became Off (plaintext). Only `postgres://`/`postgresql://`
            // prefixes are URLs, exactly as in tokio-postgres.
            assert_eq!(
                TlsPosture::from_database_url("host=db password=https://secret sslmode=require"),
                TlsPosture::Require
            );
            assert_eq!(
                TlsPosture::from_database_url(
                    "host=db options='-c foo=bar://baz' sslmode='verify-full'"
                ),
                TlsPosture::VerifyFull { root_cert: None }
            );
            // The alternate scheme spelling is still parsed as a URL.
            assert_eq!(
                TlsPosture::from_database_url("postgresql://u@h/db?sslmode=require"),
                TlsPosture::Require
            );
        }

        #[test]
        fn verify_ca_and_require_with_rootcert_fail_loudly() {
            assert!(matches!(
                TlsPosture::from_database_url("postgres://u@h/db?sslmode=verify-ca"),
                TlsPosture::Unsupported { .. }
            ));
            assert!(matches!(
                TlsPosture::from_database_url(
                    "postgres://u@h/db?sslmode=require&sslrootcert=/etc/ca.pem"
                ),
                TlsPosture::Unsupported { .. }
            ));
        }

        #[test]
        fn last_sslmode_occurrence_wins() {
            assert_eq!(
                TlsPosture::from_database_url("postgres://u@h/db?sslmode=disable&sslmode=require"),
                TlsPosture::Require
            );
        }

        #[test]
        fn sanitize_rewrites_verify_full_and_drops_sslrootcert() {
            let sanitized = sanitize_for_verify_full(
                "postgres://u@h:5432/db?application_name=app&sslmode=verify-full&sslrootcert=/etc/ca.pem",
            );
            assert!(sanitized.contains("sslmode=require"), "{sanitized}");
            assert!(!sanitized.contains("verify-full"), "{sanitized}");
            assert!(!sanitized.contains("sslrootcert"), "{sanitized}");
            assert!(sanitized.contains("application_name=app"), "{sanitized}");

            // Percent-encoded components survive byte-for-byte: form
            // re-encoding would turn %20 into `+`, which libpq and
            // tokio-postgres do not accept as a space in URI parameters.
            let sanitized = sanitize_for_verify_full(
                "postgres://u@h/db?options=-c%20search_path%3Dtenant&sslmode=verify-full&sslrootcert=/etc/ca.pem",
            );
            assert_eq!(
                sanitized, "postgres://u@h/db?options=-c%20search_path%3Dtenant&sslmode=require",
                "raw components must be preserved verbatim"
            );
            // Dropping the only params leaves a clean URL with no `?`.
            assert_eq!(
                sanitize_for_verify_full("postgres://u@h/db?sslrootcert=/etc/ca.pem"),
                "postgres://u@h/db"
            );

            let kv = sanitize_for_verify_full(
                "host=h user=u sslmode=verify-full sslrootcert=/etc/ca.pem dbname=db",
            );
            assert_eq!(kv, "host=h user=u sslmode=require dbname=db");

            // Whitespace/quoting variants normalize to a string
            // tokio-postgres parses to the same parameters.
            let kv = sanitize_for_verify_full(
                r"host=h sslmode = 'verify-full' sslrootcert='/path with space/ca.pem' password='it\'s'",
            );
            assert_eq!(kv, r"host=h sslmode=require password='it\'s'");

            // URL-like values must not push a keyword string onto the URL
            // branch (which would pass it through unsanitized).
            let kv = sanitize_for_verify_full(
                "host=h password=https://secret sslmode=verify-full sslrootcert=/etc/ca.pem",
            );
            assert_eq!(kv, "host=h password=https://secret sslmode=require");
        }

        #[test]
        fn verifying_connector_builds_with_and_without_root_cert_file() {
            assert!(verifying_connector(None).is_ok());
            assert!(
                verifying_connector(Some("/nonexistent/ca.pem")).is_err(),
                "an unreadable sslrootcert must fail loudly"
            );
        }

        #[test]
        fn relaxed_connector_builds() {
            assert!(relaxed_connector().is_ok());
        }
    }
}

// ── Synchronous TLS-aware connections (migrations, wait checks) ───────────────

/// A synchronous diesel Pg connection for migrations and startup wait
/// checks, honoring the connection string's `sslmode` (issue #1585 review).
///
/// diesel's native [`diesel::PgConnection`] connects through libpq, and the
/// workspace bundles libpq **without** SSL support (`pq-sys`'s
/// `bundled_without_openssl`) — so with `sslmode=require`/`verify-full` the
/// async pool (rustls) connects fine while the sync migration path fails at
/// startup before the app ever serves. For those postures this wraps an
/// [`AsyncPgConnection`] established through the pool's own rustls connector
/// in diesel-async's [`AsyncConnectionWrapper`], which provides the full
/// sync diesel API including `MigrationHarness`. With TLS off the native
/// `PgConnection` path is kept byte-identical to the historical behavior.
///
/// [`AsyncConnectionWrapper`]:
///     diesel_async::async_connection_wrapper::AsyncConnectionWrapper
#[allow(clippy::large_enum_variant)] // short-lived, one per migration target
pub(crate) enum MigrationConnection {
    /// The historical libpq-backed connection (TLS posture `Off`).
    Native(diesel::PgConnection),
    /// rustls-backed sync wrapper (TLS posture `Require`/`VerifyFull`).
    Rustls {
        /// Runtime owning the connection's tokio driver task when none was
        /// ambient (plain sync contexts like the CLI); `None` when an
        /// ambient runtime drives it (`spawn_blocking` contexts). Callers
        /// must keep this alive as long as `conn` — dropping the runtime
        /// kills the driver and every query after that hangs or errors.
        runtime: Option<tokio::runtime::Runtime>,
        conn: diesel_async::async_connection_wrapper::AsyncConnectionWrapper<AsyncPgConnection>,
    },
}

/// Whether migrations/wait checks for `database_url` must go through the
/// rustls wrapper rather than the native libpq path. Split out of
/// [`establish_migration_connection`] so the path selection is unit-testable
/// without a server.
fn migration_connection_needs_rustls(database_url: &str) -> bool {
    !matches!(
        tls::TlsPosture::from_database_url(database_url),
        tls::TlsPosture::Off
    )
}

/// Establish a [`MigrationConnection`] for `database_url`.
///
/// **Never call from an async executor thread**: the rustls arm `block_on`s
/// connection setup. Sync contexts (the CLI) and `spawn_blocking` tasks (the
/// startup migration path) are both fine.
///
/// # Errors
///
/// Returns the underlying [`diesel::ConnectionError`] when the connection
/// cannot be established (including [`TlsPosture::Unsupported`] postures,
/// which fail with their documented guidance).
///
/// [`TlsPosture::Unsupported`]: tls::TlsPosture::Unsupported
pub(crate) fn establish_migration_connection(
    database_url: &str,
) -> Result<MigrationConnection, diesel::ConnectionError> {
    use diesel::Connection as _;
    if !migration_connection_needs_rustls(database_url) {
        return diesel::PgConnection::establish(database_url).map(MigrationConnection::Native);
    }
    let posture = tls::TlsPosture::from_database_url(database_url);
    // The driver task tokio::spawn()ed during establish must stay driven for
    // the connection's lifetime: reuse the ambient runtime when there is one
    // (spawn_blocking context), otherwise create one and keep it alive
    // alongside the connection.
    let (runtime, handle) = if let Ok(handle) = tokio::runtime::Handle::try_current() {
        (None, handle)
    } else {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(1)
            .enable_all()
            .build()
            .map_err(|e| {
                diesel::ConnectionError::BadConnection(format!(
                    "failed to build a tokio runtime for the TLS migration connection: {e}"
                ))
            })?;
        let handle = runtime.handle().clone();
        (Some(runtime), handle)
    };
    let inner = handle.block_on(tls::establish(database_url, posture))?;
    Ok(MigrationConnection::Rustls {
        runtime,
        conn: diesel_async::async_connection_wrapper::AsyncConnectionWrapper::from(inner),
    })
}

/// Establish a synchronous diesel [`diesel::SqliteConnection`] for the `SQLite`
/// startup-migration path (issue #1614, PR3).
///
/// The `SQLite` counterpart to [`establish_migration_connection`], and much
/// thinner: `SQLite` is a single-writer local database, so there is no TLS
/// negotiation and no advisory-lock connection wrapper — diesel's
/// `MigrationHarness` runs directly on a plain `SqliteConnection`. The URL is
/// normalized through the same [`normalize_sqlite_target`] the runtime pool
/// uses, so the migration connection and the pool open the same database file.
///
/// The connection sets `PRAGMA busy_timeout = 5000` right after opening,
/// mirroring the runtime pool's per-connection `custom_setup` (see
/// [`build_sqlite_pool`]): without it, a second concurrent migrator — or a write
/// lock briefly held by the runtime pool — makes this connection fail
/// *immediately* with `SQLITE_BUSY`, and `auto_migrate_sqlite` exits the process.
/// With the timeout, migration statements WAIT up to 5s for the lock to clear
/// instead of aborting; diesel migrations are idempotent, so a migrator that
/// waits and then finds migrations already applied is fine. Only `busy_timeout`
/// is set here — NOT `foreign_keys`/`journal_mode`, because `foreign_keys = ON`
/// can break table-recreating migrations.
///
/// # Errors
///
/// Returns the underlying [`diesel::ConnectionError`] when the database cannot
/// be opened, or [`diesel::ConnectionError::CouldntSetupConfiguration`] if the
/// `busy_timeout` pragma cannot be applied.
#[cfg(feature = "sqlite")]
pub(crate) fn establish_sqlite_migration_connection(
    database_url: &str,
) -> Result<diesel::SqliteConnection, diesel::ConnectionError> {
    use diesel::Connection as _;
    use diesel::connection::SimpleConnection as _;
    let mut conn = diesel::SqliteConnection::establish(&normalize_sqlite_target(database_url))?;
    conn.batch_execute("PRAGMA busy_timeout = 5000;")
        .map_err(diesel::ConnectionError::CouldntSetupConfiguration)?;
    Ok(conn)
}

#[cfg(test)]
mod migration_connection_tests {
    use super::migration_connection_needs_rustls;

    #[test]
    fn migration_path_selection_follows_the_tls_posture() {
        // NoTls URLs keep the historical native libpq path byte-identical.
        for url in [
            "postgres://u@h/db",
            "postgres://u@h/db?sslmode=disable",
            "postgres://u@h/db?sslmode=prefer",
            "host=db user=u",
        ] {
            assert!(
                !migration_connection_needs_rustls(url),
                "must stay on the native path: {url}"
            );
        }
        // TLS-requiring strings (URL and keyword forms) — and unsupported
        // postures, which must fail with the TLS module's guidance instead
        // of libpq's — go through the rustls wrapper.
        for url in [
            "postgres://u@h/db?sslmode=require",
            "postgres://u@h/db?sslmode=verify-full",
            "postgres://u@h/db?sslmode=verify-full&sslrootcert=/etc/ca.pem",
            "postgres://u@h/db?sslmode=verify-ca",
            "host=db user=u sslmode=require",
            "host=db sslmode = 'verify-full'",
        ] {
            assert!(
                migration_connection_needs_rustls(url),
                "must use the rustls wrapper: {url}"
            );
        }
    }
}