zhaohan d55bd3c728 提交全部代码 1 mēnesi atpakaļ
..
src ba37c17547 将所有package统一为emoon,移除多余功能 5 mēneši atpakaļ
README.md ba37c17547 将所有package统一为emoon,移除多余功能 5 mēneši atpakaļ
pom.xml d55bd3c728 提交全部代码 1 mēnesi atpakaļ

README.md

数据库迁移模块

功能说明

emoon-migration 模块提供数据库表结构和数据的迁移功能,支持将源数据库的指定表迁移到目标数据库。

API 接口

1. 执行数据库迁移

接口地址: POST /migration/migrate

请求参数:

{
  "src": {
    "url": "jdbc:mysql://localhost:3306/source_db",
    "username": "src_user",
    "password": "src_password"
  },
  "tgt": {
    "url": "jdbc:mysql://localhost:3306/target_db",
    "username": "tgt_user",
    "password": "tgt_password"
  },
  "tables": ["user", "order", "product"],
  "batchSize": 1000
}

参数说明:

  • src: 源数据库配置
  • tgt: 目标数据库配置
  • tables: 需要迁移的表名列表,为空表示迁移全部表
  • batchSize: 批量处理大小,默认1000

响应示例:

{
  "code": 200,
  "msg": "数据库迁移成功",
  "data": {
    "success": true,
    "message": "成功迁移 3 个表,共 12500 条记录",
    "migratedTables": ["user", "order", "product"],
    "totalRecords": 12500,
    "duration": 3560,
    "tableInfos": [
      {
        "tableName": "user",
        "recordCount": 5000,
        "success": true,
        "duration": 1200
      },
      {
        "tableName": "order",
        "recordCount": 7500,
        "success": true,
        "duration": 1800
      },
      {
        "tableName": "product",
        "recordCount": 0,
        "success": true,
        "duration": 560
      }
    ]
  }
}

2. 验证数据库连接

接口地址: POST /migration/validate-connection

请求参数:

{
  "url": "jdbc:mysql://localhost:3306/database",
  "username": "user",
  "password": "password"
}

3. 获取服务状态

接口地址: GET /migration/status

使用示例

cURL 示例

# 执行数据库迁移
curl -X POST http://localhost:8080/migration/migrate \
  -H "Content-Type: application/json" \
  -d '{
    "src": {
      "url": "jdbc:mysql://localhost:3306/source_db",
      "username": "root",
      "password": "password"
    },
    "tgt": {
      "url": "jdbc:mysql://localhost:3306/target_db",
      "username": "root",
      "password": "password"
    },
    "tables": ["user", "role"],
    "batchSize": 500
  }'

# 验证数据库连接
curl -X POST http://localhost:8080/migration/validate-connection \
  -H "Content-Type: application/json" \
  -d '{
    "url": "jdbc:mysql://localhost:3306/test_db",
    "username": "root",
    "password": "password"
  }'

Java 代码示例

@RestController
public class TestController {

    @Autowired
    private DatabaseMigrationService migrationService;

    public void testMigration() {
        MigrationRequest request = new MigrationRequest();

        // 配置源数据库
        DatabaseConfig src = new DatabaseConfig()
            .setUrl("jdbc:mysql://localhost:3306/source_db")
            .setUsername("root")
            .setPassword("password");

        // 配置目标数据库
        DatabaseConfig tgt = new DatabaseConfig()
            .setUrl("jdbc:mysql://localhost:3306/target_db")
            .setUsername("root")
            .setPassword("password");

        request.setSrc(src);
        request.setTgt(tgt);
        request.setTables(Arrays.asList("user", "role"));
        request.setBatchSize(1000);

        // 执行迁移
        MigrationResult result = migrationService.migrateTables(request);

        if (result.getSuccess()) {
            System.out.println("迁移成功: " + result.getMessage());
        } else {
            System.out.println("迁移失败: " + result.getMessage());
        }
    }
}

注意事项

  1. 数据库驱动支持:模块默认包含 MySQL 和 PostgreSQL 驱动,如需支持其他数据库,请添加相应的驱动依赖。

  2. 数据安全

    • 建议在生产环境中使用数据库连接池
    • 敏感信息(密码)建议使用加密传输
    • 迁移前请备份目标数据库
  3. 性能优化

    • 可以通过调整 batchSize 参数优化迁移性能
    • 大数据量迁移建议分批进行
    • 迁移过程中避免对源数据库进行写操作
  4. 表结构兼容性

    • 目前支持基本数据类型的自动迁移
    • 复杂的存储过程、触发器等对象需要手动迁移
    • 不同数据库间的数据类型差异需要特别注意
  5. 事务处理

    • 每个表的迁移是独立的事务
    • 单个表迁移失败不会影响其他表的迁移
    • 建议在迁移前进行数据验证