|
|
1 mēnesi atpakaļ | |
|---|---|---|
| .. | ||
| src | 5 mēneši atpakaļ | |
| README.md | 5 mēneši atpakaļ | |
| pom.xml | 1 mēnesi atpakaļ | |
emoon-migration 模块提供数据库表结构和数据的迁移功能,支持将源数据库的指定表迁移到目标数据库。
接口地址: 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
}
]
}
}
接口地址: POST /migration/validate-connection
请求参数:
{
"url": "jdbc:mysql://localhost:3306/database",
"username": "user",
"password": "password"
}
接口地址: GET /migration/status
# 执行数据库迁移
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"
}'
@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());
}
}
}
数据库驱动支持:模块默认包含 MySQL 和 PostgreSQL 驱动,如需支持其他数据库,请添加相应的驱动依赖。
数据安全:
性能优化:
batchSize 参数优化迁移性能表结构兼容性:
事务处理: