feat: FaceFilterSettings 透明文件管理(自动迁移)

核心特性:
- 读取优先级: 文件 > 数据库 > 默认值
- 自动迁移: 数据库数据自动迁移到文件
- 向后兼容: 旧数据透明处理
- 用户无感知: 不需要手动迁移脚本

工作原理:
1. GET /file-profile
   - 优先从文件读取
   - 文件不存在时从数据库读取
   - 自动创建文件(透明迁移)

2. PUT /file-profile
   - 直接写入文件
   - 数据库不再存储新数据

优势:
- ✅ 零配置迁移(第一次访问自动迁移)
- ✅ 架构简化(文件即 primary source)
- ✅ 跨设备同步(只需同步文件)
- ✅ 调试方便(直接编辑 JSON)

删除:
- ❌ migrate_face_filter_to_files.py(不需要)

文档更新:
- docs_v1.0/DESIGN/FACE_FILTER_FILE_STORAGE.md
- 说明透明管理机制
- 添加自动迁移流程图

Git: 3658c288 (enhanced)
This commit is contained in:
Accusys
2026-10-09 16:38:20 +08:00
parent 3658c2888e
commit 1379cf59cf
2 changed files with 324 additions and 3 deletions
@@ -0,0 +1,293 @@
# FaceFilterSettings 文件存储架构
**日期**: 2026-10-09
**版本**: 2.0
**状态**: 已实施(透明管理)
---
## 架构原则
### ✅ 正确的设计
| 数据类型 | 存储位置 | 原因 |
|---------|---------|------|
| **配置参数** | `.json` 文件 | 便于跨设备同步、版本控制、手动编辑 |
| **搜索数据** | PostgreSQL + Qdrant | 快速检索、向量搜索、全文搜索 |
### 透明管理机制
**不需要手动迁移脚本,系统自动管理:**
1. **读取优先级**: 文件 > 数据库 > 默认值
2. **自动迁移**: 数据库数据自动迁移到文件
3. **向后兼容**: 旧数据透明处理
4. **用户无感知**: API 层面完全透明
---
## 实施细节
### 1. GET /api/v1/file-profile(透明读取)
```rust
// 1. 从文件读取(优先)
let disk_profile = FileProfile::load_from_disk(output_dir, &file_uuid);
// 2. 优先级: 文件 > 数据库 > 默认值
let face_filter = disk_profile
.and_then(|p| p.face_filter)
.or_else(|| db_face_filter) // 向后兼容
.unwrap_or_else(FaceFilterSettings::defaults);
// 3. 自动迁移:如果数据在数据库但文件不存在
if disk_profile.is_none() && db_face_filter.is_some() {
// 自动创建文件
let mut new_profile = create_profile(...);
new_profile.face_filter = Some(face_filter);
new_profile.save_to_disk(output_dir); // 透明迁移
}
```
**效果**:
- ✅ 第一次访问时自动迁移
- ✅ 用户完全无感知
- ✅ 不需要任何迁移脚本
- ✅ 向后兼容所有旧数据
### 2. PUT /api/v1/file-profile(直接写入文件)
```rust
// 直接更新文件(primary source)
let mut profile = load_or_create_profile(file_uuid);
profile.face_filter = Some(ff);
profile.save_to_disk(output_dir);
// 数据库不再存储 face_filter
```
---
## 透明管理工作流程
### 第一次访问(自动迁移)
```
用户请求 GET /file-profile?file_uuid=xxx
↓
尝试从文件读取
↓ (文件不存在)
尝试从数据库读取 ✅ 找到
↓
返回数据给用户
↓
后台自动创建文件 📝
↓
下次访问直接从文件读取 ✅
```
### 后续访问(直接读文件)
```
用户请求 GET /file-profile?file_uuid=xxx
↓
从文件读取 ✅
↓
返回数据给用户
```
### 保存配置(直接写文件)
```
用户请求 PUT /file-profile
↓
直接写入文件 ✅
↓
返回成功
```
---
## 优势
### 1. 零配置迁移
- ❌ 不需要迁移脚本
- ❌ 不需要手动操作
- ✅ 第一次访问自动迁移
- ✅ 用户完全无感知
### 2. 架构更简单
- ❌ 不需要数据库迁移脚本
- ❌ 不需要维护 DB 表结构
- ✅ 文件即 primary source of truth
- ✅ 便于版本控制
### 3. 向后兼容
- ✅ 旧数据自动处理
- ✅ 新旧 API 同时支持
- ✅ 不影响现有功能
### 4. 跨设备同步
```bash
# 只需同步文件即可
rsync -av ~/momentry/output/ server:~/momentry/output/
rsync -av ~/momentry/identities/ server:~/momentry/identities/
```
---
## 数据库角色
### 数据库存储
- ✅ 文件元数据(路径、名称、状态)
- ✅ 处理状态(ASR、OCR、Face 等)
- ✅ 搜索索引(FTS、向量)
- ⚠️ face_filter(仅作为向后兼容备份)
### 数据库不存储
- ❌ 配置参数(FaceFilterSettings)- 新数据不写入数据库
- ❌ 用户偏好(UI 设置)
- ❌ 业务逻辑参数(Trace 设置)
---
## 示例场景
### 场景 1: 现有数据自动迁移
```
1. 数据库中有 100 个文件的 face_filter
2. 用户访问第一个文件
- API 检测到数据库有数据,但文件不存在
- 自动创建 .profile.json
- 返回数据给用户
3. 用户继续访问其他文件
- 同样自动创建文件
4. 最终所有数据都迁移到文件
- 用户完全无感知
- 不需要任何手动操作
```
### 场景 2: 新数据直接写入文件
```
1. 用户修改 face_filter
2. API 直接写入 .profile.json
3. 数据库不更新(不需要)
4. 下次读取直接从文件读取
```
### 场景 3: 跨设备同步
```
设备 A:
- 修改 face_filter
- 写入 .profile.json
同步:
- rsync 文件到设备 B
设备 B:
- 读取 face_filter
- 直接从文件读取 ✅
- 无需同步数据库
```
---
## API 兼容性
### 向后兼容
- ✅ 旧的 API 请求格式仍然支持
- ✅ 数据自动从数据库迁移到文件
- ✅ 不影响现有功能
- ✅ 用户完全无感知
### 版本演进
| 版本 | face_filter 存储 | 迁移方式 | 用户感知 |
|------|----------------|---------|---------|
| v1.0.20261008 | 数据库 | - | - |
| v1.0.20261009+ | 文件 | 透明自动 | 无感知 |
---
## 测试计划
### 功能测试
1. **GET file-profile(透明读取)**
- [ ] 从文件读取成功
- [ ] 文件不存在时从数据库读取
- [ ] 自动创建文件成功
- [ ] 缺失文件时使用默认值
2. **PUT file-profile(直接写入)**
- [ ] 写入 `.profile.json` 成功
- [ ] 创建新文件成功
- [ ] 更新现有文件成功
3. **自动迁移**
- [ ] 第一次访问自动迁移
- [ ] 数据完整无损
- [ ] 不影响用户体验
### 集成测试
1. **前端集成**
- [ ] Studio 正常加载配置
- [ ] 保存配置成功
- [ ] UI 正确显示
- [ ] 无需修改前端代码
2. **跨设备同步**
- [ ] 文件同步后配置一致
- [ ] 无需同步数据库
- [ ] 新设备直接可用
---
## 后续工作
### Phase 1(已完成)
- ✅ 修改 API 实现透明管理
- ✅ 自动迁移机制
- ✅ 向后兼容
- ✅ 提交代码
### Phase 2(待部署)
- ⏳ 部署新版本
- ⏳ 验证自动迁移
- ⏳ 功能测试
### Phase 3(计划中)
- [ ] 逐步废弃数据库 `face_filter` 字段
- [ ] 更新文档
- [ ] 性能优化
---
## 相关文档
- `/Users/accusys/momentry_core/.opencode/plans/persistence_design/` - 设计文档
- `/Users/accusys/momentry_studio/docs/FACE_FILTER_SETTINGS_MIGRATION_GUIDE.md` - 迁移指南
- `/Users/accusys/momentry_studio/docs/FACE_FILTER_TYPES_FIX_REQUIRED.md` - TypeScript 修正
---
**Git Commit**:
- Rust: `86c776ce` - FaceFilterSettings 数据结构
- Rust: `3658c288` - 文件存储架构(透明管理)
**最后更新**: 2026-10-09
**关键特性**: 透明管理、自动迁移、用户无感知
+31 -3
View File
@@ -1005,9 +1005,10 @@ pub async fn get_file_profile_handler(
i32,
f64,
i64,
Option<serde_json::Value>, // face_filter (for backward compatibility)
)> = sqlx::query_as(&format!(
"SELECT file_uuid, file_name, file_path, status, duration, width, height, fps, \
COALESCE(total_frames, 0) FROM {} WHERE file_uuid = $1",
COALESCE(total_frames, 0), face_filter FROM {} WHERE file_uuid = $1",
videos_table
))
.bind(&params.file_uuid)
@@ -1028,19 +1029,46 @@ pub async fn get_file_profile_handler(
height,
fps,
total_frames,
db_face_filter,
) = row.ok_or(StatusCode::NOT_FOUND)?;
// Load face_filter from .profile.json (primary source)
// Try to load face_filter from .profile.json (primary source)
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
let disk_profile =
crate::core::file_profile::FileProfile::load_from_disk(output_dir, &file_uuid);
// Get face_filter from file, not database
// Priority: File > Database > Default
let face_filter = disk_profile
.as_ref()
.and_then(|p| p.face_filter.clone())
.or_else(|| {
// Fallback to database (backward compatibility)
db_face_filter
.and_then(|v| serde_json::from_value::<FaceFilterSettings>(v).ok())
})
.unwrap_or_else(FaceFilterSettings::defaults);
// Auto-migration: If face_filter exists in DB but not in file, write to file
if disk_profile.is_none() && db_face_filter.is_some() {
tracing::info!("[FileProfile] Auto-migrating face_filter to file for {}", file_uuid);
let mut new_profile = crate::core::file_profile::FileProfile::from_db_record(
&file_uuid,
&file_name,
&file_path,
"video",
"",
duration,
width,
height,
fps,
total_frames,
);
new_profile.face_filter = Some(face_filter.clone());
if let Err(e) = new_profile.save_to_disk(output_dir) {
tracing::warn!("[FileProfile] Auto-migration failed: {}", e);
}
}
// Fallback for zombie files: if DB fields are empty, try to recover from profile
let effective_name = if file_name.is_empty() {
disk_profile