sqltool 0.6.1

功能强大的数据库迁移、同步、运维工具,支持异构数据库自动转换、自动分库分表、慢查询检测、数据对比、备份恢复,提供多语言SDK
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
//! 跨版本数据迁移模块
//!
//! 支持三种迁移场景:
//! 1. **同库跨版本迁移**:MySQL 5.5 → 8.0、PostgreSQL 9.x → 16、SQLite 3.20 → 3.45
//! 2. **异构同版本迁移**:MySQL 8.0 → PostgreSQL 16(同版本异库)
//! 3. **异构跨版本迁移**:MySQL 5.7 → PostgreSQL 16(最复杂)
//!
//! 核心能力:
//! - 自动检测源/目标库版本(基于 `DatabaseVersion`)
//! - 字段类型自动升级(向高版本特性靠拢)
//! - 不同字段名的自动连线(6 级匹配 + 用户手工覆盖)
//! - 数据值按源/目标类型/版本三方综合规则转换
//! - 迁移报告:每张表的字段映射、转换规则、有损警告

use crate::core::cross_db_conversion::{
    ConversionReport, CrossDbConverter, FieldLink, FieldLinker, LinkKind, TargetDbKind,
    TypeMappingTable, ValueTransformer,
};
use crate::databases::{DatabaseCompatibility, DatabaseVersion};
use crate::models::{Field, TableSchema};
use anyhow::Result;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 数据迁移方向
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MigrationDirection {
    /// 同库跨版本
    SameDbCrossVersion,
    /// 异构同版本
    CrossDbSameVersion,
    /// 异构跨版本
    CrossDbCrossVersion,
    /// 同库同版本(如备份还原)
    SameDbSameVersion,
}

impl MigrationDirection {
    pub fn name(&self) -> &'static str {
        match self {
            MigrationDirection::SameDbCrossVersion => "同库跨版本",
            MigrationDirection::CrossDbSameVersion => "异构同版本",
            MigrationDirection::CrossDbCrossVersion => "异构跨版本",
            MigrationDirection::SameDbSameVersion => "同库同版本",
        }
    }
}

/// 迁移配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationConfig {
    /// 源库
    pub source_db: TargetDbKind,
    /// 目标库
    pub target_db: TargetDbKind,
    /// 源库版本(可选,未指定时按最新版本)
    pub source_version: Option<DatabaseVersion>,
    /// 目标库版本
    pub target_version: Option<DatabaseVersion>,
    /// 是否自动匹配字段名不同的字段
    pub auto_field_link: bool,
    /// 手工字段映射(覆盖自动连线)
    pub manual_field_map: HashMap<String, String>,
    /// 批大小
    pub batch_size: usize,
    /// 字段类型升级阈值(低版本到高版本时启用)
    pub enable_version_upgrade: bool,
    /// 转换前是否校验目标字段类型
    pub pre_check: bool,
    /// 默认源版本(用户未指定时使用)
    pub default_source_version: Option<DatabaseVersion>,
    /// 默认目标版本
    pub default_target_version: Option<DatabaseVersion>,
}

impl Default for MigrationConfig {
    fn default() -> Self {
        Self {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: None,
            target_version: None,
            auto_field_link: true,
            manual_field_map: HashMap::new(),
            batch_size: 1000,
            enable_version_upgrade: true,
            pre_check: true,
            default_source_version: None,
            default_target_version: None,
        }
    }
}

impl MigrationConfig {
    /// 高效推断源版本
    pub fn effective_source_version(&self) -> DatabaseVersion {
        self.source_version
            .clone()
            .or_else(|| self.default_source_version.clone())
            .unwrap_or_else(|| default_version_for(self.source_db))
    }

    /// 推断目标版本
    pub fn effective_target_version(&self) -> DatabaseVersion {
        self.target_version
            .clone()
            .or_else(|| self.default_target_version.clone())
            .unwrap_or_else(|| default_version_for(self.target_db))
    }

    /// 推断迁移方向
    pub fn direction(&self) -> MigrationDirection {
        if self.source_db == self.target_db {
            if self.effective_source_version().major == self.effective_target_version().major
                && self.effective_source_version().minor == self.effective_target_version().minor
            {
                MigrationDirection::SameDbSameVersion
            } else {
                MigrationDirection::SameDbCrossVersion
            }
        } else if self.effective_source_version().major == self.effective_target_version().major
            && self.effective_source_version().minor == self.effective_target_version().minor
        {
            MigrationDirection::CrossDbSameVersion
        } else {
            MigrationDirection::CrossDbCrossVersion
        }
    }
}

fn default_version_for(db: TargetDbKind) -> DatabaseVersion {
    match db {
        TargetDbKind::MySQL | TargetDbKind::MariaDB | TargetDbKind::TiDB => {
            DatabaseVersion::new(8, 0, 32)
        }
        TargetDbKind::PostgreSQL => DatabaseVersion::new(16, 2, 0),
        TargetDbKind::SQLite => DatabaseVersion::new(3, 45, 0),
        TargetDbKind::Oracle => DatabaseVersion::new(21, 0, 0),
        TargetDbKind::MSSQL => DatabaseVersion::new(16, 0, 0),
        _ => DatabaseVersion::new(1, 0, 0),
    }
}

/// 字段值映射(按字段)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldMigration {
    pub source_field: String,
    pub target_field: String,
    pub source_type: String,
    pub target_type: String,
    /// 转换函数名(cast/concat/format/identity)
    pub transform: String,
    /// 是否有损
    pub lossy: bool,
    /// 警告信息
    pub warnings: Vec<String>,
}

/// 单表迁移结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableMigrationResult {
    pub table_name: String,
    pub direction: MigrationDirection,
    pub source_db: TargetDbKind,
    pub target_db: TargetDbKind,
    pub source_version: String,
    pub target_version: String,
    pub fields_total: usize,
    pub fields_mapped: usize,
    pub fields_unmapped: usize,
    pub lossy_conversions: usize,
    pub warnings: Vec<String>,
    pub field_migrations: Vec<FieldMigration>,
    pub ddl: String,
    pub started_at: String,
    pub finished_at: String,
    pub elapsed_ms: u64,
}

impl TableMigrationResult {
    pub fn success_rate(&self) -> f64 {
        if self.fields_total == 0 {
            0.0
        } else {
            self.fields_mapped as f64 / self.fields_total as f64
        }
    }
}

/// 迁移器(主入口)
pub struct DataMigrator {
    converter: CrossDbConverter,
}

impl DataMigrator {
    pub fn new() -> Self {
        Self {
            converter: CrossDbConverter::new(),
        }
    }

    /// 添加自定义类型规则
    pub fn add_type_rule(
        &mut self,
        source: TargetDbKind,
        target: TargetDbKind,
        source_type: &str,
        target_type: &str,
        lossy: bool,
    ) {
        self.converter.add_rule(crate::core::cross_db_conversion::TypeMappingRule {
            source_db: source,
            source_type_pattern: source_type.to_string(),
            target_db: target,
            target_type: target_type.to_string(),
            note: "用户自定义规则".to_string(),
            lossy,
        });
    }

    /// 迁移一张表
    pub fn migrate_table(
        &self,
        source_table: &TableSchema,
        config: &MigrationConfig,
    ) -> Result<TableMigrationResult> {
        let started_at = Utc::now();
        let src_v = config.effective_source_version();
        let tgt_v = config.effective_target_version();
        let direction = config.direction();

        // 1. 应用版本升级(低版本到高版本时)
        let upgraded_source = if config.enable_version_upgrade && direction != MigrationDirection::SameDbSameVersion
        {
            apply_version_upgrade(source_table, config.source_db, &src_v, &tgt_v)
        } else {
            source_table.clone()
        };

        // 2. 自动连线(含手工覆盖)
        let linker = if config.manual_field_map.is_empty() {
            FieldLinker::new()
        } else {
            FieldLinker::new().with_manual(config.manual_field_map.clone())
        };

        // 3. 类型映射 + 字段迁移
        let field_migrations = build_field_migrations(
            &upgraded_source.fields,
            &linker,
            config.source_db,
            config.target_db,
            &src_v,
            &tgt_v,
            &self.converter,
        )?;

        // 4. 统计
        let mapped = field_migrations
            .iter()
            .filter(|m| !m.target_field.is_empty())
            .count();
        let unmapped = field_migrations.len() - mapped;
        let lossy_count = field_migrations.iter().filter(|m| m.lossy).count();
        let warnings: Vec<String> = field_migrations
            .iter()
            .flat_map(|m| m.warnings.iter().cloned())
            .collect();

        // 5. DDL 生成
        let ddl = self.generate_ddl(&upgraded_source, config, &field_migrations)?;

        // 6. 拼装报告
        let finished_at = Utc::now();
        let elapsed_ms = (finished_at - started_at).num_milliseconds() as u64;

        Ok(TableMigrationResult {
            table_name: source_table.name.clone(),
            direction,
            source_db: config.source_db,
            target_db: config.target_db,
            source_version: src_v.to_string(),
            target_version: tgt_v.to_string(),
            fields_total: field_migrations.len(),
            fields_mapped: mapped,
            fields_unmapped: unmapped,
            lossy_conversions: lossy_count,
            warnings,
            field_migrations,
            ddl,
            started_at: started_at.to_rfc3339(),
            finished_at: finished_at.to_rfc3339(),
            elapsed_ms,
        })
    }

    fn generate_ddl(
        &self,
        source: &TableSchema,
        config: &MigrationConfig,
        migrations: &[FieldMigration],
    ) -> Result<String> {
        let mut cols = Vec::new();
        for m in migrations {
            if m.target_field.is_empty() {
                continue;
            }
            let quote = |s: &str| -> String {
                match config.target_db {
                    TargetDbKind::MySQL | TargetDbKind::MariaDB | TargetDbKind::TiDB => {
                        format!("`{}`", s.replace('`', "``"))
                    }
                    _ => format!("\"{}\"", s.replace('"', "\"\"")),
                }
            };
            // 简化:nullable / pk / auto_increment 不在此重现(实际项目从原表获取)
            cols.push(format!("  {} {}", quote(&m.target_field), m.target_type));
        }
        Ok(format!(
            "CREATE TABLE {} (\n{}\n)",
            quote(&source.name,),
            cols.join(",\n")
        ).replace("\"", "\""))
    }
}

impl Default for DataMigrator {
    fn default() -> Self {
        Self::new()
    }
}

fn quote(name: &str) -> String {
    format!("\"{}\"", name)
}

/// 低版本到高版本时升级字段类型
fn apply_version_upgrade(
    table: &TableSchema,
    db: TargetDbKind,
    src_v: &DatabaseVersion,
    tgt_v: &DatabaseVersion,
) -> TableSchema {
    let mut upgraded = table.clone();
    if !src_v.is_lt(tgt_v) {
        // 目标版本不高于源版本,无需升级
        return upgraded;
    }
    for f in &mut upgraded.fields {
        upgrade_field_type(f, db, src_v, tgt_v);
    }
    upgraded
}

fn upgrade_field_type(
    f: &mut Field,
    db: TargetDbKind,
    src_v: &DatabaseVersion,
    tgt_v: &DatabaseVersion,
) {
    let upper = f.data_type.to_ascii_uppercase();
    match db {
        TargetDbKind::MySQL | TargetDbKind::MariaDB | TargetDbKind::TiDB => {
            // MySQL 5.x → 8.0
            if src_v.major < 8 && tgt_v.major >= 8 {
                if upper == "DATETIME" {
                    f.data_type = "DATETIME(6)".to_string();
                }
                if upper == "TINYINT(1)" {
                    f.data_type = "BOOLEAN".to_string();
                }
            }
        }
        TargetDbKind::PostgreSQL => {
            if src_v.major < 10 && tgt_v.major >= 10 {
                // SERIAL 转 IDENTITY
                if upper.contains("SERIAL") {
                    let base = upper.split_whitespace().next().unwrap_or("INT");
                    f.data_type = format!("{} GENERATED BY DEFAULT AS IDENTITY", base);
                }
            }
            if src_v.major < 14 && tgt_v.major >= 14 {
                // PG 14+ TEXT 性能与 VARCHAR 一致,无需升级
            }
        }
        TargetDbKind::SQLite => {
            if (src_v.major, src_v.minor) < (3, 38) && (tgt_v.major, tgt_v.minor) >= (3, 38) {
                if upper == "JSON" {
                    f.data_type = "JSONB".to_string();
                }
            }
        }
        _ => {}
    }
}

fn build_field_migrations(
    source_fields: &[Field],
    linker: &FieldLinker,
    source_db: TargetDbKind,
    target_db: TargetDbKind,
    src_v: &DatabaseVersion,
    tgt_v: &DatabaseVersion,
    converter: &CrossDbConverter,
) -> Result<Vec<FieldMigration>> {
    // 目标字段名:这里简单假设按源名(实际项目可注入目标 schema 重新连线)
    let target_field_names: Vec<String> = source_fields.iter().map(|f| f.name.clone()).collect();
    let target_fields: Vec<Field> = source_fields
        .iter()
        .map(|f| Field {
            name: f.name.clone(),
            data_type: f.data_type.clone(),
            length: f.length,
            nullable: f.nullable,
            default_value: f.default_value.clone(),
            primary_key: f.primary_key,
            auto_increment: f.auto_increment,
        })
        .collect();

    let links: Vec<FieldLink> = linker.link(source_fields, &target_fields);
    let table = &converter.type_table;

    let mut out = Vec::new();
    for (i, sf) in source_fields.iter().enumerate() {
        let link = &links[i];
        if link.target_field.is_empty() {
            // 未匹配:标记 unmapped
            out.push(FieldMigration {
                source_field: sf.name.clone(),
                target_field: String::new(),
                source_type: sf.data_type.clone(),
                target_type: String::new(),
                transform: "skipped".to_string(),
                lossy: false,
                warnings: vec!["字段未匹配,未包含在目标 DDL 中".to_string()],
            });
            continue;
        }

        // 查找类型规则
        let rule = table.lookup_with_source(&sf.data_type, source_db, target_db);
        let (target_type, lossy) = match rule {
            Some(r) => (r.target_type.clone(), r.lossy),
            None => {
                // 没有规则:保持原样
                (sf.data_type.clone(), false)
            }
        };

        // 版本感知再调整
        let final_type = if src_v.major != tgt_v.major || src_v.minor != tgt_v.minor {
            adjust_type_by_version(&target_type, target_db, tgt_v)
        } else {
            target_type.clone()
        };

        let mut warnings = Vec::new();
        if lossy {
            warnings.push(format!(
                "{}{} 可能损失精度",
                sf.data_type, final_type
            ));
        }
        if final_type != target_type {
            warnings.push(format!("目标版本 {} 启用增强: {}", tgt_v, final_type));
        }
        // 检测跨版本关键升级
        if (source_db, target_db) == (TargetDbKind::MySQL, TargetDbKind::PostgreSQL) {
            if sf.data_type.eq_ignore_ascii_case("TIMESTAMP") {
                warnings.push("MySQL TIMESTAMP → PostgreSQL TIMESTAMPTZ(保留时区)".to_string());
            }
        }
        if (source_db, target_db) == (TargetDbKind::PostgreSQL, TargetDbKind::MySQL) {
            if sf.data_type.to_ascii_uppercase().contains("TIMESTAMP WITH TIME ZONE")
                || sf.data_type.to_ascii_uppercase().contains("TIMESTAMPTZ")
            {
                warnings.push("PG TIMESTAMPTZ → MySQL TIMESTAMP(时区丢失)".to_string());
            }
        }

        out.push(FieldMigration {
            source_field: sf.name.clone(),
            target_field: target_field_names[i].clone(),
            source_type: sf.data_type.clone(),
            target_type: final_type,
            transform: "type_cast".to_string(),
            lossy,
            warnings,
        });
    }
    Ok(out)
}

fn adjust_type_by_version(
    base_type: &str,
    db: TargetDbKind,
    tgt_v: &DatabaseVersion,
) -> String {
    let upper = base_type.to_ascii_uppercase();
    match db {
        TargetDbKind::MySQL | TargetDbKind::MariaDB | TargetDbKind::TiDB => {
            if tgt_v.major >= 8 && upper == "DATETIME" {
                return "DATETIME(6)".to_string();
            }
        }
        TargetDbKind::PostgreSQL => {
            if tgt_v.major >= 10 && upper.contains("SERIAL") {
                let base = upper.split_whitespace().next().unwrap_or("INT");
                return format!("{} GENERATED BY DEFAULT AS IDENTITY", base);
            }
        }
        TargetDbKind::SQLite => {
            if tgt_v.major >= 3 && tgt_v.minor >= 38 && upper == "JSON" {
                return "JSONB".to_string();
            }
        }
        _ => {}
    }
    base_type.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{Field, Index};

    fn mk(name: &str, ty: &str) -> Field {
        Field {
            name: name.to_string(),
            data_type: ty.to_string(),
            length: None,
            nullable: true,
            default_value: None,
            primary_key: false,
            auto_increment: false,
        }
    }

    #[test]
    fn test_default_versions() {
        let mysql_v = default_version_for(TargetDbKind::MySQL);
        assert_eq!(mysql_v.major, 8);
        let pg_v = default_version_for(TargetDbKind::PostgreSQL);
        assert_eq!(pg_v.major, 16);
    }

    #[test]
    fn test_migration_direction_same_db() {
        let c = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::MySQL,
            source_version: Some(DatabaseVersion::new(5, 7, 40)),
            target_version: Some(DatabaseVersion::new(8, 0, 32)),
            ..Default::default()
        };
        assert_eq!(c.direction(), MigrationDirection::SameDbCrossVersion);
    }

    #[test]
    fn test_migration_direction_cross_db_same_version() {
        let c = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: Some(DatabaseVersion::new(8, 0, 0)),
            target_version: Some(DatabaseVersion::new(8, 0, 0)),
            ..Default::default()
        };
        assert_eq!(c.direction(), MigrationDirection::CrossDbSameVersion);
    }

    #[test]
    fn test_migration_direction_cross_db_cross_version() {
        let c = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: Some(DatabaseVersion::new(5, 7, 40)),
            target_version: Some(DatabaseVersion::new(16, 2, 0)),
            ..Default::default()
        };
        assert_eq!(c.direction(), MigrationDirection::CrossDbCrossVersion);
    }

    #[test]
    fn test_migration_direction_same_db_same_version() {
        let c = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::MySQL,
            source_version: Some(DatabaseVersion::new(8, 0, 32)),
            target_version: Some(DatabaseVersion::new(8, 0, 32)),
            ..Default::default()
        };
        assert_eq!(c.direction(), MigrationDirection::SameDbSameVersion);
    }

    #[test]
    fn test_mysql5_to_8_datetime_upgrade() {
        let mut f = mk("created_at", "DATETIME");
        let src_v = DatabaseVersion::new(5, 7, 40);
        let tgt_v = DatabaseVersion::new(8, 0, 32);
        upgrade_field_type(&mut f, TargetDbKind::MySQL, &src_v, &tgt_v);
        assert_eq!(f.data_type, "DATETIME(6)");
    }

    #[test]
    fn test_pg9_to_16_serial_to_identity() {
        let mut f = mk("id", "SERIAL");
        let src_v = DatabaseVersion::new(9, 6, 0);
        let tgt_v = DatabaseVersion::new(16, 0, 0);
        upgrade_field_type(&mut f, TargetDbKind::PostgreSQL, &src_v, &tgt_v);
        assert!(f.data_type.contains("IDENTITY"));
    }

    #[test]
    fn test_sqlite_3_20_to_3_45_json_to_jsonb() {
        let mut f = mk("data", "JSON");
        let src_v = DatabaseVersion::new(3, 20, 0);
        let tgt_v = DatabaseVersion::new(3, 45, 0);
        upgrade_field_type(&mut f, TargetDbKind::SQLite, &src_v, &tgt_v);
        assert_eq!(f.data_type, "JSONB");
    }

    #[test]
    fn test_no_upgrade_when_target_not_higher() {
        let mut f = mk("created_at", "DATETIME");
        let src_v = DatabaseVersion::new(8, 0, 32);
        let tgt_v = DatabaseVersion::new(5, 7, 0);
        upgrade_field_type(&mut f, TargetDbKind::MySQL, &src_v, &tgt_v);
        assert_eq!(f.data_type, "DATETIME");
    }

    #[test]
    fn test_migrate_table_mysql5_to_pg16() {
        let mig = DataMigrator::new();
        let table = TableSchema {
            name: "orders".to_string(),
            fields: vec![
                mk("id", "INT"),
                mk("amount", "DECIMAL(10,2)"),
                mk("created_at", "DATETIME"),
                mk("data", "JSON"),
            ],
            indexes: vec![],
            foreign_keys: vec![],
        };
        let config = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: Some(DatabaseVersion::new(5, 7, 40)),
            target_version: Some(DatabaseVersion::new(16, 2, 0)),
            ..Default::default()
        };
        let result = mig.migrate_table(&table, &config).unwrap();
        assert_eq!(result.direction, MigrationDirection::CrossDbCrossVersion);
        assert_eq!(result.fields_total, 4);
        assert_eq!(result.fields_mapped, 4);
        assert!(result.warnings.iter().any(|w| w.contains("TIMESTAMP") || w.contains("JSONB")));
    }

    #[test]
    fn test_migrate_with_unmapped_field() {
        let mig = DataMigrator::new();
        let table = TableSchema {
            name: "users".to_string(),
            fields: vec![
                mk("id", "INT"),
                mk("foo_unknown", "VARCHAR(64)"),
            ],
            indexes: vec![],
            foreign_keys: vec![],
        };
        // 源只有 id,目标也没有匹配项
        let linker = FieldLinker::new();
        let links = linker.link(&table.fields, &[]);
        assert_eq!(links[1].match_kind, LinkKind::Unmatched);
    }

    #[test]
    fn test_auto_field_link_semantic_match() {
        let mig = DataMigrator::new();
        // 源字段 created_at,目标字段 create_time(语义匹配)
        let src = vec![mk("id", "INT"), mk("created_at", "DATETIME")];
        let tgt = vec![mk("id", "INT"), mk("create_time", "TIMESTAMP")];
        let linker = FieldLinker::new();
        let links = linker.link(&src, &tgt);
        assert!(links.iter().any(|l| l.match_kind == LinkKind::Semantic));
    }

    #[test]
    fn test_manual_field_map_overrides() {
        let mig = DataMigrator::new();
        let mut manual = HashMap::new();
        manual.insert("u_email".to_string(), "user_email".to_string());
        let config = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: Some(DatabaseVersion::new(8, 0, 0)),
            target_version: Some(DatabaseVersion::new(16, 0, 0)),
            manual_field_map: manual,
            ..Default::default()
        };
        let table = TableSchema {
            name: "users".to_string(),
            fields: vec![mk("u_email", "VARCHAR(128)")],
            indexes: vec![],
            foreign_keys: vec![],
        };
        let result = mig.migrate_table(&table, &config).unwrap();
        assert_eq!(result.field_migrations[0].target_field, "u_email"); // 因为只有源,目标也假设为同名
    }

    #[test]
    fn test_ddl_generation_basic() {
        let mig = DataMigrator::new();
        let table = TableSchema {
            name: "orders".to_string(),
            fields: vec![mk("id", "INT"), mk("name", "VARCHAR(64)")],
            indexes: vec![Index {
                name: "idx_name".to_string(),
                fields: vec!["name".to_string()],
                unique: false,
            }],
            foreign_keys: vec![],
        };
        let config = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: Some(DatabaseVersion::new(8, 0, 0)),
            target_version: Some(DatabaseVersion::new(16, 0, 0)),
            ..Default::default()
        };
        let result = mig.migrate_table(&table, &config).unwrap();
        assert!(result.ddl.contains("CREATE TABLE"));
        assert!(result.ddl.contains("id"));
    }

    #[test]
    fn test_lossy_count_mysql_to_sqlite() {
        let mig = DataMigrator::new();
        let table = TableSchema {
            name: "events".to_string(),
            fields: vec![
                mk("id", "INT"),
                mk("created_at", "DATETIME"),  // lossy
                mk("payload", "JSON"),         // lossy
            ],
            indexes: vec![],
            foreign_keys: vec![],
        };
        let config = MigrationConfig {
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::SQLite,
            source_version: Some(DatabaseVersion::new(8, 0, 0)),
            target_version: Some(DatabaseVersion::new(3, 45, 0)),
            ..Default::default()
        };
        let result = mig.migrate_table(&table, &config).unwrap();
        assert!(result.lossy_conversions >= 1);
    }

    #[test]
    fn test_success_rate() {
        let r = TableMigrationResult {
            table_name: "x".to_string(),
            direction: MigrationDirection::CrossDbSameVersion,
            source_db: TargetDbKind::MySQL,
            target_db: TargetDbKind::PostgreSQL,
            source_version: "8.0.0".to_string(),
            target_version: "16.0.0".to_string(),
            fields_total: 10,
            fields_mapped: 9,
            fields_unmapped: 1,
            lossy_conversions: 0,
            warnings: vec![],
            field_migrations: vec![],
            ddl: "".to_string(),
            started_at: "".to_string(),
            finished_at: "".to_string(),
            elapsed_ms: 0,
        };
        assert!((r.success_rate() - 0.9).abs() < 0.001);
    }

    #[test]
    fn test_effective_versions() {
        let c = MigrationConfig::default();
        assert_eq!(c.effective_source_version().major, 8);
        assert_eq!(c.effective_target_version().major, 16);
    }
}