Files
rust-imager/src/routes/migration.rs
lychang 093924ea99
Some checks failed
Rust CI / Build Release (push) Has been cancelled
Rust CI / Cargo Check (push) Has been cancelled
update
2026-07-19 16:34:04 +08:00

48 lines
1.9 KiB
Rust

use crate::db::Database;
#[allow(unused)] use crate::db::MigrationStats;
use actix_web::{HttpResponse, Responder, web};
use std::collections::HashMap;
use std::sync::Arc;
/// GET /api/migration/stats — 查看迁移状态
pub async fn migration_stats(db: web::Data<Arc<Database>>) -> impl Responder {
let persons = db.get_all_persons_with_counts().await.len();
let aliases = db.get_all_aliases().await.len();
let albums = db.get_all_albums().await.unwrap_or_default().len();
HttpResponse::Ok().json(serde_json::json!({
"persons": persons,
"aliases": aliases,
"albums": albums,
"message": if persons == 0 { "数据库为空,建议执行迁移" } else { "数据已就绪" }
}))
}
/// POST /api/migration/run — 执行从老数据库迁移
pub async fn migration_run(db: web::Data<Arc<Database>>, query: web::Query<HashMap<String, String>>) -> impl Responder {
let old_db_path = match query.get("path") {
Some(p) if !p.is_empty() => p.clone(),
_ => return HttpResponse::BadRequest().json(serde_json::json!({
"error": "请提供老数据库路径: ?path=persons.db"
})),
};
match db.migrate_from_old_db(&old_db_path).await {
Ok(stats) => HttpResponse::Ok().json(serde_json::json!({
"success": true,
"summary": db.migration_summary(&stats),
"stats": {
"persons_imported": stats.persons_imported,
"persons_skipped": stats.persons_skipped,
"aliases_imported": stats.aliases_imported,
"albums_updated": stats.albums_updated,
"albums_missing": stats.albums_missing,
"tags_imported": stats.tags_imported,
"person_albums_created": stats.person_albums_created,
}
})),
Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({
"error": format!("迁移失败: {}", e)
})),
}
}