prax-orm-cli 0.12.2

CLI tool for the Prax ORM
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
//! Database introspection implementation.
//!
//! This module provides the actual database introspection functionality
//! using the `prax-query` introspection types.

use prax_query::introspection::{
    ColumnInfo, DatabaseSchema, ForeignKeyInfo, IndexColumn, IndexInfo, ReferentialAction,
    SortOrder, TableInfo, generate_prax_schema, normalize_type, queries,
};
// `EnumInfo`/`ViewInfo` are only constructed by the PostgreSQL introspector
// (the other backends build enums/views differently or not at all), so import
// them only when that feature is compiled to keep single-feature builds clean.
#[cfg(feature = "postgres")]
use prax_query::introspection::{EnumInfo, ViewInfo};
use prax_query::sql::DatabaseType;

use crate::config::Config;
use crate::error::{CliError, CliResult};

/// Introspection options.
#[derive(Debug, Clone)]
pub struct IntrospectionOptions {
    /// Schema/namespace to introspect.
    pub schema: Option<String>,
    /// Include views.
    pub include_views: bool,
    /// Include materialized views.
    pub include_materialized_views: bool,
    /// Table filter pattern.
    pub table_filter: Option<String>,
    /// Tables to exclude.
    pub exclude_pattern: Option<String>,
    /// Include comments.
    pub include_comments: bool,
    /// Sample size for MongoDB.
    pub sample_size: usize,
}

impl Default for IntrospectionOptions {
    fn default() -> Self {
        Self {
            schema: None,
            include_views: false,
            include_materialized_views: false,
            table_filter: None,
            exclude_pattern: None,
            include_comments: true,
            sample_size: 100,
        }
    }
}

/// Database introspector trait.
#[allow(async_fn_in_trait)]
pub trait Introspector {
    /// Introspect the database and return schema information.
    async fn introspect(&self, options: &IntrospectionOptions) -> CliResult<DatabaseSchema>;
}

/// Get the database type from provider string.
pub fn get_database_type(provider: &str) -> CliResult<DatabaseType> {
    match provider.to_lowercase().as_str() {
        "postgresql" | "postgres" | "pg" => Ok(DatabaseType::PostgreSQL),
        "mysql" | "mariadb" => Ok(DatabaseType::MySQL),
        "sqlite" | "sqlite3" => Ok(DatabaseType::SQLite),
        "mssql" | "sqlserver" | "sql_server" => Ok(DatabaseType::MSSQL),
        _ => Err(CliError::Config(format!(
            "Unsupported database provider: {}",
            provider
        ))),
    }
}

/// Get default schema for database type.
pub fn default_schema(db_type: DatabaseType) -> &'static str {
    match db_type {
        DatabaseType::PostgreSQL => "public",
        DatabaseType::MySQL => "",
        DatabaseType::SQLite => "",
        DatabaseType::MSSQL => "dbo",
    }
}

/// Introspect a database, dispatching to the backend matching `provider`.
///
/// This is the single entry point shared by `db pull` and the migration
/// engine's `resolve_source_schema`. Each backend is behind its cargo
/// feature; a provider whose feature was not compiled in returns a clear
/// `Config` error rather than silently doing nothing.
pub async fn introspect_database(
    provider: &str,
    database_url: &str,
    options: &IntrospectionOptions,
) -> CliResult<DatabaseSchema> {
    let db_type = get_database_type(provider)?;
    match db_type {
        DatabaseType::PostgreSQL => {
            #[cfg(feature = "postgres")]
            {
                postgres::PostgresIntrospector::new(database_url.to_string())
                    .introspect(options)
                    .await
            }
            #[cfg(not(feature = "postgres"))]
            {
                let _ = (database_url, options);
                Err(CliError::FeatureUnavailable(
                    "PostgreSQL introspection requires the `postgres` feature: rebuild with \
                     --features postgres."
                        .to_string(),
                ))
            }
        }
        DatabaseType::MySQL => {
            #[cfg(feature = "mysql")]
            {
                mysql::MysqlIntrospector::new(database_url.to_string())
                    .introspect(options)
                    .await
            }
            #[cfg(not(feature = "mysql"))]
            {
                let _ = (database_url, options);
                Err(CliError::FeatureUnavailable(
                    "MySQL introspection requires the `mysql` feature: rebuild with \
                     --features mysql."
                        .to_string(),
                ))
            }
        }
        DatabaseType::SQLite => {
            #[cfg(feature = "sqlite")]
            {
                sqlite::SqliteIntrospector::new(database_url.to_string())
                    .introspect(options)
                    .await
            }
            #[cfg(not(feature = "sqlite"))]
            {
                let _ = (database_url, options);
                Err(CliError::FeatureUnavailable(
                    "SQLite introspection requires the `sqlite` feature: rebuild with \
                     --features sqlite."
                        .to_string(),
                ))
            }
        }
        DatabaseType::MSSQL => {
            #[cfg(feature = "mssql")]
            {
                mssql::MssqlIntrospector::new(database_url.to_string())
                    .introspect(options)
                    .await
            }
            #[cfg(not(feature = "mssql"))]
            {
                let _ = (database_url, options);
                Err(CliError::FeatureUnavailable(
                    "MSSQL introspection requires the `mssql` feature: rebuild with \
                     --features mssql."
                        .to_string(),
                ))
            }
        }
    }
}

// ============================================================================
// PostgreSQL Introspector
// ============================================================================

#[cfg(feature = "postgres")]
pub mod postgres {
    use std::collections::HashMap;

    use super::*;
    use tokio_postgres::{Client, NoTls, Row};

    /// PostgreSQL introspector.
    pub struct PostgresIntrospector {
        connection_string: String,
    }

    impl PostgresIntrospector {
        /// Create a new PostgreSQL introspector.
        pub fn new(connection_string: String) -> Self {
            Self { connection_string }
        }

        /// Connect to the database.
        async fn connect(&self) -> CliResult<Client> {
            // Parse the DSN the same way tokio-postgres will, so the sslmode
            // it carries is honored: anything but `disable` goes through the
            // workspace's shared rustls connector (chain + hostname verified
            // against the Mozilla root store). `prefer` still falls back to
            // plaintext when the server declines TLS.
            let config = self
                .connection_string
                .parse::<tokio_postgres::Config>()
                .map_err(|e| CliError::Config(format!("Invalid connection string: {}", e)))?;

            let tls_disabled = matches!(
                config.get_ssl_mode(),
                tokio_postgres::config::SslMode::Disable
            );

            if tls_disabled && !config.get_hosts().iter().all(is_local_host) {
                crate::output::warn(
                    "sslmode=disable with a non-local host: credentials and data will be \
                     sent in plaintext.",
                );
            }

            // The two connector types produce different `Connection`
            // generics, so drive each arm independently and unify on the
            // stream-agnostic `Client`. Each connect is bounded by
            // `INTROSPECT_CONNECT_TIMEOUT_SECS` so an unreachable-but-not-
            // refused host does not hang the CLI (parity with the pool-based
            // backends).
            let connect_timeout =
                std::time::Duration::from_secs(super::INTROSPECT_CONNECT_TIMEOUT_SECS);
            let client = if tls_disabled {
                let (client, connection) = tokio::time::timeout(
                    connect_timeout,
                    tokio_postgres::connect(&self.connection_string, NoTls),
                )
                .await
                .map_err(|_| {
                    CliError::Unreachable("Failed to connect: connection timed out".to_string())
                })?
                .map_err(|e| CliError::Unreachable(format!("Failed to connect: {}", e)))?;
                tokio::spawn(async move {
                    if let Err(e) = connection.await {
                        eprintln!("Connection error: {}", e);
                    }
                });
                client
            } else {
                let (client, connection) = tokio::time::timeout(
                    connect_timeout,
                    tokio_postgres::connect(
                        &self.connection_string,
                        prax_postgres::tls::make_tls_connector(),
                    ),
                )
                .await
                .map_err(|_| {
                    CliError::Unreachable("Failed to connect: connection timed out".to_string())
                })?
                .map_err(|e| CliError::Unreachable(format!("Failed to connect: {}", e)))?;
                tokio::spawn(async move {
                    if let Err(e) = connection.await {
                        eprintln!("Connection error: {}", e);
                    }
                });
                client
            };

            Ok(client)
        }
    }

    impl Introspector for PostgresIntrospector {
        async fn introspect(&self, options: &IntrospectionOptions) -> CliResult<DatabaseSchema> {
            let client = self.connect().await?;
            let schema_name = options.schema.as_deref().unwrap_or("public");

            let mut db_schema = DatabaseSchema {
                name: "database".to_string(),
                schema: Some(schema_name.to_string()),
                ..Default::default()
            };

            // Get tables
            let tables_sql = queries::tables_query(DatabaseType::PostgreSQL, Some(schema_name));
            let table_rows = client
                .query(&tables_sql, &[])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query tables: {}", e)))?;

            for row in table_rows {
                let table_name: String = row.get(0);

                // Apply filters
                if let Some(ref pattern) = options.table_filter
                    && !matches_pattern(&table_name, pattern)
                {
                    continue;
                }
                if let Some(ref exclude) = options.exclude_pattern
                    && matches_pattern(&table_name, exclude)
                {
                    continue;
                }

                let comment: Option<String> = row.try_get(1).ok();

                db_schema.tables.push(TableInfo {
                    name: table_name,
                    schema: Some(schema_name.to_string()),
                    comment: if options.include_comments {
                        comment
                    } else {
                        None
                    },
                    ..Default::default()
                });
            }

            // Fetch columns, primary keys, foreign keys, and indexes for the
            // whole schema in one query each, then group rows by table in
            // memory: 4 round-trips total instead of 4 per table. The table
            // name is appended as the last selected column and used as the
            // first ORDER BY key so grouped rows keep the exact per-table
            // ordering of the original per-table queries.
            let cols_sql = "SELECT \
                    c.column_name, \
                    c.data_type, \
                    c.udt_name, \
                    c.is_nullable = 'YES' as nullable, \
                    c.column_default, \
                    c.character_maximum_length, \
                    c.numeric_precision, \
                    c.numeric_scale, \
                    col_description((quote_ident(c.table_schema) || '.' || quote_ident(c.table_name))::regclass, c.ordinal_position) as comment, \
                    CASE WHEN c.column_default LIKE 'nextval%' THEN true ELSE false END as auto_increment, \
                    c.table_name \
                 FROM information_schema.columns c \
                 WHERE c.table_schema = $1 \
                 ORDER BY c.table_name, c.ordinal_position";
            let col_rows = client
                .query(cols_sql, &[&schema_name])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query columns: {}", e)))?;

            let mut columns_by_table: HashMap<String, Vec<Row>> = HashMap::new();
            for col_row in col_rows {
                let table_name: String = col_row.get(10);
                columns_by_table
                    .entry(table_name)
                    .or_default()
                    .push(col_row);
            }

            let pk_sql = "SELECT a.attname as column_name, c.relname as table_name \
                 FROM pg_index i \
                 JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) \
                 JOIN pg_class c ON c.oid = i.indrelid \
                 JOIN pg_namespace n ON n.oid = c.relnamespace \
                 WHERE i.indisprimary AND n.nspname = $1 \
                 ORDER BY c.relname, array_position(i.indkey, a.attnum)";
            let pk_rows = client
                .query(pk_sql, &[&schema_name])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query primary keys: {}", e)))?;

            let mut pks_by_table: HashMap<String, Vec<Row>> = HashMap::new();
            for pk_row in pk_rows {
                let table_name: String = pk_row.get(1);
                pks_by_table.entry(table_name).or_default().push(pk_row);
            }

            let fk_sql = "SELECT \
                    tc.constraint_name, \
                    kcu.column_name, \
                    ccu.table_name as referenced_table, \
                    ccu.table_schema as referenced_schema, \
                    ccu.column_name as referenced_column, \
                    rc.delete_rule, \
                    rc.update_rule, \
                    tc.table_name \
                 FROM information_schema.table_constraints tc \
                 JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name \
                 JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name \
                 JOIN information_schema.referential_constraints rc ON rc.constraint_name = tc.constraint_name \
                 WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = $1 \
                 ORDER BY tc.table_name, tc.constraint_name, kcu.ordinal_position";
            let fk_rows = client
                .query(fk_sql, &[&schema_name])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query foreign keys: {}", e)))?;

            let mut fks_by_table: HashMap<String, Vec<Row>> = HashMap::new();
            for fk_row in fk_rows {
                let table_name: String = fk_row.get(7);
                fks_by_table.entry(table_name).or_default().push(fk_row);
            }

            let idx_sql = "SELECT \
                    i.relname as index_name, \
                    a.attname as column_name, \
                    ix.indisunique as is_unique, \
                    ix.indisprimary as is_primary, \
                    am.amname as index_type, \
                    pg_get_expr(ix.indpred, ix.indrelid) as filter, \
                    t.relname as table_name \
                 FROM pg_index ix \
                 JOIN pg_class t ON t.oid = ix.indrelid \
                 JOIN pg_class i ON i.oid = ix.indexrelid \
                 JOIN pg_namespace n ON n.oid = t.relnamespace \
                 JOIN pg_am am ON i.relam = am.oid \
                 JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) \
                 WHERE n.nspname = $1 \
                 ORDER BY t.relname, i.relname, array_position(ix.indkey, a.attnum)";
            let idx_rows = client
                .query(idx_sql, &[&schema_name])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query indexes: {}", e)))?;

            let mut indexes_by_table: HashMap<String, Vec<Row>> = HashMap::new();
            for idx_row in idx_rows {
                let table_name: String = idx_row.get(6);
                indexes_by_table
                    .entry(table_name)
                    .or_default()
                    .push(idx_row);
            }

            // Populate each table from the pre-fetched rows.
            for table in &mut db_schema.tables {
                for col_row in columns_by_table.remove(&table.name).unwrap_or_default() {
                    let col_name: String = col_row.get(0);
                    let data_type: String = col_row.get(1);
                    let udt_name: String = col_row.get(2);
                    let nullable: bool = col_row.get(3);
                    let default: Option<String> = col_row.try_get(4).ok();
                    let max_length: Option<i32> = col_row.try_get(5).ok();
                    let precision: Option<i32> = col_row.try_get(6).ok();
                    let scale: Option<i32> = col_row.try_get(7).ok();
                    let comment: Option<String> = col_row.try_get(8).ok();
                    let auto_increment: bool = col_row.try_get(9).unwrap_or(false);

                    let normalized = normalize_type(
                        DatabaseType::PostgreSQL,
                        &udt_name,
                        max_length,
                        precision,
                        scale,
                    );

                    table.columns.push(ColumnInfo {
                        name: col_name,
                        db_type: data_type,
                        normalized_type: normalized,
                        nullable,
                        default,
                        auto_increment,
                        max_length,
                        precision,
                        scale,
                        comment: if options.include_comments {
                            comment
                        } else {
                            None
                        },
                        ..Default::default()
                    });
                }

                for pk_row in pks_by_table.remove(&table.name).unwrap_or_default() {
                    let col_name: String = pk_row.get(0);
                    table.primary_key.push(col_name.clone());

                    // Mark column as primary key
                    if let Some(col) = table.columns.iter_mut().find(|c| c.name == col_name) {
                        col.is_primary_key = true;
                    }
                }

                let mut fk_map: HashMap<String, ForeignKeyInfo> = HashMap::new();
                for fk_row in fks_by_table.remove(&table.name).unwrap_or_default() {
                    let constraint_name: String = fk_row.get(0);
                    let column_name: String = fk_row.get(1);
                    let ref_table: String = fk_row.get(2);
                    let ref_schema: Option<String> = fk_row.try_get(3).ok();
                    let ref_column: String = fk_row.get(4);
                    let delete_rule: String = fk_row.get(5);
                    let update_rule: String = fk_row.get(6);

                    let fk =
                        fk_map
                            .entry(constraint_name.clone())
                            .or_insert_with(|| ForeignKeyInfo {
                                name: constraint_name,
                                columns: Vec::new(),
                                referenced_table: ref_table,
                                referenced_schema: ref_schema,
                                referenced_columns: Vec::new(),
                                on_delete: ReferentialAction::from_str(&delete_rule),
                                on_update: ReferentialAction::from_str(&update_rule),
                            });

                    fk.columns.push(column_name);
                    fk.referenced_columns.push(ref_column);
                }

                table.foreign_keys = fk_map.into_values().collect();

                let mut idx_map: HashMap<String, IndexInfo> = HashMap::new();
                for idx_row in indexes_by_table.remove(&table.name).unwrap_or_default() {
                    let idx_name: String = idx_row.get(0);
                    let col_name: String = idx_row.get(1);
                    let is_unique: bool = idx_row.get(2);
                    let is_primary: bool = idx_row.get(3);
                    let idx_type: Option<String> = idx_row.try_get(4).ok();
                    let filter: Option<String> = idx_row.try_get(5).ok();

                    let idx = idx_map
                        .entry(idx_name.clone())
                        .or_insert_with(|| IndexInfo {
                            name: idx_name,
                            columns: Vec::new(),
                            is_unique,
                            is_primary,
                            index_type: idx_type,
                            filter,
                        });

                    idx.columns.push(IndexColumn {
                        name: col_name,
                        order: SortOrder::Asc,
                        ..Default::default()
                    });
                }

                table.indexes = idx_map.into_values().collect();
            }

            // Get enums
            let enums_sql = queries::enums_query(Some(schema_name));
            let enum_rows = client
                .query(&enums_sql, &[])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query enums: {}", e)))?;

            let mut enum_map: HashMap<String, EnumInfo> = HashMap::new();
            for enum_row in enum_rows {
                let enum_name: String = enum_row.get(0);
                let enum_value: String = enum_row.get(1);

                let enum_info = enum_map
                    .entry(enum_name.clone())
                    .or_insert_with(|| EnumInfo {
                        name: enum_name,
                        schema: Some(schema_name.to_string()),
                        values: Vec::new(),
                    });

                enum_info.values.push(enum_value);
            }

            db_schema.enums = enum_map.into_values().collect();

            // Get views
            if options.include_views || options.include_materialized_views {
                let views_sql = queries::views_query(DatabaseType::PostgreSQL, Some(schema_name));
                let view_rows = client
                    .query(&views_sql, &[])
                    .await
                    .map_err(|e| CliError::Database(format!("Failed to query views: {}", e)))?;

                for view_row in view_rows {
                    let view_name: String = view_row.get(0);
                    let definition: Option<String> = view_row.try_get(1).ok();
                    let is_materialized: bool = view_row.get(2);

                    if is_materialized && !options.include_materialized_views {
                        continue;
                    }
                    if !is_materialized && !options.include_views {
                        continue;
                    }

                    db_schema.views.push(ViewInfo {
                        name: view_name,
                        schema: Some(schema_name.to_string()),
                        definition,
                        is_materialized,
                        columns: Vec::new(),
                    });
                }
            }

            Ok(db_schema)
        }
    }

    /// Whether a parsed DSN host is local (loopback TCP or a Unix socket).
    fn is_local_host(host: &tokio_postgres::config::Host) -> bool {
        match host {
            tokio_postgres::config::Host::Tcp(name) => {
                name == "localhost" || name == "127.0.0.1" || name == "::1"
            }
            tokio_postgres::config::Host::Unix(_) => true,
        }
    }
}

// ============================================================================
// Shared helpers for JSON-row backends (MySQL)
// ============================================================================

/// Bounded connect timeout for introspection pools. Introspection is a
/// short-lived, interactive step; without a bound an unreachable-but-not-
/// refused host (e.g. a firewall drop) would hang the CLI. `migrate dev`
/// relies on this so it can fall back to greenfield when a DB is unreachable.
#[cfg(any(
    feature = "postgres",
    feature = "mysql",
    feature = "sqlite",
    feature = "mssql"
))]
pub(crate) const INTROSPECT_CONNECT_TIMEOUT_SECS: u64 = 5;

/// A source of introspection rows returned as JSON objects, keyed by column
/// name. Implemented for the raw engines of the JSON-capable backends so the
/// MySQL and SQLite introspectors share one row-fetch shim instead of each
/// hand-rolling `raw_sql_query(sql, &[]) -> into_json`.
#[cfg(any(feature = "mysql", feature = "sqlite"))]
trait JsonRowSource {
    /// Run `sql` (no bind params) and return each row as a JSON object.
    async fn json_rows(&self, sql: &str) -> CliResult<Vec<serde_json::Value>>;
}

#[cfg(feature = "mysql")]
impl JsonRowSource for prax_mysql::MysqlRawEngine {
    async fn json_rows(&self, sql: &str) -> CliResult<Vec<serde_json::Value>> {
        let rows = self
            .raw_sql_query(sql, &[])
            .await
            .map_err(|e| CliError::Database(format!("Introspection query failed: {}", e)))?;
        Ok(rows.into_iter().map(|r| r.into_json()).collect())
    }
}

#[cfg(feature = "sqlite")]
impl JsonRowSource for prax_sqlite::SqliteRawEngine {
    async fn json_rows(&self, sql: &str) -> CliResult<Vec<serde_json::Value>> {
        let rows = self
            .raw_sql_query(sql, &[])
            .await
            .map_err(|e| CliError::Database(format!("Introspection query failed: {}", e)))?;
        Ok(rows.into_iter().map(|r| r.into_json()).collect())
    }
}

/// Simple glob-style pattern matching shared across introspectors.
///
/// Supported subset: `*` (match all), `pre*` (prefix), `*suf` (suffix),
/// `*mid*` (substring/contains). Interior wildcards (e.g. `a*b*c` or
/// `pre*suf`) are **not** supported — such a pattern falls through to an
/// exact-string compare and will typically match nothing. Callers should
/// stick to the four supported shapes for table include/exclude filters.
#[cfg(any(
    feature = "postgres",
    feature = "mysql",
    feature = "sqlite",
    feature = "mssql"
))]
fn matches_pattern(name: &str, pattern: &str) -> bool {
    if pattern == "*" {
        return true;
    }

    if pattern.starts_with('*') && pattern.ends_with('*') {
        let middle = &pattern[1..pattern.len() - 1];
        return name.contains(middle);
    }

    if let Some(suffix) = pattern.strip_prefix('*') {
        return name.ends_with(suffix);
    }

    if let Some(prefix) = pattern.strip_suffix('*') {
        return name.starts_with(prefix);
    }

    name == pattern
}

/// Look up a column in a JSON object row case-insensitively.
///
/// MySQL's `information_schema` returns unaliased column names in uppercase
/// (e.g. `TABLE_NAME`) while aliased expressions keep the alias case, so a
/// single query row can mix cases. Match the exact key first, then fall back
/// to a case-insensitive scan.
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn json_get<'a>(row: &'a serde_json::Value, key: &str) -> Option<&'a serde_json::Value> {
    if let Some(v) = row.get(key) {
        return Some(v);
    }
    row.as_object()?
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case(key))
        .map(|(_, v)| v)
}

/// Read an optional string column from a serde_json object row.
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn json_str(row: &serde_json::Value, key: &str) -> Option<String> {
    json_get(row, key)
        .and_then(|v| v.as_str())
        .map(str::to_string)
}

/// Read a boolean column from a JSON object row, treating MySQL's 1/0 and
/// "YES"/"NO" forms as booleans. Only the MySQL introspector needs this;
/// SQLite reads its PRAGMA booleans via `json_i32(...) != 0`.
#[cfg(feature = "mysql")]
fn json_bool(row: &serde_json::Value, key: &str) -> bool {
    match json_get(row, key) {
        Some(serde_json::Value::Bool(b)) => *b,
        Some(serde_json::Value::Number(n)) => n.as_i64().is_some_and(|i| i != 0),
        Some(serde_json::Value::String(s)) => {
            matches!(s.as_str(), "1" | "YES" | "yes" | "true" | "TRUE")
        }
        _ => false,
    }
}

/// Read an integer column from a JSON object row.
#[cfg(any(feature = "mysql", feature = "sqlite"))]
fn json_i32(row: &serde_json::Value, key: &str) -> Option<i32> {
    match json_get(row, key) {
        Some(serde_json::Value::Number(n)) => n.as_i64().and_then(|i| i32::try_from(i).ok()),
        Some(serde_json::Value::String(s)) => s.parse::<i32>().ok(),
        _ => None,
    }
}

// ============================================================================
// MySQL Introspector
// ============================================================================

#[cfg(feature = "mysql")]
pub mod mysql {
    use super::*;
    use prax_mysql::{MysqlPool, MysqlRawEngine};

    /// MySQL introspector backed by the `prax-mysql` engine's raw query API.
    pub struct MysqlIntrospector {
        connection_string: String,
    }

    impl MysqlIntrospector {
        /// Create a new MySQL introspector.
        pub fn new(connection_string: String) -> Self {
            Self { connection_string }
        }

        async fn engine(&self) -> CliResult<MysqlRawEngine> {
            let pool = MysqlPool::builder()
                .url(self.connection_string.clone())
                .connection_timeout(std::time::Duration::from_secs(
                    super::INTROSPECT_CONNECT_TIMEOUT_SECS,
                ))
                .build()
                .await
                .map_err(|e| CliError::Unreachable(format!("Failed to connect: {}", e)))?;
            Ok(MysqlRawEngine::new(pool))
        }

        /// Run introspection SQL, returning each row as a JSON object.
        /// Delegates to the shared [`super::JsonRowSource`] shim.
        async fn rows(engine: &MysqlRawEngine, sql: &str) -> CliResult<Vec<serde_json::Value>> {
            super::JsonRowSource::json_rows(engine, sql).await
        }
    }

    impl Introspector for MysqlIntrospector {
        async fn introspect(&self, options: &IntrospectionOptions) -> CliResult<DatabaseSchema> {
            let engine = self.engine().await?;
            // MySQL has no schema namespace distinct from the database; the
            // connection's default database scopes information_schema queries.
            let schema = options.schema.clone();
            let schema_ref = schema.as_deref();

            let mut db_schema = DatabaseSchema {
                name: "database".to_string(),
                schema: schema.clone(),
                ..Default::default()
            };

            let table_rows = Self::rows(
                &engine,
                &queries::tables_query(DatabaseType::MySQL, schema_ref),
            )
            .await?;
            for row in &table_rows {
                let Some(table_name) = json_str(row, "table_name") else {
                    continue;
                };
                if let Some(ref pattern) = options.table_filter
                    && !matches_pattern(&table_name, pattern)
                {
                    continue;
                }
                if let Some(ref exclude) = options.exclude_pattern
                    && matches_pattern(&table_name, exclude)
                {
                    continue;
                }

                db_schema.tables.push(TableInfo {
                    name: table_name,
                    schema: schema.clone(),
                    comment: if options.include_comments {
                        json_str(row, "comment").filter(|c| !c.is_empty())
                    } else {
                        None
                    },
                    ..Default::default()
                });
            }

            for table in &mut db_schema.tables {
                populate_table(&engine, table, schema_ref, options).await?;
            }

            Ok(db_schema)
        }
    }

    /// Fill a table's columns, primary key, foreign keys, and indexes.
    async fn populate_table(
        engine: &MysqlRawEngine,
        table: &mut TableInfo,
        schema: Option<&str>,
        options: &IntrospectionOptions,
    ) -> CliResult<()> {
        // Columns
        let col_rows = MysqlIntrospector::rows(
            engine,
            &queries::columns_query(DatabaseType::MySQL, &table.name, schema),
        )
        .await?;
        for row in &col_rows {
            let Some(name) = json_str(row, "column_name") else {
                continue;
            };
            let data_type = json_str(row, "data_type").unwrap_or_default();
            let max_length = json_i32(row, "character_maximum_length");
            let precision = json_i32(row, "numeric_precision");
            let scale = json_i32(row, "numeric_scale");
            let normalized = normalize_type(
                DatabaseType::MySQL,
                &data_type,
                max_length,
                precision,
                scale,
            );

            table.columns.push(ColumnInfo {
                name,
                db_type: data_type,
                normalized_type: normalized,
                nullable: json_bool(row, "nullable"),
                default: json_str(row, "column_default"),
                auto_increment: json_bool(row, "auto_increment"),
                max_length,
                precision,
                scale,
                comment: if options.include_comments {
                    json_str(row, "comment").filter(|c| !c.is_empty())
                } else {
                    None
                },
                ..Default::default()
            });
        }

        // Primary key
        let pk_rows = MysqlIntrospector::rows(
            engine,
            &queries::primary_keys_query(DatabaseType::MySQL, &table.name, schema),
        )
        .await?;
        for row in &pk_rows {
            if let Some(col) = json_str(row, "column_name") {
                table.primary_key.push(col.clone());
                if let Some(c) = table.columns.iter_mut().find(|c| c.name == col) {
                    c.is_primary_key = true;
                }
            }
        }

        // Foreign keys (grouped by constraint name, columns in order)
        let fk_rows = MysqlIntrospector::rows(
            engine,
            &queries::foreign_keys_query(DatabaseType::MySQL, &table.name, schema),
        )
        .await?;
        let mut fk_map: std::collections::HashMap<String, ForeignKeyInfo> =
            std::collections::HashMap::new();
        let mut fk_order: Vec<String> = Vec::new();
        for row in &fk_rows {
            let Some(cname) = json_str(row, "constraint_name") else {
                continue;
            };
            let fk = fk_map.entry(cname.clone()).or_insert_with(|| {
                fk_order.push(cname.clone());
                ForeignKeyInfo {
                    name: cname.clone(),
                    columns: Vec::new(),
                    referenced_table: json_str(row, "referenced_table").unwrap_or_default(),
                    referenced_schema: json_str(row, "referenced_schema"),
                    referenced_columns: Vec::new(),
                    on_delete: ReferentialAction::from_str(
                        &json_str(row, "delete_rule").unwrap_or_default(),
                    ),
                    on_update: ReferentialAction::from_str(
                        &json_str(row, "update_rule").unwrap_or_default(),
                    ),
                }
            });
            if let Some(col) = json_str(row, "column_name") {
                fk.columns.push(col);
            }
            if let Some(rc) = json_str(row, "referenced_column") {
                fk.referenced_columns.push(rc);
            }
        }
        table.foreign_keys = fk_order
            .into_iter()
            .filter_map(|n| fk_map.remove(&n))
            .collect();

        // Indexes (grouped by name, columns in order)
        let idx_rows = MysqlIntrospector::rows(
            engine,
            &queries::indexes_query(DatabaseType::MySQL, &table.name, schema),
        )
        .await?;
        let mut idx_map: std::collections::HashMap<String, IndexInfo> =
            std::collections::HashMap::new();
        let mut idx_order: Vec<String> = Vec::new();
        for row in &idx_rows {
            let Some(iname) = json_str(row, "index_name") else {
                continue;
            };
            let idx = idx_map.entry(iname.clone()).or_insert_with(|| {
                idx_order.push(iname.clone());
                IndexInfo {
                    name: iname.clone(),
                    columns: Vec::new(),
                    is_unique: json_bool(row, "is_unique"),
                    is_primary: json_bool(row, "is_primary"),
                    index_type: json_str(row, "index_type"),
                    filter: json_str(row, "filter"),
                }
            });
            if let Some(col) = json_str(row, "column_name") {
                idx.columns.push(IndexColumn {
                    name: col,
                    order: SortOrder::Asc,
                    ..Default::default()
                });
            }
        }
        table.indexes = idx_order
            .into_iter()
            .filter_map(|n| idx_map.remove(&n))
            .collect();

        Ok(())
    }
}

// ============================================================================
// SQLite Introspector
// ============================================================================

#[cfg(feature = "sqlite")]
pub mod sqlite {
    use super::*;
    use prax_sqlite::{SqlitePool, SqliteRawEngine};

    /// SQLite introspector backed by the `prax-sqlite` engine's raw query API.
    ///
    /// SQLite exposes structure through PRAGMAs rather than an
    /// `information_schema`, so this introspector parses PRAGMA output shapes
    /// (`table_info`, `foreign_key_list`, `index_list`/`index_info`) directly.
    pub struct SqliteIntrospector {
        connection_string: String,
    }

    impl SqliteIntrospector {
        /// Create a new SQLite introspector.
        pub fn new(connection_string: String) -> Self {
            Self { connection_string }
        }

        async fn engine(&self) -> CliResult<SqliteRawEngine> {
            let pool = SqlitePool::builder()
                .url(self.connection_string.clone())
                .connection_timeout(std::time::Duration::from_secs(
                    super::INTROSPECT_CONNECT_TIMEOUT_SECS,
                ))
                .build()
                .await
                .map_err(|e| CliError::Unreachable(format!("Failed to open database: {}", e)))?;
            Ok(SqliteRawEngine::new(pool))
        }

        async fn rows(engine: &SqliteRawEngine, sql: &str) -> CliResult<Vec<serde_json::Value>> {
            super::JsonRowSource::json_rows(engine, sql).await
        }
    }

    impl Introspector for SqliteIntrospector {
        async fn introspect(&self, options: &IntrospectionOptions) -> CliResult<DatabaseSchema> {
            let engine = self.engine().await?;

            let mut db_schema = DatabaseSchema {
                name: "database".to_string(),
                schema: None,
                ..Default::default()
            };

            // SQLite has no schema namespace; the tables_query ignores it.
            let table_rows =
                Self::rows(&engine, &queries::tables_query(DatabaseType::SQLite, None)).await?;
            for row in &table_rows {
                let Some(table_name) = json_str(row, "table_name") else {
                    continue;
                };
                if let Some(ref pattern) = options.table_filter
                    && !matches_pattern(&table_name, pattern)
                {
                    continue;
                }
                if let Some(ref exclude) = options.exclude_pattern
                    && matches_pattern(&table_name, exclude)
                {
                    continue;
                }
                db_schema.tables.push(TableInfo {
                    name: table_name,
                    ..Default::default()
                });
            }

            for table in &mut db_schema.tables {
                populate_table(&engine, table).await?;
            }

            Ok(db_schema)
        }
    }

    async fn populate_table(engine: &SqliteRawEngine, table: &mut TableInfo) -> CliResult<()> {
        // PRAGMA table_info: cid, name, type, notnull, dflt_value, pk
        // `pk` is the 1-based ordinal within the primary key (0 = not part).
        let col_rows = SqliteIntrospector::rows(
            engine,
            &queries::columns_query(DatabaseType::SQLite, &table.name, None),
        )
        .await?;
        let mut pk_positions: Vec<(i32, String)> = Vec::new();
        for row in &col_rows {
            let Some(name) = json_str(row, "name") else {
                continue;
            };
            let decl_type = json_str(row, "type").unwrap_or_default();
            // SQLite types can carry a size, e.g. VARCHAR(255); normalize on
            // the affinity keyword (leading identifier chars).
            let base_type: String = decl_type
                .split([' ', '('])
                .next()
                .unwrap_or("")
                .to_ascii_lowercase();
            let normalized = normalize_type(DatabaseType::SQLite, &base_type, None, None, None);
            let not_null = json_i32(row, "notnull").unwrap_or(0) != 0;
            let pk_pos = json_i32(row, "pk").unwrap_or(0);
            if pk_pos > 0 {
                pk_positions.push((pk_pos, name.clone()));
            }

            table.columns.push(ColumnInfo {
                name,
                db_type: decl_type,
                normalized_type: normalized,
                // Nullability comes solely from the column's NOT NULL flag;
                // PK membership is represented separately via is_primary_key
                // (→ @id), so a composite PK with a nullable member is not
                // silently forced non-null.
                nullable: !not_null,
                default: json_str(row, "dflt_value"),
                // rowid INTEGER PRIMARY KEY columns auto-increment; detected
                // below once the PK is known.
                auto_increment: false,
                is_primary_key: pk_pos > 0,
                ..Default::default()
            });
        }

        // Primary key columns in PK order.
        pk_positions.sort_by_key(|(pos, _)| *pos);
        table.primary_key = pk_positions.into_iter().map(|(_, name)| name).collect();

        // A single INTEGER PRIMARY KEY is an alias for rowid (auto-increment).
        if table.primary_key.len() == 1
            && let Some(col) = table
                .columns
                .iter_mut()
                .find(|c| c.name == table.primary_key[0])
            && col.db_type.to_ascii_lowercase().contains("int")
        {
            col.auto_increment = true;
        }

        // PRAGMA foreign_key_list: id, seq, table, from, to, on_update, on_delete, match
        // Rows for one FK share `id`; `seq` orders the columns.
        let fk_rows = SqliteIntrospector::rows(
            engine,
            &queries::foreign_keys_query(DatabaseType::SQLite, &table.name, None),
        )
        .await?;
        let mut fk_map: std::collections::BTreeMap<i64, ForeignKeyInfo> =
            std::collections::BTreeMap::new();
        for row in &fk_rows {
            let id = row.get("id").and_then(|v| v.as_i64()).unwrap_or(0);
            let fk = fk_map.entry(id).or_insert_with(|| ForeignKeyInfo {
                // SQLite FKs are unnamed; synthesize a stable name so the
                // diff engine can match them. The .prax must pin this via
                // @relation(map: "fk_<table>_<col>") to round-trip cleanly.
                name: String::new(),
                columns: Vec::new(),
                referenced_table: json_str(row, "table").unwrap_or_default(),
                referenced_schema: None,
                referenced_columns: Vec::new(),
                on_delete: ReferentialAction::from_str(
                    &json_str(row, "on_delete").unwrap_or_default(),
                ),
                on_update: ReferentialAction::from_str(
                    &json_str(row, "on_update").unwrap_or_default(),
                ),
            });
            if let Some(from) = json_str(row, "from") {
                fk.columns.push(from);
            }
            if let Some(to) = json_str(row, "to") {
                fk.referenced_columns.push(to);
            }
        }
        table.foreign_keys = fk_map
            .into_values()
            .map(|mut fk| {
                if fk.name.is_empty() {
                    fk.name = format!("fk_{}_{}", table.name, fk.columns.join("_"));
                }
                fk
            })
            .collect();

        // PRAGMA index_list: seq, name, unique, origin, partial
        // origin 'pk' is the implicit PK index; skip it (already represented).
        let idx_rows = SqliteIntrospector::rows(
            engine,
            &queries::indexes_query(DatabaseType::SQLite, &table.name, None),
        )
        .await?;
        for row in &idx_rows {
            let Some(idx_name) = json_str(row, "name") else {
                continue;
            };
            let origin = json_str(row, "origin").unwrap_or_default();
            let is_primary = origin == "pk";
            let is_unique = json_i32(row, "unique").unwrap_or(0) != 0;

            // PRAGMA index_info(name): seqno, cid, name — the indexed columns.
            let info_rows = SqliteIntrospector::rows(
                engine,
                &format!("PRAGMA index_info('{}')", idx_name.replace('\'', "''")),
            )
            .await?;
            let columns: Vec<IndexColumn> = info_rows
                .iter()
                .filter_map(|r| json_str(r, "name"))
                .map(|name| IndexColumn {
                    name,
                    order: SortOrder::Asc,
                    ..Default::default()
                })
                .collect();

            table.indexes.push(IndexInfo {
                name: idx_name,
                columns,
                is_unique,
                is_primary,
                index_type: None,
                filter: None,
            });
        }

        Ok(())
    }
}

// ============================================================================
// MSSQL Introspector
// ============================================================================

#[cfg(feature = "mssql")]
pub mod mssql {
    use super::*;
    use prax_mssql::MssqlPool;
    use prax_mssql::Row;

    /// MSSQL introspector backed by the `prax-mssql` engine's pooled
    /// connection. Reads typed `tiberius::Row` columns from the `sys.*`
    /// catalog queries.
    pub struct MssqlIntrospector {
        connection_string: String,
    }

    impl MssqlIntrospector {
        /// Create a new MSSQL introspector.
        pub fn new(connection_string: String) -> Self {
            Self { connection_string }
        }
    }

    /// Read a nullable string column by name.
    fn row_str(row: &Row, col: &str) -> Option<String> {
        row.try_get::<&str, _>(col)
            .ok()
            .flatten()
            .map(str::to_string)
    }

    /// Read a bit/int column as a bool.
    fn row_bool(row: &Row, col: &str) -> bool {
        if let Ok(Some(b)) = row.try_get::<bool, _>(col) {
            return b;
        }
        // Some bit-like columns arrive as integers.
        row_i64(row, col).is_some_and(|i| i != 0)
    }

    /// Read an integer column, tolerating i16/i32/i64/u8 widths.
    fn row_i64(row: &Row, col: &str) -> Option<i64> {
        if let Ok(Some(v)) = row.try_get::<i32, _>(col) {
            return Some(v as i64);
        }
        if let Ok(Some(v)) = row.try_get::<i64, _>(col) {
            return Some(v);
        }
        if let Ok(Some(v)) = row.try_get::<i16, _>(col) {
            return Some(v as i64);
        }
        if let Ok(Some(v)) = row.try_get::<u8, _>(col) {
            return Some(v as i64);
        }
        None
    }

    fn row_i32(row: &Row, col: &str) -> Option<i32> {
        row_i64(row, col).and_then(|v| i32::try_from(v).ok())
    }

    impl Introspector for MssqlIntrospector {
        async fn introspect(&self, options: &IntrospectionOptions) -> CliResult<DatabaseSchema> {
            let pool = MssqlPool::builder()
                .connection_string(self.connection_string.clone())
                .connection_timeout(std::time::Duration::from_secs(
                    super::INTROSPECT_CONNECT_TIMEOUT_SECS,
                ))
                .build()
                .await
                .map_err(|e| CliError::Unreachable(format!("Failed to connect: {}", e)))?;
            let mut conn = pool.get().await.map_err(|e| {
                CliError::Unreachable(format!("Failed to acquire connection: {}", e))
            })?;

            let schema_name = options.schema.clone().unwrap_or_else(|| "dbo".to_string());
            let schema_ref = Some(schema_name.as_str());

            let mut db_schema = DatabaseSchema {
                name: "database".to_string(),
                schema: Some(schema_name.clone()),
                ..Default::default()
            };

            let table_rows = conn
                .query(&queries::tables_query(DatabaseType::MSSQL, schema_ref), &[])
                .await
                .map_err(|e| CliError::Database(format!("Failed to query tables: {}", e)))?;
            for row in &table_rows {
                let Some(table_name) = row_str(row, "table_name") else {
                    continue;
                };
                if let Some(ref pattern) = options.table_filter
                    && !matches_pattern(&table_name, pattern)
                {
                    continue;
                }
                if let Some(ref exclude) = options.exclude_pattern
                    && matches_pattern(&table_name, exclude)
                {
                    continue;
                }
                db_schema.tables.push(TableInfo {
                    name: table_name,
                    schema: Some(schema_name.clone()),
                    comment: if options.include_comments {
                        row_str(row, "comment")
                    } else {
                        None
                    },
                    ..Default::default()
                });
            }

            for i in 0..db_schema.tables.len() {
                let table_name = db_schema.tables[i].name.clone();

                // Columns
                let col_rows = conn
                    .query(
                        &queries::columns_query(DatabaseType::MSSQL, &table_name, schema_ref),
                        &[],
                    )
                    .await
                    .map_err(|e| CliError::Database(format!("Failed to query columns: {}", e)))?;
                for row in &col_rows {
                    let Some(name) = row_str(row, "column_name") else {
                        continue;
                    };
                    let data_type = row_str(row, "data_type").unwrap_or_default();
                    // NOTE: sys.columns.max_length is a BYTE length, not a
                    // character count — nvarchar(255) reports 510 and MAX
                    // reports -1. Harmless today because VarChar/Char normalize
                    // to TEXT (length discarded) in the diff-source mapping; if
                    // length-sensitive types are added, halve for n-types and
                    // special-case -1 → MAX before comparing.
                    let max_length = row_i32(row, "character_maximum_length");
                    let precision = row_i32(row, "numeric_precision");
                    let scale = row_i32(row, "numeric_scale");
                    let normalized = normalize_type(
                        DatabaseType::MSSQL,
                        &data_type,
                        max_length,
                        precision,
                        scale,
                    );
                    db_schema.tables[i].columns.push(ColumnInfo {
                        name,
                        db_type: data_type,
                        normalized_type: normalized,
                        nullable: row_bool(row, "nullable"),
                        default: row_str(row, "column_default"),
                        auto_increment: row_bool(row, "auto_increment"),
                        max_length,
                        precision,
                        scale,
                        comment: if options.include_comments {
                            row_str(row, "comment")
                        } else {
                            None
                        },
                        ..Default::default()
                    });
                }

                // Primary key
                let pk_rows = conn
                    .query(
                        &queries::primary_keys_query(DatabaseType::MSSQL, &table_name, schema_ref),
                        &[],
                    )
                    .await
                    .map_err(|e| {
                        CliError::Database(format!("Failed to query primary keys: {}", e))
                    })?;
                for row in &pk_rows {
                    if let Some(col) = row_str(row, "column_name") {
                        db_schema.tables[i].primary_key.push(col.clone());
                        if let Some(c) = db_schema.tables[i]
                            .columns
                            .iter_mut()
                            .find(|c| c.name == col)
                        {
                            c.is_primary_key = true;
                        }
                    }
                }

                // Foreign keys
                let fk_rows = conn
                    .query(
                        &queries::foreign_keys_query(DatabaseType::MSSQL, &table_name, schema_ref),
                        &[],
                    )
                    .await
                    .map_err(|e| {
                        CliError::Database(format!("Failed to query foreign keys: {}", e))
                    })?;
                let mut fk_map: std::collections::HashMap<String, ForeignKeyInfo> =
                    std::collections::HashMap::new();
                let mut fk_order: Vec<String> = Vec::new();
                for row in &fk_rows {
                    let Some(cname) = row_str(row, "constraint_name") else {
                        continue;
                    };
                    let fk = fk_map.entry(cname.clone()).or_insert_with(|| {
                        fk_order.push(cname.clone());
                        ForeignKeyInfo {
                            name: cname.clone(),
                            columns: Vec::new(),
                            referenced_table: row_str(row, "referenced_table").unwrap_or_default(),
                            referenced_schema: row_str(row, "referenced_schema"),
                            referenced_columns: Vec::new(),
                            on_delete: ReferentialAction::from_str(
                                &row_str(row, "delete_rule").unwrap_or_default(),
                            ),
                            on_update: ReferentialAction::from_str(
                                &row_str(row, "update_rule").unwrap_or_default(),
                            ),
                        }
                    });
                    if let Some(col) = row_str(row, "column_name") {
                        fk.columns.push(col);
                    }
                    if let Some(rc) = row_str(row, "referenced_column") {
                        fk.referenced_columns.push(rc);
                    }
                }
                db_schema.tables[i].foreign_keys = fk_order
                    .into_iter()
                    .filter_map(|n| fk_map.remove(&n))
                    .collect();

                // Indexes
                let idx_rows = conn
                    .query(
                        &queries::indexes_query(DatabaseType::MSSQL, &table_name, schema_ref),
                        &[],
                    )
                    .await
                    .map_err(|e| CliError::Database(format!("Failed to query indexes: {}", e)))?;
                let mut idx_map: std::collections::HashMap<String, IndexInfo> =
                    std::collections::HashMap::new();
                let mut idx_order: Vec<String> = Vec::new();
                for row in &idx_rows {
                    let Some(iname) = row_str(row, "index_name") else {
                        continue;
                    };
                    let idx = idx_map.entry(iname.clone()).or_insert_with(|| {
                        idx_order.push(iname.clone());
                        IndexInfo {
                            name: iname.clone(),
                            columns: Vec::new(),
                            is_unique: row_bool(row, "is_unique"),
                            is_primary: row_bool(row, "is_primary"),
                            index_type: row_str(row, "index_type"),
                            filter: row_str(row, "filter"),
                        }
                    });
                    if let Some(col) = row_str(row, "column_name") {
                        idx.columns.push(IndexColumn {
                            name: col,
                            order: SortOrder::Asc,
                            ..Default::default()
                        });
                    }
                }
                db_schema.tables[i].indexes = idx_order
                    .into_iter()
                    .filter_map(|n| idx_map.remove(&n))
                    .collect();
            }

            Ok(db_schema)
        }
    }
}

// ============================================================================
// Output Formatters
// ============================================================================

/// Generate Prax schema output.
pub fn format_as_prax(schema: &DatabaseSchema, config: &Config) -> String {
    let mut output = String::new();

    output.push_str("// Generated by `prax db pull`\n");
    output.push_str("// Edit this file to customize your schema\n\n");

    output.push_str("datasource db {\n");
    output.push_str(&format!(
        "    provider = \"{}\"\n",
        config.database.provider
    ));
    output.push_str("    url      = env(\"DATABASE_URL\")\n");
    output.push_str("}\n\n");

    output.push_str("generator client {\n");
    output.push_str("    provider = \"prax-client-rust\"\n");
    output.push_str("    output   = \"./src/generated\"\n");
    output.push_str("}\n\n");

    // Use the generate_prax_schema function
    output.push_str(&generate_prax_schema(schema));

    output
}

/// Generate JSON output.
pub fn format_as_json(schema: &DatabaseSchema) -> CliResult<String> {
    serde_json::to_string_pretty(schema)
        .map_err(|e| CliError::Config(format!("Failed to serialize schema: {}", e)))
}

/// Generate SQL DDL output.
pub fn format_as_sql(schema: &DatabaseSchema, db_type: DatabaseType) -> String {
    let mut output = String::new();

    output.push_str("-- Generated by `prax db pull`\n");
    output.push_str(&format!("-- Database: {}\n\n", db_type_name(db_type)));

    // Generate enums (PostgreSQL only)
    if db_type == DatabaseType::PostgreSQL {
        for enum_info in &schema.enums {
            output.push_str(&format!("CREATE TYPE {} AS ENUM (\n", enum_info.name));
            let values: Vec<String> = enum_info
                .values
                .iter()
                .map(|v| format!("    '{}'", v))
                .collect();
            output.push_str(&values.join(",\n"));
            output.push_str("\n);\n\n");
        }
    }

    // Generate tables
    for table in &schema.tables {
        output.push_str(&format!(
            "CREATE TABLE {} (\n",
            quote_identifier(&table.name, db_type)
        ));

        let mut col_defs: Vec<String> = Vec::new();

        for col in &table.columns {
            let mut def = format!(
                "    {} {}",
                quote_identifier(&col.name, db_type),
                col.db_type
            );

            if !col.nullable {
                def.push_str(" NOT NULL");
            }

            if let Some(ref default) = col.default {
                def.push_str(&format!(" DEFAULT {}", default));
            }

            col_defs.push(def);
        }

        // Primary key
        if !table.primary_key.is_empty() {
            let pk_cols: Vec<String> = table
                .primary_key
                .iter()
                .map(|c| quote_identifier(c, db_type))
                .collect();
            col_defs.push(format!("    PRIMARY KEY ({})", pk_cols.join(", ")));
        }

        output.push_str(&col_defs.join(",\n"));
        output.push_str("\n);\n\n");

        // Indexes
        for idx in &table.indexes {
            if idx.is_primary {
                continue;
            }

            let unique = if idx.is_unique { "UNIQUE " } else { "" };
            let cols: Vec<String> = idx
                .columns
                .iter()
                .map(|c| quote_identifier(&c.name, db_type))
                .collect();

            output.push_str(&format!(
                "CREATE {}INDEX {} ON {} ({});\n",
                unique,
                quote_identifier(&idx.name, db_type),
                quote_identifier(&table.name, db_type),
                cols.join(", ")
            ));
        }

        output.push('\n');
    }

    output
}

fn db_type_name(db_type: DatabaseType) -> &'static str {
    match db_type {
        DatabaseType::PostgreSQL => "PostgreSQL",
        DatabaseType::MySQL => "MySQL",
        DatabaseType::SQLite => "SQLite",
        DatabaseType::MSSQL => "SQL Server",
    }
}

fn quote_identifier(name: &str, db_type: DatabaseType) -> String {
    match db_type {
        DatabaseType::PostgreSQL => format!("\"{}\"", name),
        DatabaseType::MySQL => format!("`{}`", name),
        DatabaseType::SQLite => format!("\"{}\"", name),
        DatabaseType::MSSQL => format!("[{}]", name),
    }
}