# UUID 清理方案

> Date: 2026-04-28 21:25
> Issue: Charade 1963 视频注册了 392 次

---

## 问题统计

| UUID | 类型 | 数量 | face_detections | birth_registration |
|------|------|------|-----------------|-------------------|
| **384b0ff44aaaa1f14cb2cd63b3fea966** | Legacy (16 chars) | 1 | ✅ 78 条 | ❌ NULL |
| **44ce835fa...** | Birth (32 chars) | 391 | ❌ 0 条 | ✅ 有 |

**总计**: 392 条重复记录

---

## 清理方案

### 方案 A: 保留 Legacy UUID + 添加 Birth Registration（推荐）

**优点**:
- 保留已有 face_detections 数据（78 条）
- 无需迁移数据
- 添加 birth_registration 即可

**步骤**:
1. 删除 391 个 Birth UUID videos
2. 为旧 UUID 添加 birth_registration
3. 清理重复记录

**SQL**:
```sql
-- 1. 删除重复的 Birth UUID videos
DELETE FROM dev.videos 
WHERE file_name = 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
  AND LENGTH(uuid) = 32;

-- 2. 为旧 UUID 添加 birth_registration
UPDATE dev.videos
SET birth_registration = jsonb_build_object(
    'registration_source', jsonb_build_object(
        'username', '',
        'timestamp', NOW(),
        'mac_address', 'ba:f5:ee:bc:45:78',
        'original_path', '/Users/accusys/momentry/var/sftpgo/data/demo',
        'original_filename', 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
    )
)
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966'
  AND birth_registration IS NULL;

-- 3. 验证结果
SELECT uuid, LENGTH(uuid), birth_registration IS NOT NULL as has_birth
FROM dev.videos
WHERE file_name LIKE '%Charade%';
```

**预期结果**:
- videos: 1 条（384b0ff44aaaa1f14cb2cd63b3fea966）
- face_detections: 78 条（保留）
- birth_registration: ✅ 已添加

---

### 方案 B: 保留 Birth UUID + 迁移 face_detections

**优点**:
- 使用新格式 UUID（32 chars）
- 符合 V4.0 规范

**缺点**:
- 需迁移 face_detections.file_uuid
- 需迁移其他关联表（chunks, pre_chunks, processor_results）

**步骤**:
1. 选择最新的 Birth UUID（例如 44ce835fa...）
2. 迁移 face_detections.file_uuid
3. 删除其他 Birth UUID
4. 删除旧 UUID

**SQL**:
```sql
-- 1. 选择保留的 Birth UUID
SELECT uuid FROM dev.videos 
WHERE file_name = 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
  AND LENGTH(uuid) = 32
ORDER BY created_at DESC LIMIT 1;
-- 结果: 7a9f660f1a87ea1ce474a2f2b648c013

-- 2. 迁移 face_detections
UPDATE dev.face_detections
SET file_uuid = '7a9f660f1a87ea1ce474a2f2b648c013'
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- 3. 迁移其他表
UPDATE dev.chunks
SET uuid = '7a9f660f1a87ea1ce474a2f2b648c013'
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

UPDATE dev.pre_chunks
SET file_uuid = '7a9f660f1a87ea1ce474a2f2b648c013'
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- 4. 删除旧 UUID video
DELETE FROM dev.videos WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- 5. 删除其他 Birth UUID
DELETE FROM dev.videos 
WHERE file_name = 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
  AND uuid != '7a9f660f1a87ea1ce474a2f2b648c013';
```

---

### 方案 C: 全部删除 + 重新注册

**优点**:
- 干净重新开始
- 使用正确的 Birth UUID 生成流程

**缺点**:
- 丢失所有 face_detections 数据
- 需重新运行 Face processor

**步骤**:
1. 删除所有 Charade videos（392 条）
2. 删除关联数据
3. 手动注册一次
4. 运行 Face processor

**SQL**:
```sql
-- 1. 删除所有 videos
DELETE FROM dev.videos 
WHERE file_name = 'Old_Time_Movie_Show_-_Charade_1963.HD.mov';

-- 2. 删除 face_detections（因为 file_uuid FK）
DELETE FROM dev.face_detections 
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- 3. 删除 chunks
DELETE FROM dev.chunks 
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- 4. 删除 pre_chunks
DELETE FROM dev.pre_chunks 
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';
```

**重新注册**:
```bash
# 停止 watcher（避免重复注册）
# 手动注册一次
curl -X POST http://localhost:3003/api/v1/register \
  -H "X-API-Key: muser_test_001" \
  -d '{"path": "/Users/accusys/momentry/var/sftpgo/data/demo/Old_Time_Movie_Show_-_Charade_1963.HD.mov"}'
```

---

## 推荐: 方案 A

**理由**:
1. 保留已有数据（78 face_detections）
2. 无破坏性更改
3. 添加 birth_registration 即可符合 V4.0

**影响范围**:
- 仅删除 391 个重复 videos（无数据）
- face_detections 不受影响
- chunks 不受影响

---

## 执行前检查

```bash
# 1. 检查 face_detections FK
psql -U accusys -d momentry -c "
SELECT COUNT(*) FROM dev.face_detections 
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';
"

# 2. 检查 chunks
psql -U accusys -d momentry -c "
SELECT COUNT(*) FROM dev.chunks 
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';
"

# 3. 检查 pre_chunks
psql -U accusys -d momentry -c "
SELECT COUNT(*) FROM dev.pre_chunks 
WHERE file_uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';
"
```

---

## 执行命令

### 方案 A 执行（推荐）

```bash
psql -U accusys -d momentry <<'EOF'
-- Step 1: 删除重复 Birth UUID
DELETE FROM dev.videos 
WHERE file_name = 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
  AND LENGTH(uuid) = 32;

-- Step 2: 添加 birth_registration
UPDATE dev.videos
SET birth_registration = jsonb_build_object(
    'registration_source', jsonb_build_object(
        'username', '',
        'timestamp', NOW(),
        'mac_address', 'ba:f5:ee:bc:45:78',
        'original_path', '/Users/accusys/momentry/var/sftpgo/data/demo',
        'original_filename', 'Old_Time_Movie_Show_-_Charade_1963.HD.mov'
    )
)
WHERE uuid = '384b0ff44aaaa1f14cb2cd63b3fea966';

-- Step 3: 验证
SELECT uuid, LENGTH(uuid), birth_registration IS NOT NULL 
FROM dev.videos 
WHERE file_name LIKE '%Charade%';
EOF
```

---

## 后续建议

1. **修复 watcher**：避免重复注册同一文件
2. **添加去重逻辑**：检查文件是否已注册
3. **使用稳定的 Birth UUID**：基于 filename hash（不含 timestamp）

---

## 验证清单

- [ ] 删除 391 个重复 videos
- [ ] 保留 1 个 UUID（384b0ff44aaaa1f14cb2cd63b3fea966）
- [ ] 添加 birth_registration
- [ ] face_detections 数据完整（78 条）
- [ ] chunks 数据完整（3664 条）
- [ ] pre_chunks 数据完整（3664 + 1332 条）

---

## 相关文档

- `docs_v1.0/UUID_LENGTH_ISSUE.md` - UUID 长度问题分析
- `docs_v1.0/PORTAL_FACE_DEMO_PLAN.md` - Portal Face 演示计划
