docs: B-4 execution plan - hybrid strategy
**發現問題**: - Migration 使用 chunks(複數),代碼使用 chunk(單數) - Migration 011-016 創建並重命名 talents → identities - 但 identities 已在 hardcoded DDL 中存在 **策略**: 混合策略 - 階段 1: 修正 migration(讓 41/41 成功) - 階段 2: 移除 hardcoded DDL(長期優化) **工作量**: 7.5-10.5 小時 **需要確認**: 是否開始執行?
This commit is contained in:
@@ -0,0 +1,150 @@
|
|||||||
|
# B-4 方案 A 執行計畫
|
||||||
|
|
||||||
|
**日期**: 2026-10-04
|
||||||
|
**策略**: 混合策略(先讓 migration 成功,再優化架構)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 發現的問題
|
||||||
|
|
||||||
|
### 1. 表名不一致
|
||||||
|
|
||||||
|
| 問題 | Migration | 代碼實際使用 | 修正方向 |
|
||||||
|
|------|-----------|-------------|---------|
|
||||||
|
| chunks vs chunk | chunks (複數) | chunk (單數) | Migration → chunk |
|
||||||
|
| talents | 創建 talents → 重命名為 identities | 直接使用 identities | 跳過 011-016 |
|
||||||
|
|
||||||
|
### 2. Migration 失敗原因
|
||||||
|
|
||||||
|
**008_person_identity_binding.sql**:
|
||||||
|
- `ALTER TABLE chunks` → 應該是 `ALTER TABLE chunk`
|
||||||
|
- `CREATE TABLE talents` → identities 已存在,會失敗
|
||||||
|
|
||||||
|
**010_add_chunk_visual_stats.sql**:
|
||||||
|
- `ALTER TABLE public.chunks` → 應該是 `ALTER TABLE public.chunk`
|
||||||
|
|
||||||
|
**011-016**:
|
||||||
|
- 嘗試創建和重命名 talents → identities
|
||||||
|
- 但 identities 已經在 hardcoded DDL 中存在
|
||||||
|
- 全部會失敗
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 執行計畫
|
||||||
|
|
||||||
|
### 階段 1: 修正 Migration 文件(讓 41/41 成功)
|
||||||
|
|
||||||
|
#### 1.1 修改 008_person_identity_binding.sql
|
||||||
|
|
||||||
|
**修改內容**:
|
||||||
|
```diff
|
||||||
|
- ALTER TABLE chunks
|
||||||
|
+ ALTER TABLE chunk
|
||||||
|
|
||||||
|
- CREATE TABLE IF NOT EXISTS talents
|
||||||
|
+ -- Skip: identities already exists in hardcoded DDL
|
||||||
|
```
|
||||||
|
|
||||||
|
**風險**: 中 - 需要確保沒有其他依賴
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 1.2 修改 010_add_chunk_visual_stats.sql
|
||||||
|
|
||||||
|
**修改內容**:
|
||||||
|
```diff
|
||||||
|
- ALTER TABLE public.chunks ADD COLUMN
|
||||||
|
+ ALTER TABLE public.chunk ADD COLUMN
|
||||||
|
|
||||||
|
- ALTER TABLE dev.chunks ADD COLUMN
|
||||||
|
+ ALTER TABLE dev.chunk ADD COLUMN
|
||||||
|
```
|
||||||
|
|
||||||
|
**風險**: 低 - 簡單的表名替換
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 1.3 處理 011-016 (talents 相關)
|
||||||
|
|
||||||
|
**選項 A**: 跳過執行(推薦)
|
||||||
|
- 這些 migration 的目的是創建 talents 並重命名為 identities
|
||||||
|
- identities 已經在 hardcoded DDL 中創建
|
||||||
|
- 不需要執行
|
||||||
|
|
||||||
|
**選項 B**: 標記為已執行
|
||||||
|
- 在 schema_migrations 中插入記錄
|
||||||
|
- 不實際執行
|
||||||
|
|
||||||
|
**風險**: 中 - 需要確保 identities 的欄位與後續 migration 一致
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### 1.4 其他需要修正的 migration
|
||||||
|
|
||||||
|
**待檢查**:
|
||||||
|
- 025_rename_video_uuid_to_file_uuid.sql
|
||||||
|
- 其他引用 chunks/talents 的 migration
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 階段 2: 移除 Hardcoded DDL(長期優化)
|
||||||
|
|
||||||
|
**步驟**:
|
||||||
|
1. 確認所有 migration 成功(41/41)
|
||||||
|
2. 提取 hardcoded DDL 中所有表定義
|
||||||
|
3. 補充到 002_ 或其他 migration
|
||||||
|
4. 註解掉 postgres_db.rs:1076-1640
|
||||||
|
5. 測試驗證
|
||||||
|
|
||||||
|
**時間**: 4-6 小時
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 預期結果
|
||||||
|
|
||||||
|
### 階段 1 完成
|
||||||
|
|
||||||
|
- ✅ Migration 成功率: 41/41 (100%)
|
||||||
|
- ✅ 所有表正確建立
|
||||||
|
- ✅ videos.face_settings 存在
|
||||||
|
- ✅ Worker 可正常啟動
|
||||||
|
|
||||||
|
### 階段 2 完成
|
||||||
|
|
||||||
|
- ✅ 單一 schema 來源
|
||||||
|
- ✅ Migration 歷史完整
|
||||||
|
- ✅ 未來維護容易
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 工作量估算
|
||||||
|
|
||||||
|
| 階段 | 工作項目 | 時間 |
|
||||||
|
|------|---------|------|
|
||||||
|
| 1.1 | 修改 008 | 30 分鐘 |
|
||||||
|
| 1.2 | 修改 010 | 15 分鐘 |
|
||||||
|
| 1.3 | 處理 011-016 | 1 小時 |
|
||||||
|
| 1.4 | 其他修正 | 1-2 小時 |
|
||||||
|
| 1.5 | 測試驗證 | 1 小時 |
|
||||||
|
| **小計** | **階段 1** | **3.5-4.5 小時** |
|
||||||
|
| 2 | 移除 hardcoded DDL | 4-6 小時 |
|
||||||
|
| **總計** | | **7.5-10.5 小時** |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 需要確認
|
||||||
|
|
||||||
|
1. **是否執行階段 1**?
|
||||||
|
- 修改 migration 文件
|
||||||
|
- 讓 41/41 成功
|
||||||
|
|
||||||
|
2. **如何處理 011-016**?
|
||||||
|
- 選項 A: 跳過執行
|
||||||
|
- 選項 B: 標記為已執行
|
||||||
|
|
||||||
|
3. **是否立即執行階段 2**?
|
||||||
|
- 還是等階段 1 驗證成功後再執行?
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**請 M4 確認是否開始執行**
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
# B-4 Schema 差異分析報告
|
||||||
|
|
||||||
|
**日期**: 2026-10-04
|
||||||
|
**目的**: 識別 Hardcoded DDL vs Migration 的差異
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Hardcoded DDL 建立的表(完整清單)
|
||||||
|
|
||||||
|
**來源**: `src/core/db/postgres_db.rs:1076-1640` (570 行)
|
||||||
|
|
||||||
|
### 核心表
|
||||||
|
|
||||||
|
1. videos
|
||||||
|
2. chunk (單數)
|
||||||
|
3. chunk_vectors
|
||||||
|
4. parent_chunks
|
||||||
|
5. pre_chunks
|
||||||
|
6. cuts
|
||||||
|
7. frames
|
||||||
|
8. scenes
|
||||||
|
|
||||||
|
### 處理相關表
|
||||||
|
|
||||||
|
9. monitor_jobs
|
||||||
|
10. command_queue
|
||||||
|
11. processor_results
|
||||||
|
12. jobs
|
||||||
|
13. billing_periods
|
||||||
|
|
||||||
|
### 人臉相關表
|
||||||
|
|
||||||
|
14. face_detections
|
||||||
|
15. face_clusters
|
||||||
|
16. face_identities
|
||||||
|
17. face_recognition_results
|
||||||
|
18. identities
|
||||||
|
19. identity_bindings
|
||||||
|
20. file_identities
|
||||||
|
|
||||||
|
### TKG 表
|
||||||
|
|
||||||
|
21. tkg_nodes
|
||||||
|
22. tkg_edges
|
||||||
|
|
||||||
|
### 用戶認證表
|
||||||
|
|
||||||
|
23. users
|
||||||
|
24. sessions
|
||||||
|
25. api_keys
|
||||||
|
26. api_key_audit_log
|
||||||
|
27. api_key_anomalies
|
||||||
|
28. jwt_blacklist
|
||||||
|
29. n8n_api_keys
|
||||||
|
30. gitea_tokens
|
||||||
|
|
||||||
|
### 其他表
|
||||||
|
|
||||||
|
31. characters
|
||||||
|
32. castings
|
||||||
|
33. mac_allocations
|
||||||
|
34. chunks_rule1
|
||||||
|
35. processor_versions
|
||||||
|
36. identity_history
|
||||||
|
37. trace_profiles
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Migration 期望的表
|
||||||
|
|
||||||
|
### 關鍵差異
|
||||||
|
|
||||||
|
| Migration 需要 | Hardcoded 建立 | 問題 |
|
||||||
|
|---------------|---------------|------|
|
||||||
|
| **chunks** (複數) | **chunk** (單數) | ❌ 名稱不匹配 |
|
||||||
|
| **talents** | 不存在 | ❌ 缺失 |
|
||||||
|
| **person_identities** | 不存在 | ❌ 缺失 |
|
||||||
|
| **person_appearances** | 不存在 | ❌ 缺失 |
|
||||||
|
|
||||||
|
### 欄位差異
|
||||||
|
|
||||||
|
| 表 | Migration | Hardcoded | 問題 |
|
||||||
|
|----|-----------|-----------|------|
|
||||||
|
| processor_results | video_id | video_uuid | ❌ 欄位名不符 |
|
||||||
|
| videos | face_settings | 不存在 | ❌ 缺失 |
|
||||||
|
| videos | asr_status | 不存在 | ❌ 缺失 |
|
||||||
|
| videos | completed_at | 不存在 | ❌ 缺失 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 002_public_schema_pipeline_tables.sql 建立的表
|
||||||
|
|
||||||
|
### 當前內容(12 個表)
|
||||||
|
|
||||||
|
1. videos
|
||||||
|
2. chunk (單數)
|
||||||
|
3. chunk_vectors
|
||||||
|
4. cuts
|
||||||
|
5. frames
|
||||||
|
6. monitor_jobs
|
||||||
|
7. command_queue
|
||||||
|
8. processor_results
|
||||||
|
9. processor_versions
|
||||||
|
10. parent_chunks
|
||||||
|
11. tkg_nodes
|
||||||
|
12. tkg_edges
|
||||||
|
|
||||||
|
### 需要補充的表
|
||||||
|
|
||||||
|
**基礎表**:
|
||||||
|
- ❌ chunks (複數版)
|
||||||
|
- ❌ talents
|
||||||
|
- ❌ identities
|
||||||
|
- ❌ pre_chunks
|
||||||
|
- ❌ scenes
|
||||||
|
|
||||||
|
**人臉相關**:
|
||||||
|
- ❌ face_detections
|
||||||
|
- ❌ face_clusters
|
||||||
|
- ❌ face_identities
|
||||||
|
- ❌ face_recognition_results
|
||||||
|
- ❌ identity_bindings
|
||||||
|
- ❌ file_identities
|
||||||
|
|
||||||
|
**用戶認證**:
|
||||||
|
- ❌ users
|
||||||
|
- ❌ sessions
|
||||||
|
- ❌ api_keys
|
||||||
|
- ❌ api_key_audit_log
|
||||||
|
- ❌ api_key_anomalies
|
||||||
|
|
||||||
|
**其他**:
|
||||||
|
- ❌ characters
|
||||||
|
- ❌ castings
|
||||||
|
- ❌ mac_allocations
|
||||||
|
- ❌ etc.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 修復策略
|
||||||
|
|
||||||
|
### 策略 1: 補齊 002_ (推薦)
|
||||||
|
|
||||||
|
**優點**:
|
||||||
|
- 單一 migration 建立所有基礎表
|
||||||
|
- 清晰的執行順序
|
||||||
|
- 易於維護
|
||||||
|
|
||||||
|
**缺點**:
|
||||||
|
- 002_ 會變得很大
|
||||||
|
- 需要從 hardcoded DDL 提取所有表定義
|
||||||
|
|
||||||
|
### 策略 2: 分散到多個 migration
|
||||||
|
|
||||||
|
**優點**:
|
||||||
|
- 模組化
|
||||||
|
- 每個 migration 專注特定功能
|
||||||
|
|
||||||
|
**缺點**:
|
||||||
|
- 需要調整 migration 順序
|
||||||
|
- 更複雜
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 下一步
|
||||||
|
|
||||||
|
### 立即行動
|
||||||
|
|
||||||
|
1. **補齊 002_**:
|
||||||
|
- 從 hardcoded DDL 提取所有表定義
|
||||||
|
- 新增缺失的表(chunks, talents, identities 等)
|
||||||
|
- 確保表名與 migration 一致
|
||||||
|
|
||||||
|
2. **修正欄位名**:
|
||||||
|
- processor_results.video_id vs video_uuid
|
||||||
|
- 確認正確的欄位名
|
||||||
|
|
||||||
|
3. **測試驗證**:
|
||||||
|
- 註解掉 hardcoded DDL
|
||||||
|
- 執行 migration
|
||||||
|
- 驗證所有表建立成功
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**預估工作量**: 6-8 小時
|
||||||
Reference in New Issue
Block a user