waypoint-core 0.8.1

Lightweight, Flyway-compatible SQL migration library for PostgreSQL and MySQL
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
//! Lightweight regex-based DDL extraction from SQL content.
//!
//! Used by lint, changelog, and conflict detection features.

use std::sync::LazyLock;

use regex_lite::Regex;
use serde::Serialize;

/// A DDL operation extracted from SQL.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub enum DdlOperation {
    /// A CREATE TABLE statement.
    CreateTable {
        /// Name of the table being created.
        table: String,
        /// Whether the statement includes IF NOT EXISTS.
        if_not_exists: bool,
    },
    /// A DROP TABLE statement.
    DropTable {
        /// Name of the table being dropped.
        table: String,
    },
    /// An ALTER TABLE ... ADD COLUMN statement.
    AlterTableAddColumn {
        /// Name of the table being altered.
        table: String,
        /// Name of the column being added.
        column: String,
        /// Data type of the new column.
        data_type: String,
        /// Whether the column has a DEFAULT expression.
        ///
        /// Determined from the parsed column definition only — a `DEFAULT`
        /// appearing inside a `CHECK (...)` expression, a string literal, or
        /// a comment does not count.
        has_default: bool,
        /// Whether the column has a NOT NULL constraint.
        ///
        /// Determined from the parsed column definition only — a `NOT NULL`
        /// appearing inside a `CHECK (...)` expression, a string literal, or
        /// a comment does not count.
        is_not_null: bool,
        /// Whether the clause includes `IF NOT EXISTS`.
        if_not_exists: bool,
        /// The `DEFAULT` expression of this column, if any.
        default_expr: Option<String>,
    },
    /// An ALTER TABLE ... DROP COLUMN statement.
    AlterTableDropColumn {
        /// Name of the table being altered.
        table: String,
        /// Name of the column being dropped.
        column: String,
    },
    /// An ALTER TABLE ... ALTER COLUMN statement.
    AlterTableAlterColumn {
        /// Name of the table being altered.
        table: String,
        /// Name of the column being modified.
        column: String,
    },
    /// A CREATE INDEX statement.
    CreateIndex {
        /// Name of the index being created.
        name: String,
        /// Name of the table the index is on.
        table: String,
        /// Whether the index is created CONCURRENTLY.
        is_concurrent: bool,
        /// Whether this is a UNIQUE index.
        is_unique: bool,
    },
    /// A DROP INDEX statement.
    DropIndex {
        /// Name of the index being dropped.
        name: String,
    },
    /// A CREATE VIEW or CREATE MATERIALIZED VIEW statement.
    CreateView {
        /// Name of the view being created.
        name: String,
        /// Whether this is a materialized view.
        is_materialized: bool,
    },
    /// A DROP VIEW statement.
    DropView {
        /// Name of the view being dropped.
        name: String,
    },
    /// A CREATE FUNCTION statement.
    CreateFunction {
        /// Name of the function being created.
        name: String,
    },
    /// A DROP FUNCTION statement.
    DropFunction {
        /// Name of the function being dropped.
        name: String,
    },
    /// An ALTER TABLE ... ADD CONSTRAINT statement.
    AddConstraint {
        /// Name of the table the constraint is added to.
        table: String,
        /// Type of constraint (e.g. PRIMARY KEY, UNIQUE, FOREIGN KEY).
        constraint_type: String,
    },
    /// An ALTER TABLE ... DROP CONSTRAINT statement.
    DropConstraint {
        /// Name of the table the constraint is dropped from.
        table: String,
        /// Name of the constraint being dropped.
        name: String,
    },
    /// A CREATE TYPE ... AS ENUM statement.
    CreateEnum {
        /// Name of the enum type being created.
        name: String,
    },
    /// A TRUNCATE TABLE statement.
    TruncateTable {
        /// Name of the table being truncated.
        table: String,
    },
    /// Any other SQL statement that does not match known DDL patterns.
    Other {
        /// Truncated preview of the unrecognized statement.
        statement_preview: String,
    },
}

impl std::fmt::Display for DdlOperation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DdlOperation::CreateTable {
                table,
                if_not_exists,
            } => {
                if *if_not_exists {
                    write!(f, "CREATE TABLE IF NOT EXISTS {}", table)
                } else {
                    write!(f, "CREATE TABLE {}", table)
                }
            }
            DdlOperation::DropTable { table } => write!(f, "DROP TABLE {}", table),
            DdlOperation::AlterTableAddColumn {
                table,
                column,
                data_type,
                if_not_exists,
                ..
            } => {
                let ine = if *if_not_exists { "IF NOT EXISTS " } else { "" };
                write!(
                    f,
                    "ALTER TABLE {} ADD COLUMN {}{} {}",
                    table, ine, column, data_type
                )
            }
            DdlOperation::AlterTableDropColumn { table, column } => {
                write!(f, "ALTER TABLE {} DROP COLUMN {}", table, column)
            }
            DdlOperation::AlterTableAlterColumn { table, column } => {
                write!(f, "ALTER TABLE {} ALTER COLUMN {}", table, column)
            }
            DdlOperation::CreateIndex {
                name,
                table,
                is_unique,
                is_concurrent,
            } => {
                let unique = if *is_unique { "UNIQUE " } else { "" };
                let concurrent = if *is_concurrent { "CONCURRENTLY " } else { "" };
                write!(
                    f,
                    "CREATE {}{}INDEX {} ON {}",
                    unique, concurrent, name, table
                )
            }
            DdlOperation::DropIndex { name } => write!(f, "DROP INDEX {}", name),
            DdlOperation::CreateView {
                name,
                is_materialized,
            } => {
                if *is_materialized {
                    write!(f, "CREATE MATERIALIZED VIEW {}", name)
                } else {
                    write!(f, "CREATE VIEW {}", name)
                }
            }
            DdlOperation::DropView { name } => write!(f, "DROP VIEW {}", name),
            DdlOperation::CreateFunction { name } => write!(f, "CREATE FUNCTION {}", name),
            DdlOperation::DropFunction { name } => write!(f, "DROP FUNCTION {}", name),
            DdlOperation::AddConstraint {
                table,
                constraint_type,
            } => {
                write!(
                    f,
                    "ALTER TABLE {} ADD {} CONSTRAINT",
                    table, constraint_type
                )
            }
            DdlOperation::DropConstraint { table, name } => {
                write!(f, "ALTER TABLE {} DROP CONSTRAINT {}", table, name)
            }
            DdlOperation::CreateEnum { name } => write!(f, "CREATE TYPE {} AS ENUM", name),
            DdlOperation::TruncateTable { table } => write!(f, "TRUNCATE TABLE {}", table),
            DdlOperation::Other { statement_preview } => write!(f, "{}", statement_preview),
        }
    }
}

// Regex patterns for DDL extraction
static CREATE_TABLE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)CREATE\s+TABLE\s+(IF\s+NOT\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)").unwrap()
});

static DROP_TABLE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)DROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)").unwrap()
});

static ALTER_TABLE_DROP_COLUMN_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(
        r"(?i)ALTER\s+TABLE\s+(?:(\w+)\.)?(\w+)\s+DROP\s+(?:COLUMN\s+)?(?:IF\s+EXISTS\s+)?(\w+)",
    )
    .unwrap()
});

static ALTER_TABLE_ALTER_COLUMN_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)ALTER\s+TABLE\s+(?:(\w+)\.)?(\w+)\s+ALTER\s+(?:COLUMN\s+)?(\w+)").unwrap()
});

static CREATE_INDEX_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)CREATE\s+(UNIQUE\s+)?INDEX\s+(CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?(\w+)\s+ON\s+(?:(\w+)\.)?(\w+)").unwrap()
});

static DROP_INDEX_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)DROP\s+INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)")
        .unwrap()
});

static CREATE_VIEW_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)CREATE\s+(?:OR\s+REPLACE\s+)?(MATERIALIZED\s+)?VIEW\s+(?:(\w+)\.)?(\w+)")
        .unwrap()
});

static DROP_VIEW_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)DROP\s+(MATERIALIZED\s+)?VIEW\s+(?:IF\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)").unwrap()
});

static CREATE_FUNCTION_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)CREATE\s+(?:OR\s+REPLACE\s+)?FUNCTION\s+(?:(\w+)\.)?(\w+)").unwrap()
});

static DROP_FUNCTION_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)DROP\s+FUNCTION\s+(?:IF\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)").unwrap()
});

static ADD_CONSTRAINT_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)ALTER\s+TABLE\s+(?:(\w+)\.)?(\w+)\s+ADD\s+(?:CONSTRAINT\s+\w+\s+)?(PRIMARY\s+KEY|UNIQUE|FOREIGN\s+KEY|CHECK|EXCLUDE)").unwrap()
});

static DROP_CONSTRAINT_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(
        r"(?i)ALTER\s+TABLE\s+(?:(\w+)\.)?(\w+)\s+DROP\s+CONSTRAINT\s+(?:IF\s+EXISTS\s+)?(\w+)",
    )
    .unwrap()
});

static CREATE_ENUM_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)CREATE\s+TYPE\s+(?:(\w+)\.)?(\w+)\s+AS\s+ENUM").unwrap());

static TRUNCATE_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)TRUNCATE\s+(?:TABLE\s+)?(?:(\w+)\.)?(\w+)").unwrap());

/// A DDL operation together with its position in the source SQL.
///
/// Offsets are byte offsets into the **original** SQL text (comments
/// included), so diagnostics can point at the statement that produced the
/// operation even though the analysis itself runs on a comment-stripped copy.
#[derive(Debug, Clone)]
pub struct LocatedDdl {
    /// The parsed operation.
    pub op: DdlOperation,
    /// Byte offset of the first character of the statement.
    pub start: usize,
    /// Byte offset just past the last character of the statement.
    pub end: usize,
    /// Byte offset of the token this operation is best anchored to: the
    /// column name for `ADD COLUMN`, otherwise the leading keyword.
    pub focus: usize,
}

/// Extract DDL operations from SQL content.
///
/// Comments are ignored: they can neither introduce nor suppress an
/// operation, and keywords inside them (`NOT NULL`, `DEFAULT`, ...) never
/// influence the parsed result.
pub fn extract_ddl_operations(sql: &str) -> Vec<DdlOperation> {
    extract_ddl_operations_located(sql)
        .into_iter()
        .map(|l| l.op)
        .collect()
}

/// Extract DDL operations along with their source positions.
pub fn extract_ddl_operations_located(sql: &str) -> Vec<LocatedDdl> {
    // Comments *and* string literals are blanked before matching. The patterns
    // in `parse_statement_ops` are unanchored, so without this an INSERT whose
    // payload happens to mention DDL is classified as that DDL:
    //
    //   INSERT INTO runbook (step) VALUES ('then DROP TABLE users');
    //     → DropTable { table: "users" }
    //
    // which made `safety` report a table drop that does not exist and, with
    // `block_on_danger`, refuse the migration — pushing the operator towards
    // `--force`, which then also disables the checks that are real.
    //
    // Both helpers preserve byte offsets and line breaks, so `start`, `end` and
    // `focus` below still index the original `sql`, and the two copies can be
    // used interchangeably by offset.
    //
    // `parse_statement_ops` takes both: the regex battery runs on the blanked
    // text, while `parse_add_columns` gets the literals intact. That tokenizer
    // is already literal-aware — it classifies `'…'` as a single string token,
    // so it cannot be fooled — and it is the one branch that captures a value a
    // literal may legitimately *be*: `default_expr` for
    // `ADD COLUMN c text DEFAULT 'x'`. Blanking that would corrupt a public
    // field of `DdlOperation`.
    let stripped = strip_comments(sql);
    let blanked = blank_string_literals(&stripped);
    let mut ops = Vec::new();

    for (start, end) in statement_ranges(&blanked) {
        let stmt = &blanked[start..end];
        let parsed = parse_statement_ops(stmt, &stripped[start..end]);
        if parsed.is_empty() {
            // Unrecognized statement — preview the original text so comments
            // written inside the statement still show up verbatim.
            let raw = &sql[start..end];
            let preview: String = raw.chars().take(80).collect();
            let preview = if raw.len() > 80 {
                format!("{}...", preview)
            } else {
                preview
            };
            ops.push(LocatedDdl {
                op: DdlOperation::Other {
                    statement_preview: preview,
                },
                start,
                end,
                focus: start,
            });
            continue;
        }
        for (op, offset) in parsed {
            ops.push(LocatedDdl {
                op,
                start,
                end,
                focus: start + offset,
            });
        }
    }

    ops
}

/// Parse a single comment-free statement into zero or more DDL operations.
///
/// Each operation is paired with the byte offset (relative to `stmt`) of the
/// token it should be reported against.
/// Classify one statement.
///
/// `stmt` has both comments and string-literal contents blanked and is what the
/// unanchored regex patterns below match against. `stmt_with_literals` has only
/// comments blanked, at identical byte offsets, and is used by the ADD COLUMN
/// tokenizer so a `DEFAULT 'x'` survives verbatim.
fn parse_statement_ops(stmt: &str, stmt_with_literals: &str) -> Vec<(DdlOperation, usize)> {
    // Order matters — more specific patterns first

    // ALTER TABLE ... ADD CONSTRAINT (before ADD COLUMN)
    if let Some(caps) = ADD_CONSTRAINT_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        let constraint_type = caps.get(3).unwrap().as_str().to_uppercase();
        return vec![(
            DdlOperation::AddConstraint {
                table,
                constraint_type,
            },
            caps.get(0).unwrap().start(),
        )];
    }

    // ALTER TABLE ... DROP CONSTRAINT (before DROP COLUMN)
    if let Some(caps) = DROP_CONSTRAINT_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        let name = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::DropConstraint { table, name },
            caps.get(0).unwrap().start(),
        )];
    }

    // ALTER TABLE ... ALTER COLUMN (before ADD/DROP COLUMN)
    if let Some(caps) = ALTER_TABLE_ALTER_COLUMN_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        let column = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::AlterTableAlterColumn { table, column },
            caps.get(0).unwrap().start(),
        )];
    }

    // ALTER TABLE ... DROP COLUMN
    if let Some(caps) = ALTER_TABLE_DROP_COLUMN_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        let column = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::AlterTableDropColumn { table, column },
            caps.get(0).unwrap().start(),
        )];
    }

    // ALTER TABLE ... ADD [COLUMN] [IF NOT EXISTS] <column> <type> [constraints]
    if let Some((table, clauses)) = parse_add_columns(stmt_with_literals) {
        return clauses
            .into_iter()
            .map(|c| {
                (
                    DdlOperation::AlterTableAddColumn {
                        table: table.clone(),
                        column: c.column,
                        data_type: c.data_type,
                        has_default: c.has_default,
                        is_not_null: c.is_not_null,
                        if_not_exists: c.if_not_exists,
                        default_expr: c.default_expr,
                    },
                    c.column_offset,
                )
            })
            .collect();
    }

    // CREATE TABLE
    if let Some(caps) = CREATE_TABLE_RE.captures(stmt) {
        let if_not_exists = caps.get(1).is_some();
        let table = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::CreateTable {
                table,
                if_not_exists,
            },
            caps.get(0).unwrap().start(),
        )];
    }

    // DROP TABLE
    if let Some(caps) = DROP_TABLE_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::DropTable { table },
            caps.get(0).unwrap().start(),
        )];
    }

    // CREATE INDEX
    if let Some(caps) = CREATE_INDEX_RE.captures(stmt) {
        let is_unique = caps.get(1).is_some();
        let is_concurrent = caps.get(2).is_some();
        let name = caps.get(3).unwrap().as_str().to_string();
        let table = caps.get(5).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::CreateIndex {
                name,
                table,
                is_concurrent,
                is_unique,
            },
            caps.get(0).unwrap().start(),
        )];
    }

    // DROP INDEX
    if let Some(caps) = DROP_INDEX_RE.captures(stmt) {
        let name = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::DropIndex { name },
            caps.get(0).unwrap().start(),
        )];
    }

    // CREATE [MATERIALIZED] VIEW
    if let Some(caps) = CREATE_VIEW_RE.captures(stmt) {
        let is_materialized = caps.get(1).is_some();
        let name = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::CreateView {
                name,
                is_materialized,
            },
            caps.get(0).unwrap().start(),
        )];
    }

    // DROP VIEW
    if let Some(caps) = DROP_VIEW_RE.captures(stmt) {
        let name = caps.get(3).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::DropView { name },
            caps.get(0).unwrap().start(),
        )];
    }

    // CREATE FUNCTION
    if let Some(caps) = CREATE_FUNCTION_RE.captures(stmt) {
        let name = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::CreateFunction { name },
            caps.get(0).unwrap().start(),
        )];
    }

    // DROP FUNCTION
    if let Some(caps) = DROP_FUNCTION_RE.captures(stmt) {
        let name = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::DropFunction { name },
            caps.get(0).unwrap().start(),
        )];
    }

    // CREATE TYPE ... AS ENUM
    if let Some(caps) = CREATE_ENUM_RE.captures(stmt) {
        let name = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::CreateEnum { name },
            caps.get(0).unwrap().start(),
        )];
    }

    // TRUNCATE
    if let Some(caps) = TRUNCATE_RE.captures(stmt) {
        let table = caps.get(2).unwrap().as_str().to_string();
        return vec![(
            DdlOperation::TruncateTable { table },
            caps.get(0).unwrap().start(),
        )];
    }

    Vec::new()
}

// ---------------------------------------------------------------------------
// ALTER TABLE ... ADD COLUMN parsing
// ---------------------------------------------------------------------------

/// A single `ADD [COLUMN] ...` clause parsed out of an `ALTER TABLE`.
#[derive(Debug, Clone)]
struct AddColumnClause {
    column: String,
    /// Byte offset of the column-name token within the statement.
    column_offset: usize,
    data_type: String,
    if_not_exists: bool,
    is_not_null: bool,
    has_default: bool,
    default_expr: Option<String>,
}

/// Keywords that terminate the data type and begin the constraint list of a
/// column definition. `CHARACTER` is deliberately absent — `CHARACTER
/// VARYING` and MySQL's `CHARACTER SET ...` both belong to the type.
const COLUMN_CONSTRAINT_KEYWORDS: &[&str] = &[
    "NOT",
    "NULL",
    "DEFAULT",
    "CHECK",
    "UNIQUE",
    "PRIMARY",
    "REFERENCES",
    "CONSTRAINT",
    "GENERATED",
    "COLLATE",
    "DEFERRABLE",
    "INITIALLY",
    "COMMENT",
    "AUTO_INCREMENT",
    "IDENTITY",
    "STORAGE",
    "COMPRESSION",
    "VISIBLE",
    "INVISIBLE",
    "FIRST",
    "AFTER",
];

/// Keywords that mean the token after `ADD` starts a table constraint rather
/// than a column definition.
const TABLE_CONSTRAINT_KEYWORDS: &[&str] = &[
    "CONSTRAINT",
    "PRIMARY",
    "UNIQUE",
    "FOREIGN",
    "CHECK",
    "EXCLUDE",
    "INDEX",
    "KEY",
    "FULLTEXT",
    "SPATIAL",
];

/// Parse every `ADD [COLUMN]` clause of an `ALTER TABLE` statement.
///
/// Handles the optional `COLUMN` keyword, the optional `IF NOT EXISTS`
/// clause, schema-qualified and quoted identifiers, parenthesised types, and
/// comma-separated clauses. `NOT NULL` and `DEFAULT` are only recognised at
/// the top level of the column definition, so they are never picked up from
/// inside a `CHECK (...)` expression or a string literal.
///
/// Returns `None` when the statement is not an `ALTER TABLE ... ADD <column>`.
fn parse_add_columns(stmt: &str) -> Option<(String, Vec<AddColumnClause>)> {
    let toks = tokenize(stmt);

    // Locate `ALTER TABLE`.
    let mut i = toks
        .windows(2)
        .position(|w| is_kw(&w[0], "ALTER") && is_kw(&w[1], "TABLE"))?
        + 2;

    // Optional PostgreSQL `ONLY` / `IF EXISTS` decorations.
    if kw_at(&toks, i, "IF") && kw_at(&toks, i + 1, "EXISTS") {
        i += 2;
    }
    if kw_at(&toks, i, "ONLY") {
        i += 1;
    }

    // Qualified table name: ident ('.' ident)*
    let mut name_parts: Vec<&str> = Vec::new();
    loop {
        let t = toks.get(i)?;
        if !is_identifier(t) {
            return None;
        }
        name_parts.push(t.text);
        i += 1;
        match toks.get(i) {
            Some(t) if t.kind == TokKind::Punct && t.text == "." => i += 1,
            _ => break,
        }
    }
    // PostgreSQL legacy inheritance marker: `ALTER TABLE parent * ...`
    if toks
        .get(i)
        .is_some_and(|t| t.kind == TokKind::Punct && t.text == "*")
    {
        i += 1;
    }
    let table = (*name_parts.last()?).to_string();

    // Walk the action list, picking up every top-level `ADD` clause.
    let mut clauses = Vec::new();
    let mut depth = 0usize;
    while i < toks.len() {
        let t = &toks[i];
        if t.kind == TokKind::Punct {
            match t.text {
                "(" => depth += 1,
                ")" => depth = depth.saturating_sub(1),
                _ => {}
            }
            i += 1;
            continue;
        }
        if depth == 0
            && is_kw(t, "ADD")
            && let Some((clause, next)) = parse_one_add_column(stmt, &toks, i + 1)
        {
            clauses.push(clause);
            i = next;
            continue;
        }
        i += 1;
    }

    if clauses.is_empty() {
        None
    } else {
        Some((table, clauses))
    }
}

/// Parse one `ADD [COLUMN] [IF NOT EXISTS] <column> <type> [constraints]`
/// clause starting at token index `i` (the token just after `ADD`).
///
/// Returns the clause plus the index of the first token after it.
fn parse_one_add_column<'a>(
    stmt: &'a str,
    toks: &[Tok<'a>],
    mut i: usize,
) -> Option<(AddColumnClause, usize)> {
    if kw_at(toks, i, "COLUMN") {
        i += 1;
    }

    let mut if_not_exists = false;
    if kw_at(toks, i, "IF") && kw_at(toks, i + 1, "NOT") && kw_at(toks, i + 2, "EXISTS") {
        if_not_exists = true;
        i += 3;
    }

    let col = toks.get(i)?;
    if !is_identifier(col) {
        return None;
    }
    // `ADD CONSTRAINT ...`, `ADD PRIMARY KEY ...` etc. are not columns.
    if col.kind == TokKind::Word
        && TABLE_CONSTRAINT_KEYWORDS
            .iter()
            .any(|k| col.text.eq_ignore_ascii_case(k))
    {
        return None;
    }
    let column = col.text.to_string();
    let column_offset = col.start;
    i += 1;

    // Data type: everything up to the first top-level constraint keyword,
    // clause-terminating comma, or end of statement.
    let type_first = i;
    let mut depth = 0usize;
    while i < toks.len() {
        let t = &toks[i];
        if t.kind == TokKind::Punct {
            match t.text {
                "(" => depth += 1,
                ")" => {
                    if depth == 0 {
                        break;
                    }
                    depth -= 1;
                }
                "," if depth == 0 => break,
                _ => {}
            }
            i += 1;
            continue;
        }
        if depth == 0
            && t.kind == TokKind::Word
            && COLUMN_CONSTRAINT_KEYWORDS
                .iter()
                .any(|k| t.text.eq_ignore_ascii_case(k))
        {
            break;
        }
        i += 1;
    }
    let data_type = if i > type_first {
        normalize_whitespace(&stmt[toks[type_first].start..toks[i - 1].end])
    } else {
        "unknown".to_string()
    };

    // Constraint list: only top-level tokens count.
    let mut is_not_null = false;
    let mut default_expr = None;
    let mut depth = 0usize;
    while i < toks.len() {
        let t = &toks[i];
        if t.kind == TokKind::Punct {
            match t.text {
                "(" => depth += 1,
                ")" => {
                    if depth == 0 {
                        break;
                    }
                    depth -= 1;
                }
                "," if depth == 0 => {
                    i += 1;
                    break;
                }
                _ => {}
            }
            i += 1;
            continue;
        }
        if depth == 0 && t.kind == TokKind::Word {
            if is_kw(t, "NOT") && kw_at(toks, i + 1, "NULL") {
                is_not_null = true;
                i += 2;
                continue;
            }
            if is_kw(t, "DEFAULT") {
                let (expr, next) = read_default_expr(stmt, toks, i + 1);
                default_expr = Some(expr);
                i = next;
                continue;
            }
        }
        i += 1;
    }

    Some((
        AddColumnClause {
            column,
            column_offset,
            data_type,
            if_not_exists,
            is_not_null,
            has_default: default_expr.is_some(),
            default_expr,
        },
        i,
    ))
}

/// Read the expression following a top-level `DEFAULT`, stopping at the next
/// constraint keyword or the end of the column definition.
///
/// Returns the expression text and the index of the token after it. The first
/// token is always consumed so that `DEFAULT NULL` keeps its value.
fn read_default_expr(stmt: &str, toks: &[Tok<'_>], start: usize) -> (String, usize) {
    let mut i = start;
    let mut depth = 0usize;
    while i < toks.len() {
        let t = &toks[i];
        if t.kind == TokKind::Punct {
            match t.text {
                "(" => depth += 1,
                ")" => {
                    if depth == 0 {
                        break;
                    }
                    depth -= 1;
                }
                "," if depth == 0 => break,
                _ => {}
            }
            i += 1;
            continue;
        }
        if depth == 0
            && i > start
            && t.kind == TokKind::Word
            && COLUMN_CONSTRAINT_KEYWORDS
                .iter()
                .any(|k| t.text.eq_ignore_ascii_case(k))
        {
            break;
        }
        i += 1;
    }

    let expr = if i > start {
        normalize_whitespace(&stmt[toks[start].start..toks[i - 1].end])
    } else {
        String::new()
    };
    (expr, i)
}

/// Collapse runs of whitespace (including newlines) into single spaces.
fn normalize_whitespace(s: &str) -> String {
    s.split_whitespace().collect::<Vec<_>>().join(" ")
}

// ---------------------------------------------------------------------------
// Tokenizer
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TokKind {
    /// A bare word: keyword, unquoted identifier, or number.
    Word,
    /// A quoted identifier (`"col"` or `` `col` ``); `text` excludes the quotes.
    Ident,
    /// A string literal; `text` includes the quotes.
    Literal,
    /// A single punctuation character.
    Punct,
}

#[derive(Debug, Clone, Copy)]
struct Tok<'a> {
    kind: TokKind,
    text: &'a str,
    start: usize,
    end: usize,
}

fn is_kw(tok: &Tok<'_>, kw: &str) -> bool {
    tok.kind == TokKind::Word && tok.text.eq_ignore_ascii_case(kw)
}

fn kw_at(toks: &[Tok<'_>], i: usize, kw: &str) -> bool {
    toks.get(i).is_some_and(|t| is_kw(t, kw))
}

/// Whether a token can stand in for an identifier (bare word or quoted).
fn is_identifier(tok: &Tok<'_>) -> bool {
    match tok.kind {
        TokKind::Ident => true,
        TokKind::Word => tok
            .text
            .starts_with(|c: char| c.is_alphabetic() || c == '_'),
        _ => false,
    }
}

/// Split SQL into tokens. Comments and whitespace are skipped.
fn tokenize(sql: &str) -> Vec<Tok<'_>> {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    let mut toks = Vec::new();
    let mut i = 0;

    while i < len {
        let c = bytes[i];
        if c.is_ascii_whitespace() {
            i += 1;
            continue;
        }
        if let Some(j) = skip_comment(bytes, i) {
            i = j;
            continue;
        }
        if c == b'\'' {
            let j = skip_quoted(sql, i).unwrap_or(len);
            toks.push(Tok {
                kind: TokKind::Literal,
                text: &sql[i..j],
                start: i,
                end: j,
            });
            i = j;
            continue;
        }
        if c == b'"' || c == b'`' {
            let mut j = i + 1;
            while j < len {
                if bytes[j] == c {
                    if j + 1 < len && bytes[j + 1] == c {
                        j += 2;
                        continue;
                    }
                    break;
                }
                j += 1;
            }
            let inner_end = j.min(len);
            let end = (j + 1).min(len);
            toks.push(Tok {
                kind: TokKind::Ident,
                text: &sql[i + 1..inner_end],
                start: i,
                end,
            });
            i = end;
            continue;
        }
        if c.is_ascii_alphanumeric() || c == b'_' || c >= 0x80 {
            let mut j = i;
            while j < len
                && (bytes[j].is_ascii_alphanumeric()
                    || bytes[j] == b'_'
                    || bytes[j] == b'$'
                    || bytes[j] >= 0x80)
            {
                j += 1;
            }
            toks.push(Tok {
                kind: TokKind::Word,
                text: &sql[i..j],
                start: i,
                end: j,
            });
            i = j;
            continue;
        }
        toks.push(Tok {
            kind: TokKind::Punct,
            text: &sql[i..i + 1],
            start: i,
            end: i + 1,
        });
        i += 1;
    }

    toks
}

/// Split SQL into individual statements, respecting dollar-quoted blocks,
/// string literals, quoted identifiers, and comments.
pub fn split_statements(sql: &str) -> Vec<&str> {
    statement_ranges(sql)
        .into_iter()
        .map(|(s, e)| &sql[s..e])
        .collect()
}

/// Byte ranges of the individual statements in `sql`, each trimmed of
/// surrounding whitespace. Empty statements are skipped.
fn statement_ranges(sql: &str) -> Vec<(usize, usize)> {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    let mut ranges = Vec::new();
    let mut start = 0;
    let mut i = 0;

    while i < len {
        if let Some(j) = skip_comment(bytes, i) {
            i = j;
            continue;
        }
        if let Some(j) = skip_quoted(sql, i) {
            i = j;
            continue;
        }
        if bytes[i] == b';' {
            if let Some(r) = trim_range(sql, start, i) {
                ranges.push(r);
            }
            i += 1;
            start = i;
            continue;
        }
        i += 1;
    }

    // Remainder after the last semicolon
    if let Some(r) = trim_range(sql, start, len) {
        ranges.push(r);
    }

    ranges
}

/// Narrow `start..end` to the non-whitespace content it contains, or `None`
/// if it is entirely whitespace.
fn trim_range(sql: &str, start: usize, end: usize) -> Option<(usize, usize)> {
    let slice = &sql[start..end];
    if slice.trim().is_empty() {
        return None;
    }
    let lead = slice.len() - slice.trim_start().len();
    let trail = slice.len() - slice.trim_end().len();
    Some((start + lead, end - trail))
}

/// If a comment starts at `i`, return the offset just past it.
///
/// Handles `-- line` comments (terminating before the newline) and nested
/// `/* block */` comments.
fn skip_comment(bytes: &[u8], i: usize) -> Option<usize> {
    let len = bytes.len();
    if bytes[i] == b'-' && i + 1 < len && bytes[i + 1] == b'-' {
        let mut j = i + 2;
        while j < len && bytes[j] != b'\n' {
            j += 1;
        }
        return Some(j);
    }
    if bytes[i] == b'/' && i + 1 < len && bytes[i + 1] == b'*' {
        let mut j = i + 2;
        let mut depth = 1usize;
        while j < len && depth > 0 {
            if j + 1 < len && bytes[j] == b'/' && bytes[j + 1] == b'*' {
                depth += 1;
                j += 2;
            } else if j + 1 < len && bytes[j] == b'*' && bytes[j + 1] == b'/' {
                depth -= 1;
                j += 2;
            } else {
                j += 1;
            }
        }
        return Some(j.min(len));
    }
    None
}

/// If a quoted region starts at `i`, return the offset just past it.
///
/// Covers string literals (including `E'...'` escape strings and doubled-quote
/// escapes), double-quoted / backtick-quoted identifiers, and dollar-quoted
/// blocks.
fn skip_quoted(sql: &str, i: usize) -> Option<usize> {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    match bytes[i] {
        b'\'' => {
            // E'...' escape strings honour backslash escapes.
            let is_escape_string = i > 0
                && (bytes[i - 1] == b'E' || bytes[i - 1] == b'e')
                && (i < 2 || !(bytes[i - 2].is_ascii_alphanumeric() || bytes[i - 2] == b'_'));
            let mut j = i + 1;
            while j < len {
                if is_escape_string && bytes[j] == b'\\' {
                    j += 2;
                    continue;
                }
                if bytes[j] == b'\'' {
                    if j + 1 < len && bytes[j + 1] == b'\'' {
                        j += 2; // doubled-quote escape
                    } else {
                        j += 1;
                        break;
                    }
                } else {
                    j += 1;
                }
            }
            Some(j.min(len))
        }
        q @ (b'"' | b'`') => {
            let mut j = i + 1;
            while j < len {
                if bytes[j] == q {
                    if j + 1 < len && bytes[j + 1] == q {
                        j += 2; // doubled-quote escape
                        continue;
                    }
                    j += 1;
                    break;
                }
                j += 1;
            }
            Some(j.min(len))
        }
        // Dollar-quoted string ($$...$$, $tag$...$tag$)
        b'$' => {
            let tag_start = i;
            let mut j = i + 1;
            while j < len && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_') {
                j += 1;
            }
            if j < len && bytes[j] == b'$' {
                let tag = &sql[tag_start..=j];
                j += 1;
                while j < len {
                    if bytes[j] == b'$' && sql[j..].starts_with(tag) {
                        j += tag.len();
                        break;
                    }
                    j += 1;
                }
            }
            Some(j.min(len))
        }
        _ => None,
    }
}

/// Blank out the *contents* of every string literal, preserving byte offsets,
/// line breaks and the surrounding quotes.
///
/// [`strip_comments`] skips over quoted regions but leaves their text in place,
/// so a keyword scan run on its output still sees words that are data rather
/// than SQL — `INSERT INTO runbook VALUES ('nightly VACUUM of orders')` read as
/// a `VACUUM` statement. Blanking the interior keeps every offset and line
/// number intact, so diagnostics still point at the original source.
///
/// Quoted *identifiers* (`"..."`, `` `...` ``) are deliberately left alone:
/// they name real objects and callers need to read them.
pub fn blank_string_literals(sql: &str) -> String {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    let mut out = bytes.to_vec();
    let mut i = 0;

    while i < len {
        // Comments first: an apostrophe inside a comment must not open a
        // literal (`-- don't do this`).
        if let Some(j) = skip_comment(bytes, i) {
            i = j;
            continue;
        }
        if let Some(j) = skip_quoted(sql, i) {
            if bytes[i] == b'\'' {
                // Keep the delimiters, blank what is between them.
                let inner_end = j.saturating_sub(1).max(i + 1);
                for b in &mut out[i + 1..inner_end.min(len)] {
                    if *b != b'\n' {
                        *b = b' ';
                    }
                }
            }
            i = j;
            continue;
        }
        i += 1;
    }

    // Only ASCII bytes inside literals were replaced with ASCII spaces, so a
    // multi-byte character cannot be split; the result is still valid UTF-8.
    String::from_utf8(out).unwrap_or_else(|_| sql.to_string())
}

/// Blank out every comment in `sql`, preserving byte offsets and line breaks.
///
/// Comment bytes become spaces (newlines are kept) so the result has exactly
/// the same length and line structure as the input. This lets semantic
/// analysis run on comment-free SQL while diagnostics still resolve to the
/// original source position.
pub fn strip_comments(sql: &str) -> String {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    let mut out = bytes.to_vec();
    let mut i = 0;

    while i < len {
        if let Some(j) = skip_comment(bytes, i) {
            for b in &mut out[i..j] {
                if *b != b'\n' {
                    *b = b' ';
                }
            }
            i = j;
            continue;
        }
        if let Some(j) = skip_quoted(sql, i) {
            i = j;
            continue;
        }
        i += 1;
    }

    // Only ASCII comment bytes were replaced with ASCII spaces, so the result
    // is still valid UTF-8.
    String::from_utf8(out).unwrap_or_else(|_| sql.to_string())
}

/// The 1-based line number containing the given byte offset.
pub fn line_number_at(sql: &str, offset: usize) -> usize {
    sql[..offset.min(sql.len())]
        .bytes()
        .filter(|b| *b == b'\n')
        .count()
        + 1
}

/// Split MySQL SQL into individual statements at top-level `;` terminators.
///
/// Respects single-quoted strings, double-quoted strings, backtick-quoted
/// identifiers, single-line `--` comments, and `/* ... */` block comments.
/// Does **not** handle MySQL's `DELIMITER //` blocks — stored-procedure DDL
/// that needs an alternate delimiter must be split by the caller (or
/// re-written without DELIMITER, which works for most ALTER/CREATE patterns).
///
/// Returns owned `String`s rather than borrowed slices so callers can pass
/// them directly to `mysql_async::query_drop` without lifetime gymnastics.
pub fn split_mysql_statements(sql: &str) -> Vec<String> {
    let bytes = sql.as_bytes();
    let len = bytes.len();
    let mut out = Vec::new();
    let mut start = 0;
    let mut i = 0;
    while i < len {
        let c = bytes[i];
        // Line comment
        if c == b'-' && i + 1 < len && bytes[i + 1] == b'-' {
            while i < len && bytes[i] != b'\n' {
                i += 1;
            }
            continue;
        }
        // Block comment
        if c == b'/' && i + 1 < len && bytes[i + 1] == b'*' {
            i += 2;
            while i + 1 < len && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
                i += 1;
            }
            i = (i + 2).min(len);
            continue;
        }
        // Single-quoted string
        if c == b'\'' {
            i += 1;
            while i < len && bytes[i] != b'\'' {
                if bytes[i] == b'\\' && i + 1 < len {
                    i += 2;
                } else {
                    i += 1;
                }
            }
            i += 1;
            continue;
        }
        // Double-quoted string
        if c == b'"' {
            i += 1;
            while i < len && bytes[i] != b'"' {
                if bytes[i] == b'\\' && i + 1 < len {
                    i += 2;
                } else {
                    i += 1;
                }
            }
            i += 1;
            continue;
        }
        // Backtick-quoted identifier
        if c == b'`' {
            i += 1;
            while i < len && bytes[i] != b'`' {
                i += 1;
            }
            i += 1;
            continue;
        }
        // Statement terminator
        if c == b';' {
            push_mysql_statement(&mut out, &sql[start..i]);
            i += 1;
            start = i;
            continue;
        }
        i += 1;
    }
    push_mysql_statement(&mut out, &sql[start..]);
    out
}

/// Trim a candidate statement and push it only if it carries something the
/// server can execute.
///
/// MySQL rejects an empty or comment-only query with `ER_EMPTY_QUERY (1065)`,
/// so a file ending in a trailing comment (`... ; -- done`) or containing a
/// stray `;;` must not produce a statement here. We check for executable
/// content by blanking comments and seeing whether anything remains.
fn push_mysql_statement(out: &mut Vec<String>, candidate: &str) {
    let trimmed = candidate.trim();
    if trimmed.is_empty() {
        return;
    }
    if strip_comments(trimmed).trim().is_empty() {
        return;
    }
    out.push(trimmed.to_string());
}

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

    #[test]
    fn test_split_simple_statements() {
        let sql = "SELECT 1; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts, vec!["SELECT 1", "SELECT 2"]);
    }

    #[test]
    fn test_split_respects_string_literals() {
        let sql = "SELECT 'hello;world'; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts, vec!["SELECT 'hello;world'", "SELECT 2"]);
    }

    #[test]
    fn test_split_respects_dollar_quoting() {
        let sql =
            "CREATE FUNCTION foo() RETURNS void AS $$ BEGIN; END; $$ LANGUAGE plpgsql; SELECT 1;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains("BEGIN; END;"));
    }

    #[test]
    fn test_split_respects_tagged_dollar_quoting() {
        let sql = "CREATE FUNCTION foo() RETURNS void AS $body$ BEGIN; END; $body$ LANGUAGE plpgsql; SELECT 1;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains("BEGIN; END;"));
    }

    #[test]
    fn test_split_respects_comments() {
        let sql = "-- This is a comment with ; semicolon\nSELECT 1;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 1);
    }

    #[test]
    fn test_split_no_trailing_semicolon() {
        let sql = "SELECT 1";
        let stmts = split_statements(sql);
        assert_eq!(stmts, vec!["SELECT 1"]);
    }

    #[test]
    fn test_extract_create_table() {
        let sql = "CREATE TABLE users (id SERIAL PRIMARY KEY);";
        let ops = extract_ddl_operations(sql);
        assert_eq!(ops.len(), 1);
        match &ops[0] {
            DdlOperation::CreateTable {
                table,
                if_not_exists,
            } => {
                assert_eq!(table, "users");
                assert!(!if_not_exists);
            }
            _ => panic!("Expected CreateTable"),
        }
    }

    #[test]
    fn test_extract_create_table_if_not_exists() {
        let sql = "CREATE TABLE IF NOT EXISTS users (id SERIAL);";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::CreateTable {
                table,
                if_not_exists,
            } => {
                assert_eq!(table, "users");
                assert!(if_not_exists);
            }
            _ => panic!("Expected CreateTable"),
        }
    }

    #[test]
    fn test_extract_add_column() {
        let sql = "ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL DEFAULT '';";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::AlterTableAddColumn {
                table,
                column,
                is_not_null,
                has_default,
                ..
            } => {
                assert_eq!(table, "users");
                assert_eq!(column, "email");
                assert!(is_not_null);
                assert!(has_default);
            }
            _ => panic!("Expected AlterTableAddColumn"),
        }
    }

    /// Convenience: the single AddColumn op parsed out of `sql`.
    fn add_column(sql: &str) -> DdlOperation {
        let ops = extract_ddl_operations(sql);
        assert_eq!(ops.len(), 1, "expected exactly one op, got {:?}", ops);
        ops.into_iter().next().unwrap()
    }

    #[test]
    fn test_add_column_if_not_exists_names_the_column() {
        match add_column(
            "ALTER TABLE dicom.reid_shares ADD COLUMN IF NOT EXISTS threshold smallint;",
        ) {
            DdlOperation::AlterTableAddColumn {
                table,
                column,
                data_type,
                is_not_null,
                has_default,
                if_not_exists,
                default_expr,
            } => {
                assert_eq!(table, "reid_shares");
                assert_eq!(column, "threshold");
                assert_eq!(data_type, "smallint");
                assert!(default_expr.is_none());
                assert!(!is_not_null);
                assert!(!has_default);
                assert!(if_not_exists);
            }
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_ignores_not_null_in_comments() {
        let sql = "-- Every ceremony writes the threshold NOT NULL.\n\
                   ALTER TABLE dicom.reid_shares\n  \
                     ADD COLUMN IF NOT EXISTS threshold smallint\n    \
                       CHECK (threshold IS NULL OR threshold BETWEEN 1 AND 255);";
        match add_column(sql) {
            DdlOperation::AlterTableAddColumn {
                column,
                is_not_null,
                ..
            } => {
                assert_eq!(column, "threshold");
                assert!(!is_not_null, "NOT NULL came from a comment");
            }
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_ignores_not_null_inside_check() {
        match add_column("ALTER TABLE t ADD COLUMN c text CHECK (c IS NOT NULL);") {
            DdlOperation::AlterTableAddColumn { is_not_null, .. } => assert!(!is_not_null),
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_ignores_keywords_inside_string_literals() {
        match add_column("ALTER TABLE t ADD COLUMN c text DEFAULT 'NOT NULL';") {
            DdlOperation::AlterTableAddColumn {
                is_not_null,
                has_default,
                ..
            } => {
                assert!(!is_not_null);
                assert!(has_default);
            }
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_quoted_and_parenthesised_type() {
        match add_column(
            r#"ALTER TABLE "my schema"."my table" ADD "my col" numeric(10,2) NOT NULL;"#,
        ) {
            DdlOperation::AlterTableAddColumn {
                table,
                column,
                data_type,
                is_not_null,
                ..
            } => {
                assert_eq!(table, "my table");
                assert_eq!(column, "my col");
                assert_eq!(data_type, "numeric(10,2)");
                assert!(is_not_null);
            }
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_multiword_type() {
        match add_column("ALTER TABLE t ADD COLUMN c timestamp with time zone NOT NULL;") {
            DdlOperation::AlterTableAddColumn {
                data_type,
                is_not_null,
                ..
            } => {
                assert_eq!(data_type, "timestamp with time zone");
                assert!(is_not_null);
            }
            other => panic!("Expected AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_multiple_columns_in_one_statement() {
        let ops = extract_ddl_operations(
            "ALTER TABLE t ADD COLUMN a int, ADD COLUMN IF NOT EXISTS b text NOT NULL;",
        );
        assert_eq!(ops.len(), 2);
        match (&ops[0], &ops[1]) {
            (
                DdlOperation::AlterTableAddColumn {
                    column: c1,
                    is_not_null: n1,
                    ..
                },
                DdlOperation::AlterTableAddColumn {
                    column: c2,
                    is_not_null: n2,
                    if_not_exists,
                    ..
                },
            ) => {
                assert_eq!(c1, "a");
                assert!(!n1);
                assert_eq!(c2, "b");
                assert!(n2);
                assert!(if_not_exists);
            }
            other => panic!("Expected two AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_column_captures_default_expression() {
        for (sql, expected) in [
            (
                "ALTER TABLE t ADD COLUMN c timestamptz DEFAULT now();",
                "now()",
            ),
            ("ALTER TABLE t ADD COLUMN c int NOT NULL DEFAULT 5;", "5"),
            (
                "ALTER TABLE t ADD COLUMN c text DEFAULT 'x' NOT NULL;",
                "'x'",
            ),
            ("ALTER TABLE t ADD COLUMN c text DEFAULT NULL;", "NULL"),
            (
                "ALTER TABLE t ADD COLUMN c text[] DEFAULT '{}' CHECK (c IS NOT NULL);",
                "'{}'",
            ),
        ] {
            match add_column(sql) {
                DdlOperation::AlterTableAddColumn { default_expr, .. } => {
                    assert_eq!(default_expr.as_deref(), Some(expected), "for {}", sql)
                }
                other => panic!("Expected AlterTableAddColumn, got {:?}", other),
            }
        }
    }

    #[test]
    fn test_add_column_default_is_per_clause() {
        let ops = extract_ddl_operations(
            "ALTER TABLE t ADD COLUMN a text[] DEFAULT '{}', ADD COLUMN b timestamptz NOT NULL DEFAULT now();",
        );
        assert_eq!(ops.len(), 2);
        match (&ops[0], &ops[1]) {
            (
                DdlOperation::AlterTableAddColumn {
                    default_expr: a, ..
                },
                DdlOperation::AlterTableAddColumn {
                    default_expr: b, ..
                },
            ) => {
                assert_eq!(a.as_deref(), Some("'{}'"));
                assert_eq!(b.as_deref(), Some("now()"));
            }
            other => panic!("Expected two AlterTableAddColumn, got {:?}", other),
        }
    }

    #[test]
    fn test_add_constraint_is_not_parsed_as_a_column() {
        let ops = extract_ddl_operations("ALTER TABLE t ADD CONSTRAINT t_pk PRIMARY KEY (id);");
        assert!(matches!(ops[0], DdlOperation::AddConstraint { .. }));
    }

    #[test]
    fn test_located_ops_point_at_the_column() {
        let sql =
            "-- comment with NOT NULL\n-- another\nALTER TABLE t\n  ADD COLUMN c int NOT NULL;";
        let located = extract_ddl_operations_located(sql);
        assert_eq!(located.len(), 1);
        // Statement starts on line 3, the column token is on line 4.
        assert_eq!(line_number_at(sql, located[0].start), 3);
        assert_eq!(line_number_at(sql, located[0].focus), 4);
        assert_eq!(&sql[located[0].focus..located[0].focus + 1], "c");
    }

    #[test]
    fn test_strip_comments_preserves_offsets_and_lines() {
        let sql = "-- NOT NULL\nSELECT 1; /* NOT NULL */\nSELECT 'not -- a comment';";
        let stripped = strip_comments(sql);
        assert_eq!(stripped.len(), sql.len());
        assert_eq!(stripped.lines().count(), sql.lines().count());
        assert!(!stripped.to_uppercase().contains("NOT NULL"));
        assert!(stripped.contains("'not -- a comment'"));
    }

    #[test]
    fn test_line_number_at_is_one_based() {
        let sql = "a\nb\nc";
        assert_eq!(line_number_at(sql, 0), 1);
        assert_eq!(line_number_at(sql, 2), 2);
        assert_eq!(line_number_at(sql, 4), 3);
    }

    #[test]
    fn test_extract_create_index() {
        let sql = "CREATE UNIQUE INDEX CONCURRENTLY idx_users_email ON users (email);";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::CreateIndex {
                name,
                table,
                is_concurrent,
                is_unique,
            } => {
                assert_eq!(name, "idx_users_email");
                assert_eq!(table, "users");
                assert!(is_concurrent);
                assert!(is_unique);
            }
            _ => panic!("Expected CreateIndex"),
        }
    }

    #[test]
    fn test_extract_create_function() {
        let sql = "CREATE OR REPLACE FUNCTION my_func() RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::CreateFunction { name } => {
                assert_eq!(name, "my_func");
            }
            _ => panic!("Expected CreateFunction, got {:?}", ops[0]),
        }
    }

    #[test]
    fn test_extract_create_enum() {
        let sql = "CREATE TYPE mood AS ENUM ('happy', 'sad');";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::CreateEnum { name } => {
                assert_eq!(name, "mood");
            }
            _ => panic!("Expected CreateEnum"),
        }
    }

    #[test]
    fn test_extract_multiple() {
        let sql = "CREATE TABLE users (id SERIAL); CREATE INDEX idx_users ON users (id); DROP TABLE old_table;";
        let ops = extract_ddl_operations(sql);
        assert_eq!(ops.len(), 3);
    }

    #[test]
    fn test_extract_truncate() {
        let sql = "TRUNCATE TABLE users;";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::TruncateTable { table } => assert_eq!(table, "users"),
            _ => panic!("Expected TruncateTable"),
        }
    }

    #[test]
    fn test_extract_drop_column() {
        let sql = "ALTER TABLE users DROP COLUMN email;";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::AlterTableDropColumn { table, column } => {
                assert_eq!(table, "users");
                assert_eq!(column, "email");
            }
            _ => panic!("Expected AlterTableDropColumn"),
        }
    }

    #[test]
    fn test_extract_alter_column() {
        let sql = "ALTER TABLE users ALTER COLUMN name TYPE text;";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::AlterTableAlterColumn { table, column } => {
                assert_eq!(table, "users");
                assert_eq!(column, "name");
            }
            _ => panic!("Expected AlterTableAlterColumn"),
        }
    }

    #[test]
    fn test_extract_materialized_view() {
        let sql = "CREATE MATERIALIZED VIEW user_stats AS SELECT count(*) FROM users;";
        let ops = extract_ddl_operations(sql);
        match &ops[0] {
            DdlOperation::CreateView {
                name,
                is_materialized,
            } => {
                assert_eq!(name, "user_stats");
                assert!(is_materialized);
            }
            _ => panic!("Expected CreateView"),
        }
    }

    #[test]
    fn test_block_comment_with_semicolons() {
        let sql = "/* comment; with; semicolons */ SELECT 1;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 1);
    }

    #[test]
    fn test_escaped_string_quotes() {
        let sql = "SELECT 'it''s; here'; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_split_respects_e_escape_strings() {
        let sql = r"SELECT E'hello\';world'; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains(r"E'hello\';world'"));
    }

    #[test]
    fn test_split_e_string_with_backslash() {
        let sql = r"SELECT E'it\'s a test; really'; SELECT 1;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_split_nested_block_comments() {
        let sql = "SELECT /* outer /* inner */ outer */ 1; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert_eq!(stmts[1], "SELECT 2");
    }

    #[test]
    fn test_split_whitespace_only() {
        let stmts = split_statements("   \n\t  ");
        assert!(stmts.is_empty());
    }

    #[test]
    fn test_split_comment_only() {
        let stmts = split_statements("-- just a comment\n");
        assert_eq!(stmts.len(), 1);
        assert_eq!(stmts[0], "-- just a comment");
    }

    #[test]
    fn test_split_mixed_e_and_regular_strings() {
        let sql = r"SELECT 'normal;string', E'escape\';string'; SELECT 2;";
        let stmts = split_statements(sql);
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_split_mysql_basic() {
        let sql = "CREATE TABLE a (id INT); CREATE TABLE b (id INT);";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains("CREATE TABLE a"));
        assert!(stmts[1].contains("CREATE TABLE b"));
    }

    #[test]
    fn test_split_mysql_respects_backticks_with_semicolons() {
        // A backtick-quoted identifier with `;` inside should NOT split.
        let sql = "CREATE TABLE `weird;name` (id INT); CREATE TABLE b (id INT);";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains("`weird;name`"));
    }

    #[test]
    fn test_split_mysql_respects_string_literals_with_semicolons() {
        let sql = "INSERT INTO t VALUES ('a;b'); INSERT INTO t VALUES ('c;d');";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_split_mysql_keeps_leading_comments_with_statement() {
        // The first chunk contains both the comment header and the CREATE TABLE.
        // Splitter doesn't emit comment-only fragments.
        let sql = "-- header comment\nCREATE TABLE a (id INT);\nCREATE TABLE b (id INT);";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 2);
        assert!(stmts[0].contains("CREATE TABLE a"));
    }

    #[test]
    fn test_split_mysql_handles_block_comments() {
        let sql = "/* block ; comment */ CREATE TABLE a (id INT); CREATE TABLE b (id INT);";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 2);
    }

    #[test]
    fn test_split_mysql_no_trailing_semicolon() {
        let sql = "CREATE TABLE a (id INT)";
        let stmts = split_mysql_statements(sql);
        assert_eq!(stmts.len(), 1);
        assert!(stmts[0].contains("CREATE TABLE a"));
    }

    #[test]
    fn test_split_mysql_drops_trailing_comment_only_statement() {
        // A file ending in a comment after the last `;` must not yield a
        // statement — MySQL answers ER_EMPTY_QUERY (1065).
        let sql = "CREATE TABLE t (id INT);\n-- done\n";
        assert_eq!(
            split_mysql_statements(sql),
            vec!["CREATE TABLE t (id INT)".to_string()]
        );
    }

    #[test]
    fn test_split_mysql_drops_empty_statements() {
        let sql = "SELECT 1;; SELECT 2;";
        assert_eq!(
            split_mysql_statements(sql),
            vec!["SELECT 1".to_string(), "SELECT 2".to_string()]
        );
    }

    #[test]
    fn test_split_mysql_trims_every_statement() {
        let sql = "SELECT 1;\n  SELECT 2  ;\n";
        assert_eq!(
            split_mysql_statements(sql),
            vec!["SELECT 1".to_string(), "SELECT 2".to_string()]
        );
    }

    #[test]
    fn test_split_mysql_drops_block_comment_only_statement() {
        let sql = "SELECT 1; /* just a note */ ;";
        assert_eq!(split_mysql_statements(sql), vec!["SELECT 1".to_string()]);
    }

    #[test]
    fn test_split_mysql_keeps_statement_with_leading_comment() {
        let sql = "-- set up\nCREATE TABLE t (id INT);";
        assert_eq!(
            split_mysql_statements(sql),
            vec!["-- set up\nCREATE TABLE t (id INT)".to_string()]
        );
    }

    #[test]
    fn test_blank_string_literals_preserves_offsets_and_quotes() {
        let sql = "INSERT INTO t VALUES ('nightly VACUUM run');";
        let out = blank_string_literals(sql);
        assert_eq!(out.len(), sql.len(), "byte offsets must be preserved");
        assert!(
            !out.contains("VACUUM"),
            "literal contents must be blanked: {out}"
        );
        assert!(
            out.contains("INSERT INTO t VALUES ("),
            "SQL outside the literal is untouched"
        );
        assert_eq!(out.matches('\'').count(), 2, "the quotes themselves stay");
    }

    #[test]
    fn test_blank_string_literals_keeps_line_structure() {
        let sql = "INSERT INTO t VALUES ('line one\nline two');\nSELECT 1;";
        let out = blank_string_literals(sql);
        assert_eq!(out.len(), sql.len());
        assert_eq!(
            out.matches('\n').count(),
            sql.matches('\n').count(),
            "newlines inside literals must survive so line numbers stay right"
        );
    }

    #[test]
    fn test_blank_string_literals_leaves_quoted_identifiers_alone() {
        // Quoted identifiers name real objects; callers need to read them.
        let sql = "CREATE TABLE \"my VACUUM table\" (id int);";
        let out = blank_string_literals(sql);
        assert!(out.contains("my VACUUM table"), "got: {out}");
    }

    #[test]
    fn test_blank_string_literals_handles_doubled_quote_escape() {
        let sql = "SELECT 'it''s VACUUM time', 1;";
        let out = blank_string_literals(sql);
        assert_eq!(out.len(), sql.len());
        assert!(!out.contains("VACUUM"), "got: {out}");
        assert!(
            out.trim_end().ends_with(", 1;"),
            "parsing resumed too early: {out}"
        );
    }

    #[test]
    fn test_blank_string_literals_ignores_apostrophe_in_comment() {
        // An apostrophe in a comment must not be read as opening a literal and
        // swallow the statement that follows.
        let sql = "-- don't blank this\nSELECT 'x' FROM t;";
        let out = blank_string_literals(sql);
        assert_eq!(out.len(), sql.len());
        assert!(out.contains("FROM t;"), "got: {out}");
    }

    #[test]
    fn test_ddl_keywords_inside_string_literals_are_not_operations() {
        // An INSERT whose payload mentions DDL is an INSERT. Reading it as a
        // DROP made `safety` report a table drop that does not exist and, with
        // block_on_danger, refuse a valid migration.
        for sql in [
            "INSERT INTO runbook (step) VALUES ('then DROP TABLE users');",
            "INSERT INTO n (m) VALUES ('remember to TRUNCATE TABLE orders');",
            "UPDATE notes SET body = 'ALTER TABLE t DROP COLUMN c' WHERE id = 1;",
        ] {
            let ops = extract_ddl_operations(sql);
            assert!(
                ops.iter().all(|o| matches!(o, DdlOperation::Other { .. })),
                "literal payload classified as DDL for {sql:?}: {ops:?}"
            );
        }
    }

    #[test]
    fn test_real_ddl_is_still_detected_after_literal_blanking() {
        assert!(matches!(
            extract_ddl_operations("DROP TABLE users;").as_slice(),
            [DdlOperation::DropTable { table }] if table == "users"
        ));
        assert!(matches!(
            extract_ddl_operations("TRUNCATE TABLE orders;").as_slice(),
            [DdlOperation::TruncateTable { table }] if table == "orders"
        ));
    }

    #[test]
    fn test_string_literal_default_survives_blanking() {
        // `default_expr` is a public field, and a literal is a legitimate
        // default. The blanked copy is used for matching only.
        match extract_ddl_operations("ALTER TABLE t ADD COLUMN c text DEFAULT 'busy waiting';")
            .as_slice()
        {
            [DdlOperation::AlterTableAddColumn { default_expr, .. }] => {
                assert_eq!(default_expr.as_deref(), Some("'busy waiting'"))
            }
            other => panic!("expected one AlterTableAddColumn, got {other:?}"),
        }
    }
}