rustango 0.60.7

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
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
//! Audit log — one table that records every tracked write (insert,
//! update, delete, soft-delete) for models declared with
//! `#[rustango(audit(...))]`.
//!
//! Rows are keyed by `(entity_table, entity_pk)` instead of a per-model FK,
//! so one flat table serves any number of models with any PK shape. Query
//! `WHERE entity_table = 'post' AND entity_pk = '42'` for one row's
//! history, or drop the second clause for a per-table activity feed.
//!
//! The table is **per-tenant** in tenancy projects and per-database
//! otherwise.
//!
//! ## Source of change
//!
//! [`AuditSource`] travels in a tokio task-local, so handlers, seed
//! scripts and jobs can say who made a write without passing a context
//! object through every ORM call. The default is [`AuditSource::System`].
//! Override one call with `Model::save_on_with(conn, source)`.
//!
//! ## What gets logged
//!
//! Per-row writes record before/after values for every field named in the
//! model's `audit(track = "...")`. Bulk writes collect their entries and
//! insert them in multi-row statements sized to the bind limit.
//!
//! Rows the database removes or changes through an FK `ON DELETE`
//! action, or that PG's `truncate` (`CASCADE`) empties, are not audited
//! (planned for 0.61.0).

use serde_json::{Map, Value};

use crate::sql::sqlx;

// The PG-typed helpers below use PgRow / PgPool / Row directly. SQLite and
// MySQL go through the `*_pool` helpers further down, which dispatch per
// backend.
#[cfg(feature = "postgres")]
use crate::sql::sqlx::{postgres::PgRow, PgPool, Row};

/// Source of the change recorded in the audit log.
///
/// `System` is the default: jobs, seed scripts, framework internals.
/// `User { id }` is for request flows; admin handlers set it from the
/// session at request entry. `Custom` holds a project label such as
/// `"webhook:stripe"` or `"cli:backfill"`.
#[derive(Debug, Clone)]
pub enum AuditSource {
    System,
    User { id: String },
    Custom(String),
}

impl AuditSource {
    /// Stable string written to `audit_log.source`. The format is fixed,
    /// so a downstream index can join on it without parsing.
    #[must_use]
    pub fn as_token(&self) -> String {
        match self {
            Self::System => "system".to_owned(),
            Self::User { id } => format!("user:{id}"),
            Self::Custom(s) => s.clone(),
        }
    }
}

impl Default for AuditSource {
    fn default() -> Self {
        Self::System
    }
}

tokio::task_local! {
    /// Task-local audit source, set for the length of a request or a seed
    /// closure. Outside any scope, `current_source()` returns
    /// [`AuditSource::System`].
    pub static AUDIT_SOURCE: AuditSource;
}

tokio::task_local! {
    /// Tenant whose user the active source names; `None` when unbound.
    static SOURCE_TENANT: Option<String>;
    /// Tenant the current writes go to, where the framework knows it.
    static WRITE_TENANT: WriteScope;
    /// Pool the current audited write goes through, set by the `&Pool`
    /// write paths that emit from inside a transaction.
    static WRITE_POOL: crate::sql::PoolId;
}

/// Where writes go. With `pool` set (a `with_tenant` pass), a source
/// bound to `tenant` is kept only on writes through that pool (#2123).
#[derive(Clone)]
struct WriteScope {
    tenant: String,
    pool: Option<crate::sql::PoolId>,
}

/// How an emitter reached the database, for [`source_token`].
#[derive(Clone, Copy)]
enum Via<'a> {
    /// A caller's executor: its pool is unknown.
    Unknown,
    /// This pool.
    Pool(&'a crate::sql::Pool),
    /// The pool the enclosing `&Pool` write path set in `WRITE_POOL`.
    Scoped,
}

/// Read the active audit source. Returns [`AuditSource::System`] when no
/// [`with_source`] scope is active, and when the source belongs to one
/// tenant but the writes are not known to go to that tenant (#1229).
#[must_use]
pub fn current_source() -> AuditSource {
    let source = AUDIT_SOURCE
        .try_with(Clone::clone)
        .unwrap_or(AuditSource::System);
    match SOURCE_TENANT.try_with(Clone::clone).ok().flatten() {
        None => source,
        Some(bound)
            if WRITE_TENANT
                .try_with(|w| w.tenant == bound)
                .unwrap_or(false) =>
        {
            source
        }
        Some(_) => AuditSource::System,
    }
}

/// `source` as written through `via`. Inside a pool-bound scope the
/// tenant-bound source is kept only on that pool's writes; anywhere
/// else, a tenant B pool or the registry, it is `system` (#2123).
fn source_token(source: &AuditSource, via: Via<'_>) -> String {
    let Some(scope) = WRITE_TENANT.try_with(|w| w.pool.clone()).ok().flatten() else {
        return source.as_token();
    };
    let token = source.as_token();
    let bound = SOURCE_TENANT.try_with(|t| t.is_some()).unwrap_or(false)
        && AUDIT_SOURCE
            .try_with(|s| s.as_token() == token)
            .unwrap_or(false);
    let through_scope = match via {
        Via::Unknown => false,
        Via::Pool(pool) => scope.is(pool),
        Via::Scoped => WRITE_POOL.try_with(|p| p.same(&scope)).unwrap_or(false),
    };
    if bound && !through_scope {
        AuditSource::System.as_token()
    } else {
        token
    }
}

/// Run `fut` with its audited writes going through `pool`.
#[cfg_attr(
    not(any(feature = "admin", feature = "tenancy", feature = "forms")),
    allow(dead_code)
)]
async fn writing_via<F: std::future::Future>(pool: &crate::sql::Pool, fut: F) -> F::Output {
    WRITE_POOL.scope(crate::sql::PoolId::of(pool), fut).await
}

/// Run `fut` with `source` as the active audit source. Every audit entry
/// produced inside the future, single-row or bulk, records it.
///
/// Wrap a request handler or a seed closure with this. Outside such a
/// scope, writes record `AuditSource::System`.
pub async fn with_source<F, T>(source: AuditSource, fut: F) -> T
where
    F: std::future::Future<Output = T>,
{
    AUDIT_SOURCE
        .scope(source, SOURCE_TENANT.scope(None, fut))
        .await
}

/// [`with_source`] for a tenant request: `source` names a user of the
/// tenant with slug `tenant`. Work handed off from here (a job, a
/// `for_each_tenant` pass) records it only on that tenant's writes.
#[cfg(feature = "tenancy")]
pub async fn with_tenant_source<F, T>(source: AuditSource, tenant: String, fut: F) -> T
where
    F: std::future::Future<Output = T>,
{
    let scope = WriteScope {
        tenant: tenant.clone(),
        pool: None,
    };
    let writes = WRITE_TENANT.scope(scope, fut);
    AUDIT_SOURCE
        .scope(source, SOURCE_TENANT.scope(Some(tenant), writes))
        .await
}

/// Run `fut` with its writes going to `tenant`, as a per-tenant sweep
/// does; with `pool`, only writes through that pool count (#2123).
#[cfg(feature = "tenancy")]
pub(crate) async fn writing_to_tenant<F, T>(
    tenant: String,
    pool: Option<&crate::sql::Pool>,
    fut: F,
) -> T
where
    F: std::future::Future<Output = T>,
{
    let pool = pool.map(crate::sql::PoolId::of);
    WRITE_TENANT.scope(WriteScope { tenant, pool }, fut).await
}

/// An explicitly set source and the tenant it belongs to, for deferred
/// work. Where the writes go is not captured: the work decides that.
#[derive(Debug, Clone)]
pub(crate) struct CapturedSource {
    source: AuditSource,
    tenant: Option<String>,
}

impl CapturedSource {
    /// `None` outside any [`with_source`] scope.
    pub(crate) fn capture() -> Option<Self> {
        let source = AUDIT_SOURCE.try_with(Clone::clone).ok()?;
        let tenant = SOURCE_TENANT.try_with(Clone::clone).ok().flatten();
        Some(Self { source, tenant })
    }

    pub(crate) fn source(&self) -> &AuditSource {
        &self.source
    }

    /// The tenant the source belongs to, if bound.
    #[cfg(feature = "jobs-postgres")]
    pub(crate) fn tenant(&self) -> Option<&str> {
        self.tenant.as_deref()
    }

    /// Rebuild one read back from a stored job row.
    #[cfg(feature = "jobs-postgres")]
    pub(crate) fn from_parts(source: AuditSource, tenant: Option<String>) -> Self {
        Self { source, tenant }
    }

    pub(crate) async fn scope<F, T>(self, fut: F) -> T
    where
        F: std::future::Future<Output = T>,
    {
        AUDIT_SOURCE
            .scope(self.source, SOURCE_TENANT.scope(self.tenant, fut))
            .await
    }
}

/// One pending audit log entry. The generated write paths build these in
/// memory, then [`emit_one`] / [`emit_many`] writes them out with, or just
/// after, the data write.
#[derive(Debug, Clone)]
pub struct PendingEntry {
    pub entity_table: &'static str,
    pub entity_pk: String,
    pub operation: AuditOp,
    pub source: AuditSource,
    pub changes: Value,
}

impl PendingEntry {
    /// An `Update` entry holding the field diff, or `None` when nothing
    /// changed, so a no-op save writes no audit row (#1907).
    #[must_use]
    pub fn update_diff(
        entity_table: &'static str,
        entity_pk: String,
        before: &[(&str, Value)],
        after: &[(&str, Value)],
    ) -> Option<Self> {
        let changes = diff_changes(before, after);
        if changes.as_object().is_some_and(Map::is_empty) {
            return None;
        }
        Some(Self {
            entity_table,
            entity_pk,
            operation: AuditOp::Update,
            source: current_source(),
            changes,
        })
    }

    /// Replace `column`'s recorded value, only if the column is tracked.
    pub fn set_tracked(&mut self, column: &str, value: Value) {
        if let Some(slot) = self.changes.get_mut(column) {
            *slot = value;
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuditOp {
    Create,
    Update,
    Delete,
    SoftDelete,
    Restore,
    /// An operator action that is not a row write: impersonation start or
    /// end, an org config edit, a branding upload. Written by the operator
    /// console.
    Action,
}

impl AuditOp {
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Create => "create",
            Self::Update => "update",
            Self::Delete => "delete",
            Self::SoftDelete => "soft_delete",
            Self::Restore => "restore",
            Self::Action => "action",
        }
    }
}

/// Emit a single entry against a Postgres executor. Used by per-row write
/// paths on PG. For all backends, see [`emit_one_pool`].
///
/// # `occurred_at` is bound, not defaulted
///
/// Every emit path binds it — all three dialects, single-row and batch.
/// The column default is only a backstop for hand-written SQL.
///
/// SQLite is the reason. An older database still defaults to
/// `CURRENT_TIMESTAMP`, whose `YYYY-MM-DD HH:MM:SS` spelling sorts *below*
/// the canonical `…T…` one, and SQLite cannot `ALTER TABLE` a default
/// away. A defaulted write there would store a legacy-shaped value, which
/// inverts `ORDER BY occurred_at DESC` and makes `cleanup_keep_last_n`
/// delete the newest entries. PG and MySQL bind it too, for uniformity.
///
/// # Errors
/// Driver / SQL failures from the INSERT.
#[cfg(feature = "postgres")]
pub async fn emit_one<'c, E>(executor: E, entry: &PendingEntry) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::Postgres>,
{
    emit_one_pg(executor, entry, Via::Unknown).await
}

#[cfg(feature = "postgres")]
async fn emit_one_pg<'c, E>(
    executor: E,
    entry: &PendingEntry,
    via: Via<'_>,
) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::Postgres>,
{
    sqlx::query(
        r#"INSERT INTO "rustango_audit_log"
              ("entity_table", "entity_pk", "operation", "source", "changes", "occurred_at")
           VALUES ($1, $2, $3, $4, $5, $6)"#,
    )
    .bind(entry.entity_table)
    .bind(&entry.entity_pk)
    .bind(entry.operation.as_str())
    .bind(source_token(&entry.source, via))
    .bind(&entry.changes)
    .bind(chrono::Utc::now())
    .execute(executor)
    .await?;
    Ok(())
}

/// Emit a batch of entries on Postgres, one multi-row INSERT per
/// bind-limit-sized chunk. Several chunks run in one transaction (a
/// savepoint when `conn` is already inside one). For all backends, see
/// [`emit_many_pool`].
///
/// # Errors
/// As [`emit_one`].
#[cfg(feature = "postgres")]
pub async fn emit_many<'c, A>(conn: A, entries: &[PendingEntry]) -> Result<(), sqlx::Error>
where
    A: sqlx::Acquire<'c, Database = sqlx::Postgres>,
{
    if entries.is_empty() {
        return Ok(());
    }
    let per_insert = audit_rows_per_insert(&crate::sql::Postgres);
    if entries.len() <= per_insert {
        let mut c = conn.acquire().await?;
        return emit_chunk_pg(&mut c, entries).await;
    }
    let mut tx = conn.begin().await?;
    for chunk in entries.chunks(per_insert) {
        emit_chunk_pg(&mut tx, chunk).await?;
    }
    tx.commit().await
}

#[cfg(feature = "postgres")]
async fn emit_chunk_pg(
    conn: &mut sqlx::PgConnection,
    entries: &[PendingEntry],
) -> Result<(), sqlx::Error> {
    let stmt = audit_insert_stmt(&crate::sql::Postgres, entries, Via::Unknown)?;
    let mut q = sqlx::query(&stmt.sql);
    for value in stmt.params {
        q = crate::sql::bind_query(q, value);
    }
    q.execute(conn).await?;
    Ok(())
}

/// The audit log's table.
#[cfg(feature = "admin")]
pub(crate) const AUDIT_TABLE: &str = "rustango_audit_log";

/// Columns of an audit INSERT, in bind order.
const AUDIT_COLUMNS: [&str; 6] = [
    "entity_table",
    "entity_pk",
    "operation",
    "source",
    "changes",
    "occurred_at",
];

/// Most entries per audit INSERT. Each carries a row snapshot, so the
/// bind cap alone could pass MySQL's `max_allowed_packet` (64 MB).
const AUDIT_ROWS_PER_INSERT_MAX: usize = 100;

/// Entries per audit INSERT, so their binds fit the dialect's cap.
fn audit_rows_per_insert(dialect: &dyn crate::sql::Dialect) -> usize {
    (dialect.max_bind_params() / AUDIT_COLUMNS.len()).clamp(1, AUDIT_ROWS_PER_INSERT_MAX)
}

/// One multi-row audit INSERT, rendered by the bulk-insert writer.
fn audit_insert_stmt(
    dialect: &dyn crate::sql::Dialect,
    entries: &[PendingEntry],
    via: Via<'_>,
) -> Result<crate::sql::CompiledStatement, sqlx::Error> {
    use crate::core::{Model as _, SqlValue};
    let rows = entries
        .iter()
        .map(|e| {
            vec![
                SqlValue::String(e.entity_table.to_owned()),
                SqlValue::String(e.entity_pk.clone()),
                SqlValue::String(e.operation.as_str().to_owned()),
                SqlValue::String(source_token(&e.source, via)),
                SqlValue::Json(e.changes.clone()),
                // Stamped per row, as `emit_one` does.
                SqlValue::DateTime(chrono::Utc::now()),
            ]
        })
        .collect();
    let query = crate::core::BulkInsertQuery::new(AuditLog::SCHEMA, AUDIT_COLUMNS.to_vec(), rows);
    dialect
        .compile_bulk_insert(&query)
        .map_err(|e| sqlx::Error::Protocol(e.to_string()))
}

/// Build a `{ "field": { "before": <v>, "after": <v> } }` JSON object from
/// two slices of `(field_name, json_value)` pairs. Fields whose value did
/// not change are left out.
#[must_use]
pub fn diff_changes(before: &[(&str, Value)], after: &[(&str, Value)]) -> Value {
    let mut out = Map::new();
    for (name, after_val) in after {
        let before_val = before
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, v)| v.clone())
            .unwrap_or(Value::Null);
        if &before_val != after_val {
            let mut entry = Map::new();
            entry.insert("before".into(), before_val);
            entry.insert("after".into(), after_val.clone());
            out.insert((*name).into(), Value::Object(entry));
        }
    }
    Value::Object(out)
}

/// Build a `{ "field": <after-value> }` JSON object for create,
/// soft_delete and restore, where there is no useful "before" state.
#[must_use]
pub fn snapshot_changes(after: &[(&str, Value)]) -> Value {
    let mut out = Map::new();
    for (name, val) in after {
        out.insert((*name).to_string(), val.clone());
    }
    Value::Object(out)
}

/// Render the audit-log SELECT used by [`fetch_for_entity_pool`] through
/// the dialect emitters, so no SQL here is hand-written per backend.
fn audit_select_sql(dialect: &dyn crate::sql::Dialect) -> String {
    use std::fmt::Write as _;
    let t = dialect.quote_ident("rustango_audit_log");
    let id = dialect.quote_ident("id");
    let et = dialect.quote_ident("entity_table");
    let ek = dialect.quote_ident("entity_pk");
    let op = dialect.quote_ident("operation");
    let src = dialect.quote_ident("source");
    let ch = dialect.quote_ident("changes");
    let oa = dialect.quote_ident("occurred_at");
    let p1 = dialect.placeholder(1);
    let p2 = dialect.placeholder(2);
    let mut sql = String::new();
    let _ = write!(
        sql,
        "SELECT {id}, {et}, {ek}, {op}, {src}, {ch}, {oa} \
         FROM {t} \
         WHERE {et} = {p1} AND {ek} = {p2} \
         ORDER BY {oa} DESC, {id} DESC",
    );
    sql
}

/// Render the `DELETE … WHERE occurred_at < $1` used by
/// [`cleanup_older_than_pool`] through the dialect emitter.
///
/// SQLite gets a second leg that normalises the **stored** value before
/// comparing, so the sweep is correct whether a row holds the old
/// `CURRENT_TIMESTAMP` shape or the RFC3339 one written today. The
/// `migrate` sweep converts old rows, but this is a DELETE and runs
/// before migrations on an upgraded install, where a legacy value sorts
/// below every canonical cutoff and history would be destroyed.
///
/// The shape matters, and two simpler ones are wrong:
///
/// - The leading range must stay bare so the `occurred_at` index is used.
///   Wrapping the column in `strftime` forces a full scan, O(table)
///   instead of O(rows deleted).
/// - Comparing the cutoff in both spellings over-deletes: `' '` sorts
///   below `'T'`, so every same-date legacy row looks older than a
///   canonical cutoff, whatever its clock time.
///
/// So the second leg filters the first rather than replacing it, and it
/// normalises instead of matching known spellings. A width-keyed `LIKE`
/// missed `2026-09-20 08:00:00.123456` — the dump spelling, and what
/// sqlx writes for a bound `NaiveDateTime` — and deleted rows hours
/// after the cutoff.
fn audit_cleanup_older_than_sql(dialect: &dyn crate::sql::Dialect) -> String {
    let t = dialect.quote_ident("rustango_audit_log");
    let oa = dialect.quote_ident("occurred_at");
    let p1 = dialect.placeholder(1);
    if dialect.name() == "sqlite" {
        let fmt = crate::sql::SQLITE_DATETIME_FORMAT;
        let p2 = dialect.placeholder(2);
        return format!(
            "DELETE FROM {t} WHERE {oa} < {p1} \
             AND strftime('{fmt}', {oa}) < {p2}"
        );
    }
    format!("DELETE FROM {t} WHERE {oa} < {p1}")
}

/// Test hook for [`audit_cleanup_older_than_sql`]. The index-plan guard
/// must check the renderer's own SQL: a guard that retypes the statement
/// cannot notice the statement changing.
#[doc(hidden)]
#[must_use]
pub fn __test_cleanup_older_than_sql(dialect: &dyn crate::sql::Dialect) -> String {
    audit_cleanup_older_than_sql(dialect)
}

/// Render the per-row retention DELETE used by
/// [`cleanup_keep_last_n_pool`]. `ROW_NUMBER() OVER (PARTITION BY)` works
/// on PG, MySQL 8+ and SQLite 3.25+, so only quoting and placeholders
/// differ.
fn audit_cleanup_keep_last_n_sql(dialect: &dyn crate::sql::Dialect) -> String {
    let t = dialect.quote_ident("rustango_audit_log");
    let id = dialect.quote_ident("id");
    let et = dialect.quote_ident("entity_table");
    let ek = dialect.quote_ident("entity_pk");
    let oa = dialect.quote_ident("occurred_at");
    let p1 = dialect.placeholder(1);
    format!(
        "DELETE FROM {t} WHERE {id} IN ( \
            SELECT {id} FROM ( \
              SELECT {id}, \
                     ROW_NUMBER() OVER ( \
                         PARTITION BY {et}, {ek} \
                         ORDER BY {oa} DESC, {id} DESC \
                     ) AS _rn \
              FROM {t} \
            ) ranked \
            WHERE _rn > {p1} \
         )"
    )
}

/// Read every audit entry for one `(entity_table, entity_pk)` pair,
/// newest first. Used by the admin's per-row audit trail panel.
///
/// Postgres-typed, kept for older call sites; it delegates to
/// [`fetch_for_entity_pool`], which is the one to use on any backend.
///
/// # Errors
/// Driver / SQL failures.
#[cfg(feature = "postgres")]
pub async fn fetch_for_entity(
    pool: &PgPool,
    entity_table: &str,
    entity_pk: &str,
) -> Result<Vec<AuditEntry>, sqlx::Error> {
    fetch_for_entity_pool(
        &crate::sql::Pool::from(pool.clone()),
        entity_table,
        entity_pk,
    )
    .await
}

/// Schema-registration model for `rustango_audit_log`, so
/// `makemigrations` owns the audit-log schema. Reads go through
/// [`AuditEntry`], which has the per-dialect `changes`-column decoders.
/// The two indexes here are the `(entity_table, entity_pk)` composite and
/// one on `occurred_at`.
#[derive(crate::Model, Debug, Clone)]
#[rustango(
    table = "rustango_audit_log",
    index_together = "entity_table, entity_pk"
)]
#[allow(dead_code)]
pub struct AuditLog {
    #[rustango(primary_key)]
    pub id: crate::sql::Auto<i64>,
    // `entity_table` + `entity_pk` are the `index_together` key, so they
    // MUST have a max_length: MySQL cannot index an unbounded TEXT column
    // without a prefix length (error 1170).
    #[rustango(max_length = 255)]
    pub entity_table: String,
    #[rustango(max_length = 255)]
    pub entity_pk: String,
    #[rustango(max_length = 32)]
    pub operation: String,
    #[rustango(max_length = 255)]
    pub source: String,
    pub changes: serde_json::Value,
    #[rustango(index, default = "now()")]
    pub occurred_at: chrono::DateTime<chrono::Utc>,
}

/// Decoded audit-log row.
#[derive(Debug, Clone)]
pub struct AuditEntry {
    pub id: i64,
    pub entity_table: String,
    pub entity_pk: String,
    pub operation: String,
    pub source: String,
    pub changes: Value,
    pub occurred_at: chrono::DateTime<chrono::Utc>,
}

#[cfg(feature = "postgres")]
impl AuditEntry {
    fn from_row(row: &PgRow) -> Result<Self, sqlx::Error> {
        Ok(Self {
            id: row.try_get("id")?,
            entity_table: row.try_get("entity_table")?,
            entity_pk: row.try_get("entity_pk")?,
            operation: row.try_get("operation")?,
            source: row.try_get("source")?,
            changes: row.try_get("changes")?,
            occurred_at: row.try_get("occurred_at")?,
        })
    }
}

/// Per-backend row decoder. The `changes` column is JSONB on PG (decodes
/// straight to `Value`), JSON on MySQL (via `sqlx::types::Json<Value>`)
/// and TEXT on SQLite (parsed with `serde_json::from_str`).
#[cfg(feature = "mysql")]
impl AuditEntry {
    fn from_my_row(row: &sqlx::mysql::MySqlRow) -> Result<Self, sqlx::Error> {
        use sqlx::Row as _;
        let changes: sqlx::types::Json<Value> = row.try_get("changes")?;
        Ok(Self {
            id: row.try_get("id")?,
            entity_table: row.try_get("entity_table")?,
            entity_pk: row.try_get("entity_pk")?,
            operation: row.try_get("operation")?,
            source: row.try_get("source")?,
            changes: changes.0,
            occurred_at: row.try_get("occurred_at")?,
        })
    }
}

#[cfg(feature = "sqlite")]
impl AuditEntry {
    fn from_sq_row(row: &sqlx::sqlite::SqliteRow) -> Result<Self, sqlx::Error> {
        use sqlx::Row as _;
        let changes_text: String = row.try_get("changes")?;
        let changes: Value = serde_json::from_str(&changes_text).map_err(|e| {
            sqlx::Error::Decode(Box::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("audit `changes` is not valid JSON: {e}"),
            )))
        })?;
        Ok(Self {
            id: row.try_get("id")?,
            entity_table: row.try_get("entity_table")?,
            entity_pk: row.try_get("entity_pk")?,
            operation: row.try_get("operation")?,
            source: row.try_get("source")?,
            changes,
            occurred_at: row.try_get("occurred_at")?,
        })
    }
}

/// Delete audit entries older than `cutoff_days` from `pool`'s audit
/// table. Returns the number of rows removed.
///
/// Nothing schedules this for you — wire it into a cron job or a
/// maintenance task. Each tenant's audit table is its own retention
/// boundary, so this only touches the tenant `pool` points at.
///
/// `cutoff_days = 0` clears the whole table. A negative value clamps to 0.
///
/// Postgres-typed; [`cleanup_older_than_pool`] works on any backend.
///
/// The cutoff comes from Rust, not `NOW()`. Since [`emit_one`] binds
/// `occurred_at`, a database-side `NOW()` would compare two clocks: if
/// the app host runs ahead, its rows sit in the database's future and a
/// zero-day sweep deletes none of them.
///
/// # Errors
/// Driver / SQL failures from the DELETE.
#[cfg(feature = "postgres")]
pub async fn cleanup_older_than(pool: &PgPool, cutoff_days: i64) -> Result<u64, sqlx::Error> {
    let cutoff = cutoff_days.max(0);
    let cutoff_ts = chrono::Utc::now() - chrono::Duration::days(cutoff);
    let result = sqlx::query(r#"DELETE FROM "rustango_audit_log" WHERE "occurred_at" < $1"#)
        .bind(cutoff_ts)
        .execute(pool)
        .await?;
    Ok(result.rows_affected())
}

/// Per-row retention: keep the `keep` newest audit entries for each
/// `(entity_table, entity_pk)` pair and delete the rest. Use it when you
/// must keep the edit chain of every row but cap how far the table grows.
///
/// One window-function DELETE does the work: each entry gets a
/// `ROW_NUMBER()` ordered by `occurred_at DESC, id DESC`, and rows ranked
/// above `keep` are dropped. One round-trip, however many pairs the table
/// holds.
///
/// `keep = 0` clears the whole table; negative values clamp to 0. Returns
/// the number of rows removed.
///
/// Postgres-typed; [`cleanup_keep_last_n_pool`] works on any backend.
///
/// # Errors
/// Driver / SQL failures from the DELETE.
#[cfg(feature = "postgres")]
pub async fn cleanup_keep_last_n(pool: &PgPool, keep: i64) -> Result<u64, sqlx::Error> {
    let keep = keep.max(0);
    let result = sqlx::query(
        r#"DELETE FROM "rustango_audit_log" WHERE "id" IN (
              SELECT "id" FROM (
                SELECT "id",
                       ROW_NUMBER() OVER (
                           PARTITION BY "entity_table", "entity_pk"
                           ORDER BY "occurred_at" DESC, "id" DESC
                       ) AS _rn
                FROM "rustango_audit_log"
              ) ranked
              WHERE _rn > $1
           )"#,
    )
    .bind(keep)
    .execute(pool)
    .await?;
    Ok(result.rows_affected())
}

/// Make sure the table exists in `pool`'s database or schema. Does nothing
/// when it is already there. Handy in tests and ad-hoc setup.
///
/// Postgres-typed; [`ensure_table_pool`] works on any backend.
///
/// # Errors
/// Driver / SQL failures from the emitted DDL.
#[cfg(feature = "postgres")]
pub async fn ensure_table(pool: &PgPool) -> Result<(), sqlx::Error> {
    ensure_table_pool(&crate::sql::Pool::Postgres(pool.clone())).await
}

// ============================================================ all-backend audit

/// Create the audit-log table on any backend, sending the per-dialect DDL
/// through the right driver.
///
/// MySQL has no `CREATE INDEX IF NOT EXISTS`, so duplicate-index errors
/// are ignored and the call stays idempotent.
///
/// # Errors
/// Driver / SQL failures, other than the ignored duplicate-index ones.
pub async fn ensure_table_pool(pool: &crate::sql::Pool) -> Result<(), sqlx::Error> {
    // Render the table and its indexes from `AuditLog::SCHEMA` through the
    // migration path, so this cannot drift from the model. The table is
    // shared per database, so this stays as a defensive helper next to the
    // system migrations.
    use crate::core::Model as _;
    let snapshot = crate::migrate::SchemaSnapshot::from_models(&[AuditLog::SCHEMA]);
    crate::migrate::apply_idempotent(pool, &snapshot).await?;
    Ok(())
}

/// MySQL counterpart of [`emit_one`] — `?` placeholders and backtick
/// quoting. Used for audited writes over a MySQL transaction.
///
/// # Errors
/// Driver / SQL failures from the INSERT.
#[cfg(feature = "mysql")]
pub async fn emit_one_my<'c, E>(executor: E, entry: &PendingEntry) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::MySql>,
{
    emit_one_my_via(executor, entry, Via::Unknown).await
}

#[cfg(feature = "mysql")]
async fn emit_one_my_via<'c, E>(
    executor: E,
    entry: &PendingEntry,
    via: Via<'_>,
) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::MySql>,
{
    // `occurred_at` is bound, not defaulted — see `emit_one`.
    sqlx::query(
        r#"INSERT INTO `rustango_audit_log`
              (`entity_table`, `entity_pk`, `operation`, `source`, `changes`, `occurred_at`)
           VALUES (?, ?, ?, ?, ?, ?)"#,
    )
    .bind(entry.entity_table)
    .bind(&entry.entity_pk)
    .bind(entry.operation.as_str())
    .bind(source_token(&entry.source, via))
    .bind(sqlx::types::Json(&entry.changes))
    .bind(chrono::Utc::now())
    .execute(executor)
    .await?;
    Ok(())
}

/// SQLite counterpart of [`emit_one`] — double-quoted identifiers and `?`
/// placeholders. The `changes` JSON goes into a TEXT column.
///
/// # Errors
/// Driver / SQL failures from the INSERT.
#[cfg(feature = "sqlite")]
pub async fn emit_one_sqlite<'c, E>(executor: E, entry: &PendingEntry) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::Sqlite>,
{
    emit_one_sqlite_via(executor, entry, Via::Unknown).await
}

#[cfg(feature = "sqlite")]
async fn emit_one_sqlite_via<'c, E>(
    executor: E,
    entry: &PendingEntry,
    via: Via<'_>,
) -> Result<(), sqlx::Error>
where
    E: sqlx::Executor<'c, Database = sqlx::Sqlite>,
{
    // `occurred_at` is bound, not defaulted — see `emit_one`. This is the
    // dialect the rule exists for: an upgraded database's default cannot
    // be replaced, so a defaulted write here would store a legacy-shaped
    // value among canonical ones, forever.
    sqlx::query(
        r#"INSERT INTO "rustango_audit_log"
              ("entity_table", "entity_pk", "operation", "source", "changes", "occurred_at")
           VALUES (?, ?, ?, ?, ?, ?)"#,
    )
    .bind(entry.entity_table)
    .bind(&entry.entity_pk)
    .bind(entry.operation.as_str())
    .bind(source_token(&entry.source, via))
    .bind(sqlx::types::Json(&entry.changes))
    .bind(crate::sql::encode_datetime(chrono::Utc::now()))
    .execute(executor)
    .await?;
    Ok(())
}

/// Per-row audit emit over [`crate::sql::Pool`], dispatching to the right
/// backend helper. **It does not share a transaction with the data
/// write.** For that, open a transaction yourself and call the
/// per-backend `emit_one*` on it.
///
/// # Errors
/// As [`emit_one`].
pub async fn emit_one_pool(
    pool: &crate::sql::Pool,
    entry: &PendingEntry,
) -> Result<(), sqlx::Error> {
    match pool {
        #[cfg(feature = "postgres")]
        crate::sql::Pool::Postgres(pg) => emit_one_pg(pg, entry, Via::Pool(pool)).await,
        #[cfg(feature = "mysql")]
        crate::sql::Pool::Mysql(my) => emit_one_my_via(my, entry, Via::Pool(pool)).await,
        #[cfg(feature = "sqlite")]
        crate::sql::Pool::Sqlite(sq) => emit_one_sqlite_via(sq, entry, Via::Pool(pool)).await,
    }
}

/// Codename a non-superuser needs to read the admin audit feed. Rows
/// are still limited to tables they hold `{table}.view` on. Not a CRUD
/// action, so no table's `{table}.view` grants it (#1979).
pub const VIEW_CODENAME: &str = "rustango_audit_log.view_feed";

/// Codename a non-superuser needs to run the admin audit cleanup.
/// Cleanup spans every table, whatever `{table}.view` the user holds.
pub const DELETE_CODENAME: &str = "rustango_audit_log.clean_feed";

/// A permission on the admin audit feed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuditPerm {
    /// [`VIEW_CODENAME`].
    View,
    /// [`DELETE_CODENAME`].
    Delete,
}

impl AuditPerm {
    /// The codename that grants it.
    #[must_use]
    pub fn codename(self) -> &'static str {
        match self {
            Self::View => VIEW_CODENAME,
            Self::Delete => DELETE_CODENAME,
        }
    }

    /// The pre-#1979 name, a model table `audit`'s own CRUD codename.
    fn legacy_codename(self) -> &'static str {
        match self {
            Self::View => "audit.view",
            Self::Delete => "audit.delete",
        }
    }

    /// `true` when `perms` grants it. The legacy name counts only while
    /// no model uses table `audit`.
    #[must_use]
    pub fn granted_by(self, perms: &std::collections::HashSet<String>) -> bool {
        perms.contains(self.codename())
            || (perms.contains(self.legacy_codename())
                && crate::core::ModelEntry::for_table("audit").is_none())
    }
}

/// Filter for the admin's audit-log activity feed. Every field is
/// optional; `None` means the column is not constrained. [`list`] and
/// [`count`] turn this into a WHERE clause.
#[derive(Debug, Clone, Default)]
pub struct AuditFilter {
    pub entity_table: Option<String>,
    pub entity_pk: Option<String>,
    pub operation: Option<String>,
    pub source: Option<String>,
}

impl AuditFilter {
    /// Collect the active filters as `(column, value)` pairs. The order is
    /// stable because it drives placeholder numbering.
    fn active_pairs(&self) -> Vec<(&'static str, &str)> {
        let mut out = Vec::with_capacity(4);
        if let Some(v) = self.entity_table.as_deref() {
            if !v.is_empty() {
                out.push(("entity_table", v));
            }
        }
        if let Some(v) = self.entity_pk.as_deref() {
            if !v.is_empty() {
                out.push(("entity_pk", v));
            }
        }
        if let Some(v) = self.operation.as_deref() {
            if !v.is_empty() {
                out.push(("operation", v));
            }
        }
        if let Some(v) = self.source.as_deref() {
            if !v.is_empty() {
                out.push(("source", v));
            }
        }
        out
    }
}

/// One page of audit entries matching `filter`, newest first. Works on
/// any backend.
///
/// # Errors
/// Driver / SQL failures from the SELECT, or a JSON decode failure on
/// SQLite when the `changes` TEXT column is not valid JSON.
pub async fn list(
    pool: &crate::sql::Pool,
    filter: &AuditFilter,
    page_size: i64,
    offset: i64,
) -> Result<Vec<AuditEntry>, sqlx::Error> {
    list_in(pool, filter, None, page_size, offset).await
}

/// [`list`] limited to rows whose `entity_table` is in `tables`
/// (`None` = every table). The admin feed's per-user scope.
pub(crate) async fn list_in(
    pool: &crate::sql::Pool,
    filter: &AuditFilter,
    tables: Option<&[String]>,
    page_size: i64,
    offset: i64,
) -> Result<Vec<AuditEntry>, sqlx::Error> {
    if tables.is_some_and(<[String]>::is_empty) {
        return Ok(Vec::new());
    }
    let pairs = filter.active_pairs();
    let sql = audit_list_sql(pool.dialect(), &pairs, tables);
    let binds: Vec<&str> = pairs
        .iter()
        .map(|(_, v)| *v)
        .chain(tables.unwrap_or_default().iter().map(String::as_str))
        .collect();
    // bind+fetch cannot be shared: `sqlx::Executor` is bound per Database.
    // Only the decode is shared, via the `AuditEntry::from_*_row` helpers.
    match pool {
        #[cfg(feature = "postgres")]
        crate::sql::Pool::Postgres(pg) => {
            let mut q = sqlx::query(&sql);
            for v in &binds {
                q = q.bind(*v);
            }
            let rows = q.bind(page_size).bind(offset).fetch_all(pg).await?;
            rows.iter().map(AuditEntry::from_row).collect()
        }
        #[cfg(feature = "mysql")]
        crate::sql::Pool::Mysql(my) => {
            let mut q = sqlx::query(&sql);
            for v in &binds {
                q = q.bind(*v);
            }
            let rows = q.bind(page_size).bind(offset).fetch_all(my).await?;
            rows.iter().map(AuditEntry::from_my_row).collect()
        }
        #[cfg(feature = "sqlite")]
        crate::sql::Pool::Sqlite(sq) => {
            let mut q = sqlx::query(&sql);
            for v in &binds {
                q = q.bind(*v);
            }
            let rows = q.bind(page_size).bind(offset).fetch_all(sq).await?;
            rows.iter().map(AuditEntry::from_sq_row).collect()
        }
    }
}

/// Total row count for the audit-log pager, using the same
/// [`AuditFilter`] as [`list`].
///
/// # Errors
/// Driver / SQL failures from the SELECT COUNT(*).
pub async fn count(pool: &crate::sql::Pool, filter: &AuditFilter) -> Result<i64, sqlx::Error> {
    count_in(pool, filter, None).await
}

/// [`count`] limited to `tables`, as [`list_in`].
pub(crate) async fn count_in(
    pool: &crate::sql::Pool,
    filter: &AuditFilter,
    tables: Option<&[String]>,
) -> Result<i64, sqlx::Error> {
    use crate::core::SqlValue;
    if tables.is_some_and(<[String]>::is_empty) {
        return Ok(0);
    }
    let pairs = filter.active_pairs();
    let t = pool.dialect().quote_ident("rustango_audit_log");
    let sql = format!(
        "SELECT COUNT(*) FROM {t}{}",
        audit_where_sql(pool.dialect(), &pairs, tables)
    );
    let binds: Vec<SqlValue> = pairs
        .iter()
        .map(|(_, v)| SqlValue::String((*v).to_owned()))
        .chain(
            tables
                .unwrap_or_default()
                .iter()
                .map(|t| SqlValue::String(t.clone())),
        )
        .collect();
    // `raw_query_pool::<(i64,)>` decodes the single COUNT column the same
    // way on every backend, so no per-backend arm is needed.
    let rows: Vec<(i64,)> = crate::sql::raw_query_pool(&sql, binds, pool)
        .await
        .map_err(|e| match e {
            crate::sql::ExecError::Driver(err) => err,
            other => sqlx::Error::Protocol(format!("{other}")),
        })?;
    // COUNT(*) always returns exactly one row.
    Ok(rows.into_iter().next().map_or(0, |t| t.0))
}

/// `(value, count)` facets for the audit-log side panel, ordered by count
/// descending then value ascending. `column` must be `entity_table`,
/// `operation` or `source`.
///
/// # Errors
/// Driver / SQL failures from the SELECT, or `Error::ColumnNotFound` when
/// `column` is not one of the three allowed names.
pub async fn facet_counts(
    pool: &crate::sql::Pool,
    column: &str,
) -> Result<Vec<(String, i64)>, sqlx::Error> {
    facet_counts_in(pool, column, None).await
}

/// [`facet_counts`] limited to `tables`, as [`list_in`].
pub(crate) async fn facet_counts_in(
    pool: &crate::sql::Pool,
    column: &str,
    tables: Option<&[String]>,
) -> Result<Vec<(String, i64)>, sqlx::Error> {
    use crate::core::SqlValue;
    // `column` is interpolated into the SQL, so it must be allowlisted.
    if !matches!(column, "entity_table" | "operation" | "source") {
        return Err(sqlx::Error::ColumnNotFound(column.to_owned()));
    }
    if tables.is_some_and(<[String]>::is_empty) {
        return Ok(Vec::new());
    }
    let sql = audit_facet_sql(pool.dialect(), column, tables);
    let binds: Vec<SqlValue> = tables
        .unwrap_or_default()
        .iter()
        .map(|t| SqlValue::String(t.clone()))
        .collect();
    // The `(String, i64)` tuple decodes positionally, so it matches the
    // `facet_value, facet_count` column order in `audit_facet_sql`.
    crate::sql::raw_query_pool::<(String, i64)>(&sql, binds, pool)
        .await
        .map_err(|e| match e {
            crate::sql::ExecError::Driver(err) => err,
            other => sqlx::Error::Protocol(format!("{other}")),
        })
}

/// `WHERE col = ? AND … AND entity_table IN (?, …)`. Binds go `pairs`
/// values first, then `tables`, matching the text order on every dialect.
fn audit_where_sql(
    dialect: &dyn crate::sql::Dialect,
    pairs: &[(&'static str, &str)],
    tables: Option<&[String]>,
) -> String {
    use std::fmt::Write as _;
    let mut sql = String::new();
    let mut idx = 1usize;
    for (col, _) in pairs {
        let prefix = if idx == 1 { " WHERE " } else { " AND " };
        let _ = write!(
            sql,
            "{prefix}{} = {}",
            dialect.quote_ident(col),
            dialect.placeholder(idx)
        );
        idx += 1;
    }
    if let Some(tables) = tables {
        let prefix = if idx == 1 { " WHERE " } else { " AND " };
        let phs: Vec<String> = (idx..idx + tables.len())
            .map(|i| dialect.placeholder(i))
            .collect();
        let _ = write!(
            sql,
            "{prefix}{} IN ({})",
            dialect.quote_ident("entity_table"),
            phs.join(", ")
        );
    }
    sql
}

/// Render the paginated activity-feed SELECT. `pairs` gives the active
/// filter columns in a stable order, so placeholder numbering is fixed.
fn audit_list_sql(
    dialect: &dyn crate::sql::Dialect,
    pairs: &[(&'static str, &str)],
    tables: Option<&[String]>,
) -> String {
    let t = dialect.quote_ident("rustango_audit_log");
    let id = dialect.quote_ident("id");
    let et = dialect.quote_ident("entity_table");
    let ek = dialect.quote_ident("entity_pk");
    let op = dialect.quote_ident("operation");
    let src = dialect.quote_ident("source");
    let ch = dialect.quote_ident("changes");
    let oa = dialect.quote_ident("occurred_at");
    let bind_idx = 1 + pairs.len() + tables.map_or(0, <[String]>::len);
    let p_limit = dialect.placeholder(bind_idx);
    let p_offset = dialect.placeholder(bind_idx + 1);
    format!(
        "SELECT {id}, {et}, {ek}, {op}, {src}, {ch}, {oa} FROM {t}{} \
         ORDER BY {oa} DESC, {id} DESC LIMIT {p_limit} OFFSET {p_offset}",
        audit_where_sql(dialect, pairs, tables)
    )
}

/// Render the facet group-by. `column` must already be allowlisted by
/// [`facet_counts`].
fn audit_facet_sql(
    dialect: &dyn crate::sql::Dialect,
    column: &str,
    tables: Option<&[String]>,
) -> String {
    let t = dialect.quote_ident("rustango_audit_log");
    let col = dialect.quote_ident(column);
    format!(
        "SELECT {col} AS facet_value, COUNT(*) AS facet_count \
         FROM {t}{} GROUP BY {col} ORDER BY facet_count DESC, {col}",
        audit_where_sql(dialect, &[], tables)
    )
}

/// Batched audit emit on any backend: chunked multi-row INSERTs, all in
/// one transaction.
///
/// Empty input returns at once.
///
/// # Errors
/// Driver / SQL failures from the INSERT(s) or the transaction.
pub async fn emit_many_pool(
    pool: &crate::sql::Pool,
    entries: &[PendingEntry],
) -> Result<(), sqlx::Error> {
    if entries.is_empty() {
        return Ok(());
    }
    let to_sqlx = |e: crate::sql::ExecError| match e {
        crate::sql::ExecError::Driver(err) => err,
        other => sqlx::Error::Protocol(format!("{other}")),
    };
    let mut tx = crate::sql::transaction_pool(pool).await.map_err(to_sqlx)?;
    emit_many_tx(&mut tx, Via::Pool(pool), entries)
        .await
        .map_err(to_sqlx)?;
    tx.commit().await
}

/// All-backend counterpart of [`fetch_for_entity`]. The `changes` column
/// is JSON on PG and MySQL and TEXT on SQLite; both decode into
/// `serde_json::Value`, so the audit panel renders the same everywhere.
///
/// # Errors
/// Driver / SQL failures from the SELECT, or a JSON decode failure, for
/// example SQLite TEXT that is not valid JSON.
pub async fn fetch_for_entity_pool(
    pool: &crate::sql::Pool,
    entity_table: &str,
    entity_pk: &str,
) -> Result<Vec<AuditEntry>, sqlx::Error> {
    // One SELECT template for every backend; only quoting and
    // placeholders differ. Decode uses the per-backend `from_*_row`.
    let sql = audit_select_sql(pool.dialect());
    match pool {
        #[cfg(feature = "postgres")]
        crate::sql::Pool::Postgres(pg) => {
            let rows = sqlx::query(&sql)
                .bind(entity_table)
                .bind(entity_pk)
                .fetch_all(pg)
                .await?;
            rows.iter().map(AuditEntry::from_row).collect()
        }
        #[cfg(feature = "mysql")]
        crate::sql::Pool::Mysql(my) => {
            let rows = sqlx::query(&sql)
                .bind(entity_table)
                .bind(entity_pk)
                .fetch_all(my)
                .await?;
            rows.iter().map(AuditEntry::from_my_row).collect()
        }
        #[cfg(feature = "sqlite")]
        crate::sql::Pool::Sqlite(sq) => {
            let rows = sqlx::query(&sql)
                .bind(entity_table)
                .bind(entity_pk)
                .fetch_all(sq)
                .await?;
            rows.iter().map(AuditEntry::from_sq_row).collect()
        }
    }
}

/// All-backend counterpart of [`cleanup_older_than`]. The cutoff is
/// computed in Rust and bound, so the SQL needs no
/// `NOW() - INTERVAL '… day'`.
///
/// `cutoff_days = 0` clears the whole table. A negative value clamps to 0.
///
/// **Under tenancy, pass a tenant-scoped pool.** The audit table is
/// per-tenant, so this trims only the tenant `pool` points at. Pass a
/// registry pool in schema mode and it trims `public` alone, reports
/// success, and every tenant's history keeps growing. Fan out with
/// [`crate::tenancy::for_each_tenant`].
///
/// # Errors
/// Driver / SQL failures from the DELETE.
pub async fn cleanup_older_than_pool(
    pool: &crate::sql::Pool,
    cutoff_days: i64,
) -> Result<u64, sqlx::Error> {
    use crate::core::SqlValue;
    let cutoff = cutoff_days.max(0);
    let cutoff_ts = chrono::Utc::now() - chrono::Duration::days(cutoff);
    let sql = audit_cleanup_older_than_sql(pool.dialect());
    // SQLite binds the cutoff twice: once for the indexed range, once for
    // the second leg. Both use the canonical spelling — the second leg
    // normalises the stored column, not the cutoff, so no legacy-shaped
    // value is ever bound here.
    let mut binds = vec![SqlValue::DateTime(cutoff_ts)];
    if pool.dialect().name() == "sqlite" {
        binds.push(SqlValue::DateTime(cutoff_ts));
    }
    crate::sql::raw_execute_pool(pool, &sql, binds)
        .await
        .map_err(|e| match e {
            crate::sql::ExecError::Driver(err) => err,
            other => sqlx::Error::Protocol(format!("{other}")),
        })
}

/// All-backend counterpart of [`cleanup_keep_last_n`].
/// `ROW_NUMBER() OVER (PARTITION BY …)` works on PG, MySQL 8+ and
/// SQLite 3.25+, so only identifier quoting differs.
///
/// `keep = 0` clears the whole table; negative values clamp to 0.
///
/// # Errors
/// Driver / SQL failures from the DELETE. On MySQL 5.7 or SQLite 3.24 and
/// older you get an "unsupported window function" error; there, write
/// your own retention DELETE instead.
pub async fn cleanup_keep_last_n_pool(
    pool: &crate::sql::Pool,
    keep: i64,
) -> Result<u64, sqlx::Error> {
    use crate::core::SqlValue;
    let keep = keep.max(0);
    let sql = audit_cleanup_keep_last_n_sql(pool.dialect());
    crate::sql::raw_execute_pool(pool, &sql, vec![SqlValue::I64(keep)])
        .await
        .map_err(|e| match e {
            crate::sql::ExecError::Driver(err) => err,
            other => sqlx::Error::Protocol(format!("{other}")),
        })
}

/// Run a `DeleteQuery` and emit its audit entry in one transaction, so
/// the row and its audit record commit together. No row is written when
/// nothing was deleted. Used by the generated `Model::delete_pool`.
///
/// # Errors
/// Any [`crate::sql::ExecError`] from compile, bind or execute, plus
/// `sqlx::Error` from the audit emit (wrapped as `ExecError::Driver`).
pub async fn delete_one_with_audit(
    pool: &crate::sql::Pool,
    query: &crate::core::DeleteQuery,
    entry: &PendingEntry,
) -> Result<u64, crate::sql::ExecError> {
    let stmt = pool.dialect().compile_delete(query)?;
    let mut tx = crate::sql::transaction_pool(pool).await?;
    let affected = crate::sql::raw_execute_tx(&mut tx, &stmt.sql, stmt.params).await?;
    if affected > 0 {
        emit_one_tx(&mut tx, Via::Pool(pool), entry).await?;
    }
    tx.commit().await?;
    Ok(affected)
}

/// Emit an audit entry inside an open `PoolTx`, so callers stay on the
/// `PoolTx` API instead of unwrapping the backend variant themselves.
async fn emit_one_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    via: Via<'_>,
    entry: &PendingEntry,
) -> Result<(), sqlx::Error> {
    match tx {
        #[cfg(feature = "postgres")]
        crate::sql::PoolTx::Postgres(t) => emit_one_pg(&mut **t, entry, via).await,
        #[cfg(feature = "mysql")]
        crate::sql::PoolTx::Mysql(t) => emit_one_my_via(&mut **t, entry, via).await,
        #[cfg(feature = "sqlite")]
        crate::sql::PoolTx::Sqlite(t) => emit_one_sqlite_via(&mut **t, entry, via).await,
    }
}

/// Run an `UpdateQuery` and emit its audit entry in one transaction,
/// unless nothing was updated. Used by the generated `Model::save_pool`.
///
/// The entry is a **snapshot**: `changes` holds the values after the
/// write, with no `before` side. For a field-level diff, use
/// [`save_one_with_diff`], which runs the pre-UPDATE SELECT.
///
/// # Errors
/// Any [`crate::sql::ExecError`] from compile, bind or execute, plus
/// `sqlx::Error` from the audit emit.
pub async fn save_one_with_audit(
    pool: &crate::sql::Pool,
    query: &crate::core::UpdateQuery,
    entry: &PendingEntry,
) -> Result<u64, crate::sql::ExecError> {
    query.validate()?;
    let stmt = pool.dialect().compile_update(query)?;
    let mut tx = crate::sql::transaction_pool(pool).await?;
    let affected = crate::sql::raw_execute_tx(&mut tx, &stmt.sql, stmt.params).await?;
    if affected > 0 {
        emit_one_tx(&mut tx, Via::Pool(pool), entry).await?;
    }
    tx.commit().await?;
    Ok(affected)
}

/// Who writes an admin write's audit entry.
#[cfg(feature = "admin")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DiffEmit {
    /// The helper, in the write's transaction: a failed emit undoes the write.
    InTx,
    /// The caller, which gets the entry back: best-effort after commit,
    /// or batched into its own transaction.
    Deferred,
}

#[cfg(feature = "admin")]
impl DiffEmit {
    /// An `audit(...)` model audits in the write's tx; others best-effort.
    pub(crate) fn for_model(model: &crate::core::ModelSchema) -> Self {
        if model.audit_track.is_some() {
            Self::InTx
        } else {
            Self::Deferred
        }
    }
}

/// Write `entries` in the caller's `tx`: a failed emit fails the write.
#[cfg(feature = "admin")]
pub(crate) async fn emit_in_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    pool: &crate::sql::Pool,
    entries: &[PendingEntry],
) -> Result<(), crate::sql::ExecError> {
    emit_many_tx(tx, Via::Pool(pool), entries).await
}

/// Outcome of [`update_one_with_row_diff_tx`].
#[cfg(feature = "admin")]
#[derive(Debug)]
pub(crate) enum RowDiffWrite {
    /// The locked read found no row: nothing was written.
    Gone,
    /// The UPDATE ran; `deferred` is the entry left for the caller.
    Written { deferred: Option<PendingEntry> },
}

/// Lock the row `before` selects in `tx`, build the entry from that row,
/// then UPDATE. A concurrent edit cannot slip between the diff's read and
/// the write. The caller commits, so later writes can share the tx.
#[cfg(feature = "admin")]
pub(crate) async fn update_one_with_row_diff_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    pool: &crate::sql::Pool,
    query: &crate::core::UpdateQuery,
    mut before: crate::core::SelectQuery,
    fields: &[&'static crate::core::FieldSchema],
    entry_of: impl FnOnce(&serde_json::Value) -> Option<PendingEntry>,
    emit: DiffEmit,
) -> Result<RowDiffWrite, crate::sql::ExecError> {
    query.validate()?;
    let stmt = pool.dialect().compile_update(query)?;
    before.lock_mode = Some(crate::core::LockMode {
        silent_on_sqlite: true,
        ..crate::core::LockMode::default()
    });
    let Some(row) = crate::sql::select_one_row_as_json_tx(tx, &before, fields).await? else {
        return Ok(RowDiffWrite::Gone);
    };
    let entry = entry_of(&row);
    let affected = crate::sql::raw_execute_tx(tx, &stmt.sql, stmt.params).await?;
    let entry = entry.filter(|_| affected > 0);
    if let (DiffEmit::InTx, Some(entry)) = (emit, &entry) {
        emit_one_tx(tx, Via::Pool(pool), entry).await?;
    }
    Ok(RowDiffWrite::Written {
        deferred: entry.filter(|_| emit == DiffEmit::Deferred),
    })
}

/// INSERT `query`, then build its entry from the new PK. With
/// [`DiffEmit::InTx`] the entry commits with the row (#2101); otherwise
/// it comes back for the caller's best-effort emit.
#[cfg(feature = "admin")]
pub(crate) async fn insert_one_with_entry(
    pool: &crate::sql::Pool,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
    entry_of: impl FnOnce(&crate::core::SqlValue) -> PendingEntry,
    emit: DiffEmit,
) -> Result<(crate::core::SqlValue, Option<PendingEntry>), crate::sql::ExecError> {
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    let written = insert_one_with_entry_tx(&mut tx, pool, query, pk_field, entry_of, emit).await?;
    tx.commit().await?;
    Ok(written)
}

/// [`insert_one_with_entry`] in the caller's `tx`; the caller commits.
#[cfg(feature = "admin")]
pub(crate) async fn insert_one_with_entry_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    pool: &crate::sql::Pool,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
    entry_of: impl FnOnce(&crate::core::SqlValue) -> PendingEntry,
    emit: DiffEmit,
) -> Result<(crate::core::SqlValue, Option<PendingEntry>), crate::sql::ExecError> {
    let returning = crate::sql::insert_returning_tx(tx, query).await?;
    let pk = crate::sql::inserted_pk(query, &returning, pk_field)?;
    let entry = entry_of(&pk);
    if emit == DiffEmit::InTx {
        emit_one_tx(tx, Via::Pool(pool), &entry).await?;
    }
    Ok((pk, (emit == DiffEmit::Deferred).then_some(entry)))
}

/// Run an `InsertQuery`, write the assigned PK back into `model`, then
/// emit `entry(model)` in the same transaction, so the audit row carries
/// the real PK. Used by the generated `Model::insert_pool` for audited
/// models.
///
/// MySQL fills in only the first `Auto<T>` field (one `LAST_INSERT_ID()`),
/// so other tracked `Auto` and generated fields audit as `null` there.
///
/// # Errors
/// Any [`crate::sql::ExecError`] from compile, bind, execute or PK
/// decode, plus `sqlx::Error` from the audit emit.
pub async fn insert_one_with_audit<M>(
    pool: &crate::sql::Pool,
    query: &crate::core::InsertQuery,
    model: &mut M,
    entry: impl FnOnce(&M) -> PendingEntry,
) -> Result<(), crate::sql::ExecError>
where
    M: crate::sql::AssignAutoPkPool,
{
    // `insert_returning_tx` already handles each backend's return shape.
    let mut tx = crate::sql::transaction_pool(pool).await?;
    let returning = crate::sql::insert_returning_tx(&mut tx, query).await?;
    crate::sql::apply_auto_pk(returning, model)?;
    emit_one_tx(&mut tx, Via::Pool(pool), &entry(model)).await?;
    tx.commit().await?;
    Ok(())
}

/// Emit a batch of entries inside an open `PoolTx`, in bind-limit-sized
/// multi-row INSERTs.
async fn emit_many_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    via: Via<'_>,
    entries: &[PendingEntry],
) -> Result<(), crate::sql::ExecError> {
    let per_insert = audit_rows_per_insert(tx.dialect());
    for chunk in entries.chunks(per_insert) {
        let stmt = audit_insert_stmt(tx.dialect(), chunk, via)?;
        crate::sql::raw_execute_tx(tx, &stmt.sql, stmt.params).await?;
    }
    Ok(())
}

/// PKs per bulk-write statement: under SQLite's oldest bind limit (999).
const BULK_AUDIT_CHUNK: usize = 500;

/// `pk IN (pks)` on `model`.
fn pk_in(
    model: &'static crate::core::ModelSchema,
    pks: Vec<crate::core::SqlValue>,
) -> Result<crate::core::WhereExpr, crate::sql::ExecError> {
    let pk = model
        .primary_key()
        .ok_or(crate::sql::ExecError::MissingPrimaryKey { table: model.table })?;
    Ok(crate::core::WhereExpr::Predicate(crate::core::Filter::new(
        pk.column,
        crate::core::Op::In,
        crate::core::SqlValue::List(pks),
    )))
}

/// Rows matching `where_clause`, read in `tx` (`FOR UPDATE` on PG/MySQL).
async fn rows_in_tx<M>(
    tx: &mut crate::sql::PoolTx<'_>,
    model: &'static crate::core::ModelSchema,
    where_clause: crate::core::WhereExpr,
    lock: bool,
) -> Result<Vec<M>, crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    let mut select = crate::core::SelectQuery::new(model).where_clause(where_clause);
    if lock {
        select.lock_mode = Some(crate::core::LockMode {
            silent_on_sqlite: true,
            ..crate::core::LockMode::default()
        });
    }
    crate::sql::select_rows_tx_with_related::<M>(tx, &select).await
}

/// `true` when only the row's own columns decide whether it matches, so
/// writing other rows cannot move it in or out of the set.
fn is_row_local(where_clause: &crate::core::WhereExpr) -> bool {
    use crate::core::WhereExpr as W;
    match where_clause {
        W::Predicate(_) => true,
        W::And(items) | W::Or(items) | W::Xor(items) => items.iter().all(is_row_local),
        W::Not(child) => is_row_local(child),
        _ => false,
    }
}

/// Rows a bulk write goes through, one locked page of
/// [`BULK_AUDIT_CHUNK`] at a time, inside the write's transaction.
enum Pages<M> {
    /// Row-local WHERE: `… AND pk > last ORDER BY pk LIMIT n FOR UPDATE`.
    Keyset {
        where_clause: crate::core::WhereExpr,
        last: Option<crate::core::SqlValue>,
        done: bool,
    },
    /// The WHERE reads other rows (a subquery, e.g. a `limit()` bound),
    /// which our own writes would shift, so the set is read once.
    Pinned(std::vec::IntoIter<M>),
}

impl<M> Pages<M>
where
    M: crate::sql::HasPkValue
        + crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    async fn start(
        tx: &mut crate::sql::PoolTx<'_>,
        model: &'static crate::core::ModelSchema,
        where_clause: &crate::core::WhereExpr,
    ) -> Result<Self, crate::sql::ExecError> {
        if is_row_local(where_clause) {
            return Ok(Self::Keyset {
                where_clause: where_clause.clone(),
                last: None,
                done: false,
            });
        }
        let rows: Vec<M> = rows_in_tx(tx, model, where_clause.clone(), true).await?;
        Ok(Self::Pinned(rows.into_iter()))
    }

    /// The next page; empty when the set is exhausted.
    async fn next(
        &mut self,
        tx: &mut crate::sql::PoolTx<'_>,
        model: &'static crate::core::ModelSchema,
    ) -> Result<Vec<M>, crate::sql::ExecError> {
        match self {
            Self::Pinned(rows) => Ok(rows.by_ref().take(BULK_AUDIT_CHUNK).collect()),
            Self::Keyset {
                where_clause,
                last,
                done,
            } => {
                if *done {
                    return Ok(Vec::new());
                }
                let pk = model
                    .primary_key()
                    .ok_or(crate::sql::ExecError::MissingPrimaryKey { table: model.table })?;
                let mut page_where = where_clause.clone();
                if let Some(last) = last.take() {
                    page_where.push_and(crate::core::WhereExpr::Predicate(
                        crate::core::Filter::new(pk.column, crate::core::Op::Gt, last),
                    ));
                }
                let mut select = crate::core::SelectQuery::new(model).where_clause(page_where);
                select.order_by = vec![crate::core::OrderItem::column(pk.column, false)];
                select.limit = Some(BULK_AUDIT_CHUNK as i64);
                select.lock_mode = Some(crate::core::LockMode {
                    silent_on_sqlite: true,
                    ..crate::core::LockMode::default()
                });
                let rows: Vec<M> =
                    crate::sql::select_rows_tx_with_related::<M>(tx, &select).await?;
                *done = rows.len() < BULK_AUDIT_CHUNK;
                *last = rows.last().map(M::__rustango_pk_value_impl);
                Ok(rows)
            }
        }
    }
}

/// Run a bulk `DeleteQuery` with one `Delete` audit row per deleted row,
/// all in one transaction. Matching rows are locked and deleted by PK a
/// page at a time, so the audit set is exactly the deleted set.
///
/// # Errors
/// As [`delete_one_with_audit`], plus the pre-delete SELECT.
pub async fn delete_many_with_audit<M>(
    pool: &crate::sql::Pool,
    query: &crate::core::DeleteQuery,
    entry: impl Fn(&M) -> PendingEntry,
) -> Result<u64, crate::sql::ExecError>
where
    M: crate::sql::HasPkValue
        + crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    let mut pages = Pages::<M>::start(&mut tx, query.model, &query.where_clause).await?;
    let mut affected = 0;
    loop {
        let chunk = pages.next(&mut tx, query.model).await?;
        if chunk.is_empty() {
            break;
        }
        let pks = chunk.iter().map(M::__rustango_pk_value_impl).collect();
        let delete = crate::core::DeleteQuery {
            model: query.model,
            where_clause: pk_in(query.model, pks)?,
        };
        affected += crate::sql::delete_tx(&mut tx, &delete).await?;
        let entries: Vec<PendingEntry> = chunk.iter().map(&entry).collect();
        emit_many_tx(&mut tx, Via::Pool(pool), &entries).await?;
    }
    tx.commit().await?;
    Ok(affected)
}

/// Run a bulk `UpdateQuery` with one `Update` audit row per updated row,
/// all in one transaction. Rows are locked a page at a time, updated by
/// PK, then re-read so each entry is an after-write snapshot, as on `save_pool`.
///
/// # Errors
/// As [`save_one_with_audit`], plus the SELECTs around the update.
pub async fn update_many_with_audit<M>(
    pool: &crate::sql::Pool,
    query: &crate::core::UpdateQuery,
    entry: impl Fn(&M) -> PendingEntry,
) -> Result<u64, crate::sql::ExecError>
where
    M: crate::sql::HasPkValue
        + crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    let pk = query
        .model
        .primary_key()
        .ok_or(crate::sql::ExecError::MissingPrimaryKey {
            table: query.model.table,
        })?;
    // The after-write re-read is by PK, so a PK change would go unaudited.
    if query.set.iter().any(|a| a.column == pk.column) {
        return Err(crate::sql::ExecError::AuditUnsupported {
            table: query.model.table,
            reason: "a bulk update cannot change the primary key",
        });
    }
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    let mut pages = Pages::<M>::start(&mut tx, query.model, &query.where_clause).await?;
    let mut affected = 0;
    loop {
        let chunk = pages.next(&mut tx, query.model).await?;
        if chunk.is_empty() {
            break;
        }
        let pks: Vec<crate::core::SqlValue> =
            chunk.iter().map(M::__rustango_pk_value_impl).collect();
        let update = crate::core::UpdateQuery::new(
            query.model,
            query.set.clone(),
            pk_in(query.model, pks.clone())?,
        );
        affected += crate::sql::update_tx(&mut tx, &update).await?;
        let after: Vec<M> =
            rows_in_tx(&mut tx, query.model, pk_in(query.model, pks)?, false).await?;
        let entries: Vec<PendingEntry> = after.iter().map(&entry).collect();
        emit_many_tx(&mut tx, Via::Pool(pool), &entries).await?;
    }
    tx.commit().await?;
    Ok(affected)
}

/// Audited `UPDATE` runner a model hands to generic code, through
/// `Model::__rustango_audited_update`.
pub type AuditedUpdate = for<'a> fn(
    &'a crate::sql::Pool,
    &'a crate::core::UpdateQuery,
    AuditOp,
) -> std::pin::Pin<
    Box<dyn std::future::Future<Output = Result<u64, crate::sql::ExecError>> + Send + 'a>,
>;

/// Audited `DELETE` runner, through `Model::__rustango_audited_delete`.
pub type AuditedDelete = for<'a> fn(
    &'a crate::sql::Pool,
    &'a crate::core::DeleteQuery,
) -> std::pin::Pin<
    Box<dyn std::future::Future<Output = Result<u64, crate::sql::ExecError>> + Send + 'a>,
>;

/// Audited `create` recorder, through `Model::__rustango_audited_create`:
/// re-reads the new row by PK in the insert's transaction and emits its entry.
pub type AuditedCreate = for<'a, 't> fn(
    &'a mut crate::sql::PoolTx<'t>,
    crate::core::SqlValue,
) -> std::pin::Pin<
    Box<dyn std::future::Future<Output = Result<(), crate::sql::ExecError>> + Send + 'a>,
>;

/// Audited `update` recorder, through `Model::__rustango_audited_update_record`:
/// re-reads the updated row by PK in the update's transaction (#2010).
pub type AuditedUpdateRecord = for<'a, 't> fn(
    &'a mut crate::sql::PoolTx<'t>,
    crate::core::SqlValue,
) -> std::pin::Pin<
    Box<dyn std::future::Future<Output = Result<(), crate::sql::ExecError>> + Send + 'a>,
>;

/// A one-row update that can run inside a caller's transaction with its
/// audit entry (#2010). Absent for a model whose audit runs only on a pool.
#[cfg_attr(not(any(feature = "admin", feature = "tenancy")), allow(dead_code))]
#[derive(Clone, Copy)]
pub(crate) struct TxUpdate(Option<AuditedUpdateRecord>);

#[cfg_attr(not(any(feature = "admin", feature = "tenancy")), allow(dead_code))]
impl TxUpdate {
    /// `None` when `model` audits updates but has no in-transaction
    /// recorder (a hand-written `ModelEntry`): its update must go through
    /// [`update`], or its audit entry would be lost.
    pub(crate) fn for_model(model: &crate::core::ModelSchema) -> Option<Self> {
        Self::for_entry(crate::core::ModelEntry::for_schema(model))
    }

    fn for_entry(entry: Option<&crate::core::ModelEntry>) -> Option<Self> {
        let Some(entry) = entry else {
            return Some(Self(None));
        };
        match (entry.audited_update_record(), entry.audited_update()) {
            (Some(record), _) => Some(Self(Some(record))),
            (None, Some(_)) => None,
            (None, None) => Some(Self(None)),
        }
    }

    /// Run `query` in `tx`, then record the row `pk` re-read.
    ///
    /// # Errors
    /// As [`crate::sql::update_tx`], plus the audit write.
    pub(crate) async fn run(
        self,
        tx: &mut crate::sql::PoolTx<'_>,
        pool: &crate::sql::Pool,
        query: &crate::core::UpdateQuery,
        pk: crate::core::SqlValue,
    ) -> Result<u64, crate::sql::ExecError> {
        let n = crate::sql::update_tx(tx, query).await?;
        if let (true, Some(record)) = (n > 0, self.0) {
            writing_via(pool, record(tx, pk)).await.map_err(|e| {
                crate::sql::ExecError::AuditWrite {
                    table: query.model.table,
                    source: Box::new(e),
                }
            })?;
        }
        Ok(n)
    }
}

/// Emit a `Create` entry for the row with primary key `pk`, read in `tx`.
///
/// # Errors
/// As the re-read SELECT and the emit.
pub async fn record_create<M>(
    tx: &mut crate::sql::PoolTx<'_>,
    model: &'static crate::core::ModelSchema,
    pk: crate::core::SqlValue,
    entry: impl Fn(&M) -> PendingEntry,
) -> Result<(), crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    record_by_pk(
        tx,
        model,
        pk,
        entry,
        "the created row was not found by its primary key",
    )
    .await
}

/// Emit an entry for the row with primary key `pk` after an update in
/// `tx`, read in `tx` (#2010).
///
/// # Errors
/// As the re-read SELECT and the emit.
#[doc(hidden)]
pub async fn record_update<M>(
    tx: &mut crate::sql::PoolTx<'_>,
    model: &'static crate::core::ModelSchema,
    pk: crate::core::SqlValue,
    entry: impl Fn(&M) -> PendingEntry,
) -> Result<(), crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    record_by_pk(
        tx,
        model,
        pk,
        entry,
        "the updated row was not found by its primary key",
    )
    .await
}

async fn record_by_pk<M>(
    tx: &mut crate::sql::PoolTx<'_>,
    model: &'static crate::core::ModelSchema,
    pk: crate::core::SqlValue,
    entry: impl Fn(&M) -> PendingEntry,
    missing: &'static str,
) -> Result<(), crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    let rows: Vec<M> = rows_in_tx(tx, model, pk_in(model, vec![pk])?, false).await?;
    if rows.len() != 1 {
        return Err(crate::sql::ExecError::AuditUnsupported {
            table: model.table,
            reason: missing,
        });
    }
    let entries: Vec<PendingEntry> = rows.iter().map(&entry).collect();
    emit_many_tx(tx, Via::Scoped, &entries).await
}

#[cfg_attr(
    not(any(feature = "admin", feature = "tenancy", feature = "forms")),
    allow(dead_code)
)]
fn audited_create(query: &crate::core::InsertQuery) -> Option<AuditedCreate> {
    crate::core::ModelEntry::for_schema(query.model).and_then(|e| e.audited_create())
}

/// `true` when inserts on `model` write a `create` audit row.
#[cfg_attr(not(feature = "template_views"), allow(dead_code))]
pub(crate) fn audits_creates(model: &crate::core::ModelSchema) -> bool {
    crate::core::ModelEntry::for_schema(model).is_some_and(|e| e.audited_create().is_some())
}

/// Run `query` and return the new row's PK, writing a `create` audit row
/// in the same transaction when its model is audited (#1816).
///
/// Crate-private: an `on_conflict` insert may write no row, which the
/// create audit can't tell apart from a new one.
///
/// # Errors
/// As [`crate::sql::insert_returning_pool`], plus the audit write.
#[cfg_attr(
    not(any(feature = "admin", feature = "tenancy", feature = "forms")),
    allow(dead_code)
)]
pub(crate) async fn insert(
    pool: &crate::sql::Pool,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
) -> Result<crate::core::SqlValue, crate::sql::ExecError> {
    Ok(insert_returning(pool, query, pk_field).await?.0)
}

/// [`insert`], also handing back the RETURNING row.
///
/// # Errors
/// As [`insert`].
#[cfg_attr(
    not(any(feature = "admin", feature = "tenancy", feature = "forms")),
    allow(dead_code)
)]
pub(crate) async fn insert_returning(
    pool: &crate::sql::Pool,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
) -> Result<(crate::core::SqlValue, crate::sql::InsertReturningPool), crate::sql::ExecError> {
    if audited_create(query).is_none() {
        let returning = crate::sql::insert_returning_pool(pool, query).await?;
        let pk = crate::sql::inserted_pk(query, &returning, pk_field)?;
        return Ok((pk, returning));
    }
    let mut tx = crate::sql::transaction_pool(pool).await?;
    // The create recorder only has the transaction.
    let inserted = writing_via(pool, insert_returning_tx(&mut tx, query, pk_field)).await?;
    tx.commit().await?;
    Ok(inserted)
}

/// [`insert`] inside an open transaction.
///
/// # Errors
/// As [`crate::sql::insert_returning_tx`], plus the audit write.
#[cfg_attr(not(any(feature = "admin", feature = "tenancy")), allow(dead_code))]
pub(crate) async fn insert_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
) -> Result<crate::core::SqlValue, crate::sql::ExecError> {
    Ok(insert_returning_tx(tx, query, pk_field).await?.0)
}

#[cfg_attr(
    not(any(feature = "admin", feature = "tenancy", feature = "forms")),
    allow(dead_code)
)]
async fn insert_returning_tx(
    tx: &mut crate::sql::PoolTx<'_>,
    query: &crate::core::InsertQuery,
    pk_field: &crate::core::FieldSchema,
) -> Result<(crate::core::SqlValue, crate::sql::InsertReturningPool), crate::sql::ExecError> {
    let returning = crate::sql::insert_returning_tx(tx, query).await?;
    let pk = crate::sql::inserted_pk(query, &returning, pk_field)?;
    if let Some(record) = audited_create(query) {
        record(tx, pk.clone())
            .await
            .map_err(|e| crate::sql::ExecError::AuditWrite {
                table: query.model.table,
                source: Box::new(e),
            })?;
    }
    Ok((pk, returning))
}

/// Run `query`, auditing each row when its model is audited (#1794). The
/// choke point for schema-driven writes that have no `M` to call.
///
/// # Errors
/// As [`update_many_with_audit`] or [`crate::sql::update_pool`].
pub async fn update(
    pool: &crate::sql::Pool,
    query: &crate::core::UpdateQuery,
) -> Result<u64, crate::sql::ExecError> {
    update_as(pool, query, AuditOp::Update).await
}

/// [`update`], recording each row as `op` (soft delete and restore).
///
/// # Errors
/// As [`update`].
pub async fn update_as(
    pool: &crate::sql::Pool,
    query: &crate::core::UpdateQuery,
    op: AuditOp,
) -> Result<u64, crate::sql::ExecError> {
    match crate::core::ModelEntry::for_schema(query.model).and_then(|e| e.audited_update()) {
        Some(run) => run(pool, query, op).await,
        None => crate::sql::update_pool(pool, query).await,
    }
}

/// Run `query`, auditing each deleted row when its model is audited (#1794).
///
/// # Errors
/// As [`delete_many_with_audit`] or [`crate::sql::delete_pool`].
pub async fn delete(
    pool: &crate::sql::Pool,
    query: &crate::core::DeleteQuery,
) -> Result<u64, crate::sql::ExecError> {
    match crate::core::ModelEntry::for_schema(query.model).and_then(|e| e.audited_delete()) {
        Some(run) => run(pool, query).await,
        None => crate::sql::delete_pool(pool, query).await,
    }
}

/// Run a `BulkUpdateQuery` (`Model::bulk_update`) and one `Update` entry
/// per updated row, re-read after the write, in one transaction.
///
/// # Errors
/// As [`crate::sql::bulk_update_pool`], plus the re-read and the emit.
pub async fn bulk_update_with_audit<M>(
    pool: &crate::sql::Pool,
    query: &crate::core::BulkUpdateQuery,
    entry: impl Fn(&M) -> PendingEntry,
) -> Result<u64, crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    if query.rows.is_empty() {
        return Ok(0);
    }
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    // Each row binds its PK and one value per update column. Small pages
    // also keep SQLite's per-row CTE lookup cheap.
    let per_row = query.update_columns.len() + 1;
    let max_rows = (tx.dialect().max_bind_params() / per_row).clamp(1, BULK_AUDIT_CHUNK);
    let mut affected = 0;
    for chunk in query.rows.chunks(max_rows) {
        let batch = crate::core::BulkUpdateQuery::new(
            query.model,
            query.update_columns.clone(),
            chunk.to_vec(),
        );
        let stmt = tx.dialect().compile_bulk_update(&batch)?;
        affected += crate::sql::raw_execute_tx(&mut tx, &stmt.sql, stmt.params).await?;
    }
    // Each row is `[pk, …update cols]`.
    let pks: Vec<crate::core::SqlValue> = query
        .rows
        .iter()
        .filter_map(|r| r.first().cloned())
        .collect();
    for chunk in pks.chunks(BULK_AUDIT_CHUNK) {
        let after: Vec<M> = rows_in_tx(
            &mut tx,
            query.model,
            pk_in(query.model, chunk.to_vec())?,
            false,
        )
        .await?;
        let entries: Vec<PendingEntry> = after.iter().map(&entry).collect();
        emit_many_tx(&mut tx, Via::Pool(pool), &entries).await?;
    }
    tx.commit().await?;
    Ok(affected)
}

/// Run a conflict-handling bulk insert (`bulk_upsert_pool`,
/// `bulk_insert_or_ignore_pool`) with one audit row per row it wrote, in
/// one transaction (#1795). Each row is first tried with `DoNothing`; only
/// the rows that skipped go to the `DoUpdate`, audited as `Update`.
///
/// The first pass runs one statement per row. On PG a row deleted by
/// another transaction between the passes is recorded as `Update`. MySQL
/// has no `RETURNING`, so it is refused.
///
/// # Errors
/// [`crate::sql::ExecError::AuditUnsupported`] on MySQL, else as
/// [`crate::sql::bulk_insert_pool`], plus the re-read and the emit.
pub async fn bulk_insert_with_audit<M>(
    pool: &crate::sql::Pool,
    query: &crate::core::BulkInsertQuery,
    entry: impl Fn(&M, AuditOp) -> PendingEntry,
) -> Result<(), crate::sql::ExecError>
where
    M: crate::sql::MaybePgFromRow
        + crate::sql::MaybeMyFromRow
        + crate::sql::MaybeSqliteFromRow
        + crate::sql::LoadRelated
        + crate::sql::MaybeMyLoadRelated
        + crate::sql::MaybeSqliteLoadRelated
        + Send
        + Unpin,
{
    if !pool.dialect().supports_returning() {
        return Err(crate::sql::ExecError::AuditUnsupported {
            table: query.model.table,
            reason:
                "without RETURNING a conflict-handling bulk insert cannot tell which rows it wrote",
        });
    }
    if query.rows.is_empty() {
        return Ok(());
    }
    let pk_field = query
        .model
        .primary_key()
        .ok_or(crate::sql::ExecError::MissingPrimaryKey {
            table: query.model.table,
        })?;
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    let mut created = Vec::new();
    let mut skipped = Vec::new();
    for row in &query.rows {
        let probe = crate::core::InsertQuery::new(query.model, query.columns.clone(), row.clone())
            .returning(vec![pk_field.column])
            .on_conflict(crate::core::ConflictClause::DoNothing);
        match crate::sql::insert_returning_tx(&mut tx, &probe).await {
            Ok(returning) => {
                created.push(crate::sql::inserted_pk(&probe, &returning, pk_field)?);
            }
            Err(crate::sql::ExecError::Driver(sqlx::Error::RowNotFound)) => {
                skipped.push(row.clone());
            }
            Err(e) => return Err(e),
        }
    }
    let mut updated = Vec::new();
    if let (Some(clause @ crate::core::ConflictClause::DoUpdate { .. }), false) =
        (&query.on_conflict, skipped.is_empty())
    {
        let upsert = crate::core::BulkInsertQuery::new(query.model, query.columns.clone(), skipped)
            .on_conflict(clause.clone());
        updated = crate::sql::bulk_insert_pks_tx(&mut tx, &upsert).await?;
    }
    for (pks, op) in [(created, AuditOp::Create), (updated, AuditOp::Update)] {
        for chunk in pks.chunks(BULK_AUDIT_CHUNK) {
            let rows: Vec<M> = rows_in_tx(
                &mut tx,
                query.model,
                pk_in(query.model, chunk.to_vec())?,
                false,
            )
            .await?;
            let entries: Vec<PendingEntry> = rows.iter().map(|r| entry(r, op)).collect();
            emit_many_tx(&mut tx, Via::Pool(pool), &entries).await?;
        }
    }
    tx.commit().await?;
    Ok(())
}

/// A single-row `INSERT … ON CONFLICT` on PG, with whether it inserted
/// (`Create`) or updated (`Update`) the row: a `DoUpdate` runs a
/// `DoNothing` pass first and upserts only when that skipped (#1795).
/// A row another transaction deletes between the passes is recorded as `Update`.
///
/// # Errors
/// As [`crate::sql::insert_returning_pool`].
#[cfg(feature = "postgres")]
pub async fn upsert_returning_on(
    conn: &mut sqlx::PgConnection,
    query: &crate::core::InsertQuery,
) -> Result<(sqlx::postgres::PgRow, AuditOp), crate::sql::ExecError> {
    use crate::sql::insert_returning_on;
    if !matches!(
        query.on_conflict,
        Some(crate::core::ConflictClause::DoUpdate { .. })
    ) {
        return Ok((
            insert_returning_on(&mut *conn, query).await?,
            AuditOp::Create,
        ));
    }
    let mut probe = query.clone();
    probe.on_conflict = Some(crate::core::ConflictClause::DoNothing);
    match insert_returning_on(&mut *conn, &probe).await {
        Ok(row) => Ok((row, AuditOp::Create)),
        Err(crate::sql::ExecError::Driver(sqlx::Error::RowNotFound)) => Ok((
            insert_returning_on(&mut *conn, query).await?,
            AuditOp::Update,
        )),
        Err(e) => Err(e),
    }
}

/// Run a table-wide statement (`Model::truncate`) and one bulk `Delete`
/// entry naming it, in one transaction. No per-row PK is known here.
///
/// # Errors
/// As [`crate::sql::raw_execute_tx`], plus the audit emit.
pub async fn truncate_with_audit(
    pool: &crate::sql::Pool,
    entity_table: &'static str,
    sql: &str,
) -> Result<u64, crate::sql::ExecError> {
    let mut tx = crate::sql::write_transaction_pool(pool).await?;
    let affected = crate::sql::raw_execute_tx(&mut tx, sql, Vec::new()).await?;
    let entry = PendingEntry {
        entity_table,
        entity_pk: String::new(),
        operation: AuditOp::Delete,
        source: current_source(),
        changes: serde_json::json!({ "bulk": "truncate" }),
    };
    emit_one_tx(&mut tx, Via::Pool(pool), &entry).await?;
    tx.commit().await?;
    Ok(affected)
}

/// Postgres bind helper, exposed so generated bodies on the audited
/// `save_pool` diff path can bind `SqlValue` arguments to a transaction.
/// Not part of the public API.
#[doc(hidden)]
#[cfg(feature = "postgres")]
pub fn __bind_value_pg(
    q: sqlx::query::Query<'_, sqlx::Postgres, sqlx::postgres::PgArguments>,
    value: crate::core::SqlValue,
) -> sqlx::query::Query<'_, sqlx::Postgres, sqlx::postgres::PgArguments> {
    crate::sql::bind_query(q, value)
}

/// MySQL counterpart of [`__bind_value_pg`].
#[doc(hidden)]
#[cfg(feature = "mysql")]
pub fn __bind_value_my(
    q: sqlx::query::Query<'_, sqlx::MySql, sqlx::mysql::MySqlArguments>,
    value: crate::core::SqlValue,
) -> sqlx::query::Query<'_, sqlx::MySql, sqlx::mysql::MySqlArguments> {
    crate::sql::bind_query_my(q, value)
}

/// SQLite counterpart of [`__bind_value_pg`].
#[doc(hidden)]
#[cfg(feature = "sqlite")]
pub fn __bind_value_sqlite<'q>(
    q: sqlx::query::Query<'q, sqlx::Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
    value: crate::core::SqlValue,
) -> sqlx::query::Query<'q, sqlx::Sqlite, sqlx::sqlite::SqliteArguments<'q>> {
    crate::sql::bind_query_sqlite(q, value)
}

/// The BEFORE read of an audited update: `columns` of the row at `pk_value`.
#[doc(hidden)]
#[must_use]
pub fn before_image_query(
    schema: &'static crate::core::ModelSchema,
    pk_column: &'static str,
    pk_value: crate::core::SqlValue,
    columns: &[&'static str],
) -> crate::core::SelectQuery {
    let mut q = crate::core::SelectQuery::by_pk(schema, pk_column, pk_value);
    q.projection = Some(columns.to_vec());
    q
}

/// Per-row audited save with a field-level diff, on any backend. All of
/// it runs in one transaction:
///
/// 1. SELECT the tracked columns and decode them as the BEFORE pairs.
/// 2. Run the compiled UPDATE.
/// 3. Diff `after_pairs` against BEFORE.
/// 4. Emit an `Update` audit entry, then commit.
///
/// The closure argument types ([`crate::sql::PgReturningRow`] and its
/// siblings) resolve to uninhabited types when a backend feature is off,
/// so generated closure bodies still typecheck in any feature set.
///
/// # Errors
/// Any [`crate::sql::ExecError`] from the SELECT or UPDATE, plus
/// `sqlx::Error` from the audit emit.
#[allow(clippy::too_many_arguments)]
pub async fn save_one_with_diff<F1, F2, F3>(
    pool: &crate::sql::Pool,
    update_query: &crate::core::UpdateQuery,
    before_query: &crate::core::SelectQuery,
    entity_table: &'static str,
    entity_pk: String,
    after_pairs: Vec<(&'static str, serde_json::Value)>,
    decode_before_pg: F1,
    decode_before_my: F2,
    decode_before_sqlite: F3,
) -> Result<u64, crate::sql::ExecError>
where
    F1: FnOnce(&crate::sql::PgReturningRow) -> Vec<(&'static str, serde_json::Value)>,
    F2: FnOnce(&crate::sql::MyReturningRow) -> Vec<(&'static str, serde_json::Value)>,
    F3: FnOnce(&crate::sql::SqliteReturningRow) -> Vec<(&'static str, serde_json::Value)>,
{
    let _ = (&decode_before_pg, &decode_before_my, &decode_before_sqlite);
    update_query.validate()?;
    let stmt = pool.dialect().compile_update(update_query)?;
    let before = pool.dialect().compile_select(before_query)?;
    // Only the pre-update SELECT differs per backend: each row type is a
    // different concrete type, so each arm calls its own
    // `decode_before_*`. The UPDATE, emit and commit are shared by
    // wrapping the transaction in a `PoolTx`.
    match pool {
        #[cfg(feature = "postgres")]
        crate::sql::Pool::Postgres(pg) => {
            let mut tx = pg.begin().await?;
            let pk_q = before
                .params
                .iter()
                .cloned()
                .fold(sqlx::query(&before.sql), crate::sql::bind_query);
            let before_pairs: Option<Vec<(&'static str, serde_json::Value)>> =
                // A failed pre-read must not let the UPDATE commit unaudited.
                pk_q.fetch_optional(&mut *tx)
                    .await?
                    .map(|row| decode_before_pg(&row));
            let mut wrapped = crate::sql::PoolTx::Postgres(tx);
            let _affected = finish_update_with_audit_diff(
                &mut wrapped,
                Via::Pool(pool),
                &stmt,
                before_pairs,
                &after_pairs,
                entity_table,
                &entity_pk,
            )
            .await?;
            wrapped.commit().await?;
            Ok(_affected)
        }
        #[cfg(feature = "mysql")]
        crate::sql::Pool::Mysql(my) => {
            let mut tx = my.begin().await?;
            let pk_q = before
                .params
                .iter()
                .cloned()
                .fold(sqlx::query(&before.sql), crate::sql::bind_query_my);
            let before_pairs: Option<Vec<(&'static str, serde_json::Value)>> =
                // A failed pre-read must not let the UPDATE commit unaudited.
                pk_q.fetch_optional(&mut *tx)
                    .await?
                    .map(|row| decode_before_my(&row));
            let mut wrapped = crate::sql::PoolTx::Mysql(tx);
            let _affected = finish_update_with_audit_diff(
                &mut wrapped,
                Via::Pool(pool),
                &stmt,
                before_pairs,
                &after_pairs,
                entity_table,
                &entity_pk,
            )
            .await?;
            wrapped.commit().await?;
            Ok(_affected)
        }
        #[cfg(feature = "sqlite")]
        crate::sql::Pool::Sqlite(sq) => {
            let mut tx = sq.begin().await?;
            let pk_q = before
                .params
                .iter()
                .cloned()
                .fold(sqlx::query(&before.sql), crate::sql::bind_query_sqlite);
            let before_pairs: Option<Vec<(&'static str, serde_json::Value)>> =
                // A failed pre-read must not let the UPDATE commit unaudited.
                pk_q.fetch_optional(&mut *tx)
                    .await?
                    .map(|row| decode_before_sqlite(&row));
            let mut wrapped = crate::sql::PoolTx::Sqlite(tx);
            let _affected = finish_update_with_audit_diff(
                &mut wrapped,
                Via::Pool(pool),
                &stmt,
                before_pairs,
                &after_pairs,
                entity_table,
                &entity_pk,
            )
            .await?;
            wrapped.commit().await?;
            Ok(_affected)
        }
    }
}

/// Shared tail of every [`save_one_with_diff`] arm: run the compiled
/// UPDATE, then emit the audit row if a BEFORE snapshot was captured.
async fn finish_update_with_audit_diff(
    tx: &mut crate::sql::PoolTx<'_>,
    via: Via<'_>,
    stmt: &crate::sql::CompiledStatement,
    before_pairs: Option<Vec<(&'static str, serde_json::Value)>>,
    after_pairs: &[(&'static str, serde_json::Value)],
    entity_table: &'static str,
    entity_pk: &str,
) -> Result<u64, crate::sql::ExecError> {
    // Return rows-affected: 0 means the PK no longer exists.
    let _affected = crate::sql::raw_execute_tx(tx, &stmt.sql, stmt.params.clone()).await?;
    let entry = before_pairs.and_then(|before| {
        PendingEntry::update_diff(entity_table, entity_pk.to_owned(), &before, after_pairs)
    });
    if let Some(entry) = entry {
        emit_one_tx(tx, via, &entry).await?;
    }
    Ok(_affected)
}

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

    fn runner<'a>(
        _: &'a crate::sql::Pool,
        _: &'a crate::core::UpdateQuery,
        _: AuditOp,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<u64, crate::sql::ExecError>> + Send + 'a>,
    > {
        Box::pin(async { Ok(0) })
    }

    /// A hand-written entry with a pool runner but no recorder keeps its
    /// audit: no in-transaction update for it (#2010 review).
    #[test]
    fn a_pool_only_audited_update_never_runs_in_a_transaction() {
        use crate::core::{Model as _, ModelEntry};
        let schema = crate::i18n::db::Translation::SCHEMA;
        let audited =
            ModelEntry::new(schema, "app").with_audited(|| Some(runner as AuditedUpdate), || None);
        assert!(TxUpdate::for_entry(Some(&audited)).is_none());
        assert!(TxUpdate::for_entry(Some(&ModelEntry::new(schema, "app"))).is_some());
        assert!(TxUpdate::for_entry(None).is_some());
    }
}