drizzle-migrations 0.1.16

Migration infrastructure for drizzle-rs
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
//! Schema snapshot builder from parsed schema files.
//!
//! This module converts [`ParseResult`] from the schema parser into
//! [`Snapshot`] values used for migration diffing.
//!
//! It is shared by runtime/build-time migration generation flows that do not
//! rely on the CLI.
//!
//! # Producer parity
//!
//! The entities built here mirror what the derive macros' runtime
//! `Schema::to_snapshot()` produces for the same source (see
//! `procmacros/src/{sqlite,postgres}/schema.rs` and `table/traits.rs`): the
//! same SQL type spellings, the same default rendering, and the same
//! constraint-name derivations (`{table}_pk` / `fk_..._fk` for `SQLite`,
//! `{table}_pkey` / `{table}_{col}_fkey` / `{table}_{col}_key` for
//! `PostgreSQL`). Foreign-key targets resolve through the referenced table's
//! metadata (explicit `name = "..."` renames included), primary keys are ONE
//! entity per table, `PostgreSQL` identity sequence options / index
//! `method`-`where`-`concurrently` / `NULLS NOT DISTINCT` are preserved, and
//! enum types carry their declared `#[postgres_enum(schema = "...")]` schema
//! — all matching the runtime producer. Remaining deliberate divergence:
//!
//! * Custom (non-enum) column types resolve `type_schema` to `public`; the
//!   runtime reads `DrizzlePostgresColumn::SCHEMA`, whose default is
//!   `public` but which a hand-written trait impl could override — the
//!   parser cannot evaluate user trait impls.

use crate::parser::{
    ColumnSpec, ParseResult, ParsedDefault, ParsedField, ParsedIndex, ParsedTable,
};
use crate::postgres::PostgresSnapshot;
use crate::schema::Snapshot;
use crate::sqlite::SQLiteSnapshot;
use drizzle_types::{Casing, Dialect};
use heck::ToSnakeCase;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};

impl Snapshot {
    /// Build a snapshot from a parsed schema file, for migration diffing.
    ///
    /// Uses the provided `dialect` from config rather than the parser-detected
    /// dialect, allowing users to have multi-dialect schema files and select
    /// which to use via config.
    ///
    /// `casing` is accepted for API compatibility but is ignored for name
    /// derivation: the table macros unconditionally snake-case inferred names
    /// (`procmacros/src/common/table_pipeline.rs`), so honoring a camelCase
    /// casing here would produce names the runtime never uses. The casing
    /// config still affects introspection codegen (field naming), which is a
    /// separate flow.
    ///
    /// # Panics
    ///
    /// Panics for [`Dialect::MySQL`], which has no snapshot representation yet
    /// (same contract as [`Snapshot::empty`]). All upstream flows guard the
    /// dialect before calling (`build::run`, CLI dialect validation).
    #[must_use]
    pub fn from_parse_result(
        result: &ParseResult,
        dialect: Dialect,
        casing: Option<Casing>,
    ) -> Self {
        let _ = casing;
        match dialect {
            Dialect::SQLite => Self::Sqlite(build_sqlite_snapshot(result)),
            Dialect::PostgreSQL => Self::Postgres(build_postgres_snapshot(result)),
            Dialect::MySQL => {
                panic!("MySQL snapshot generation is not supported yet")
            }
        }
    }
}

// =============================================================================
// Shared name resolution
// =============================================================================

fn resolve_table_name(table: &ParsedTable) -> String {
    table
        .spec
        .explicit_name
        .clone()
        .unwrap_or_else(|| table.name.to_snake_case())
}

fn resolve_field_name(field: &ParsedField) -> String {
    field
        .spec
        .explicit_name
        .clone()
        .unwrap_or_else(|| field.name.to_snake_case())
}

/// Name maps built once per snapshot: struct ident -> resolved table name and
/// (struct ident, field ident) -> resolved column name.
struct NameMaps {
    tables: HashMap<String, String>,
    fields: HashMap<(String, String), String>,
}

impl NameMaps {
    fn build(tables: &[&ParsedTable]) -> Self {
        let mut table_map = HashMap::new();
        let mut field_map = HashMap::new();
        for table in tables {
            table_map.insert(table.name.clone(), resolve_table_name(table));
            for field in &table.fields {
                field_map.insert(
                    (table.name.clone(), field.name.clone()),
                    resolve_field_name(field),
                );
            }
        }
        Self {
            tables: table_map,
            fields: field_map,
        }
    }

    fn table(&self, struct_name: &str) -> String {
        self.tables
            .get(struct_name)
            .cloned()
            .unwrap_or_else(|| struct_name.to_snake_case())
    }

    fn field(&self, struct_name: &str, field_name: &str) -> String {
        self.fields
            .get(&(struct_name.to_string(), field_name.to_string()))
            .cloned()
            .unwrap_or_else(|| field_name.to_snake_case())
    }
}

/// Member-type filter when a schema struct of the matching dialect exists.
///
/// Mirrors runtime semantics: only schema members end up in
/// `Schema::to_snapshot()`. Applied to tables (and transitively to the
/// indexes/policies that target them); views and enums always pass through
/// because the introspection codegen intentionally omits them from generated
/// schema structs.
fn schema_members(result: &ParseResult, dialect: Dialect) -> Option<HashSet<&str>> {
    result
        .schema
        .as_ref()
        .filter(|schema| schema.dialect == dialect)
        .map(|schema| schema.member_types.iter().map(String::as_str).collect())
}

fn tables_for(result: &ParseResult, dialect: Dialect) -> Vec<&ParsedTable> {
    let members = schema_members(result, dialect);
    let mut tables: Vec<&ParsedTable> = result
        .tables
        .values()
        .filter(|t| t.dialect == dialect)
        .filter(|t| {
            members
                .as_ref()
                .is_none_or(|members| members.contains(t.name.as_str()))
        })
        .collect();
    tables.sort_by_key(|t| t.order);
    tables
}

fn indexes_for<'a>(
    result: &'a ParseResult,
    dialect: Dialect,
    kept_tables: &HashSet<&str>,
) -> Vec<&'a ParsedIndex> {
    let mut indexes: Vec<&ParsedIndex> = result
        .indexes
        .values()
        .filter(|i| i.dialect == dialect)
        .filter(|i| {
            i.table_name()
                .is_none_or(|table| kept_tables.contains(table))
        })
        .collect();
    indexes.sort_by_key(|i| i.order);
    indexes
}

// =============================================================================
// SQLite
// =============================================================================

/// `sqlite::field::parenthesized_sql_expression`.
fn parenthesized_sql_expression(expression: &str) -> String {
    let trimmed = expression.trim();
    if trimmed.starts_with('(') && trimmed.ends_with(')') {
        trimmed.to_string()
    } else {
        format!("({trimmed})")
    }
}

/// `sqlite::field::normalize_default_sql` — bare `CURRENT_*` keywords stay
/// verbatim, everything else gets parenthesized.
fn normalize_sqlite_default_sql(expression: &str) -> String {
    let trimmed = expression.trim();
    match trimmed.to_ascii_uppercase().as_str() {
        "CURRENT_TIME" | "CURRENT_DATE" | "CURRENT_TIMESTAMP" => trimmed.to_string(),
        _ => parenthesized_sql_expression(trimmed),
    }
}

/// SQL default string for a `SQLite` column, matching
/// `sqlite::table::traits::sqlite_default_sql` (string defaults become
/// SQL-quoted `'...'` with `''` doubling; booleans become `1`/`0`).
fn sqlite_default(spec: &ColumnSpec) -> Option<String> {
    if spec.generated.is_some() {
        // The macro rejects generated + default combinations at compile time.
        return None;
    }
    if let Some(default_sql) = &spec.default_sql {
        return Some(normalize_sqlite_default_sql(default_sql));
    }
    match spec.default.as_ref()? {
        ParsedDefault::Int(token) | ParsedDefault::Float(token) => Some(token.clone()),
        ParsedDefault::Bool(b) => Some(if *b { "1" } else { "0" }.to_string()),
        ParsedDefault::Str(s) => Some(format!("'{}'", s.replace('\'', "''"))),
        // Non-literal defaults are dropped by the macros.
        ParsedDefault::Unsupported(_) => None,
    }
}

/// Build an `SQLite` snapshot from parsed schema
#[allow(clippy::too_many_lines)]
fn build_sqlite_snapshot(result: &ParseResult) -> SQLiteSnapshot {
    use crate::sqlite::{
        CheckConstraint, ForeignKey, Generated, GeneratedType, Index, IndexColumn, IndexOrigin,
        PrimaryKey, SqliteEntity, Table, UniqueConstraint, View,
    };

    let mut snapshot = SQLiteSnapshot::new();

    let tables = tables_for(result, Dialect::SQLite);
    let kept: HashSet<&str> = tables.iter().map(|t| t.name.as_str()).collect();
    let maps = NameMaps::build(&tables);

    for table in &tables {
        let table_name = maps.table(&table.name);

        let mut sqlite_table = Table::new(table_name.clone());
        sqlite_table.strict = table.spec.strict;
        sqlite_table.without_rowid = table.spec.without_rowid;
        snapshot.add_entity(SqliteEntity::Table(sqlite_table));

        let mut pk_columns = Vec::new();

        for field in &table.fields {
            let col_name = maps.field(&table.name, &field.name);
            let spec = &field.spec;

            let mut col = crate::sqlite::Column::new(
                table_name.clone(),
                col_name.clone(),
                spec.sqlite_type.clone().unwrap_or_default(),
            );
            if !spec.nullable {
                col = col.not_null();
            }
            if spec.autoincrement {
                col = col.autoincrement();
            }
            if let Some(default) = sqlite_default(spec) {
                col = col.default_value(default);
            }
            if let Some(generated) = &spec.generated {
                col.generated = Some(Generated {
                    expression: Cow::Owned(parenthesized_sql_expression(&generated.expression)),
                    gen_type: if generated.stored {
                        GeneratedType::Stored
                    } else {
                        GeneratedType::Virtual
                    },
                });
            }
            if let Some(collate) = &spec.collate {
                col.collate = Some(Cow::Owned(collate.clone()));
            }
            snapshot.add_entity(SqliteEntity::Column(col));

            if spec.primary {
                pk_columns.push(col_name.clone());
            }

            // Column-level UNIQUE (macro: only when not also primary).
            if spec.unique && !spec.primary {
                snapshot.add_entity(SqliteEntity::UniqueConstraint(
                    UniqueConstraint::from_strings(
                        table_name.clone(),
                        format!("{table_name}_{col_name}_unique"),
                        vec![col_name.clone()],
                    ),
                ));
            }

            // Column-level CHECK, named like the macro's constraint refs.
            if let Some(check) = &spec.check {
                snapshot.add_entity(SqliteEntity::CheckConstraint(CheckConstraint::new(
                    table_name.clone(),
                    format!("{table_name}_{col_name}_check"),
                    check.clone(),
                )));
            }

            // Column-level foreign key. Name, target table, and target
            // column all resolve through the referenced table's actual
            // names (the macro resolves them via the target's `NAME`
            // consts, honoring explicit `name = "..."` renames).
            if let Some(reference) = &spec.references {
                let target_table = maps.table(&reference.table);
                let target_column = maps.field(&reference.table, &reference.column);
                let mut fk = ForeignKey::from_strings(
                    table_name.clone(),
                    format!("fk_{table_name}_{col_name}_{target_table}_{target_column}_fk"),
                    vec![col_name.clone()],
                    target_table,
                    vec![target_column],
                );
                fk.on_delete = spec.on_delete.clone().map(Cow::Owned);
                fk.on_update = spec.on_update.clone().map(Cow::Owned);
                snapshot.add_entity(SqliteEntity::ForeignKey(fk));
            }
        }

        // ONE PrimaryKey entity, canonical `{table}_pk` name.
        if !pk_columns.is_empty() {
            snapshot.add_entity(SqliteEntity::PrimaryKey(PrimaryKey::from_strings(
                table_name.clone(),
                format!("{table_name}_pk"),
                pk_columns,
            )));
        }

        // Composite FOREIGN_KEY(...) attributes. Source and target columns
        // resolve through the respective tables' field metadata (matching
        // the macro, which honors explicit `name = "..."` renames on both
        // sides); actions pass through verbatim.
        for cfk in &table.spec.composite_fks {
            let source_columns: Vec<String> = cfk
                .source_columns
                .iter()
                .map(|field| maps.field(&table.name, field))
                .collect();
            let target_table = maps.table(&cfk.target_table);
            let target_columns: Vec<String> = cfk
                .target_columns
                .iter()
                .map(|c| maps.field(&cfk.target_table, c))
                .collect();
            let fk_name = format!(
                "fk_{table_name}_{}_{}_{}_fk",
                source_columns.join("_"),
                target_table,
                target_columns.join("_")
            );
            let mut fk = ForeignKey::from_strings(
                table_name.clone(),
                fk_name,
                source_columns,
                target_table,
                target_columns,
            );
            fk.on_delete = cfk.on_delete.clone().map(Cow::Owned);
            fk.on_update = cfk.on_update.clone().map(Cow::Owned);
            snapshot.add_entity(SqliteEntity::ForeignKey(fk));
        }

        // Table-level UNIQUE(...) attributes (columns resolve through field
        // names, like the macro's constraint refs).
        for unique in &table.spec.unique_constraints {
            let columns: Vec<String> = unique
                .columns
                .iter()
                .map(|field| maps.field(&table.name, field))
                .collect();
            let name = unique
                .name
                .clone()
                .unwrap_or_else(|| format!("{table_name}_{}_unique", columns.join("_")));
            let mut constraint = UniqueConstraint::from_strings(table_name.clone(), name, columns);
            constraint.name_explicit = unique.name.is_some();
            snapshot.add_entity(SqliteEntity::UniqueConstraint(constraint));
        }

        // Table-level CHECK(...) attributes with the macro's naming: a single
        // check is `{table}_check`, several are `{table}_check{N}`.
        let check_count = table.spec.check_constraints.len();
        for (idx, check) in table.spec.check_constraints.iter().enumerate() {
            let name = check.name.clone().unwrap_or_else(|| {
                if check_count == 1 {
                    format!("{table_name}_check")
                } else {
                    format!("{table_name}_check{}", idx + 1)
                }
            });
            snapshot.add_entity(SqliteEntity::CheckConstraint(CheckConstraint::new(
                table_name.clone(),
                name,
                check.expr.clone(),
            )));
        }
    }

    // Indexes targeting kept tables. Index names derive from the struct
    // ident with the macro's fold; an explicit `name = "..."` (parser
    // extension) wins.
    for index in indexes_for(result, Dialect::SQLite, &kept) {
        let table_struct = index.table_name().unwrap_or_default();
        let index_name = index
            .spec
            .explicit_name
            .clone()
            .unwrap_or_else(|| crate::parser::sqlite_index_name(&index.name));
        let columns: Vec<IndexColumn> = index
            .spec
            .column_refs
            .iter()
            .map(|(table, field)| IndexColumn::new(maps.field(table, field)))
            .collect();

        snapshot.add_entity(SqliteEntity::Index(Index {
            table: maps.table(table_struct).into(),
            name: index_name.into(),
            columns,
            is_unique: index.spec.unique,
            where_clause: index.spec.where_clause.clone().map(Cow::Owned),
            origin: IndexOrigin::Manual,
        }));
    }

    // Views (never scoped by schema membership; see `schema_members`).
    let mut views: Vec<_> = result
        .views
        .values()
        .filter(|v| v.dialect == Dialect::SQLite)
        .collect();
    views.sort_by_key(|v| v.order);
    for parsed in views {
        let name = parsed
            .explicit_name
            .clone()
            .unwrap_or_else(|| parsed.name.to_snake_case());
        let mut view = View::new(name);
        view.definition = parsed.definition.clone().map(Cow::Owned);
        view.is_existing = parsed.existing;
        snapshot.add_entity(SqliteEntity::View(view));
    }

    snapshot
}

// =============================================================================
// PostgreSQL
// =============================================================================

/// SQL default string for a `PostgreSQL` column, matching
/// `postgres::field::parse_column_attribute` / `default_to_string` (string
/// defaults become `'...'` with `''` doubling; booleans stay `true`/`false`;
/// `default_sql` passes through verbatim; serial/identity/generated columns
/// have no default).
fn postgres_default(spec: &ColumnSpec) -> Option<String> {
    if spec.serial.is_some() || spec.identity.is_some() || spec.generated.is_some() {
        return None;
    }
    if let Some(default_sql) = &spec.default_sql {
        return Some(default_sql.clone());
    }
    match spec.default.as_ref()? {
        ParsedDefault::Int(token) | ParsedDefault::Float(token) => Some(token.clone()),
        ParsedDefault::Bool(b) => Some(b.to_string()),
        ParsedDefault::Str(s) => Some(format!("'{}'", s.replace('\'', "''"))),
        ParsedDefault::Unsupported(_) => None,
    }
}

/// Build a `PostgreSQL` snapshot from parsed schema
#[allow(clippy::too_many_lines)]
fn build_postgres_snapshot(result: &ParseResult) -> PostgresSnapshot {
    use crate::postgres::ddl::{GeneratedType, IdentityType};
    use crate::postgres::{
        CheckConstraint, Column, Enum as PgEnum, ForeignKey, Generated, Identity, Index,
        IndexColumn, Policy, PostgresEntity, PrimaryKey, Schema as PgSchema, Table,
        UniqueConstraint, View,
    };

    let mut snapshot = PostgresSnapshot::new();

    let tables = tables_for(result, Dialect::PostgreSQL);
    let kept: HashSet<&str> = tables.iter().map(|t| t.name.as_str()).collect();
    let maps = NameMaps::build(&tables);

    let mut table_schemas: HashMap<String, String> = HashMap::new();
    for table in &tables {
        let schema = table
            .spec
            .schema
            .clone()
            .unwrap_or_else(|| "public".to_string());
        table_schemas.insert(table.name.clone(), schema);
    }
    let schema_of = |struct_name: &str| -> String {
        table_schemas
            .get(struct_name)
            .cloned()
            .unwrap_or_else(|| "public".to_string())
    };

    // Enum ident -> declared schema (`#[postgres_enum(schema = "...")]`),
    // used to resolve `type_schema` on custom/enum columns the way the
    // macros do (`DrizzlePostgresColumn::SCHEMA`, default `public`).
    let enum_schemas: HashMap<&str, &str> = result
        .enums
        .values()
        .filter(|e| e.dialect == Dialect::PostgreSQL)
        .map(|e| (e.name.as_str(), e.schema.as_deref().unwrap_or("public")))
        .collect();
    let type_schema_of = |sql_type: &str| -> String {
        enum_schemas
            .get(sql_type)
            .map_or("public", |schema| *schema)
            .to_string()
    };

    // Schema entities appear when the first table, policy target, enum, or
    // view in that schema appears, mirroring the runtime producer — an enum
    // or view may be a schema's only occupant, and its CREATE needs the
    // CREATE SCHEMA first.
    let mut seen_schemas: HashSet<String> = HashSet::new();
    let mut schema_entities: Vec<String> = Vec::new();
    for table in &tables {
        let schema = schema_of(&table.name);
        if seen_schemas.insert(schema.clone()) {
            schema_entities.push(schema);
        }
    }
    let mut policies: Vec<_> = result
        .policies
        .values()
        .filter(|p| p.dialect == Dialect::PostgreSQL)
        .filter(|p| kept.contains(p.table.as_str()) || !table_schemas.contains_key(&p.table))
        .collect();
    policies.sort_by_key(|p| p.order);
    for policy in &policies {
        let schema = schema_of(&policy.table);
        if seen_schemas.insert(schema.clone()) {
            schema_entities.push(schema);
        }
    }
    let mut enum_decls: Vec<_> = result
        .enums
        .values()
        .filter(|e| e.dialect == Dialect::PostgreSQL)
        .collect();
    enum_decls.sort_by_key(|e| e.order);
    for parsed in enum_decls {
        let schema = parsed
            .schema
            .clone()
            .unwrap_or_else(|| "public".to_string());
        if seen_schemas.insert(schema.clone()) {
            schema_entities.push(schema);
        }
    }
    let mut view_decls: Vec<_> = result
        .views
        .values()
        .filter(|v| v.dialect == Dialect::PostgreSQL)
        .collect();
    view_decls.sort_by_key(|v| v.order);
    for parsed in view_decls {
        let schema = parsed
            .schema
            .clone()
            .unwrap_or_else(|| "public".to_string());
        if seen_schemas.insert(schema.clone()) {
            schema_entities.push(schema);
        }
    }
    for schema in schema_entities {
        snapshot.add_entity(PostgresEntity::Schema(PgSchema::new(schema)));
    }

    for table in &tables {
        let table_name = maps.table(&table.name);
        let schema_name = schema_of(&table.name);

        let mut pg_table = Table::new(schema_name.clone(), table_name.clone());
        // Mirror the builder interplay in the DDL type: TEMPORARY and
        // UNLOGGED are mutually exclusive; temporary wins when both are set.
        if table.spec.temporary {
            pg_table = pg_table.temporary();
        } else if table.spec.unlogged {
            pg_table = pg_table.unlogged();
        }
        if let Some(inherits) = &table.spec.inherits {
            pg_table = pg_table.inherits(inherits.clone());
        }
        if let Some(tablespace) = &table.spec.tablespace {
            pg_table = pg_table.tablespace(tablespace.clone());
        }
        if table.spec.rls {
            pg_table = pg_table.rls_enabled();
        }
        if let Some(comment) = &table.spec.comment {
            pg_table = pg_table.comment(comment.clone());
        }
        snapshot.add_entity(PostgresEntity::Table(pg_table));

        let mut pk_columns = Vec::new();

        for field in &table.fields {
            let col_name = maps.field(&table.name, &field.name);
            let spec = &field.spec;
            let sql_type = spec.pg_type.clone().unwrap_or_default();

            let identity = if spec.serial.is_none() {
                spec.identity.as_ref().map(|parsed| Identity {
                    name: Cow::Owned(format!("{table_name}_{col_name}_seq")),
                    schema: Some(Cow::Owned(schema_name.clone())),
                    type_: if parsed.always {
                        IdentityType::Always
                    } else {
                        IdentityType::ByDefault
                    },
                    increment: parsed.increment.clone().map(Cow::Owned),
                    min_value: parsed.min_value.clone().map(Cow::Owned),
                    max_value: parsed.max_value.clone().map(Cow::Owned),
                    start_with: parsed.start.clone().map(Cow::Owned),
                    cache: parsed.cache,
                    cycle: if parsed.cycle { Some(true) } else { None },
                })
            } else {
                None
            };

            let generated = spec.generated.as_ref().map(|parsed| Generated {
                expression: Cow::Owned(parsed.expression.clone()),
                gen_type: if parsed.stored {
                    GeneratedType::Stored
                } else {
                    GeneratedType::Virtual
                },
            });

            // Enum/custom types carry the TYPE's own schema: for enums the
            // declared `#[postgres_enum(schema = "...")]`, otherwise the
            // `DrizzlePostgresColumn::SCHEMA` default of `public` (the
            // parser cannot evaluate user trait impls, so custom non-enum
            // types resolve to `public` like the trait default).
            let type_schema = if matches!(
                crate::postgres::grammar::PgTypeCategory::from_sql_type(&sql_type),
                crate::postgres::grammar::PgTypeCategory::Custom
            ) {
                Some(Cow::Owned(type_schema_of(&sql_type)))
            } else {
                None
            };

            let column = Column {
                schema: schema_name.clone().into(),
                table: table_name.clone().into(),
                name: col_name.clone().into(),
                sql_type: sql_type.into(),
                type_schema,
                not_null: !spec.nullable,
                default: postgres_default(spec).map(Cow::Owned),
                generated,
                identity,
                dimensions: spec.pg_dimensions,
                comment: spec.comment.clone().map(Cow::Owned),
                collate: spec.collate.clone().map(Cow::Owned),
                ordinal_position: None,
            };
            snapshot.add_entity(PostgresEntity::Column(column));

            if spec.primary {
                pk_columns.push(col_name.clone());
            }

            if spec.unique && !spec.primary {
                snapshot.add_entity(PostgresEntity::UniqueConstraint(
                    UniqueConstraint::from_strings(
                        schema_name.clone(),
                        table_name.clone(),
                        format!("{table_name}_{col_name}_key"),
                        vec![col_name.clone()],
                    ),
                ));
            }

            if let Some(check) = &spec.check {
                snapshot.add_entity(PostgresEntity::CheckConstraint(CheckConstraint::new(
                    schema_name.clone(),
                    table_name.clone(),
                    format!("{table_name}_{col_name}_check"),
                    check.clone(),
                )));
            }

            // Column-level foreign key: `{table}_{col}_fkey` naming; target
            // schema/table/column resolve through the referenced table's
            // metadata (the macro resolves the target column via its `NAME`
            // const, honoring explicit `name = "..."` renames).
            if let Some(reference) = &spec.references {
                let mut fk = ForeignKey::from_strings(
                    schema_name.clone(),
                    table_name.clone(),
                    format!("{table_name}_{col_name}_fkey"),
                    vec![col_name.clone()],
                    schema_of(&reference.table),
                    maps.table(&reference.table),
                    vec![maps.field(&reference.table, &reference.column)],
                );
                if let Some(on_delete) = &spec.on_delete {
                    fk = fk.on_delete(on_delete.clone());
                }
                if let Some(on_update) = &spec.on_update {
                    fk = fk.on_update(on_update.clone());
                }
                if spec.deferrable {
                    fk = fk.deferrable();
                }
                if spec.initially_deferred {
                    fk = fk.initially_deferred();
                }
                snapshot.add_entity(PostgresEntity::ForeignKey(fk));
            }
        }

        // ONE PrimaryKey entity covering all PK columns. (The runtime
        // producer emits one per column for composite PKs; single entity is
        // the spec-correct shape and matches the macro's compile-time
        // metadata.)
        if !pk_columns.is_empty() {
            snapshot.add_entity(PostgresEntity::PrimaryKey(PrimaryKey::from_strings(
                schema_name.clone(),
                table_name.clone(),
                format!("{table_name}_pkey"),
                pk_columns,
            )));
        }

        // Composite FOREIGN_KEY(...) attributes: `{table}_{first_col}_fkey`
        // naming with source columns resolved through field names (Postgres
        // macro behavior); actions pass through verbatim.
        for cfk in &table.spec.composite_fks {
            let source_columns: Vec<String> = cfk
                .source_columns
                .iter()
                .map(|field| maps.field(&table.name, field))
                .collect();
            let fk_name = format!(
                "{table_name}_{}_fkey",
                source_columns.first().cloned().unwrap_or_default()
            );
            let mut fk = ForeignKey::from_strings(
                schema_name.clone(),
                table_name.clone(),
                fk_name,
                source_columns,
                schema_of(&cfk.target_table),
                maps.table(&cfk.target_table),
                cfk.target_columns
                    .iter()
                    .map(|c| maps.field(&cfk.target_table, c))
                    .collect(),
            );
            if let Some(on_delete) = &cfk.on_delete {
                fk = fk.on_delete(on_delete.clone());
            }
            if let Some(on_update) = &cfk.on_update {
                fk = fk.on_update(on_update.clone());
            }
            if cfk.deferrable {
                fk = fk.deferrable();
            }
            if cfk.initially_deferred {
                fk = fk.initially_deferred();
            }
            snapshot.add_entity(PostgresEntity::ForeignKey(fk));
        }

        // Table-level UNIQUE(...) attributes: `{table}_{cols}_key` naming.
        for unique in &table.spec.unique_constraints {
            let columns: Vec<String> = unique
                .columns
                .iter()
                .map(|field| maps.field(&table.name, field))
                .collect();
            let name = unique
                .name
                .clone()
                .unwrap_or_else(|| format!("{table_name}_{}_key", columns.join("_")));
            let mut constraint = UniqueConstraint::from_strings(
                schema_name.clone(),
                table_name.clone(),
                name,
                columns,
            );
            constraint.name_explicit = unique.name.is_some();
            constraint.nulls_not_distinct = unique.nulls_not_distinct;
            constraint.deferrable = unique.deferrable;
            constraint.initially_deferred = unique.initially_deferred;
            snapshot.add_entity(PostgresEntity::UniqueConstraint(constraint));
        }

        // Table-level CHECK(...) attributes, macro naming (see SQLite twin).
        let check_count = table.spec.check_constraints.len();
        for (idx, check) in table.spec.check_constraints.iter().enumerate() {
            let name = check.name.clone().unwrap_or_else(|| {
                if check_count == 1 {
                    format!("{table_name}_check")
                } else {
                    format!("{table_name}_check{}", idx + 1)
                }
            });
            snapshot.add_entity(PostgresEntity::CheckConstraint(CheckConstraint::new(
                schema_name.clone(),
                table_name.clone(),
                name,
                check.expr.clone(),
            )));
        }
    }

    // Indexes targeting kept tables.
    for index in indexes_for(result, Dialect::PostgreSQL, &kept) {
        let table_struct = index.table_name().unwrap_or_default();
        let index_name = index
            .spec
            .explicit_name
            .clone()
            .unwrap_or_else(|| crate::parser::postgres_index_name(&index.name));
        let columns: Vec<IndexColumn> = index
            .spec
            .column_refs
            .iter()
            .map(|(table, field)| IndexColumn::new(maps.field(table, field)))
            .collect();

        snapshot.add_entity(PostgresEntity::Index(Index {
            schema: schema_of(table_struct).into(),
            table: maps.table(table_struct).into(),
            name: index_name.into(),
            name_explicit: false,
            columns,
            is_unique: index.spec.unique,
            where_clause: index.spec.where_clause.clone().map(Cow::Owned),
            method: index.spec.method.clone().map(Cow::Owned),
            with: None,
            concurrently: index.spec.concurrent,
        }));
    }

    // Enums: created in the schema from `#[postgres_enum(schema = "...")]`
    // (default `public`), matching the derive's `ENUM_SCHEMA` const.
    let mut enums: Vec<_> = result
        .enums
        .values()
        .filter(|e| e.dialect == Dialect::PostgreSQL)
        .collect();
    enums.sort_by_key(|e| e.order);
    for parsed in enums {
        snapshot.add_entity(PostgresEntity::Enum(PgEnum::from_strings(
            parsed
                .schema
                .clone()
                .unwrap_or_else(|| "public".to_string()),
            parsed.name.clone(),
            parsed.variants.clone(),
        )));
    }

    // Views.
    let mut views: Vec<_> = result
        .views
        .values()
        .filter(|v| v.dialect == Dialect::PostgreSQL)
        .collect();
    views.sort_by_key(|v| v.order);
    for parsed in views {
        let schema = parsed
            .schema
            .clone()
            .unwrap_or_else(|| "public".to_string());
        let name = parsed
            .explicit_name
            .clone()
            .unwrap_or_else(|| parsed.name.to_snake_case());
        let mut view = View::new(schema, name);
        view.definition = parsed.definition.clone().map(Cow::Owned);
        view.materialized = parsed.materialized;
        view.is_existing = parsed.existing;
        view.with_no_data = if parsed.with_no_data {
            Some(true)
        } else {
            None
        };
        view.using = parsed.using.clone().map(Cow::Owned);
        view.tablespace = parsed.tablespace.clone().map(Cow::Owned);
        snapshot.add_entity(PostgresEntity::View(view));
    }

    // Policies (RLS) targeting kept tables.
    for parsed in policies {
        let schema = schema_of(&parsed.table);
        let table = maps.table(&parsed.table);
        let name = parsed
            .explicit_name
            .clone()
            .unwrap_or_else(|| heck::AsSnakeCase(parsed.name.as_str()).to_string());
        let mut policy = Policy::new(schema, table, name);
        policy.as_clause = parsed.as_clause.clone().map(Cow::Owned);
        policy.for_clause = parsed.for_clause.clone().map(Cow::Owned);
        if !parsed.to.is_empty() {
            policy.to = Some(parsed.to.iter().cloned().map(Cow::Owned).collect());
        }
        policy.using = parsed.using.clone().map(Cow::Owned);
        policy.with_check = parsed.with_check.clone().map(Cow::Owned);
        snapshot.add_entity(PostgresEntity::Policy(policy));
    }

    snapshot
}

// =============================================================================
// Tests
// =============================================================================

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

    fn sqlite_snapshot(code: &str) -> SQLiteSnapshot {
        let result = SchemaParser::parse(code);
        assert!(
            result.errors.is_empty(),
            "parse errors: {:?}",
            result.errors
        );
        match Snapshot::from_parse_result(&result, Dialect::SQLite, None) {
            Snapshot::Sqlite(s) => s,
            Snapshot::Postgres(_) => panic!("expected SQLite snapshot"),
        }
    }

    fn postgres_snapshot(code: &str) -> PostgresSnapshot {
        let result = SchemaParser::parse(code);
        assert!(
            result.errors.is_empty(),
            "parse errors: {:?}",
            result.errors
        );
        match Snapshot::from_parse_result(&result, Dialect::PostgreSQL, None) {
            Snapshot::Postgres(s) => s,
            Snapshot::Sqlite(_) => panic!("expected Postgres snapshot"),
        }
    }

    #[test]
    fn test_postgres_snapshot_preserves_column_markers() {
        use crate::postgres::ddl::{GeneratedType, IdentityType, PostgresEntity};

        let code = r#"
#[PostgresTable(schema = "app")]
pub struct PgMarkers {
    #[column(serial, primary)]
    pub id: i32,
    #[column(smallserial)]
    pub small_id: i16,
    #[column(bigserial)]
    pub big_id: i64,
    #[column(json)]
    pub json_doc: AppDoc,
    #[column(jsonb)]
    pub jsonb_doc: AppDoc,
    #[column(identity(by_default))]
    pub identity_id: i32,
    #[column(generated(stored, "first_name || ' ' || last_name"))]
    pub full_name: String,
    #[column(collate = "C")]
    pub sortable: String,
}
"#;

        let snap = postgres_snapshot(code);
        let column = |name: &str| {
            snap.ddl
                .iter()
                .find_map(|entity| {
                    if let PostgresEntity::Column(column) = entity
                        && column.name.as_ref() == name
                    {
                        Some(column)
                    } else {
                        None
                    }
                })
                .expect("expected column")
        };

        // SQL types use the macro's spelling (uppercase `to_sql_type`).
        assert_eq!(column("id").sql_type.as_ref(), "SERIAL");
        assert!(column("id").identity.is_none());
        assert!(column("id").default.is_none());
        assert_eq!(column("small_id").sql_type.as_ref(), "SMALLSERIAL");
        assert_eq!(column("big_id").sql_type.as_ref(), "BIGSERIAL");
        assert_eq!(column("json_doc").sql_type.as_ref(), "JSON");
        assert_eq!(column("jsonb_doc").sql_type.as_ref(), "JSONB");

        let identity = column("identity_id")
            .identity
            .as_ref()
            .expect("expected identity");
        assert_eq!(identity.type_, IdentityType::ByDefault);
        assert_eq!(identity.name.as_ref(), "pg_markers_identity_id_seq");
        assert_eq!(identity.schema.as_deref(), Some("app"));

        let generated = column("full_name")
            .generated
            .as_ref()
            .expect("expected generated column");
        assert_eq!(generated.gen_type, GeneratedType::Stored);
        assert_eq!(
            generated.expression.as_ref(),
            "first_name || ' ' || last_name"
        );
        assert!(column("full_name").identity.is_none());
        assert!(column("full_name").default.is_none());

        assert_eq!(column("sortable").collate.as_deref(), Some("C"));
    }

    #[test]
    fn test_sqlite_uuid_snapshot_storage_respects_column_type() {
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct UuidStorage {
    #[column(primary)]
    pub id: i64,
    pub blob_uuid: uuid::Uuid,
    #[column(text)]
    pub text_uuid: uuid::Uuid,
    #[blob]
    pub legacy_blob_uuid: uuid::Uuid,
    #[text]
    pub legacy_text_uuid: uuid::Uuid,
}
"#;

        let snap = sqlite_snapshot(code);
        let column_type = |name: &str| {
            snap.ddl
                .iter()
                .find_map(|entity| {
                    if let SqliteEntity::Column(column) = entity
                        && column.name.as_ref() == name
                    {
                        Some(column.sql_type.as_ref())
                    } else {
                        None
                    }
                })
                .expect("expected column")
        };

        assert_eq!(column_type("blob_uuid"), "BLOB");
        assert_eq!(column_type("text_uuid"), "TEXT");
        assert_eq!(column_type("legacy_blob_uuid"), "BLOB");
        assert_eq!(column_type("legacy_text_uuid"), "TEXT");
    }

    #[test]
    fn test_sqlite_snapshot_preserves_partial_index_predicate() {
        use crate::sqlite::SqliteEntity;

        let snap = sqlite_snapshot(
            r#"
#[SQLiteTable]
pub struct Jobs {
    #[column(primary)]
    pub id: i64,
    pub builder: Option<String>,
}

#[SQLiteIndex(unique, where = "builder IS NULL")]
pub struct IdxPlanJobs(Jobs::id);
"#,
        );

        let index = snap
            .ddl
            .iter()
            .find_map(|entity| match entity {
                SqliteEntity::Index(index) => Some(index),
                _ => None,
            })
            .expect("expected partial index");
        assert!(index.is_unique);
        assert_eq!(index.where_clause.as_deref(), Some("builder IS NULL"));
    }

    #[test]
    fn test_sqlite_string_defaults_are_sql_quoted() {
        // P4: string defaults become SQL-quoted with '' doubling, matching
        // the macro's rendering; P9: default_sql passes through with the
        // macro's normalization.
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Defaults {
    #[column(default = "hello")]
    pub greeting: String,
    #[column(default = "it's")]
    pub quoted: String,
    #[column(default_sql = "CURRENT_TIMESTAMP")]
    pub created_at: String,
    #[column(default_sql = "strftime('%s','now')")]
    pub epoch: i64,
    #[column(default = true)]
    pub active: bool,
}
"#;

        let snap = sqlite_snapshot(code);
        let default_of = |name: &str| {
            snap.ddl
                .iter()
                .find_map(|entity| {
                    if let SqliteEntity::Column(column) = entity
                        && column.name.as_ref() == name
                    {
                        Some(column.default.as_deref().map(str::to_string))
                    } else {
                        None
                    }
                })
                .expect("expected column")
        };

        assert_eq!(default_of("greeting").as_deref(), Some("'hello'"));
        assert_eq!(default_of("quoted").as_deref(), Some("'it''s'"));
        assert_eq!(
            default_of("created_at").as_deref(),
            Some("CURRENT_TIMESTAMP")
        );
        assert_eq!(
            default_of("epoch").as_deref(),
            Some("(strftime('%s','now'))")
        );
        assert_eq!(default_of("active").as_deref(), Some("1"));
    }

    #[test]
    fn test_sqlite_fk_actions_are_normalized_and_names_canonical() {
        // P3: SET_NULL renders as `SET NULL`; P14: canonical `fk_..._fk` /
        // `{table}_pk` names.
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Users {
    #[column(primary)]
    pub id: i64,
}

#[SQLiteTable]
pub struct Posts {
    #[column(primary)]
    pub id: i64,
    #[column(references = Users::id, on_delete = SET_NULL, on_update = Cascade)]
    pub author_id: i64,
}
"#;

        let snap = sqlite_snapshot(code);
        let fk = snap
            .ddl
            .iter()
            .find_map(|e| {
                if let SqliteEntity::ForeignKey(fk) = e {
                    Some(fk)
                } else {
                    None
                }
            })
            .expect("expected foreign key");
        assert_eq!(fk.name.as_ref(), "fk_posts_author_id_users_id_fk");
        assert_eq!(fk.on_delete.as_deref(), Some("SET NULL"));
        assert_eq!(fk.on_update.as_deref(), Some("CASCADE"));

        let pk_names: Vec<&str> = snap
            .ddl
            .iter()
            .filter_map(|e| {
                if let SqliteEntity::PrimaryKey(pk) = e {
                    Some(pk.name.as_ref())
                } else {
                    None
                }
            })
            .collect();
        assert!(pk_names.contains(&"users_pk"));
        assert!(pk_names.contains(&"posts_pk"));
    }

    #[test]
    fn test_sqlite_generated_collate_check_table_constraints() {
        // P10 + P8: generated/collate/check plus table-level
        // FOREIGN_KEY/UNIQUE/CHECK all survive into the snapshot.
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Parent {
    #[column(primary)]
    pub id_a: i64,
    #[column(primary)]
    pub id_b: i64,
}

#[SQLiteTable(
    FOREIGN_KEY(columns(pa, pb), references(Parent, id_a, id_b), on_delete = "CASCADE"),
    UNIQUE(columns(first, last), name = "people_name_unique"),
    CHECK(expr = "score >= 0")
)]
pub struct People {
    #[column(primary)]
    pub id: i64,
    pub pa: i64,
    pub pb: i64,
    pub first: String,
    #[column(collate = NOCASE)]
    pub last: String,
    #[column(check = "score <= 100")]
    pub score: i64,
    #[column(generated(stored, "first || ' ' || last"))]
    pub full_name: String,
    #[column(generated(virtual, "score * 2"))]
    pub double_score: i64,
}
"#;

        let snap = sqlite_snapshot(code);

        // Composite PK keeps declaration order in one entity.
        let parent_pk = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                SqliteEntity::PrimaryKey(pk) if pk.table.as_ref() == "parent" => Some(pk),
                _ => None,
            })
            .expect("parent pk");
        assert_eq!(parent_pk.name.as_ref(), "parent_pk");
        assert_eq!(
            parent_pk
                .columns
                .iter()
                .map(std::convert::AsRef::as_ref)
                .collect::<Vec<_>>(),
            vec!["id_a", "id_b"]
        );

        let fk = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                SqliteEntity::ForeignKey(fk) => Some(fk),
                _ => None,
            })
            .expect("composite fk");
        assert_eq!(fk.name.as_ref(), "fk_people_pa_pb_parent_id_a_id_b_fk");
        assert_eq!(fk.on_delete.as_deref(), Some("CASCADE"));

        let unique = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                SqliteEntity::UniqueConstraint(u) => Some(u),
                _ => None,
            })
            .expect("table unique");
        assert_eq!(unique.name.as_ref(), "people_name_unique");
        assert!(unique.name_explicit);

        let checks: Vec<&crate::sqlite::CheckConstraint> = snap
            .ddl
            .iter()
            .filter_map(|e| match e {
                SqliteEntity::CheckConstraint(c) => Some(c),
                _ => None,
            })
            .collect();
        assert!(
            checks
                .iter()
                .any(|c| c.name.as_ref() == "people_score_check")
        );
        assert!(checks.iter().any(|c| c.name.as_ref() == "people_check"));

        let column = |name: &str| {
            snap.ddl
                .iter()
                .find_map(|e| match e {
                    SqliteEntity::Column(c) if c.name.as_ref() == name => Some(c),
                    _ => None,
                })
                .expect("column")
        };
        assert_eq!(column("last").collate.as_deref(), Some("NOCASE"));
        let full_name = column("full_name").generated.as_ref().expect("generated");
        assert_eq!(full_name.expression.as_ref(), "(first || ' ' || last)");
        assert_eq!(full_name.gen_type, crate::sqlite::GeneratedType::Stored);
        assert_eq!(
            column("double_score")
                .generated
                .as_ref()
                .expect("generated")
                .gen_type,
            crate::sqlite::GeneratedType::Virtual
        );
    }

    #[test]
    fn test_postgres_schema_and_index_options_are_preserved() {
        use crate::postgres::ddl::PostgresEntity;

        let code = r#"
#[PostgresTable(schema = "auth")]
pub struct Users {
    #[column(primary)]
    pub id: i32,
}

#[PostgresTable(schema = "app")]
pub struct Sessions {
    #[column(primary)]
    pub id: i32,
    #[column(references = Users::id)]
    pub user_id: i32,
}

#[PostgresIndex(concurrent, method = "gin", where = "user_id > 0")]
pub struct SessionsUserIdx(Sessions::user_id);
"#;

        let snap = postgres_snapshot(code);

        let has_auth_schema = snap
            .ddl
            .iter()
            .any(|e| matches!(e, PostgresEntity::Schema(s) if s.name.as_ref() == "auth"));
        let has_app_schema = snap
            .ddl
            .iter()
            .any(|e| matches!(e, PostgresEntity::Schema(s) if s.name.as_ref() == "app"));
        assert!(has_auth_schema, "missing auth schema entity");
        assert!(has_app_schema, "missing app schema entity");

        let fk = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::ForeignKey(fk) = e {
                Some(fk)
            } else {
                None
            }
        });
        let fk = fk.expect("expected foreign key");
        assert_eq!(fk.schema.as_ref(), "app");
        assert_eq!(fk.schema_to.as_ref(), "auth");
        assert_eq!(fk.name.as_ref(), "sessions_user_id_fkey");

        let idx = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::Index(i) = e {
                Some(i)
            } else {
                None
            }
        });
        let idx = idx.expect("expected index");
        assert!(idx.concurrently);
        assert_eq!(idx.method.as_deref(), Some("gin"));
        assert_eq!(idx.where_clause.as_deref(), Some("user_id > 0"));
        assert_eq!(idx.schema.as_ref(), "app");
        assert_eq!(idx.name.as_ref(), "sessions_user_idx");
    }

    #[test]
    fn test_postgres_deferrable_arrays_and_key_names() {
        // P13: deferrable FKs, Vec<T> arrays; P14: `{t}_pkey` / `{t}_{c}_key`.
        use crate::postgres::ddl::PostgresEntity;

        let code = r#"
#[PostgresTable]
pub struct Users {
    #[column(primary)]
    pub id: i32,
    #[column(unique)]
    pub email: String,
    pub tags: Vec<String>,
}

#[PostgresTable]
pub struct Sessions {
    #[column(primary)]
    pub id: i32,
    #[column(references = Users::id, deferrable, initially_deferred, on_delete = SET_NULL)]
    pub user_id: i32,
}
"#;

        let snap = postgres_snapshot(code);

        let tags = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::Column(c) if c.name.as_ref() == "tags" => Some(c),
                _ => None,
            })
            .expect("tags column");
        assert_eq!(tags.sql_type.as_ref(), "TEXT");
        assert_eq!(tags.dimensions, Some(1));

        let fk = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::ForeignKey(fk) => Some(fk),
                _ => None,
            })
            .expect("fk");
        assert!(fk.deferrable);
        assert!(fk.initially_deferred);
        assert_eq!(fk.on_delete.as_deref(), Some("SET NULL"));

        let unique = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::UniqueConstraint(u) => Some(u),
                _ => None,
            })
            .expect("unique");
        assert_eq!(unique.name.as_ref(), "users_email_key");

        let pk = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::PrimaryKey(pk) if pk.table.as_ref() == "users" => Some(pk),
                _ => None,
            })
            .expect("pk");
        assert_eq!(pk.name.as_ref(), "users_pkey");
    }

    #[test]
    fn test_postgres_enum_view_policy_entities() {
        // P8: enums, views, and policies materialize as snapshot entities.
        use crate::postgres::ddl::PostgresEntity;

        let code = r#"
#[derive(PostgresEnum, Default, Clone)]
pub enum OrderStatus {
    #[default]
    Pending,
    Shipped,
}

#[PostgresTable(RLS)]
pub struct Orders {
    #[column(primary)]
    pub id: i32,
    #[column(enum)]
    pub status: OrderStatus,
}

#[PostgresView(definition = "SELECT id FROM orders WHERE status = 'Pending'")]
pub struct PendingOrders {
    pub id: i32,
}

#[PostgresPolicy(AS = "PERMISSIVE", FOR = "SELECT", TO(authenticated), USING = "true")]
pub struct OrdersPolicy(Orders);
"#;

        let snap = postgres_snapshot(code);

        let pg_enum = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::Enum(en) => Some(en),
                _ => None,
            })
            .expect("enum entity");
        assert_eq!(pg_enum.name.as_ref(), "OrderStatus");
        assert_eq!(pg_enum.schema.as_ref(), "public");
        assert_eq!(
            pg_enum
                .values
                .iter()
                .map(std::convert::AsRef::as_ref)
                .collect::<Vec<_>>(),
            vec!["Pending", "Shipped"]
        );

        let status = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::Column(c) if c.name.as_ref() == "status" => Some(c),
                _ => None,
            })
            .expect("status column");
        assert_eq!(status.sql_type.as_ref(), "OrderStatus");
        assert_eq!(status.type_schema.as_deref(), Some("public"));

        let orders = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::Table(t) => Some(t),
                _ => None,
            })
            .expect("orders table");
        assert_eq!(orders.is_rls_enabled, Some(true));

        let view = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::View(v) => Some(v),
                _ => None,
            })
            .expect("view entity");
        assert_eq!(view.name.as_ref(), "pending_orders");
        assert_eq!(
            view.definition.as_deref(),
            Some("SELECT id FROM orders WHERE status = 'Pending'")
        );

        let policy = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                PostgresEntity::Policy(p) => Some(p),
                _ => None,
            })
            .expect("policy entity");
        assert_eq!(policy.name.as_ref(), "orders_policy");
        assert_eq!(policy.table.as_ref(), "orders");
        assert_eq!(policy.as_clause.as_deref(), Some("PERMISSIVE"));
        assert_eq!(policy.using.as_deref(), Some("true"));
    }

    #[test]
    fn test_sqlite_view_entity() {
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Users {
    #[column(primary)]
    pub id: i64,
}

#[SQLiteView(definition = "SELECT id FROM users")]
pub struct AllUsers {
    pub id: i64,
}
"#;

        let snap = sqlite_snapshot(code);
        let view = snap
            .ddl
            .iter()
            .find_map(|e| match e {
                SqliteEntity::View(v) => Some(v),
                _ => None,
            })
            .expect("view entity");
        assert_eq!(view.name.as_ref(), "all_users");
        assert_eq!(view.definition.as_deref(), Some("SELECT id FROM users"));
        assert!(!view.is_existing);
    }

    #[test]
    fn test_casing_config_is_ignored_for_names() {
        // P15: the macros always snake-case inferred names; a camelCase
        // casing config must not change parser output.
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct UserAccounts {
    #[column(primary)]
    pub id: i64,
    pub displayName: String,
}
"#;

        let result = SchemaParser::parse(code);
        let snapshot =
            Snapshot::from_parse_result(&result, Dialect::SQLite, Some(Casing::CamelCase));
        let snap = match snapshot {
            Snapshot::Sqlite(s) => s,
            Snapshot::Postgres(_) => panic!("expected SQLite snapshot"),
        };

        assert!(
            snap.ddl
                .iter()
                .any(|e| matches!(e, SqliteEntity::Table(t) if t.name.as_ref() == "user_accounts"))
        );
        assert!(
            snap.ddl
                .iter()
                .any(|e| matches!(e, SqliteEntity::Column(c) if c.name.as_ref() == "display_name"))
        );
    }

    #[test]
    fn test_schema_struct_scopes_tables() {
        // Membership scoping matches runtime semantics: with a schema struct
        // present, only member tables (and their indexes) are emitted.
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Users {
    #[column(primary)]
    pub id: i64,
}

#[SQLiteTable]
pub struct Orphan {
    #[column(primary)]
    pub id: i64,
}

#[derive(SQLiteSchema)]
pub struct Schema {
    pub users: Users,
}
"#;

        let snap = sqlite_snapshot(code);
        let table_names: Vec<&str> = snap
            .ddl
            .iter()
            .filter_map(|e| match e {
                SqliteEntity::Table(t) => Some(t.name.as_ref()),
                _ => None,
            })
            .collect();
        assert_eq!(table_names, vec!["users"]);
    }

    #[test]
    fn test_no_schema_struct_keeps_all_tables() {
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable]
pub struct Users {
    #[column(primary)]
    pub id: i64,
}

#[SQLiteTable]
pub struct Posts {
    #[column(primary)]
    pub id: i64,
}
"#;

        let snap = sqlite_snapshot(code);
        let table_count = snap
            .ddl
            .iter()
            .filter(|e| matches!(e, SqliteEntity::Table(_)))
            .count();
        assert_eq!(table_count, 2);
    }

    #[test]
    fn test_sqlite_table_options_and_pk_name_are_preserved() {
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable(strict, without_rowid)]
pub struct Accounts {
    #[column(primary)]
    pub id: i64,
}
"#;

        let snap = sqlite_snapshot(code);
        let table = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::Table(t) = e {
                Some(t)
            } else {
                None
            }
        });
        let table = table.expect("expected sqlite table");
        assert!(table.strict, "strict should be preserved");
        assert!(table.without_rowid, "without_rowid should be preserved");

        let pk = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::PrimaryKey(pk) = e {
                Some(pk)
            } else {
                None
            }
        });
        let pk = pk.expect("expected sqlite primary key");
        // Canonical macro/introspection name (`types::sqlite::ddl::name_for_pk`).
        assert_eq!(pk.name.as_ref(), "accounts_pk");
    }

    #[test]
    fn test_sqlite_casing_preserves_explicit_names() {
        use crate::sqlite::SqliteEntity;

        let code = r#"
#[SQLiteTable(name = "users_tbl")]
pub struct UsersTable {
    #[column(name = "user_id", primary)]
    pub userId: i64,
    pub emailAddress: String,
}

#[SQLiteIndex(name = "users_tbl_email_idx")]
pub struct UsersEmailIdx(UsersTable::emailAddress);
"#;

        let result = SchemaParser::parse(code);
        let snapshot =
            Snapshot::from_parse_result(&result, Dialect::SQLite, Some(Casing::SnakeCase));
        let snap = match snapshot {
            Snapshot::Sqlite(s) => s,
            Snapshot::Postgres(_) => panic!("Expected SQLite snapshot"),
        };

        let table = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::Table(t) = e {
                Some(t)
            } else {
                None
            }
        });
        let table = table.expect("expected sqlite table");
        assert_eq!(table.name.as_ref(), "users_tbl");

        let user_id = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::Column(c) = e
                && c.name.as_ref() == "user_id"
            {
                Some(c)
            } else {
                None
            }
        });
        assert!(user_id.is_some(), "expected explicit column name user_id");

        let email_col = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::Column(c) = e
                && c.name.as_ref() == "email_address"
            {
                Some(c)
            } else {
                None
            }
        });
        assert!(
            email_col.is_some(),
            "expected inferred snake_case column name"
        );

        let index = snap.ddl.iter().find_map(|e| {
            if let SqliteEntity::Index(i) = e {
                Some(i)
            } else {
                None
            }
        });
        let index = index.expect("expected sqlite index");
        assert_eq!(index.name.as_ref(), "users_tbl_email_idx");
        assert_eq!(index.columns[0].value.as_ref(), "email_address");
    }

    #[test]
    fn test_postgres_casing_preserves_explicit_names() {
        use crate::postgres::ddl::PostgresEntity;

        let code = r#"
#[PostgresTable(schema = "auth", name = "users_tbl")]
pub struct UsersTable {
    #[column(name = "user_id", primary)]
    pub userId: i32,
    pub createdAt: String,
}

#[PostgresIndex(name = "users_tbl_created_idx")]
pub struct UsersCreatedIdx(UsersTable::createdAt);
"#;

        let result = SchemaParser::parse(code);
        let snapshot =
            Snapshot::from_parse_result(&result, Dialect::PostgreSQL, Some(Casing::SnakeCase));
        let snap = match snapshot {
            Snapshot::Postgres(s) => s,
            Snapshot::Sqlite(_) => panic!("Expected Postgres snapshot"),
        };

        let table = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::Table(t) = e {
                Some(t)
            } else {
                None
            }
        });
        let table = table.expect("expected postgres table");
        assert_eq!(table.schema.as_ref(), "auth");
        assert_eq!(table.name.as_ref(), "users_tbl");

        let user_id = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::Column(c) = e
                && c.name.as_ref() == "user_id"
            {
                Some(c)
            } else {
                None
            }
        });
        assert!(user_id.is_some(), "expected explicit column name user_id");

        let created_at = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::Column(c) = e
                && c.name.as_ref() == "created_at"
            {
                Some(c)
            } else {
                None
            }
        });
        assert!(
            created_at.is_some(),
            "expected inferred snake_case column name created_at"
        );

        let index = snap.ddl.iter().find_map(|e| {
            if let PostgresEntity::Index(i) = e {
                Some(i)
            } else {
                None
            }
        });
        let index = index.expect("expected postgres index");
        assert_eq!(index.name.as_ref(), "users_tbl_created_idx");
        assert_eq!(index.schema.as_ref(), "auth");
    }

    /// Test that changing a column from Option<String> to String generates table recreation
    #[test]
    fn test_nullable_to_not_null_generates_migration() {
        use crate::sqlite::collection::SQLiteDDL;
        use crate::sqlite::diff::compute_migration;

        // Previous schema: email is nullable (Option<String>)
        let prev_code = r#"
#[SQLiteTable]
pub struct User {
    #[column(primary)]
    pub id: i64,
    pub name: String,
    pub email: Option<String>,
}
"#;

        // Current schema: email is NOT nullable (String)
        let cur_code = r#"
#[SQLiteTable]
pub struct User {
    #[column(primary)]
    pub id: i64,
    pub name: String,
    pub email: String,
}
"#;

        let prev_snap = sqlite_snapshot(prev_code);
        let cur_snap = sqlite_snapshot(cur_code);

        let prev_ddl = SQLiteDDL::from_entities(prev_snap.ddl.clone());
        let cur_ddl = SQLiteDDL::from_entities(cur_snap.ddl.clone());

        // Check that previous email column is nullable and current is not
        let prev_email = prev_ddl
            .columns
            .one("user", "email")
            .expect("email column in prev");
        let cur_email = cur_ddl
            .columns
            .one("user", "email")
            .expect("email column in cur");
        assert!(!prev_email.not_null, "Previous email should be nullable");
        assert!(cur_email.not_null, "Current email should be NOT NULL");

        // Compute migration
        let migration = compute_migration(&prev_ddl, &cur_ddl);

        // Should have SQL statements for table recreation
        assert!(
            !migration.sql_statements.is_empty(),
            "Should generate migration SQL for nullable change"
        );

        // Verify individual SQL statements for table recreation pattern
        assert_eq!(migration.sql_statements[0], "PRAGMA foreign_keys=OFF;");
        assert!(
            migration.sql_statements[1].starts_with("CREATE TABLE `__new_user`"),
            "Expected CREATE TABLE `__new_user`, got: {}",
            migration.sql_statements[1]
        );
        assert!(
            migration.sql_statements[1].contains("`email` TEXT NOT NULL"),
            "New table should have NOT NULL on email: {}",
            migration.sql_statements[1]
        );
        assert_eq!(
            migration.sql_statements[2],
            "INSERT INTO `__new_user`(`id`, `name`, `email`) SELECT `id`, `name`, `email` FROM `user`;"
        );
        assert_eq!(migration.sql_statements[3], "DROP TABLE `user`;");
        assert_eq!(
            migration.sql_statements[4],
            "ALTER TABLE `__new_user` RENAME TO `user`;"
        );
        assert_eq!(migration.sql_statements[5], "PRAGMA foreign_keys=ON;");
    }

    /// Test that changing a column from String to Option<String> generates table recreation
    #[test]
    fn test_not_null_to_nullable_generates_migration() {
        use crate::sqlite::collection::SQLiteDDL;
        use crate::sqlite::diff::compute_migration;

        // Previous schema: email is NOT nullable (String)
        let prev_code = r#"
#[SQLiteTable]
pub struct User {
    #[column(primary)]
    pub id: i64,
    pub email: String,
}
"#;

        // Current schema: email is nullable (Option<String>)
        let cur_code = r#"
#[SQLiteTable]
pub struct User {
    #[column(primary)]
    pub id: i64,
    pub email: Option<String>,
}
"#;

        let prev_snap = sqlite_snapshot(prev_code);
        let cur_snap = sqlite_snapshot(cur_code);

        let prev_ddl = SQLiteDDL::from_entities(prev_snap.ddl.clone());
        let cur_ddl = SQLiteDDL::from_entities(cur_snap.ddl.clone());

        // Compute migration
        let migration = compute_migration(&prev_ddl, &cur_ddl);

        // Should have SQL statements for table recreation
        assert!(
            !migration.sql_statements.is_empty(),
            "Should generate migration SQL for nullable change"
        );

        // Verify individual SQL statements for table recreation pattern
        assert_eq!(migration.sql_statements[0], "PRAGMA foreign_keys=OFF;");
        assert!(
            migration.sql_statements[1].starts_with("CREATE TABLE `__new_user`"),
            "Expected CREATE TABLE `__new_user`, got: {}",
            migration.sql_statements[1]
        );
        assert_eq!(migration.sql_statements[3], "DROP TABLE `user`;");
        assert_eq!(
            migration.sql_statements[4],
            "ALTER TABLE `__new_user` RENAME TO `user`;"
        );
        assert_eq!(migration.sql_statements[5], "PRAGMA foreign_keys=ON;");
    }
}