sql-splitter 1.19.0

High-performance CLI tool for splitting large SQL dump files into individual table files
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
//! Data type mapping between SQL dialects.
//!
//! Handles conversion of data types including:
//! - Integer types (TINYINT, SMALLINT, INT, BIGINT)
//! - Float types (FLOAT, DOUBLE, DECIMAL)
//! - String types (CHAR, VARCHAR, TEXT)
//! - Binary types (BLOB, BYTEA)
//! - Date/time types (DATE, DATETIME, TIMESTAMP)
//! - Special types (ENUM, SET, JSON)

use crate::parser::SqlDialect;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};

use super::warnings::{ConvertWarning, WarningCollector};

/// Map a single column's `source_type` from `from` to `to`, reusing the same
/// regex-driven rules [`TypeMapper::convert`] applies to a whole statement.
///
/// This is the seam the synthetic-data renderer (`render::ddl`) calls
/// instead of maintaining its own generation-only type mapping: a column's
/// `source_type` (e.g. `"VARCHAR(255)"`) is itself a valid input to
/// [`TypeMapper::convert`], since every rule matches on the type token with
/// word boundaries rather than requiring surrounding `CREATE TABLE` context.
///
/// Same-dialect conversions are the identity (returned as-is, no warning).
/// A conversion that narrows a MySQL `ENUM`/`SET` to a plain string type
/// records a [`ConvertWarning::LossyConversion`] so callers that surface
/// warnings (e.g. `--emit-config`) can report it.
pub(crate) fn map_column_type(
    source_type: &str,
    from: SqlDialect,
    to: SqlDialect,
    warnings: &mut WarningCollector,
) -> String {
    if from == to {
        return source_type.to_string();
    }
    let mapped = TypeMapper::convert(source_type, from, to);
    if is_narrowed_by_conversion(source_type, to) {
        warnings.add(ConvertWarning::LossyConversion {
            from_type: source_type.to_string(),
            to_type: mapped.clone(),
            table: None,
            column: None,
        });
    }
    mapped
}

/// Whether mapping `source_type` to the `to` dialect loses type semantics the
/// target cannot preserve — a lossy conversion the caller should warn about (and
/// fail under `--strict`):
///
/// * `ENUM`/`SET` have no equivalent outside MySQL and always collapse to a
///   plain string, for every target dialect.
/// * `JSON`/`JSONB` become an unvalidated text column on engines without a JSON
///   type (SQLite, MSSQL).
/// * `UUID`/`UNIQUEIDENTIFIER` lose their fixed 128-bit domain when stored as
///   text (MySQL, SQLite).
fn is_narrowed_by_conversion(source_type: &str, to: SqlDialect) -> bool {
    let lower = source_type.to_lowercase();
    if lower.contains("enum(") || lower.contains("set(") {
        return true;
    }
    if lower.contains("json") && matches!(to, SqlDialect::Sqlite | SqlDialect::Mssql) {
        return true;
    }
    if (lower.contains("uuid") || lower.contains("uniqueidentifier"))
        && matches!(to, SqlDialect::MySql | SqlDialect::Sqlite)
    {
        return true;
    }
    // An unbounded VARCHAR holds up to 1GB in PostgreSQL and is unlimited in
    // SQLite, but MySQL and SQL Server both require a width and have no
    // unbounded alternative legal in every position, so it maps to
    // VARCHAR(255) — anything longer is truncated at load time.
    if is_unbounded_varchar(source_type) && matches!(to, SqlDialect::MySql | SqlDialect::Mssql) {
        return true;
    }
    // Exact fixed-point (DECIMAL/NUMERIC) becomes a binary float (REAL) on
    // SQLite, silently losing precision.
    if (lower.contains("decimal") || lower.contains("numeric")) && to == SqlDialect::Sqlite {
        return true;
    }
    // A time-zone-bearing timestamp loses its zone when mapped to MySQL
    // DATETIME (which has no tz). MSSQL keeps it via DATETIMEOFFSET, and the
    // SQLite mapping renders TEXT that retains the zoned literal.
    // ("without time zone" does not contain the substring "with time zone".)
    if (lower.contains("timestamptz") || lower.contains("with time zone"))
        && to == SqlDialect::MySql
    {
        return true;
    }
    false
}

/// Whether `source_type` is a variable-length string type declared without a
/// length (`VARCHAR`, `CHARACTER VARYING`, …) — the form PostgreSQL allows and
/// MySQL does not.
fn is_unbounded_varchar(source_type: &str) -> bool {
    RE_VARCHAR_DECL
        .captures(source_type)
        .is_some_and(|caps| caps.name("len").is_none())
}

/// Give every length-less VARCHAR declaration an explicit width, for targets
/// that cannot store one without.
///
/// 255 rather than an unbounded type because every unbounded alternative is
/// invalid in some position a column can occupy: MySQL `TEXT` cannot carry a
/// DEFAULT (ERROR 1101) or be a key without a prefix length (ERROR 1170), and
/// SQL Server `VARCHAR(MAX)` cannot be an index key at all. pg_dump emits
/// PRIMARY KEY and UNIQUE as separate ALTER statements, so the column
/// definition alone never reveals whether a key is coming — only a width valid
/// everywhere is safe. It also matches the ENUM and UUID rules alongside it.
///
/// Declarations that already carry a length are left exactly as they are, as is
/// a `::` cast: those are removed wholesale by `strip_postgres_casts`, which
/// matches bare identifiers and would leave a dangling `(255)` behind.
///
/// ponytail: fixed 255 truncates longer values (`map_column_type` records a
/// LossyConversion for it); a configurable width is the upgrade path.
fn size_unbounded_varchar(stmt: &str) -> String {
    RE_VARCHAR_DECL
        .replace_all(stmt, |caps: &Captures| {
            if caps.name("cast").is_some() || caps.name("len").is_some() {
                caps[0].to_string()
            } else {
                "VARCHAR(255)".to_string()
            }
        })
        .to_string()
}

/// Type mapper for converting between dialects
pub struct TypeMapper;

impl TypeMapper {
    /// Convert all data types in a statement
    pub fn convert(stmt: &str, from: SqlDialect, to: SqlDialect) -> String {
        let converted = Self::convert_dialect_pair(stmt, from, to);
        if from == to {
            return converted;
        }

        // MySQL and SQL Server both require an explicit length on VARCHAR, and
        // they fail differently: MySQL rejects a bare declaration with ERROR
        // 1064, while SQL Server quietly reads it as VARCHAR(1) and then throws
        // Msg 2628 on the first row that does not fit. Applied per target rather
        // than per dialect pair because every source that permits an unbounded
        // declaration — PostgreSQL and SQLite — reaches both of them.
        match to {
            SqlDialect::MySql | SqlDialect::Mssql => size_unbounded_varchar(&converted),
            _ => converted,
        }
    }

    fn convert_dialect_pair(stmt: &str, from: SqlDialect, to: SqlDialect) -> String {
        match (from, to) {
            (SqlDialect::MySql, SqlDialect::Postgres) => Self::mysql_to_postgres(stmt),
            (SqlDialect::MySql, SqlDialect::Sqlite) => Self::mysql_to_sqlite(stmt),
            (SqlDialect::MySql, SqlDialect::Mssql) => Self::mysql_to_mssql(stmt),
            (SqlDialect::Postgres, SqlDialect::MySql) => Self::postgres_to_mysql(stmt),
            (SqlDialect::Postgres, SqlDialect::Sqlite) => Self::postgres_to_sqlite(stmt),
            (SqlDialect::Postgres, SqlDialect::Mssql) => Self::postgres_to_mssql(stmt),
            (SqlDialect::Sqlite, SqlDialect::MySql) => Self::sqlite_to_mysql(stmt),
            (SqlDialect::Sqlite, SqlDialect::Postgres) => Self::sqlite_to_postgres(stmt),
            (SqlDialect::Sqlite, SqlDialect::Mssql) => Self::sqlite_to_mssql(stmt),
            (SqlDialect::Mssql, SqlDialect::MySql) => Self::mssql_to_mysql(stmt),
            (SqlDialect::Mssql, SqlDialect::Postgres) => Self::mssql_to_postgres(stmt),
            (SqlDialect::Mssql, SqlDialect::Sqlite) => Self::mssql_to_sqlite(stmt),
            _ => stmt.to_string(),
        }
    }

    /// Convert MySQL types to PostgreSQL
    fn mysql_to_postgres(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // Integer types - strip display width, PostgreSQL doesn't use it
        result = RE_TINYINT_BOOL.replace_all(&result, "BOOLEAN").to_string();
        result = RE_TINYINT.replace_all(&result, "SMALLINT").to_string();
        result = RE_SMALLINT.replace_all(&result, "SMALLINT").to_string();
        result = RE_MEDIUMINT.replace_all(&result, "INTEGER").to_string();
        result = RE_INT_SIZE.replace_all(&result, "INTEGER").to_string();
        result = RE_BIGINT_SIZE.replace_all(&result, "BIGINT").to_string();

        // Float types
        result = RE_DOUBLE
            .replace_all(&result, "DOUBLE PRECISION")
            .to_string();
        result = RE_FLOAT.replace_all(&result, "REAL").to_string();

        // Text types
        result = RE_LONGTEXT.replace_all(&result, "TEXT").to_string();
        result = RE_MEDIUMTEXT.replace_all(&result, "TEXT").to_string();
        result = RE_TINYTEXT.replace_all(&result, "TEXT").to_string();

        // Binary types
        result = RE_LONGBLOB.replace_all(&result, "BYTEA").to_string();
        result = RE_MEDIUMBLOB.replace_all(&result, "BYTEA").to_string();
        result = RE_TINYBLOB.replace_all(&result, "BYTEA").to_string();
        result = RE_BLOB.replace_all(&result, "BYTEA").to_string();
        result = RE_VARBINARY.replace_all(&result, "BYTEA").to_string();
        result = RE_BINARY.replace_all(&result, "BYTEA").to_string();

        // Date/time types
        result = RE_DATETIME.replace_all(&result, "TIMESTAMP").to_string();

        // JSON
        result = RE_JSON.replace_all(&result, "JSONB").to_string();

        // ENUM - convert to VARCHAR (with warning)
        result = RE_ENUM.replace_all(&result, "VARCHAR(255)").to_string();

        // SET - convert to VARCHAR (with warning)
        result = RE_SET.replace_all(&result, "VARCHAR(255)").to_string();

        // UNSIGNED - remove
        result = RE_UNSIGNED.replace_all(&result, "").to_string();

        // ZEROFILL - remove
        result = RE_ZEROFILL.replace_all(&result, "").to_string();

        result
    }

    /// Convert MySQL types to SQLite
    fn mysql_to_sqlite(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // SQLite is lenient with types, but we normalize for consistency

        // Integer types - SQLite uses INTEGER
        result = RE_TINYINT.replace_all(&result, "INTEGER").to_string();
        result = RE_SMALLINT.replace_all(&result, "INTEGER").to_string();
        result = RE_MEDIUMINT.replace_all(&result, "INTEGER").to_string();
        result = RE_INT_SIZE.replace_all(&result, "INTEGER").to_string();
        result = RE_BIGINT_SIZE.replace_all(&result, "INTEGER").to_string();

        // Float types - SQLite uses REAL
        result = RE_DOUBLE.replace_all(&result, "REAL").to_string();
        result = RE_FLOAT.replace_all(&result, "REAL").to_string();
        result = RE_DECIMAL.replace_all(&result, "REAL").to_string();

        // Text types - all become TEXT
        result = RE_LONGTEXT.replace_all(&result, "TEXT").to_string();
        result = RE_MEDIUMTEXT.replace_all(&result, "TEXT").to_string();
        result = RE_TINYTEXT.replace_all(&result, "TEXT").to_string();
        result = RE_VARCHAR.replace_all(&result, "TEXT").to_string();
        result = RE_CHAR.replace_all(&result, "TEXT").to_string();

        // Binary types - SQLite uses BLOB
        result = RE_LONGBLOB.replace_all(&result, "BLOB").to_string();
        result = RE_MEDIUMBLOB.replace_all(&result, "BLOB").to_string();
        result = RE_TINYBLOB.replace_all(&result, "BLOB").to_string();
        result = RE_VARBINARY.replace_all(&result, "BLOB").to_string();
        result = RE_BINARY.replace_all(&result, "BLOB").to_string();

        // Date/time - SQLite stores as TEXT or INTEGER
        result = RE_DATETIME.replace_all(&result, "TEXT").to_string();
        result = RE_TIMESTAMP.replace_all(&result, "TEXT").to_string();
        result = RE_DATE.replace_all(&result, "TEXT").to_string();
        result = RE_TIME.replace_all(&result, "TEXT").to_string();

        // JSON - SQLite stores as TEXT
        result = RE_JSON.replace_all(&result, "TEXT").to_string();

        // ENUM/SET - convert to TEXT
        result = RE_ENUM.replace_all(&result, "TEXT").to_string();
        result = RE_SET.replace_all(&result, "TEXT").to_string();

        // UNSIGNED - remove
        result = RE_UNSIGNED.replace_all(&result, "").to_string();

        // ZEROFILL - remove
        result = RE_ZEROFILL.replace_all(&result, "").to_string();

        result
    }

    /// Convert PostgreSQL types to MySQL
    fn postgres_to_mysql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // SERIAL → INT AUTO_INCREMENT
        result = RE_BIGSERIAL
            .replace_all(&result, "BIGINT AUTO_INCREMENT")
            .to_string();
        result = RE_SERIAL
            .replace_all(&result, "INT AUTO_INCREMENT")
            .to_string();
        result = RE_SMALLSERIAL
            .replace_all(&result, "SMALLINT AUTO_INCREMENT")
            .to_string();

        // BYTEA → LONGBLOB
        result = RE_BYTEA.replace_all(&result, "LONGBLOB").to_string();

        // DOUBLE PRECISION → DOUBLE
        result = RE_DOUBLE_PRECISION
            .replace_all(&result, "DOUBLE")
            .to_string();

        // REAL → FLOAT
        result = RE_REAL.replace_all(&result, "FLOAT").to_string();

        // BOOLEAN → TINYINT(1)
        result = RE_BOOLEAN.replace_all(&result, "TINYINT(1)").to_string();

        // TIMESTAMPTZ → DATETIME
        result = RE_TIMESTAMPTZ.replace_all(&result, "DATETIME").to_string();

        // TIMESTAMP WITH TIME ZONE → DATETIME
        result = RE_TIMESTAMP_WITH_TZ
            .replace_all(&result, "DATETIME")
            .to_string();

        // TIMESTAMP WITHOUT TIME ZONE → DATETIME
        result = RE_TIMESTAMP_NO_TZ
            .replace_all(&result, "DATETIME")
            .to_string();

        // JSONB → JSON
        result = RE_JSONB.replace_all(&result, "JSON").to_string();

        // UUID → VARCHAR(36)
        result = RE_UUID.replace_all(&result, "VARCHAR(36)").to_string();

        result
    }

    /// Convert PostgreSQL types to SQLite
    fn postgres_to_sqlite(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // SERIAL → INTEGER (SQLite auto-increments INTEGER PRIMARY KEY)
        result = RE_BIGSERIAL.replace_all(&result, "INTEGER").to_string();
        result = RE_SERIAL.replace_all(&result, "INTEGER").to_string();
        result = RE_SMALLSERIAL.replace_all(&result, "INTEGER").to_string();

        // BYTEA → BLOB
        result = RE_BYTEA.replace_all(&result, "BLOB").to_string();

        // DOUBLE PRECISION → REAL
        result = RE_DOUBLE_PRECISION.replace_all(&result, "REAL").to_string();

        // BOOLEAN → INTEGER
        result = RE_BOOLEAN.replace_all(&result, "INTEGER").to_string();

        // Timestamps → TEXT
        result = RE_TIMESTAMPTZ.replace_all(&result, "TEXT").to_string();
        result = RE_TIMESTAMP_WITH_TZ
            .replace_all(&result, "TEXT")
            .to_string();
        result = RE_TIMESTAMP_NO_TZ.replace_all(&result, "TEXT").to_string();

        // JSONB/JSON → TEXT
        result = RE_JSONB.replace_all(&result, "TEXT").to_string();
        result = RE_JSON.replace_all(&result, "TEXT").to_string();

        // UUID → TEXT
        result = RE_UUID.replace_all(&result, "TEXT").to_string();

        // VARCHAR → TEXT
        result = RE_VARCHAR.replace_all(&result, "TEXT").to_string();

        result
    }

    /// Convert SQLite types to MySQL
    fn sqlite_to_mysql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // SQLite uses TEXT for everything, but we can preserve some type info
        // REAL → DOUBLE
        result = RE_REAL.replace_all(&result, "DOUBLE").to_string();

        // BLOB stays BLOB
        // TEXT stays TEXT
        // INTEGER stays INTEGER (MySQL will handle it)

        result
    }

    /// Convert SQLite types to PostgreSQL
    fn sqlite_to_postgres(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // REAL → DOUBLE PRECISION
        result = RE_REAL.replace_all(&result, "DOUBLE PRECISION").to_string();

        // BLOB → BYTEA
        result = RE_BLOB.replace_all(&result, "BYTEA").to_string();

        // INTEGER stays INTEGER
        // TEXT stays TEXT

        result
    }

    /// Convert MySQL types to MSSQL
    fn mysql_to_mssql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // AUTO_INCREMENT → IDENTITY(1,1) (handled elsewhere in convert_auto_increment)

        // Integer types - strip display width
        result = RE_TINYINT_BOOL.replace_all(&result, "BIT").to_string();
        result = RE_TINYINT.replace_all(&result, "TINYINT").to_string();
        result = RE_SMALLINT.replace_all(&result, "SMALLINT").to_string();
        result = RE_MEDIUMINT.replace_all(&result, "INT").to_string();
        result = RE_INT_SIZE.replace_all(&result, "INT").to_string();
        result = RE_BIGINT_SIZE.replace_all(&result, "BIGINT").to_string();

        // Float types
        result = RE_DOUBLE.replace_all(&result, "FLOAT").to_string();
        result = RE_FLOAT.replace_all(&result, "REAL").to_string();

        // Text types
        result = RE_LONGTEXT
            .replace_all(&result, "NVARCHAR(MAX)")
            .to_string();
        result = RE_MEDIUMTEXT
            .replace_all(&result, "NVARCHAR(MAX)")
            .to_string();
        result = RE_TINYTEXT
            .replace_all(&result, "NVARCHAR(255)")
            .to_string();

        // Binary types
        result = RE_LONGBLOB
            .replace_all(&result, "VARBINARY(MAX)")
            .to_string();
        result = RE_MEDIUMBLOB
            .replace_all(&result, "VARBINARY(MAX)")
            .to_string();
        result = RE_TINYBLOB
            .replace_all(&result, "VARBINARY(255)")
            .to_string();
        result = RE_BLOB.replace_all(&result, "VARBINARY(MAX)").to_string();

        // Date/time types
        result = RE_DATETIME.replace_all(&result, "DATETIME2").to_string();

        // JSON → NVARCHAR(MAX)
        result = RE_JSON.replace_all(&result, "NVARCHAR(MAX)").to_string();

        // ENUM → NVARCHAR(255)
        result = RE_ENUM.replace_all(&result, "NVARCHAR(255)").to_string();

        // SET → NVARCHAR(255)
        result = RE_SET.replace_all(&result, "NVARCHAR(255)").to_string();

        // UNSIGNED - remove
        result = RE_UNSIGNED.replace_all(&result, "").to_string();

        // ZEROFILL - remove
        result = RE_ZEROFILL.replace_all(&result, "").to_string();

        result
    }

    /// Convert PostgreSQL types to MSSQL
    fn postgres_to_mssql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // SERIAL → INT IDENTITY(1,1)
        result = RE_BIGSERIAL
            .replace_all(&result, "BIGINT IDENTITY(1,1)")
            .to_string();
        result = RE_SERIAL
            .replace_all(&result, "INT IDENTITY(1,1)")
            .to_string();
        result = RE_SMALLSERIAL
            .replace_all(&result, "SMALLINT IDENTITY(1,1)")
            .to_string();

        // BYTEA → VARBINARY(MAX)
        result = RE_BYTEA.replace_all(&result, "VARBINARY(MAX)").to_string();

        // DOUBLE PRECISION → FLOAT
        result = RE_DOUBLE_PRECISION
            .replace_all(&result, "FLOAT")
            .to_string();

        // REAL stays REAL

        // BOOLEAN → BIT
        result = RE_BOOLEAN.replace_all(&result, "BIT").to_string();

        // TIMESTAMPTZ → DATETIMEOFFSET
        result = RE_TIMESTAMPTZ
            .replace_all(&result, "DATETIMEOFFSET")
            .to_string();

        // TIMESTAMP WITH TIME ZONE → DATETIMEOFFSET
        result = RE_TIMESTAMP_WITH_TZ
            .replace_all(&result, "DATETIMEOFFSET")
            .to_string();

        // TIMESTAMP WITHOUT TIME ZONE → DATETIME2
        result = RE_TIMESTAMP_NO_TZ
            .replace_all(&result, "DATETIME2")
            .to_string();

        // JSONB → NVARCHAR(MAX)
        result = RE_JSONB.replace_all(&result, "NVARCHAR(MAX)").to_string();

        // JSON → NVARCHAR(MAX)
        result = RE_JSON.replace_all(&result, "NVARCHAR(MAX)").to_string();

        // UUID → UNIQUEIDENTIFIER
        result = RE_UUID.replace_all(&result, "UNIQUEIDENTIFIER").to_string();

        // TEXT → NVARCHAR(MAX)
        result = RE_TEXT.replace_all(&result, "NVARCHAR(MAX)").to_string();

        result
    }

    /// Convert SQLite types to MSSQL
    fn sqlite_to_mssql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // REAL → FLOAT
        result = RE_REAL.replace_all(&result, "FLOAT").to_string();

        // BLOB → VARBINARY(MAX)
        result = RE_BLOB.replace_all(&result, "VARBINARY(MAX)").to_string();

        // TEXT → NVARCHAR(MAX)
        result = RE_TEXT.replace_all(&result, "NVARCHAR(MAX)").to_string();

        result
    }

    /// Convert MSSQL types to MySQL
    fn mssql_to_mysql(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // IDENTITY → AUTO_INCREMENT (handled elsewhere)

        // BIT → TINYINT(1)
        result = RE_BIT.replace_all(&result, "TINYINT(1)").to_string();

        // NVARCHAR(MAX) → LONGTEXT
        result = RE_NVARCHAR_MAX.replace_all(&result, "LONGTEXT").to_string();

        // NVARCHAR(n) → VARCHAR(n)
        result = RE_NVARCHAR.replace_all(&result, "VARCHAR$1").to_string();

        // NCHAR(n) → CHAR(n)
        result = RE_NCHAR.replace_all(&result, "CHAR$1").to_string();

        // NTEXT → LONGTEXT
        result = RE_NTEXT.replace_all(&result, "LONGTEXT").to_string();

        // VARCHAR(MAX) → LONGTEXT
        result = RE_VARCHAR_MAX.replace_all(&result, "LONGTEXT").to_string();

        // VARBINARY(MAX) → LONGBLOB
        result = RE_VARBINARY_MAX
            .replace_all(&result, "LONGBLOB")
            .to_string();

        // IMAGE → LONGBLOB
        result = RE_IMAGE.replace_all(&result, "LONGBLOB").to_string();

        // DATETIME2 → DATETIME(6)
        result = RE_DATETIME2.replace_all(&result, "DATETIME(6)").to_string();

        // DATETIMEOFFSET → DATETIME
        result = RE_DATETIMEOFFSET
            .replace_all(&result, "DATETIME")
            .to_string();

        // SMALLDATETIME → DATETIME
        result = RE_SMALLDATETIME
            .replace_all(&result, "DATETIME")
            .to_string();

        // MONEY → DECIMAL(19,4)
        result = RE_MONEY.replace_all(&result, "DECIMAL(19,4)").to_string();

        // SMALLMONEY → DECIMAL(10,4)
        result = RE_SMALLMONEY
            .replace_all(&result, "DECIMAL(10,4)")
            .to_string();

        // UNIQUEIDENTIFIER → VARCHAR(36)
        result = RE_UNIQUEIDENTIFIER
            .replace_all(&result, "VARCHAR(36)")
            .to_string();

        // XML → LONGTEXT
        result = RE_XML.replace_all(&result, "LONGTEXT").to_string();

        // ROWVERSION/MSSQL TIMESTAMP → BINARY(8)
        result = RE_MSSQL_TIMESTAMP_BRACKETED
            .replace_all(&result, "BINARY(8)")
            .to_string();
        result = RE_ROWVERSION_ONLY
            .replace_all(&result, "BINARY(8)")
            .to_string();

        // Strip MSSQL-specific clauses
        result = RE_ON_PRIMARY.replace_all(&result, "").to_string();
        result = RE_CLUSTERED.replace_all(&result, "").to_string();
        result = RE_NONCLUSTERED.replace_all(&result, "").to_string();

        result
    }

    /// Convert MSSQL types to PostgreSQL
    fn mssql_to_postgres(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // IDENTITY → SERIAL (handled elsewhere)

        // IMPORTANT: Handle ROWVERSION first (before any TIMESTAMP conversion)
        // In MSSQL, TIMESTAMP is an alias for ROWVERSION (a binary type, not datetime!)
        // Use a more specific regex that matches MSSQL TIMESTAMP but not PostgreSQL TIMESTAMP
        result = RE_MSSQL_TIMESTAMP_BRACKETED
            .replace_all(&result, "BYTEA")
            .to_string();
        result = RE_ROWVERSION_ONLY.replace_all(&result, "BYTEA").to_string();

        // BIT → BOOLEAN
        result = RE_BIT.replace_all(&result, "BOOLEAN").to_string();

        // NVARCHAR(MAX) → TEXT
        result = RE_NVARCHAR_MAX.replace_all(&result, "TEXT").to_string();

        // NVARCHAR(n) → VARCHAR(n)
        result = RE_NVARCHAR.replace_all(&result, "VARCHAR$1").to_string();

        // NCHAR(n) → CHAR(n)
        result = RE_NCHAR.replace_all(&result, "CHAR$1").to_string();

        // NTEXT → TEXT
        result = RE_NTEXT.replace_all(&result, "TEXT").to_string();

        // VARCHAR(MAX) → TEXT
        result = RE_VARCHAR_MAX.replace_all(&result, "TEXT").to_string();

        // VARBINARY(MAX) → BYTEA
        result = RE_VARBINARY_MAX.replace_all(&result, "BYTEA").to_string();

        // VARBINARY(n) → BYTEA
        result = RE_VARBINARY.replace_all(&result, "BYTEA").to_string();

        // IMAGE → BYTEA
        result = RE_IMAGE.replace_all(&result, "BYTEA").to_string();

        // DATETIME2 → TIMESTAMP
        result = RE_DATETIME2.replace_all(&result, "TIMESTAMP").to_string();

        // DATETIME → TIMESTAMP (but not MSSQL TIMESTAMP which is already converted)
        result = RE_DATETIME.replace_all(&result, "TIMESTAMP").to_string();

        // DATETIMEOFFSET → TIMESTAMPTZ
        result = RE_DATETIMEOFFSET
            .replace_all(&result, "TIMESTAMPTZ")
            .to_string();

        // SMALLDATETIME → TIMESTAMP
        result = RE_SMALLDATETIME
            .replace_all(&result, "TIMESTAMP")
            .to_string();

        // MONEY → DECIMAL(19,4)
        result = RE_MONEY.replace_all(&result, "DECIMAL(19,4)").to_string();

        // SMALLMONEY → DECIMAL(10,4)
        result = RE_SMALLMONEY
            .replace_all(&result, "DECIMAL(10,4)")
            .to_string();

        // UNIQUEIDENTIFIER → UUID
        result = RE_UNIQUEIDENTIFIER.replace_all(&result, "UUID").to_string();

        // XML → XML (PostgreSQL supports XML type)

        // FLOAT → DOUBLE PRECISION
        result = RE_FLOAT
            .replace_all(&result, "DOUBLE PRECISION")
            .to_string();

        // Strip MSSQL-specific clauses
        result = RE_ON_PRIMARY.replace_all(&result, "").to_string();
        result = RE_CLUSTERED.replace_all(&result, "").to_string();
        result = RE_NONCLUSTERED.replace_all(&result, "").to_string();

        result
    }

    /// Convert MSSQL types to SQLite
    fn mssql_to_sqlite(stmt: &str) -> String {
        let mut result = stmt.to_string();

        // BIT → INTEGER
        result = RE_BIT.replace_all(&result, "INTEGER").to_string();

        // NVARCHAR → TEXT
        result = RE_NVARCHAR_MAX.replace_all(&result, "TEXT").to_string();
        result = RE_NVARCHAR.replace_all(&result, "TEXT").to_string();

        // NCHAR → TEXT
        result = RE_NCHAR.replace_all(&result, "TEXT").to_string();

        // NTEXT → TEXT
        result = RE_NTEXT.replace_all(&result, "TEXT").to_string();

        // VARCHAR(MAX) → TEXT
        result = RE_VARCHAR_MAX.replace_all(&result, "TEXT").to_string();

        // VARBINARY → BLOB
        result = RE_VARBINARY_MAX.replace_all(&result, "BLOB").to_string();
        result = RE_VARBINARY.replace_all(&result, "BLOB").to_string();

        // IMAGE → BLOB
        result = RE_IMAGE.replace_all(&result, "BLOB").to_string();

        // Date/time → TEXT
        result = RE_DATETIME2.replace_all(&result, "TEXT").to_string();
        result = RE_DATETIME.replace_all(&result, "TEXT").to_string();
        result = RE_DATETIMEOFFSET.replace_all(&result, "TEXT").to_string();
        result = RE_SMALLDATETIME.replace_all(&result, "TEXT").to_string();

        // MONEY → REAL
        result = RE_MONEY.replace_all(&result, "REAL").to_string();
        result = RE_SMALLMONEY.replace_all(&result, "REAL").to_string();

        // UNIQUEIDENTIFIER → TEXT
        result = RE_UNIQUEIDENTIFIER.replace_all(&result, "TEXT").to_string();

        // XML → TEXT
        result = RE_XML.replace_all(&result, "TEXT").to_string();

        // ROWVERSION/MSSQL TIMESTAMP → BLOB
        result = RE_MSSQL_TIMESTAMP_BRACKETED
            .replace_all(&result, "BLOB")
            .to_string();
        result = RE_ROWVERSION_ONLY.replace_all(&result, "BLOB").to_string();

        // FLOAT → REAL
        result = RE_FLOAT.replace_all(&result, "REAL").to_string();

        // Strip MSSQL-specific clauses
        result = RE_ON_PRIMARY.replace_all(&result, "").to_string();
        result = RE_CLUSTERED.replace_all(&result, "").to_string();
        result = RE_NONCLUSTERED.replace_all(&result, "").to_string();

        result
    }
}

// Pre-compiled regexes for type matching
static RE_TINYINT_BOOL: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bTINYINT\s*\(\s*1\s*\)").unwrap());
static RE_TINYINT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bTINYINT\s*(\(\s*\d+\s*\))?").unwrap());
static RE_SMALLINT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bSMALLINT\s*(\(\s*\d+\s*\))?").unwrap());
static RE_MEDIUMINT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bMEDIUMINT\s*(\(\s*\d+\s*\))?").unwrap());
static RE_INT_SIZE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bINT\s*\(\s*\d+\s*\)").unwrap());
static RE_BIGINT_SIZE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bBIGINT\s*\(\s*\d+\s*\)").unwrap());

static RE_DOUBLE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bDOUBLE\b").unwrap());
static RE_FLOAT: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bFLOAT\s*(\(\s*\d+\s*(,\s*\d+\s*)?\))?").unwrap());
static RE_DECIMAL: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bDECIMAL\s*\(\s*\d+\s*(,\s*\d+\s*)?\)").unwrap());

static RE_LONGTEXT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bLONGTEXT\b").unwrap());
static RE_MEDIUMTEXT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bMEDIUMTEXT\b").unwrap());
static RE_TINYTEXT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bTINYTEXT\b").unwrap());
static RE_VARCHAR: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bVARCHAR\s*\(\s*\d+\s*\)").unwrap());
/// A PostgreSQL variable-length string declaration in any of its spellings,
/// with the length (if declared) captured as `len` and a leading `::` cast
/// marker captured as `cast`.
///
/// `pg_dump` writes `character varying`, never `varchar`, so the long spellings
/// are the ones that actually turn up in real dumps. Matching the optional
/// `::` prefix is what keeps the rewrite out of a cast such as
/// `DEFAULT 'active'::character varying`: those are removed wholesale later by
/// `strip_postgres_casts`, which matches bare identifiers and would leave a
/// dangling `(255)` behind if the type inside it had been rewritten.
static RE_VARCHAR_DECL: Lazy<Regex> = Lazy::new(|| {
    Regex::new(
        r"(?i)(?P<cast>::\s*)?\b(?:VARCHAR|(?:NATIONAL\s+)?(?:CHARACTER|CHAR)\s+VARYING)(?P<len>\s*\(\s*\d+\s*\))?",
    )
    .unwrap()
});
static RE_CHAR: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bCHAR\s*\(\s*\d+\s*\)").unwrap());

static RE_LONGBLOB: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bLONGBLOB\b").unwrap());
static RE_MEDIUMBLOB: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bMEDIUMBLOB\b").unwrap());
static RE_TINYBLOB: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bTINYBLOB\b").unwrap());
static RE_BLOB: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBLOB\b").unwrap());
static RE_VARBINARY: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bVARBINARY\s*\(\s*\d+\s*\)").unwrap());
static RE_BINARY: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBINARY\s*\(\s*\d+\s*\)").unwrap());

static RE_DATETIME: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bDATETIME(\(\s*\d+\s*\))?").unwrap());
static RE_TIMESTAMP: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bTIMESTAMP\s*(\(\s*\d+\s*\))?").unwrap());
static RE_DATE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bDATE\b").unwrap());
static RE_TIME: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bTIME\s*(\(\s*\d+\s*\))?").unwrap());

static RE_JSON: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bJSON\b").unwrap());

static RE_ENUM: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bENUM\s*\([^)]+\)").unwrap());
static RE_SET: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bSET\s*\([^)]+\)").unwrap());

static RE_UNSIGNED: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\s+UNSIGNED\b").unwrap());
static RE_ZEROFILL: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\s+ZEROFILL\b").unwrap());

// PostgreSQL specific types
static RE_SERIAL: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bSERIAL\b").unwrap());
static RE_BIGSERIAL: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBIGSERIAL\b").unwrap());
static RE_SMALLSERIAL: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bSMALLSERIAL\b").unwrap());
static RE_BYTEA: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBYTEA\b").unwrap());
static RE_DOUBLE_PRECISION: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bDOUBLE\s+PRECISION\b").unwrap());
static RE_REAL: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bREAL\b").unwrap());
static RE_BOOLEAN: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBOOLEAN\b").unwrap());
static RE_TIMESTAMPTZ: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bTIMESTAMPTZ\b").unwrap());
static RE_TIMESTAMP_WITH_TZ: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bTIMESTAMP\s+WITH\s+TIME\s+ZONE\b").unwrap());
static RE_TIMESTAMP_NO_TZ: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bTIMESTAMP\s+WITHOUT\s+TIME\s+ZONE\b").unwrap());
static RE_JSONB: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bJSONB\b").unwrap());
static RE_UUID: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bUUID\b").unwrap());
static RE_TEXT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bTEXT\b").unwrap());

// MSSQL specific types
static RE_BIT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bBIT\b").unwrap());
static RE_NVARCHAR_MAX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bNVARCHAR\s*\(\s*MAX\s*\)").unwrap());
static RE_NVARCHAR: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bNVARCHAR\s*(\(\s*\d+\s*\))").unwrap());
static RE_NCHAR: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bNCHAR\s*(\(\s*\d+\s*\))").unwrap());
static RE_NTEXT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bNTEXT\b").unwrap());
static RE_VARCHAR_MAX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bVARCHAR\s*\(\s*MAX\s*\)").unwrap());
static RE_VARBINARY_MAX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bVARBINARY\s*\(\s*MAX\s*\)").unwrap());
static RE_IMAGE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bIMAGE\b").unwrap());
static RE_DATETIME2: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bDATETIME2\s*(\(\s*\d+\s*\))?").unwrap());
static RE_DATETIMEOFFSET: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bDATETIMEOFFSET\s*(\(\s*\d+\s*\))?").unwrap());
static RE_SMALLDATETIME: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bSMALLDATETIME\b").unwrap());
static RE_MONEY: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bMONEY\b").unwrap());
static RE_SMALLMONEY: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bSMALLMONEY\b").unwrap());
static RE_UNIQUEIDENTIFIER: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\bUNIQUEIDENTIFIER\b").unwrap());
static RE_XML: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bXML\b").unwrap());
// MSSQL TIMESTAMP type (binary versioning, NOT datetime) - only match bracketed [TIMESTAMP]
// or as column type after brackets. We can't match unbracketed standalone TIMESTAMP safely
// because it would conflict with PostgreSQL TIMESTAMP result. So we rely on context.
static RE_MSSQL_TIMESTAMP_BRACKETED: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\[\s*TIMESTAMP\s*\]").unwrap());
static RE_ROWVERSION_ONLY: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bROWVERSION\b").unwrap());

// MSSQL-specific clauses to strip when converting to other dialects
static RE_ON_PRIMARY: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?i)\s*ON\s*\[\s*PRIMARY\s*\]").unwrap());
static RE_CLUSTERED: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bCLUSTERED\s+").unwrap());
static RE_NONCLUSTERED: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bNONCLUSTERED\s+").unwrap());

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

    #[test]
    fn same_dialect_is_identity_and_warning_free() {
        let mut warnings = WarningCollector::new();
        let mapped = map_column_type(
            "VARCHAR(255)",
            SqlDialect::MySql,
            SqlDialect::MySql,
            &mut warnings,
        );
        assert_eq!(mapped, "VARCHAR(255)");
        assert!(!warnings.has_warnings());
    }

    #[test]
    fn cross_dialect_reuses_the_statement_level_regex_rules() {
        let mut warnings = WarningCollector::new();
        let mapped = map_column_type(
            "BIGINT(20)",
            SqlDialect::MySql,
            SqlDialect::Postgres,
            &mut warnings,
        );
        assert_eq!(mapped, "BIGINT");
    }

    #[test]
    fn postgres_unbounded_varchar_maps_to_a_sized_mysql_varchar() {
        for source in [
            "varchar",
            "VARCHAR",
            "character varying",
            "CHARACTER VARYING",
            "char varying",
        ] {
            let mut warnings = WarningCollector::new();
            let mapped = map_column_type(
                source,
                SqlDialect::Postgres,
                SqlDialect::MySql,
                &mut warnings,
            );
            assert_eq!(mapped, "VARCHAR(255)", "source: {source}");
            // PostgreSQL stores up to 1GB in an unbounded VARCHAR, so pinning it
            // to 255 truncates: callers surfacing warnings (and --strict) must
            // see this rather than discover it at load time.
            assert!(warnings.has_warnings(), "source: {source}");
            assert!(matches!(
                warnings.warnings()[0],
                ConvertWarning::LossyConversion { .. }
            ));
        }
    }

    #[test]
    fn postgres_sized_varchar_is_already_valid_mysql_and_is_left_alone() {
        for source in ["varchar(255)", "character varying(120)", "VARCHAR (64)"] {
            let mut warnings = WarningCollector::new();
            let mapped = map_column_type(
                source,
                SqlDialect::Postgres,
                SqlDialect::MySql,
                &mut warnings,
            );
            assert_eq!(mapped, source, "source: {source}");
            assert!(!warnings.has_warnings(), "source: {source}");
        }
    }

    #[test]
    fn narrowing_enum_to_a_plain_string_records_a_lossy_warning() {
        let mut warnings = WarningCollector::new();
        let mapped = map_column_type(
            "ENUM('a','b')",
            SqlDialect::MySql,
            SqlDialect::Postgres,
            &mut warnings,
        );
        assert_eq!(mapped, "VARCHAR(255)");
        assert!(warnings.has_warnings());
        assert!(matches!(
            warnings.warnings()[0],
            ConvertWarning::LossyConversion { .. }
        ));
    }

    #[test]
    fn json_to_an_engine_without_a_json_type_is_lossy() {
        let mut warnings = WarningCollector::new();
        map_column_type("JSON", SqlDialect::MySql, SqlDialect::Sqlite, &mut warnings);
        assert!(warnings.has_warnings());

        // PostgreSQL keeps a native JSON type, so the same source is not lossy.
        let mut kept = WarningCollector::new();
        map_column_type("JSON", SqlDialect::MySql, SqlDialect::Postgres, &mut kept);
        assert!(!kept.has_warnings());
    }

    #[test]
    fn uuid_stored_as_text_is_lossy() {
        let mut warnings = WarningCollector::new();
        map_column_type(
            "UUID",
            SqlDialect::Postgres,
            SqlDialect::MySql,
            &mut warnings,
        );
        assert!(warnings.has_warnings());

        // MSSQL has UNIQUEIDENTIFIER, so a UUID keeps its native domain there.
        let mut kept = WarningCollector::new();
        map_column_type("UUID", SqlDialect::Postgres, SqlDialect::Mssql, &mut kept);
        assert!(!kept.has_warnings());
    }

    #[test]
    fn decimal_to_sqlite_real_is_lossy() {
        // SQLite has no exact fixed-point type; the mapper renders DECIMAL as
        // REAL (binary float), silently losing exact precision.
        let mut warnings = WarningCollector::new();
        map_column_type(
            "DECIMAL(10,2)",
            SqlDialect::MySql,
            SqlDialect::Sqlite,
            &mut warnings,
        );
        assert!(warnings.has_warnings());

        // PostgreSQL keeps a native DECIMAL, so the same source is not lossy.
        let mut kept = WarningCollector::new();
        map_column_type(
            "DECIMAL(10,2)",
            SqlDialect::MySql,
            SqlDialect::Postgres,
            &mut kept,
        );
        assert!(!kept.has_warnings());
    }

    #[test]
    fn timestamptz_to_mysql_drops_the_timezone_and_is_lossy() {
        // MySQL DATETIME has no time-zone component, so the tz is dropped.
        let mut warnings = WarningCollector::new();
        map_column_type(
            "TIMESTAMPTZ",
            SqlDialect::Postgres,
            SqlDialect::MySql,
            &mut warnings,
        );
        assert!(warnings.has_warnings());

        // MSSQL DATETIMEOFFSET preserves the time zone, so it is not lossy.
        let mut kept = WarningCollector::new();
        map_column_type(
            "TIMESTAMPTZ",
            SqlDialect::Postgres,
            SqlDialect::Mssql,
            &mut kept,
        );
        assert!(!kept.has_warnings());
    }
}