ormer 0.2.8

A minimalist ORM framework that supports SQLite, PostgreSQL, MySQL, and SqlServer
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
//! Versioned schema migration primitives.
//!
//! The migration API deliberately keeps migration steps structured until the
//! final dialect rendering stage.  Applications can therefore inspect a plan
//! before executing it, while small hand-written migrations remain possible.

use crate::abstract_layer::DbType;
use crate::abstract_layer::common::{Database, Transaction, common_helpers};
#[cfg(any(feature = "postgresql", feature = "mysql"))]
use crate::model::CompressionAlgorithm;
use crate::model::{ColumnSchema, WritableModel};
use std::collections::{BTreeMap, BTreeSet};
use std::marker::PhantomData;

pub const MIGRATION_TABLE_NAME: &str = "__ormer_migrations";

/// A single migration operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MigrationStep {
    CreateType {
        name: String,
        definition: String,
    },
    AlterType {
        name: String,
        definition: String,
    },
    CreateTable {
        table: String,
        definition: String,
    },
    AddColumn {
        table: String,
        column: String,
        definition: String,
    },
    BackfillColumn {
        table: String,
        column: String,
        expression: String,
    },
    AlterColumn {
        table: String,
        column: String,
        definition: String,
        using: Option<String>,
    },
    AddConstraint {
        table: String,
        definition: String,
    },
    CreateIndex {
        name: String,
        table: String,
        columns: Vec<String>,
        unique: bool,
    },
    AddForeignKey {
        table: String,
        column: String,
        ref_table: String,
        ref_column: String,
    },
    Sql {
        sql: String,
    },
}

impl MigrationStep {
    pub fn sql(&self, db_type: DbType) -> crate::Result<String> {
        let table = crate::model::quote_qualified_identifier(db_type, table_name(self));
        let columns = |names: &[String]| {
            names
                .iter()
                .map(|name| crate::model::quote_identifier(db_type, name))
                .collect::<Vec<_>>()
                .join(", ")
        };

        match self {
            Self::CreateType { definition, .. } | Self::AlterType { definition, .. } => {
                Ok(definition.clone())
            }
            Self::CreateTable { definition, .. } => Ok(definition.clone()),
            Self::AddColumn {
                column, definition, ..
            } => {
                let column = crate::model::quote_identifier(db_type, column);
                Ok(format!(
                    "ALTER TABLE {table} ADD COLUMN {column} {definition}"
                ))
            }
            Self::BackfillColumn {
                column, expression, ..
            } => {
                let column = crate::model::quote_identifier(db_type, column);
                Ok(format!("UPDATE {table} SET {column} = {expression}"))
            }
            Self::AlterColumn {
                column,
                definition,
                using,
                ..
            } => {
                let _ = (definition, using);
                let column = crate::model::quote_identifier(db_type, column);
                match db_type {
                    #[cfg(feature = "sqlite")]
                    DbType::Sqlite => Err(crate::ormer_error!(
                        "SQLite does not support ALTER COLUMN; use a hand-written table rebuild"
                    )),
                    #[cfg(feature = "postgresql")]
                    DbType::PostgreSQL => {
                        let using = using
                            .as_deref()
                            .map(|expression| format!(" USING {expression}"))
                            .unwrap_or_default();
                        Ok(format!(
                            "ALTER TABLE {table} ALTER COLUMN {column} {definition}{using}"
                        ))
                    }
                    #[cfg(feature = "mysql")]
                    DbType::MySQL => Ok(format!(
                        "ALTER TABLE {table} MODIFY COLUMN {column} {definition}"
                    )),
                    #[cfg(feature = "mssql")]
                    DbType::MSSQL => Ok(format!(
                        "ALTER TABLE {table} ALTER COLUMN {column} {definition}"
                    )),
                    #[cfg(feature = "duckdb")]
                    DbType::DuckDB => {
                        if using.is_some() {
                            return Err(crate::ormer_error!(
                                "DuckDB does not support USING expressions in ALTER COLUMN"
                            ));
                        }
                        Ok(format!(
                            "ALTER TABLE {table} ALTER COLUMN {column} {definition}"
                        ))
                    }
                    #[cfg(feature = "clickhouse")]
                    DbType::ClickHouse => {
                        if using.is_some() {
                            return Err(crate::ormer_error!(
                                "ClickHouse does not support USING expressions in ALTER COLUMN"
                            ));
                        }
                        Ok(format!(
                            "ALTER TABLE {table} MODIFY COLUMN {column} {definition}"
                        ))
                    }
                }
            }
            Self::AddConstraint { definition, .. } => {
                Ok(format!("ALTER TABLE {table} ADD {definition}"))
            }
            Self::CreateIndex {
                name,
                columns: index_columns,
                unique,
                ..
            } => {
                let unique = if *unique { " UNIQUE" } else { "" };
                let if_not_exists = match db_type {
                    #[cfg(feature = "mysql")]
                    DbType::MySQL => "",
                    #[cfg(feature = "mssql")]
                    DbType::MSSQL => "",
                    #[cfg(feature = "sqlite")]
                    DbType::Sqlite => " IF NOT EXISTS",
                    #[cfg(feature = "postgresql")]
                    DbType::PostgreSQL => " IF NOT EXISTS",
                    #[cfg(any(feature = "duckdb", feature = "clickhouse"))]
                    _ => " IF NOT EXISTS",
                };
                Ok(format!(
                    "CREATE{unique} INDEX{if_not_exists} {} ON {table} ({})",
                    crate::model::quote_identifier(db_type, name),
                    columns(index_columns)
                ))
            }
            Self::AddForeignKey {
                column,
                ref_table,
                ref_column,
                ..
            } => {
                #[cfg(feature = "sqlite")]
                if matches!(db_type, DbType::Sqlite) {
                    return Err(crate::ormer_error!(
                        "SQLite does not support adding a foreign key after table creation"
                    ));
                }
                Ok(format!(
                    "ALTER TABLE {table} ADD FOREIGN KEY ({}) REFERENCES {} ({})",
                    crate::model::quote_identifier(db_type, column),
                    crate::model::quote_qualified_identifier(db_type, ref_table),
                    crate::model::quote_identifier(db_type, ref_column)
                ))
            }
            Self::Sql { sql } => Ok(sql.clone()),
        }
    }
}

fn table_name(step: &MigrationStep) -> &str {
    match step {
        MigrationStep::AddColumn { table, .. }
        | MigrationStep::BackfillColumn { table, .. }
        | MigrationStep::AlterColumn { table, .. }
        | MigrationStep::AddConstraint { table, .. }
        | MigrationStep::CreateIndex { table, .. }
        | MigrationStep::AddForeignKey { table, .. } => table,
        _ => "",
    }
}

/// A deterministic, inspectable migration plan.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrationPlan {
    table_name: String,
    db_type: DbType,
    steps: Vec<MigrationStep>,
    warnings: Vec<String>,
}

impl MigrationPlan {
    pub fn new(table_name: impl Into<String>, db_type: DbType) -> Self {
        Self {
            table_name: table_name.into(),
            db_type,
            steps: Vec::new(),
            warnings: Vec::new(),
        }
    }

    pub fn table_name(&self) -> &str {
        &self.table_name
    }

    pub fn db_type(&self) -> DbType {
        self.db_type
    }

    pub fn steps(&self) -> &[MigrationStep] {
        &self.steps
    }

    pub fn warnings(&self) -> &[String] {
        &self.warnings
    }

    pub fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    pub fn to_sql(&self) -> crate::Result<String> {
        let mut sql = Vec::with_capacity(self.steps.len());
        for step in &self.steps {
            sql.push(step.sql(self.db_type)?);
        }
        Ok(sql.join(";\n"))
    }

    fn push(&mut self, step: MigrationStep) {
        self.steps.push(step);
    }
}

/// A migration file/definition with a stable version identifier.
pub trait Migration: Send + Sync {
    fn version(&self) -> u64;
    fn name(&self) -> &str;
    fn up(&self) -> Vec<MigrationStep>;

    fn down(&self) -> Vec<MigrationStep> {
        Vec::new()
    }

    fn checksum(&self) -> u64 {
        let mut hash = 0xcbf29ce484222325u64;
        for byte in self
            .name()
            .bytes()
            .chain(format!("{:?}", self.up()).bytes())
            .chain(format!("{:?}", self.down()).bytes())
        {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(0x100000001b3);
        }
        hash
    }
}

impl<T: Migration + ?Sized> Migration for &T {
    fn version(&self) -> u64 {
        (**self).version()
    }

    fn name(&self) -> &str {
        (**self).name()
    }

    fn up(&self) -> Vec<MigrationStep> {
        (**self).up()
    }

    fn down(&self) -> Vec<MigrationStep> {
        (**self).down()
    }

    fn checksum(&self) -> u64 {
        (**self).checksum()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrationInfo {
    pub version: u64,
    pub name: String,
    pub checksum: u64,
}

/// One executable statement in a migration dry-run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrationDryRunStep {
    pub version: u64,
    pub migration_name: String,
    pub migration_index: usize,
    pub step_index: usize,
    pub sql: String,
}

/// An inspectable execution plan for pending versioned migrations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrationDryRun {
    pub backend: DbType,
    pub transactional: bool,
    pub completed_versions: Vec<u64>,
    pub steps: Vec<MigrationDryRunStep>,
    pub warnings: Vec<String>,
}

/// The precise resume point after a migration was interrupted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrationExecutionStatus {
    pub backend: DbType,
    pub completed: Vec<MigrationInfo>,
    pub pending: Vec<MigrationInfo>,
    pub resume_version: Option<u64>,
    pub transactional: bool,
}

impl MigrationInfo {
    pub fn new(version: u64, name: impl Into<String>) -> Self {
        Self {
            version,
            name: name.into(),
            checksum: 0,
        }
    }

    fn with_checksum(version: u64, name: impl Into<String>, checksum: u64) -> Self {
        Self {
            version,
            name: name.into(),
            checksum,
        }
    }
}

/// A reusable migration runner over a statically owned migration list.
pub struct MigrationRunner<'a, M: Migration> {
    db: &'a Database,
    migrations: &'a [M],
}

impl<'a, M: Migration> MigrationRunner<'a, M> {
    pub fn new(db: &'a Database, migrations: &'a [M]) -> Self {
        Self { db, migrations }
    }

    pub async fn pending(&self) -> crate::Result<Vec<MigrationInfo>> {
        self.db.pending_migrations(self.migrations).await
    }

    pub async fn execute(&self) -> crate::Result<usize> {
        self.db.apply_migrations(self.migrations).await
    }

    /// Render every pending statement without executing it.
    ///
    /// For non-transactional backends each item is one statement, so a failed
    /// statement identifies the exact recovery point.
    pub async fn dry_run(&self) -> crate::Result<MigrationDryRun> {
        let backend = self.db.db_type();
        let transactional = backend.is_transactional();
        let pending = self.pending().await?;
        let applied: BTreeSet<u64> = self
            .db
            .migration_history()
            .await?
            .into_iter()
            .map(|migration| migration.version)
            .collect();
        let mut warnings = Vec::new();
        if !transactional {
            warnings.push(
                "backend migrations are not transactional; resume from the first pending version"
                    .to_string(),
            );
        }

        let mut by_version = self
            .migrations
            .iter()
            .map(|migration| (migration.version(), migration))
            .collect::<BTreeMap<_, _>>();
        let mut steps = Vec::new();
        for (migration_index, info) in pending.iter().enumerate() {
            let migration = by_version
                .remove(&info.version)
                .ok_or_else(|| crate::OrmerError::migration("pending migration disappeared"))?;
            for (step_index, step) in migration.up().into_iter().enumerate() {
                let sql = step.sql(backend)?;
                if !transactional && sql.contains(';') {
                    return Err(crate::OrmerError::migration(format!(
                        "non-transactional migration {} step {} must contain one statement",
                        info.version, step_index
                    )));
                }
                steps.push(MigrationDryRunStep {
                    version: info.version,
                    migration_name: info.name.clone(),
                    migration_index,
                    step_index,
                    sql,
                });
            }
        }

        Ok(MigrationDryRun {
            backend,
            transactional,
            completed_versions: applied.into_iter().collect(),
            steps,
            warnings,
        })
    }

    /// Report completed work and the next safe resume version.
    pub async fn execution_status(&self) -> crate::Result<MigrationExecutionStatus> {
        let backend = self.db.db_type();
        let completed = self.db.migration_history().await?;
        let pending = self.pending().await?;
        let resume_version = pending.first().map(|migration| migration.version);
        Ok(MigrationExecutionStatus {
            backend,
            transactional: backend.is_transactional(),
            completed,
            pending,
            resume_version,
        })
    }

    pub async fn rollback_last(&self) -> crate::Result<()> {
        let history = self.db.migration_history().await?;
        let Some(last) = history.last() else {
            return Err(crate::OrmerError::migration(
                "no applied migrations to roll back",
            ));
        };
        let migration = self
            .migrations
            .iter()
            .find(|migration| migration.version() == last.version)
            .ok_or_else(|| {
                crate::OrmerError::migration(format!(
                    "migration {} is not present in the configured migration list",
                    last.version
                ))
            })?;
        let steps = migration.down();
        if steps.is_empty() {
            return Err(crate::OrmerError::UnsupportedFeature {
                backend: self.db.db_type(),
                feature: "migration rollback without down steps",
            });
        }

        let mut transaction = self.db.begin().await?;
        let result = async {
            execute_steps(&mut transaction, self.db.db_type(), &steps).await?;
            transaction
                .execute_sql(format!(
                    "DELETE FROM {MIGRATION_TABLE_NAME} WHERE version = {}",
                    last.version
                ))
                .await?;
            Ok::<(), crate::OrmerError>(())
        }
        .await;
        match result {
            Ok(()) => transaction.commit().await,
            Err(error) => {
                let _ = transaction.rollback().await;
                Err(error)
            }
        }
    }
}

/// Builder returned by `Database::migrate_table`.
pub struct TableMigration<'a, T: WritableModel> {
    db: &'a Database,
    marker: PhantomData<T>,
}

impl<'a, T: WritableModel> TableMigration<'a, T> {
    #[cfg(feature = "sqlite")]
    pub async fn sqlite_rebuild_plan(&self) -> crate::Result<MigrationPlan> {
        let db_type = self.db.db_type();
        if !matches!(db_type, DbType::Sqlite) {
            return Err(crate::OrmerError::UnsupportedFeature {
                backend: db_type,
                feature: "SQLite table rebuild",
            });
        }

        let table_name = T::table_name_for_db(db_type);
        let actual = self.db.schema_columns(table_name).await?.ok_or_else(|| {
            crate::ormer_error!(
                "SQLite table {} does not exist; use create_table instead of a rebuild",
                table_name
            )
        })?;
        let actual_by_name = actual
            .iter()
            .map(|column| (column.name.as_str(), column))
            .collect::<BTreeMap<_, _>>();
        let mut plan = MigrationPlan::new(table_name, db_type);
        plan.warnings.push(
            "SQLite rebuild replaces the table in one transaction; review copy rules before execution"
                .to_string(),
        );
        plan.push(MigrationStep::Sql {
            sql: sqlite_rebuild_sql::<T>(table_name, &actual_by_name)?,
        });
        Ok(plan)
    }

    #[cfg(feature = "sqlite")]
    pub async fn sqlite_rebuild_sql(&self) -> crate::Result<String> {
        self.sqlite_rebuild_plan().await?.to_sql()
    }

    pub async fn plan(&self) -> crate::Result<MigrationPlan> {
        let db_type = self.db.db_type();
        let table_name = T::table_name_for_db(db_type);
        let mut plan = MigrationPlan::new(table_name, db_type);
        let actual = self.db.schema_columns(table_name).await?;

        let Some(actual) = actual else {
            plan.push(MigrationStep::CreateTable {
                table: table_name.to_string(),
                definition: crate::generate_create_table_sql::<T>(db_type)?,
            });
            return Ok(plan);
        };

        self.db.validate_hypertable_for_migration::<T>().await?;

        let actual_names: BTreeSet<&str> =
            actual.iter().map(|column| column.name.as_str()).collect();
        let expected_names: BTreeSet<&str> =
            T::COLUMN_SCHEMA.iter().map(|column| column.name).collect();
        let actual_by_name: BTreeMap<&str, &SchemaColumn> = actual
            .iter()
            .map(|column| (column.name.as_str(), column))
            .collect();

        for column in &actual {
            if !expected_names.contains(column.name.as_str()) {
                return Err(crate::ormer_error!(
                    "Cannot migrate table {} because existing column {} is not present in the model",
                    table_name,
                    column.name
                ));
            }
        }

        // Additive changes are safe to infer when a non-null column can be
        // populated. A populated table without a model default needs an
        // explicit backfill because the ORM cannot invent its value.
        let mut added_columns = BTreeSet::new();
        for column in T::COLUMN_SCHEMA {
            if !actual_names.contains(column.name) {
                if column.is_primary {
                    return Err(crate::ormer_error!(
                        "Cannot infer adding primary key column {}; write an explicit migration",
                        column.name
                    ));
                }
                if !column.is_nullable
                    && column.default.is_none()
                    && self.db.table_row_count(table_name).await? > 0
                {
                    return Err(crate::ormer_error!(
                        "Cannot add NOT NULL column {} to populated table {}; \
                         write an explicit migration with a backfill",
                        column.name,
                        table_name
                    ));
                }
                plan.push(MigrationStep::AddColumn {
                    table: table_name.to_string(),
                    column: column.name.to_string(),
                    definition: column_definition(db_type, column)?,
                });
                added_columns.insert(column.name);
            }
        }

        if !added_columns.is_empty() {
            let available_columns =
                |name: &str| actual_names.contains(name) || added_columns.contains(name);

            let mut indexes = BTreeMap::<i32, Vec<&ColumnSchema>>::new();
            for column in T::COLUMN_SCHEMA {
                if !column.is_indexed {
                    continue;
                }
                if let Some(group) = column.index_group {
                    indexes.entry(group).or_default().push(column);
                } else if added_columns.contains(column.name) {
                    indexes.entry(i32::MIN).or_default().push(column);
                }
            }

            for (group, columns) in indexes {
                if group != i32::MIN
                    && !columns
                        .iter()
                        .any(|column| added_columns.contains(column.name))
                {
                    continue;
                }
                if !columns.iter().all(|column| available_columns(column.name)) {
                    continue;
                }
                let name = columns
                    .iter()
                    .find_map(|column| column.index_name)
                    .map(ToString::to_string)
                    .unwrap_or_else(|| {
                        if group == i32::MIN {
                            format!("idx_{}_{}", table_name.replace('.', "_"), columns[0].name)
                        } else {
                            format!("idx_{}_{}", table_name.replace('.', "_"), group)
                        }
                    });
                plan.push(index_migration_step(
                    db_type, name, table_name, &columns, false,
                )?);
            }

            let mut unique_groups = BTreeMap::<i32, Vec<&ColumnSchema>>::new();
            for column in T::COLUMN_SCHEMA {
                if let Some(group) = column.unique_group {
                    unique_groups.entry(group).or_default().push(column);
                }
            }
            for (group, columns) in unique_groups {
                if !columns
                    .iter()
                    .any(|column| added_columns.contains(column.name))
                    || !columns.iter().all(|column| available_columns(column.name))
                {
                    continue;
                }
                let name = columns
                    .iter()
                    .find_map(|column| column.unique_name)
                    .map(ToString::to_string)
                    .unwrap_or_else(|| format!("uq_{}_{}", table_name.replace('.', "_"), group));
                plan.push(index_migration_step(
                    db_type, name, table_name, &columns, true,
                )?);
            }

            for column in T::COLUMN_SCHEMA {
                if !added_columns.contains(column.name) {
                    continue;
                }
                if let Some(foreign_key) = &column.foreign_key {
                    let sqlite_backend = {
                        #[cfg(feature = "sqlite")]
                        {
                            matches!(db_type, DbType::Sqlite)
                        }
                        #[cfg(not(feature = "sqlite"))]
                        {
                            false
                        }
                    };
                    if sqlite_backend {
                        return Err(crate::ormer_error!(
                            "Cannot add foreign key column {} to SQLite table {}; write an explicit table-rebuild migration",
                            column.name,
                            table_name
                        ));
                    }
                    plan.push(MigrationStep::AddForeignKey {
                        table: table_name.to_string(),
                        column: column.name.to_string(),
                        ref_table: crate::model::normalize_table_name_for_db(
                            db_type,
                            foreign_key.ref_table,
                        )
                        .to_string(),
                        ref_column: foreign_key.get_ref_column().to_string(),
                    });
                }
            }
        }

        #[cfg(feature = "sqlite")]
        let mut sqlite_rebuild_required = false;
        for expected in T::COLUMN_SCHEMA {
            let Some(actual) = actual_by_name.get(expected.name) else {
                continue;
            };
            if actual.primary_key != expected.is_primary {
                return Err(crate::ormer_error!(
                    "Cannot infer primary-key migration for column {}; write an explicit migration",
                    expected.name
                ));
            }

            if actual.type_name.is_empty() {
                return Err(crate::ormer_error!(
                    "Cannot determine the database type of column {}",
                    expected.name
                ));
            }

            let expected_type = column_type_definition(db_type, expected);
            let type_changed = !types_equivalent(db_type, &actual.type_name, &expected_type);
            let nullable_changed = !expected.is_primary && actual.nullable != expected.is_nullable;
            let compression_changed = {
                #[cfg(feature = "postgresql")]
                if matches!(db_type, DbType::PostgreSQL) {
                    let expected_compression = crate::model::column_compression_algorithm(expected);
                    actual.compression.as_deref()
                        != expected_compression.map(CompressionAlgorithm::as_str)
                } else {
                    false
                }
                #[cfg(not(feature = "postgresql"))]
                {
                    false
                }
            };

            if !type_changed && !nullable_changed && !compression_changed {
                continue;
            }

            if expected.is_primary && type_changed {
                return Err(crate::ormer_error!(
                    "Cannot infer primary-key type migration for column {}; write an explicit migration",
                    expected.name
                ));
            }

            #[cfg(feature = "sqlite")]
            if matches!(db_type, DbType::Sqlite) {
                sqlite_rebuild_required = true;
                continue;
            }

            #[cfg(any(
                feature = "postgresql",
                feature = "mysql",
                feature = "mssql",
                feature = "duckdb"
            ))]
            {
                if type_changed {
                    let (definition, using) = match db_type {
                        #[cfg(feature = "postgresql")]
                        DbType::PostgreSQL => (
                            format!("TYPE {expected_type}"),
                            postgresql_using_expression(db_type, actual, expected, &expected_type),
                        ),
                        #[cfg(feature = "mysql")]
                        DbType::MySQL => (column_definition(db_type, expected)?, None),
                        #[cfg(feature = "mssql")]
                        DbType::MSSQL => (column_definition(db_type, expected)?, None),
                        #[cfg(feature = "duckdb")]
                        DbType::DuckDB => (format!("SET DATA TYPE {expected_type}"), None),
                        #[cfg(feature = "sqlite")]
                        DbType::Sqlite => unreachable!("SQLite uses table rebuilds"),
                        #[cfg(feature = "clickhouse")]
                        DbType::ClickHouse => (column_definition(db_type, expected)?, None),
                    };
                    plan.push(MigrationStep::AlterColumn {
                        table: table_name.to_string(),
                        column: expected.name.to_string(),
                        definition,
                        using,
                    });
                }

                if nullable_changed && !type_changed {
                    let definition = match db_type {
                        #[cfg(feature = "postgresql")]
                        DbType::PostgreSQL => {
                            if expected.is_nullable {
                                "DROP NOT NULL".to_string()
                            } else {
                                "SET NOT NULL".to_string()
                            }
                        }
                        #[cfg(feature = "mysql")]
                        DbType::MySQL => column_definition(db_type, expected)?,
                        #[cfg(feature = "mssql")]
                        DbType::MSSQL => column_definition(db_type, expected)?,
                        #[cfg(feature = "sqlite")]
                        DbType::Sqlite => unreachable!("SQLite uses table rebuilds"),
                        #[cfg(feature = "duckdb")]
                        DbType::DuckDB => {
                            if expected.is_nullable {
                                "DROP NOT NULL".to_string()
                            } else {
                                "SET NOT NULL".to_string()
                            }
                        }
                        #[cfg(feature = "clickhouse")]
                        DbType::ClickHouse => column_definition(db_type, expected)?,
                    };
                    plan.push(MigrationStep::AlterColumn {
                        table: table_name.to_string(),
                        column: expected.name.to_string(),
                        definition,
                        using: None,
                    });
                }

                #[cfg(feature = "postgresql")]
                if compression_changed {
                    let expected_compression = crate::model::column_compression_algorithm(expected);
                    if let Some(step) = column_compression_migration_step(
                        db_type,
                        table_name,
                        expected.name,
                        expected_compression,
                    )? {
                        plan.push(step);
                    }
                }
            }
        }

        #[cfg(feature = "mysql")]
        if matches!(db_type, DbType::MySQL) {
            let expected_compression = crate::model::table_compression_algorithm::<T>()?;
            let actual_compression = actual
                .iter()
                .find_map(|column| column.compression.as_deref())
                .and_then(parse_compression_algorithm);
            if actual_compression != expected_compression {
                if let Some(step) = column_compression_migration_step(
                    db_type,
                    table_name,
                    "",
                    expected_compression,
                )? {
                    plan.push(step);
                }
            }
        }

        #[cfg(feature = "sqlite")]
        if sqlite_rebuild_required {
            plan.steps.clear();
            plan.push(MigrationStep::Sql {
                sql: sqlite_rebuild_sql::<T>(table_name, &actual_by_name)?,
            });
        }

        Ok(plan)
    }

    pub async fn execute(&self) -> crate::Result<()> {
        let plan = self.plan().await?;
        if plan.is_empty() {
            return Ok(());
        }

        let mut transaction = self.db.begin().await?;
        let result = execute_steps(&mut transaction, plan.db_type(), &plan.steps).await;
        match result {
            Ok(()) => transaction.commit().await,
            Err(error) => {
                let _ = transaction.rollback().await;
                Err(error)
            }
        }
    }
}

fn column_definition(db_type: DbType, column: &ColumnSchema) -> crate::Result<String> {
    let mut definition = db_type.sql_type(
        column.data_type.unwrap_or(column.rust_type),
        false,
        column.is_auto_increment,
        column.is_nullable,
        column.enum_variants,
    );

    #[cfg(feature = "postgresql")]
    if let Some(compression) = crate::model::column_compression_algorithm(column) {
        if matches!(db_type, DbType::PostgreSQL) {
            if !matches!(
                compression,
                CompressionAlgorithm::Pglz | CompressionAlgorithm::Lz4
            ) {
                return Err(crate::ormer_error!(
                    "PostgreSQL does not support compression algorithm {}",
                    compression.as_str()
                ));
            }
            let suffix = " NOT NULL";
            let nullable = definition.ends_with(suffix);
            if nullable {
                definition.truncate(definition.len() - suffix.len());
            }
            definition.push_str(" COMPRESSION ");
            definition.push_str(compression.as_str());
            if nullable {
                definition.push_str(suffix);
            }
        }
    }

    if let Some(default) = column.default {
        definition.push_str(" DEFAULT ");
        definition.push_str(&default.to_sql(db_type));
    }

    validate_compression(db_type, column)?;

    if let Some(check) = column.check {
        definition.push_str(" CHECK (");
        definition.push_str(check.expr);
        definition.push(')');
    }

    Ok(definition)
}

fn validate_compression(db_type: DbType, column: &ColumnSchema) -> crate::Result<()> {
    let Some(algorithm) = crate::model::column_compression_algorithm(column) else {
        return Ok(());
    };

    match db_type {
        #[cfg(feature = "postgresql")]
        DbType::PostgreSQL => {
            if !matches!(
                algorithm,
                CompressionAlgorithm::Pglz | CompressionAlgorithm::Lz4
            ) {
                return Err(crate::ormer_error!(
                    "PostgreSQL does not support compression algorithm {} for column {}",
                    algorithm.as_str(),
                    column.name
                ));
            }
        }
        #[cfg(feature = "mysql")]
        DbType::MySQL => {
            if !matches!(
                algorithm,
                CompressionAlgorithm::Lz4 | CompressionAlgorithm::Zlib
            ) {
                return Err(crate::ormer_error!(
                    "MySQL does not support compression algorithm {} for column {}",
                    algorithm.as_str(),
                    column.name
                ));
            }
        }
        _ => {
            return Err(crate::OrmerError::UnsupportedFeature {
                backend: db_type,
                feature: "column compression",
            });
        }
    }
    Ok(())
}

#[cfg(any(feature = "postgresql", feature = "mysql"))]
fn column_compression_migration_step(
    db_type: DbType,
    table_name: &str,
    column_name: &str,
    compression: Option<CompressionAlgorithm>,
) -> crate::Result<Option<MigrationStep>> {
    #[cfg(feature = "postgresql")]
    if matches!(db_type, DbType::PostgreSQL) {
        let method = compression
            .map(CompressionAlgorithm::as_str)
            .unwrap_or("default");
        return Ok(Some(MigrationStep::Sql {
            sql: format!(
                "ALTER TABLE {} ALTER COLUMN {} SET COMPRESSION {}",
                crate::model::quote_qualified_identifier(DbType::PostgreSQL, table_name),
                crate::model::quote_identifier(DbType::PostgreSQL, column_name),
                method,
            ),
        }));
    }

    #[cfg(feature = "mysql")]
    if matches!(db_type, DbType::MySQL) {
        let method = compression
            .map(CompressionAlgorithm::as_upper_str)
            .unwrap_or("NONE");
        return Ok(Some(MigrationStep::Sql {
            sql: format!(
                "ALTER TABLE {} COMPRESSION='{}'",
                crate::model::quote_qualified_identifier(DbType::MySQL, table_name),
                method,
            ),
        }));
    }

    let _ = (db_type, table_name, column_name, compression);
    Ok(None)
}

#[cfg(feature = "mysql")]
fn parse_compression_algorithm(value: &str) -> Option<CompressionAlgorithm> {
    match value.to_ascii_lowercase().as_str() {
        "pglz" => Some(CompressionAlgorithm::Pglz),
        "lz4" => Some(CompressionAlgorithm::Lz4),
        "zlib" => Some(CompressionAlgorithm::Zlib),
        "zstd" => Some(CompressionAlgorithm::Zstd),
        _ => None,
    }
}

fn index_migration_step(
    db_type: DbType,
    name: String,
    table: &str,
    columns: &[&ColumnSchema],
    unique: bool,
) -> crate::Result<MigrationStep> {
    let has_order_or_predicate = columns
        .iter()
        .any(|column| column.index_order.is_some() || column.index_where.is_some());
    if !has_order_or_predicate {
        return Ok(MigrationStep::CreateIndex {
            name,
            table: table.to_string(),
            columns: columns
                .iter()
                .map(|column| column.name.to_string())
                .collect(),
            unique,
        });
    }
    let has_predicate = columns.iter().any(|column| column.index_where.is_some());
    if has_predicate {
        #[cfg(feature = "sqlite")]
        if matches!(db_type, DbType::Sqlite) {
            return Err(common_helpers::unsupported_partial_index_where(db_type));
        }
        #[cfg(feature = "duckdb")]
        if matches!(db_type, DbType::DuckDB) {
            return Err(common_helpers::unsupported_partial_index_where(db_type));
        }
    }

    let unique_sql = if unique { " UNIQUE" } else { "" };
    let if_not_exists = match db_type {
        #[cfg(feature = "mysql")]
        DbType::MySQL => "",
        #[cfg(feature = "mssql")]
        DbType::MSSQL => "",
        #[cfg(feature = "sqlite")]
        DbType::Sqlite => " IF NOT EXISTS",
        #[cfg(feature = "postgresql")]
        DbType::PostgreSQL => " IF NOT EXISTS",
        #[cfg(any(feature = "duckdb", feature = "clickhouse"))]
        _ => " IF NOT EXISTS",
    };
    let columns_sql = columns
        .iter()
        .map(|column| {
            let mut value = crate::model::quote_identifier(db_type, column.name);
            if let Some(order) = column.index_order {
                value.push(' ');
                value.push_str(order);
            }
            value
        })
        .collect::<Vec<_>>()
        .join(", ");
    let predicate = columns
        .iter()
        .find_map(|column| column.index_where)
        .map(|where_clause| format!(" WHERE {where_clause}"))
        .unwrap_or_default();
    Ok(MigrationStep::Sql {
        sql: format!(
            "CREATE{unique_sql} INDEX{if_not_exists} {} ON {} ({columns_sql}){predicate}",
            crate::model::quote_identifier(db_type, &name),
            crate::model::quote_qualified_identifier(db_type, table),
        ),
    })
}

fn column_type_definition(db_type: DbType, column: &ColumnSchema) -> String {
    db_type.sql_type(
        column.data_type.unwrap_or(column.rust_type),
        false,
        false,
        true,
        column.enum_variants,
    )
}

fn types_equivalent(db_type: DbType, actual: &str, expected: &str) -> bool {
    normalize_type(db_type, actual) == normalize_type(db_type, expected)
}

fn normalize_type(db_type: DbType, type_name: &str) -> String {
    let upper = type_name
        .trim()
        .to_ascii_uppercase()
        .replace(" NOT NULL", "");
    match db_type {
        #[cfg(feature = "sqlite")]
        DbType::Sqlite => {
            if upper.contains("INT") {
                "INTEGER".to_string()
            } else if upper.contains("CHAR") || upper.contains("CLOB") || upper.contains("TEXT") {
                "TEXT".to_string()
            } else if upper.contains("REAL")
                || upper.contains("FLOA")
                || upper.contains("DOUB")
                || upper.contains("NUM")
            {
                "REAL".to_string()
            } else if upper.contains("BLOB") || upper.is_empty() {
                "BLOB".to_string()
            } else {
                upper
            }
        }
        #[cfg(feature = "postgresql")]
        DbType::PostgreSQL => {
            let compact = upper.replace(' ', "");
            match compact.as_str() {
                "_TEXT" | "TEXT[]" | "_VARCHAR" | "VARCHAR[]" | "_BPCHAR" | "CHAR[]" => {
                    "TEXT[]".to_string()
                }
                "_INT2" | "INT2[]" | "SMALLINT[]" => "SMALLINT[]".to_string(),
                "_INT4" | "INT4[]" | "INTEGER[]" => "INTEGER[]".to_string(),
                "_INT8" | "INT8[]" | "BIGINT[]" => "BIGINT[]".to_string(),
                "INT2" | "SMALLINT" => "SMALLINT".to_string(),
                "INT4" | "INT" | "INTEGER" | "SERIAL" => "INTEGER".to_string(),
                "INT8" | "BIGINT" | "BIGSERIAL" => "BIGINT".to_string(),
                "BOOL" | "BOOLEAN" => "BOOLEAN".to_string(),
                "FLOAT4" | "REAL" => "REAL".to_string(),
                "FLOAT8" | "DOUBLEPRECISION" | "FLOAT" => "DOUBLE PRECISION".to_string(),
                "TIMESTAMPTZ" | "TIMESTAMPWITHTIMEZONE" => "TIMESTAMPTZ".to_string(),
                "TIMESTAMP" | "TIMESTAMPWITHOUTTIMEZONE" => "TIMESTAMP".to_string(),
                "UUID" => "UUID".to_string(),
                "CHARACTERVARYING" | "VARCHAR" | "CHAR" | "BPCHAR" | "TEXT" => "TEXT".to_string(),
                _ => upper,
            }
        }
        #[cfg(feature = "mysql")]
        DbType::MySQL => {
            let base = upper.split('(').next().unwrap_or(&upper);
            match base {
                "CHAR" | "VARCHAR" => upper,
                "TINYTEXT" | "TEXT" | "MEDIUMTEXT" | "LONGTEXT" => "TEXT".to_string(),
                "TINYINT" if upper.contains("(1)") => "BOOLEAN".to_string(),
                "TINYINT" => "TINYINT".to_string(),
                "INTEGER" | "INT" => "INT".to_string(),
                "BIGINT" => "BIGINT".to_string(),
                "SMALLINT" => "SMALLINT".to_string(),
                "FLOAT" => "FLOAT".to_string(),
                "DOUBLE" | "DOUBLE PRECISION" => "DOUBLE".to_string(),
                "DATETIME" | "TIMESTAMP" => "DATETIME".to_string(),
                "JSON" => "JSON".to_string(),
                "BLOB" | "TINYBLOB" | "MEDIUMBLOB" | "LONGBLOB" => "BLOB".to_string(),
                _ => base.to_string(),
            }
        }
        #[cfg(feature = "mssql")]
        DbType::MSSQL => {
            let base = upper.split('(').next().unwrap_or(&upper);
            match base {
                "NCHAR" | "NVARCHAR" | "CHAR" | "VARCHAR" | "NTEXT" | "TEXT" => "TEXT".to_string(),
                "BIT" => "BOOLEAN".to_string(),
                "TINYINT" => "TINYINT".to_string(),
                "SMALLINT" => "SMALLINT".to_string(),
                "INT" | "INTEGER" => "INT".to_string(),
                "BIGINT" => "BIGINT".to_string(),
                "REAL" => "REAL".to_string(),
                "FLOAT" => "FLOAT".to_string(),
                "DATETIME" | "DATETIME2" => "DATETIME".to_string(),
                "DATE" => "DATE".to_string(),
                "TIME" => "TIME".to_string(),
                "VARBINARY" | "BINARY" | "IMAGE" => "BLOB".to_string(),
                "UNIQUEIDENTIFIER" => "UUID".to_string(),
                _ => base.to_string(),
            }
        }
        #[cfg(any(feature = "duckdb", feature = "clickhouse"))]
        _ => upper,
    }
}

#[cfg(feature = "postgresql")]
fn postgresql_using_expression(
    db_type: DbType,
    actual: &SchemaColumn,
    expected: &ColumnSchema,
    expected_type: &str,
) -> Option<String> {
    if !matches!(db_type, DbType::PostgreSQL) {
        return None;
    }

    let column = expected.name;
    if expected_type.eq_ignore_ascii_case("TEXT[]")
        && matches!(
            normalize_type(DbType::PostgreSQL, &actual.type_name).as_str(),
            "TEXT" | "JSONB" | "JSON"
        )
    {
        let null_value = if expected.is_nullable {
            "NULL::TEXT[]"
        } else {
            "ARRAY[]::TEXT[]"
        };
        if matches!(
            normalize_type(DbType::PostgreSQL, &actual.type_name).as_str(),
            "JSONB" | "JSON"
        ) {
            return Some(format!(
                "CASE WHEN {column} IS NULL THEN {null_value} \
                 WHEN jsonb_typeof({column}::jsonb) = 'array' \
                 THEN ARRAY(SELECT jsonb_array_elements_text({column}::jsonb)) \
                 WHEN jsonb_typeof({column}::jsonb) = 'string' \
                 THEN ARRAY[({column}::jsonb #>> '{{}}')]::TEXT[] \
                 ELSE ARRAY[{column}::text]::TEXT[] END"
            ));
        }
        return Some(format!(
            "CASE WHEN {column} IS NULL THEN {null_value} \
             WHEN btrim({column}::text) = '' THEN ARRAY[]::TEXT[] \
                 WHEN left(btrim({column}::text), 1) = '[' \
             THEN ARRAY(SELECT jsonb_array_elements_text({column}::jsonb)) \
             ELSE ARRAY[{column}::text]::TEXT[] END"
        ));
    }
    Some(format!("{column}::{expected_type}"))
}

#[cfg(feature = "sqlite")]
fn sqlite_rebuild_sql<T: WritableModel>(
    table_name: &str,
    actual_by_name: &BTreeMap<&str, &SchemaColumn>,
) -> crate::Result<String> {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    table_name.hash(&mut hasher);
    for column in T::COLUMN_SCHEMA {
        column.name.hash(&mut hasher);
        column.rust_type.hash(&mut hasher);
        column.is_nullable.hash(&mut hasher);
    }
    let temporary_table = format!("__ormer_migrate_{}", hasher.finish());
    let check_table = format!("__ormer_migrate_check_{}", hasher.finish());

    let temporary_create =
        crate::generate_create_table_sql_with_name::<T>(DbType::Sqlite, Some(&temporary_table))?;
    let mut temporary_statements = split_sql_statements(&temporary_create);
    let create_table = temporary_statements
        .drain(..1)
        .next()
        .ok_or_else(|| crate::ormer_error!("Generated SQLite migration table SQL is empty"))?;

    let mut statements = Vec::new();
    let mut validation_statements = Vec::new();

    let mut insert_columns = Vec::new();
    let mut select_expressions = Vec::new();
    for column in T::COLUMN_SCHEMA {
        let Some(actual) = actual_by_name.get(column.name) else {
            continue;
        };
        insert_columns.push(column.name);
        let target_type = column_type_definition(DbType::Sqlite, column);
        let expression = if types_equivalent(DbType::Sqlite, &actual.type_name, &target_type) {
            column.name.to_string()
        } else {
            if let Some(validation) = sqlite_conversion_validation_sql(
                &check_table,
                table_name,
                column.name,
                &actual.type_name,
                &target_type,
            ) {
                validation_statements.push(validation.clone());
                validation_statements.push(validation);
            }
            sqlite_conversion_expression(column.name, &actual.type_name, &target_type)?
        };
        select_expressions.push(expression);
    }

    if !validation_statements.is_empty() {
        statements.push(format!("DROP TABLE IF EXISTS {check_table}"));
        statements.push(format!(
            "CREATE TABLE {check_table} (ok INTEGER PRIMARY KEY)"
        ));
        statements.extend(validation_statements);
        statements.push(format!("DROP TABLE {check_table}"));
    }
    statements.push(format!("DROP TABLE IF EXISTS {temporary_table}"));
    statements.push(create_table.to_string());

    if !insert_columns.is_empty() {
        statements.push(format!(
            "INSERT INTO {temporary_table} ({}) SELECT {} FROM {table_name}",
            insert_columns.join(", "),
            select_expressions.join(", ")
        ));
    }
    statements.push(format!("DROP TABLE {table_name}"));
    statements.push(format!(
        "ALTER TABLE {temporary_table} RENAME TO {table_name}"
    ));

    let original_create = crate::generate_create_table_sql::<T>(DbType::Sqlite)?;
    statements.extend(
        split_sql_statements(&original_create)
            .into_iter()
            .skip(1)
            .map(|statement| statement),
    );
    Ok(statements.join(";\n"))
}

#[cfg(feature = "sqlite")]
fn sqlite_conversion_expression(
    column: &str,
    actual_type: &str,
    target_type: &str,
) -> crate::Result<String> {
    let actual = normalize_type(DbType::Sqlite, actual_type);
    let target = normalize_type(DbType::Sqlite, target_type);
    match (actual.as_str(), target.as_str()) {
        ("TEXT", "INTEGER") => Ok(format!("CAST(trim({column}) AS INTEGER)")),
        ("INTEGER", "REAL") => Ok(format!("CAST({column} AS REAL)")),
        (_, "TEXT") => Ok(format!("CAST({column} AS TEXT)")),
        _ => Err(crate::ormer_error!(
            "Cannot safely infer SQLite type migration for column {column}: {actual_type} -> {target_type}"
        )),
    }
}

#[cfg(feature = "sqlite")]
fn sqlite_conversion_validation_sql(
    check_table: &str,
    table_name: &str,
    column: &str,
    actual_type: &str,
    target_type: &str,
) -> Option<String> {
    let actual = normalize_type(DbType::Sqlite, actual_type);
    let target = normalize_type(DbType::Sqlite, target_type);
    if actual != "TEXT" || target != "INTEGER" {
        return None;
    }

    let value = format!("trim({column})");
    let unsigned_digits = format!("({value} GLOB '[0-9]*' AND {value} NOT GLOB '*[^0-9]*')");
    let signed_digits = format!(
        "(({value} GLOB '+[0-9]*' OR {value} GLOB '-[0-9]*') \
         AND length(substr({value}, 2)) > 0 \
         AND substr({value}, 2) NOT GLOB '*[^0-9]*')"
    );
    let valid = format!("(length({value}) > 0 AND ({unsigned_digits} OR {signed_digits}))");

    Some(format!(
        "INSERT INTO {check_table} (ok) \
         SELECT 1 FROM {table_name} \
         WHERE {column} IS NOT NULL AND NOT ({valid}) \
         LIMIT 1"
    ))
}

async fn execute_steps(
    transaction: &mut Transaction<'_>,
    db_type: DbType,
    steps: &[MigrationStep],
) -> crate::Result<()> {
    for step in steps {
        let sql = step.sql(db_type)?;
        for statement in split_sql_statements(&sql) {
            transaction.execute_sql(statement).await?;
        }
    }
    Ok(())
}

fn split_sql_statements(sql: &str) -> Vec<String> {
    let mut statements = Vec::new();
    let mut current = String::new();
    let mut word = String::new();
    let mut in_single = false;
    let mut in_double = false;
    let mut in_backtick = false;
    let mut in_bracket = false;
    let mut in_line_comment = false;
    let mut in_block_comment = false;
    let mut trigger_mode = false;
    let mut trigger_depth = 0usize;
    let mut chars = sql.chars().peekable();

    while let Some(c) = chars.next() {
        if in_line_comment {
            current.push(c);
            if c == '\n' {
                in_line_comment = false;
            }
            continue;
        }

        if in_block_comment {
            current.push(c);
            if c == '*' && matches!(chars.peek(), Some('/')) {
                current.push(chars.next().expect("peeked slash"));
                in_block_comment = false;
            }
            continue;
        }

        if in_single {
            current.push(c);
            if c == '\'' {
                if matches!(chars.peek(), Some('\'')) {
                    current.push(chars.next().expect("peeked quote"));
                } else {
                    in_single = false;
                }
            }
            continue;
        }

        if in_double {
            current.push(c);
            if c == '"' {
                if matches!(chars.peek(), Some('"')) {
                    current.push(chars.next().expect("peeked quote"));
                } else {
                    in_double = false;
                }
            }
            continue;
        }

        if in_backtick {
            current.push(c);
            if c == '`' {
                if matches!(chars.peek(), Some('`')) {
                    current.push(chars.next().expect("peeked backtick"));
                } else {
                    in_backtick = false;
                }
            }
            continue;
        }

        if in_bracket {
            current.push(c);
            if c == ']' {
                if matches!(chars.peek(), Some(']')) {
                    current.push(chars.next().expect("peeked bracket"));
                } else {
                    in_bracket = false;
                }
            }
            continue;
        }

        if c == '-' && matches!(chars.peek(), Some('-')) {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            current.push(chars.next().expect("peeked comment dash"));
            in_line_comment = true;
            continue;
        }

        if c == '/' && matches!(chars.peek(), Some('*')) {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            current.push(chars.next().expect("peeked comment star"));
            in_block_comment = true;
            continue;
        }

        if c == '\'' {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            in_single = true;
            continue;
        }

        if c == '"' {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            in_double = true;
            continue;
        }

        if c == '`' {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            in_backtick = true;
            continue;
        }

        if c == '[' {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            in_bracket = true;
            continue;
        }

        if c == ';' {
            flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
            current.push(c);
            if trigger_mode && trigger_depth > 0 {
                continue;
            }

            let statement = current.trim().trim_end_matches(';').trim();
            if !statement.is_empty() {
                statements.push(statement.to_string());
            }
            current.clear();
            trigger_mode = false;
            trigger_depth = 0;
            continue;
        }

        if c.is_ascii_alphanumeric() || c == '_' {
            current.push(c);
            word.push(c);
            if !trigger_mode
                && current
                    .trim_start()
                    .to_ascii_uppercase()
                    .starts_with("CREATE TRIGGER")
            {
                trigger_mode = true;
                trigger_depth = 0;
            }
            continue;
        }

        flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
        current.push(c);
    }

    flush_sql_word(&mut word, trigger_mode, &mut trigger_depth);
    let statement = current.trim().trim_end_matches(';').trim();
    if !statement.is_empty() {
        statements.push(statement.to_string());
    }

    statements
}

fn flush_sql_word(word: &mut String, trigger_mode: bool, trigger_depth: &mut usize) {
    if trigger_mode {
        match word.to_ascii_uppercase().as_str() {
            "BEGIN" => *trigger_depth += 1,
            "END" => {
                if *trigger_depth > 0 {
                    *trigger_depth -= 1;
                }
            }
            _ => {}
        }
    }
    word.clear();
}

fn validate_migrations<M: Migration>(migrations: &[M]) -> crate::Result<Vec<&M>> {
    let mut sorted: Vec<&M> = migrations.iter().collect();
    sorted.sort_by_key(|migration| migration.version());
    let mut versions = BTreeSet::new();
    for migration in &sorted {
        if !versions.insert(migration.version()) {
            return Err(crate::ormer_error!(
                "Duplicate migration version {}",
                migration.version()
            ));
        }
    }
    Ok(sorted)
}

impl Database {
    pub fn db_type(&self) -> DbType {
        match self {
            #[cfg(feature = "sqlite")]
            Database::Sqlite(_) => DbType::Sqlite,
            #[cfg(feature = "postgresql")]
            Database::PostgreSQL(_) => DbType::PostgreSQL,
            #[cfg(feature = "mysql")]
            Database::MySQL(_) => DbType::MySQL,
            #[cfg(feature = "mssql")]
            Database::MSSQL(_) => DbType::MSSQL,
            #[cfg(feature = "duckdb")]
            Database::DuckDB(_) => DbType::DuckDB,
            #[cfg(feature = "clickhouse")]
            Database::ClickHouse(_) => DbType::ClickHouse,
        }
    }

    pub fn migrate_table<T: WritableModel>(&self) -> TableMigration<'_, T> {
        TableMigration {
            db: self,
            marker: PhantomData,
        }
    }

    pub fn migrations<'a, M: Migration>(&'a self, migrations: &'a [M]) -> MigrationRunner<'a, M> {
        MigrationRunner::new(self, migrations)
    }

    pub async fn migration_history(&self) -> crate::Result<Vec<MigrationInfo>> {
        #[cfg(feature = "clickhouse")]
        if let Database::ClickHouse(db) = self {
            db.ensure_migration_table().await?;
        }
        self.ensure_migration_table().await?;
        let rows = self.migration_history_rows().await?;
        Ok(rows
            .into_iter()
            .map(|(version, name, checksum)| MigrationInfo::with_checksum(version, name, checksum))
            .collect())
    }

    pub async fn pending_migrations<M: Migration>(
        &self,
        migrations: &[M],
    ) -> crate::Result<Vec<MigrationInfo>> {
        self.ensure_migration_table().await?;
        let applied: BTreeMap<u64, u64> = self
            .migration_history_rows()
            .await?
            .into_iter()
            .map(|(version, _, checksum)| (version, checksum))
            .collect();
        let sorted = validate_migrations(migrations)?;
        let mut pending = Vec::new();
        for migration in sorted {
            if let Some(checksum) = applied.get(&migration.version()) {
                if *checksum != migration.checksum() {
                    return Err(crate::ormer_error!(
                        "Migration {} checksum changed after it was applied",
                        migration.version()
                    ));
                }
                continue;
            }
            pending.push(MigrationInfo::with_checksum(
                migration.version(),
                migration.name(),
                migration.checksum(),
            ));
        }
        Ok(pending)
    }

    pub async fn apply_migrations<M: Migration>(&self, migrations: &[M]) -> crate::Result<usize> {
        #[cfg(feature = "clickhouse")]
        if let Database::ClickHouse(db) = self {
            return db.apply_migrations(migrations).await;
        }
        self.ensure_migration_table().await?;
        let applied: BTreeMap<u64, u64> = self
            .migration_history_rows()
            .await?
            .into_iter()
            .map(|(version, _, checksum)| (version, checksum))
            .collect();
        let sorted = validate_migrations(migrations)?;
        let mut pending = Vec::new();
        for migration in sorted {
            if let Some(checksum) = applied.get(&migration.version()) {
                if *checksum != migration.checksum() {
                    return Err(crate::ormer_error!(
                        "Migration {} checksum changed after it was applied",
                        migration.version()
                    ));
                }
                continue;
            }
            pending.push(migration);
        }
        if pending.is_empty() {
            return Ok(0);
        }

        let db_type = self.db_type();
        let mut transaction = self.begin().await?;
        let result = async {
            for migration in &pending {
                execute_steps(&mut transaction, db_type, &migration.up()).await?;
                let name = migration.name().replace('\'', "''");
                let sql = format!(
                    "INSERT INTO {MIGRATION_TABLE_NAME} (version, name, checksum) VALUES ({}, '{}', '{}')",
                    migration.version(),
                    name,
                    migration.checksum()
                );
                transaction.execute_sql(&sql).await?;
            }
            Ok::<(), crate::OrmerError>(())
        }
        .await;

        match result {
            Ok(()) => {
                transaction.commit().await?;
                Ok(pending.len())
            }
            Err(error) => {
                let _ = transaction.rollback().await;
                Err(error)
            }
        }
    }

    async fn ensure_migration_table(&self) -> crate::Result<()> {
        #[cfg(feature = "clickhouse")]
        if let Database::ClickHouse(db) = self {
            return db.ensure_migration_table().await;
        }
        let sql = match self.db_type() {
            #[cfg(feature = "sqlite")]
            DbType::Sqlite => format!(
                "CREATE TABLE IF NOT EXISTS {MIGRATION_TABLE_NAME} \
                 (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP)"
            ),
            #[cfg(feature = "postgresql")]
            DbType::PostgreSQL => format!(
                "CREATE TABLE IF NOT EXISTS {MIGRATION_TABLE_NAME} \
                 (version BIGINT PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP)"
            ),
            #[cfg(feature = "mysql")]
            DbType::MySQL => format!(
                "CREATE TABLE IF NOT EXISTS {MIGRATION_TABLE_NAME} \
                 (version BIGINT PRIMARY KEY, name VARCHAR(255) NOT NULL, checksum VARCHAR(32) NOT NULL, applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
            ),
            #[cfg(feature = "mssql")]
            DbType::MSSQL => format!(
                "IF OBJECT_ID(N'{MIGRATION_TABLE_NAME}', N'U') IS NULL \
                 CREATE TABLE {MIGRATION_TABLE_NAME} \
                 (version BIGINT NOT NULL PRIMARY KEY, name NVARCHAR(255) NOT NULL, checksum NVARCHAR(32) NOT NULL, applied_at DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME())"
            ),
            #[cfg(feature = "duckdb")]
            DbType::DuckDB => format!(
                "CREATE TABLE IF NOT EXISTS {MIGRATION_TABLE_NAME} \
                 (version BIGINT PRIMARY KEY, name VARCHAR NOT NULL, checksum VARCHAR NOT NULL, applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"
            ),
            #[cfg(feature = "clickhouse")]
            DbType::ClickHouse => unreachable!("handled by the ClickHouse backend"),
        };
        self.execute_sql(&sql).await?;
        Ok(())
    }

    async fn migration_history_rows(&self) -> crate::Result<Vec<(u64, String, u64)>> {
        match self {
            #[cfg(feature = "sqlite")]
            Database::Sqlite(db) => db.migration_history().await,
            #[cfg(feature = "postgresql")]
            Database::PostgreSQL(db) => db.migration_history().await,
            #[cfg(feature = "mysql")]
            Database::MySQL(db) => db.migration_history().await,
            #[cfg(feature = "mssql")]
            Database::MSSQL(db) => db.migration_history().await,
            #[cfg(feature = "duckdb")]
            Database::DuckDB(db) => db.migration_history().await,
            #[cfg(feature = "clickhouse")]
            Database::ClickHouse(db) => Ok(db
                .migration_history()
                .await?
                .into_iter()
                .map(|migration| (migration.version, migration.name, migration.checksum))
                .collect()),
        }
    }

    async fn schema_columns(
        &self,
        table_name: &str,
    ) -> crate::Result<Option<Vec<crate::migration::SchemaColumn>>> {
        match self {
            #[cfg(feature = "sqlite")]
            Database::Sqlite(db) => db.schema_columns(table_name).await,
            #[cfg(feature = "postgresql")]
            Database::PostgreSQL(db) => db.schema_columns(table_name).await,
            #[cfg(feature = "mysql")]
            Database::MySQL(db) => db.schema_columns(table_name).await,
            #[cfg(feature = "mssql")]
            Database::MSSQL(db) => db.schema_columns(table_name).await,
            #[cfg(feature = "duckdb")]
            Database::DuckDB(db) => db.schema_columns(table_name).await,
            #[cfg(feature = "clickhouse")]
            Database::ClickHouse(_) => Err(crate::OrmerError::UnsupportedFeature {
                backend: DbType::ClickHouse,
                feature: "migrate_table schema introspection",
            }),
        }
    }

    async fn validate_hypertable_for_migration<T: WritableModel>(&self) -> crate::Result<()> {
        match self {
            #[cfg(feature = "postgresql")]
            Database::PostgreSQL(db) => db.validate_hypertable_for_migration::<T>().await,
            #[cfg(feature = "sqlite")]
            Database::Sqlite(_) => Ok(()),
            #[cfg(feature = "mysql")]
            Database::MySQL(_) => Ok(()),
            #[cfg(feature = "mssql")]
            Database::MSSQL(_) => Ok(()),
            #[cfg(feature = "duckdb")]
            Database::DuckDB(_) => Ok(()),
            #[cfg(feature = "clickhouse")]
            Database::ClickHouse(_) => Ok(()),
        }
    }
}

/// Backend-independent table-column metadata used by schema planning.
#[derive(Debug, Clone)]
pub(crate) struct SchemaColumn {
    pub(crate) name: String,
    pub(crate) type_name: String,
    pub(crate) nullable: bool,
    pub(crate) primary_key: bool,
    #[allow(dead_code)]
    pub(crate) compression: Option<String>,
}

/// Keep a deterministic map available to backend implementations without
/// exposing driver-specific row types through the public API.
#[allow(dead_code)]
pub(crate) fn schema_column(
    name: impl Into<String>,
    type_name: impl Into<String>,
    nullable: bool,
    primary_key: bool,
) -> SchemaColumn {
    schema_column_with_compression(name, type_name, nullable, primary_key, None)
}

pub(crate) fn schema_column_with_compression(
    name: impl Into<String>,
    type_name: impl Into<String>,
    nullable: bool,
    primary_key: bool,
    compression: Option<String>,
) -> SchemaColumn {
    SchemaColumn {
        name: name.into(),
        type_name: type_name.into(),
        nullable,
        primary_key,
        compression,
    }
}

#[cfg(all(test, feature = "postgresql"))]
mod compression_tests {
    use super::{CompressionAlgorithm, MigrationStep, column_compression_migration_step};
    use crate::abstract_layer::DbType;

    #[test]
    fn postgres_compression_migration_renders_column_step() {
        let step = column_compression_migration_step(
            DbType::PostgreSQL,
            "public.documents",
            "payload",
            Some(CompressionAlgorithm::Lz4),
        )
        .expect("compression migration step")
        .expect("postgres compression migration step");
        assert_eq!(
            step.sql(DbType::PostgreSQL).unwrap(),
            "ALTER TABLE public.documents ALTER COLUMN payload SET COMPRESSION lz4"
        );
        assert!(matches!(step, MigrationStep::Sql { .. }));
    }
}

#[cfg(all(test, feature = "duckdb"))]
mod duckdb_tests {
    use super::MigrationStep;
    use crate::abstract_layer::DbType;

    #[test]
    fn duckdb_alter_column_renders_type_and_nullability_changes() {
        let type_change = MigrationStep::AlterColumn {
            table: "events".to_string(),
            column: "kind".to_string(),
            definition: "SET DATA TYPE BIGINT".to_string(),
            using: None,
        };
        assert_eq!(
            type_change.sql(DbType::DuckDB).unwrap(),
            "ALTER TABLE events ALTER COLUMN kind SET DATA TYPE BIGINT"
        );

        let nullable_change = MigrationStep::AlterColumn {
            table: "events".to_string(),
            column: "kind".to_string(),
            definition: "SET NOT NULL".to_string(),
            using: None,
        };
        assert_eq!(
            nullable_change.sql(DbType::DuckDB).unwrap(),
            "ALTER TABLE events ALTER COLUMN kind SET NOT NULL"
        );
    }
}

#[cfg(all(test, feature = "clickhouse"))]
mod clickhouse_tests {
    use super::MigrationStep;
    use crate::abstract_layer::DbType;

    #[test]
    fn clickhouse_alter_column_renders_modify_column() {
        let step = MigrationStep::AlterColumn {
            table: "events".to_string(),
            column: "kind".to_string(),
            definition: "Nullable(String)".to_string(),
            using: None,
        };
        assert_eq!(
            step.sql(DbType::ClickHouse).unwrap(),
            "ALTER TABLE events MODIFY COLUMN kind Nullable(String)"
        );
    }
}

#[cfg(all(test, feature = "mysql"))]
mod mysql_compression_tests {
    use super::{CompressionAlgorithm, column_compression_migration_step};
    use crate::abstract_layer::DbType;

    #[test]
    fn mysql_compression_migration_renders_table_step() {
        let step = column_compression_migration_step(
            DbType::MySQL,
            "documents",
            "",
            Some(CompressionAlgorithm::Lz4),
        )
        .expect("compression migration step")
        .expect("mysql compression migration step");
        assert_eq!(
            step.sql(DbType::MySQL).unwrap(),
            "ALTER TABLE documents COMPRESSION='LZ4'"
        );
    }
}