fix(migration): deep fix for B-4 multi-statement and path issues
**問題** (M4 報告): - 1. 多陳述句無法執行 (sqlx::query 限制) - 2. 相對路徑 migrations/ (依賴 CWD) - 3. Worker 路徑缺失 - 4. 欄位缺失 (face_settings, asr_status, completed_at) **修復**: - 使用 sqlx::raw_sql() 支持多陳述句 - 路徑改用 current_exe() 推導 - Worker 啟動時也執行 migration - 新增 migration 039 補充缺失欄位 **文件**: - src/core/db/postgres_db.rs: 修復 migration runner - src/main.rs: 添加 worker migration - migrations/039_add_missing_video_columns.sql: 新增缺失欄位 **測試**: 待 M5Max128 驗證 Ref: docs_v1.0/M4_workspace/2026-10-04_B-4_Deep_Fix_Plan.md
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
# B-4 Migration 深度修復計畫
|
||||
|
||||
**日期**: 2026-10-04
|
||||
**狀態**: 🔴 失敗(M5Max128 實測)
|
||||
**優先級**: 最高
|
||||
|
||||
---
|
||||
|
||||
## 問題根源(M4 報告)
|
||||
|
||||
### 決定性證據
|
||||
|
||||
**錯誤**: `cannot insert multiple commands into a prepared statement`
|
||||
|
||||
**原因**: `postgres_db.rs:1703` 使用 `sqlx::query()` 執行整個 .sql 文件
|
||||
|
||||
**結果**: 0/40 migration 成功,45 個表全部來自 hardcoded DDL
|
||||
|
||||
---
|
||||
|
||||
## 五個獨立問題
|
||||
|
||||
| # | 問題 | 證據 | 嚴重度 |
|
||||
|---|------|------|--------|
|
||||
| 1 | 多陳述句無法執行 | `sqlx::query()` 限制 | 🔴 致命 |
|
||||
| 2 | 相對路徑 migrations/ | 依賴 CWD | 🔴 高 |
|
||||
| 3 | 只綁 server 不綁 worker | worker 走不到 | 🟡 中 |
|
||||
| 4 | 排序錯誤 | 3002 排最後 | 🟡 中 |
|
||||
| 5 | 無 migration 新增欄位 | face_settings 不存在 | 🔴 高 |
|
||||
|
||||
---
|
||||
|
||||
## 深度分析
|
||||
|
||||
### 問題 1:多陳述句執行
|
||||
|
||||
**當前代碼**:
|
||||
```rust
|
||||
sqlx::query(&sql_with_schema).execute(pool).await
|
||||
```
|
||||
|
||||
**問題**: PostgreSQL extended query protocol 不允許一個 prepared statement 包含多個命令
|
||||
|
||||
**解決方案**:
|
||||
- **方案 A**: 使用 `sqlx::raw_sql()` (推薦)
|
||||
- **方案 B**: 按 `;` 切分逐條執行
|
||||
- **方案 C**: 使用 `sqlx::query_as_untyped()`
|
||||
|
||||
### 問題 2:路徑解析
|
||||
|
||||
**當前代碼**:
|
||||
```rust
|
||||
let migrations_dir = std::path::Path::new("migrations");
|
||||
```
|
||||
|
||||
**問題**: 依賴 CWD,delivery 包中無此目錄
|
||||
|
||||
**解決方案**:
|
||||
```rust
|
||||
let migrations_dir = crate::core::path_utils::config_dir()
|
||||
.join("migrations");
|
||||
```
|
||||
|
||||
或使用 `current_exe()` 推導:
|
||||
```rust
|
||||
let exe_dir = std::env::current_exe()?
|
||||
.parent()
|
||||
.context("Failed to get exe directory")?;
|
||||
let migrations_dir = exe_dir.join("migrations");
|
||||
```
|
||||
|
||||
### 問題 3:Worker 路徑
|
||||
|
||||
**當前**: 只在 `api/server.rs:70` 呼叫
|
||||
|
||||
**需要**: 在 worker 啟動路徑也呼叫
|
||||
|
||||
**解決方案**:
|
||||
- 在 `job_worker.rs` 啟動時呼叫 `run_migrations()`
|
||||
- 或在 `PostgresDb::init()` 中統一呼叫
|
||||
|
||||
### 問題 4:排序邏輯
|
||||
|
||||
**當前**:
|
||||
```rust
|
||||
entries.sort_by_key(|e| e.file_name());
|
||||
```
|
||||
|
||||
**問題**: 檔名排序導致 `3002_` 排在最後
|
||||
|
||||
**正確順序**:
|
||||
1. 基礎表(001, 003, 006, 007, 011)
|
||||
2. 擴充欄位(012-016, 025-029)
|
||||
3. 功能表(030, 033, 3002)
|
||||
|
||||
**解決方案**:
|
||||
- 引入版本號排序
|
||||
- 或明確指定執行順序
|
||||
- 或重命名 migration 文件
|
||||
|
||||
### 問題 5:欄位缺失
|
||||
|
||||
**缺失欄位**:
|
||||
- `videos.face_settings`
|
||||
- `videos.asr_status`
|
||||
- `videos.completed_at`
|
||||
- `videos.vlm_description_localized`
|
||||
|
||||
**當前狀態**: 40 個 migration 文件中都沒有
|
||||
|
||||
**解決方案**: 新增 migration 文件補充這些欄位
|
||||
|
||||
---
|
||||
|
||||
## 修復方案
|
||||
|
||||
### 🔥 立即修復(最小改動)
|
||||
|
||||
**步驟 1**: 修改 `postgres_db.rs`
|
||||
- 使用 `sqlx::raw_sql()` 支持多陳述句
|
||||
- 修改路徑解析(使用 `current_exe()`)
|
||||
- 添加錯誤處理
|
||||
|
||||
**步驟 2**: 添加 worker migration
|
||||
- 在 `job_worker.rs` 啟動時呼叫
|
||||
|
||||
**步驟 3**: 新增 migration
|
||||
- 補充缺失欄位
|
||||
|
||||
**步驟 4**: 修復排序
|
||||
- 明確指定執行順序
|
||||
|
||||
**預估時間**: 3-4 小時
|
||||
|
||||
---
|
||||
|
||||
### 長期方案(架構重構)
|
||||
|
||||
**目標**: 統一 migration 機制
|
||||
|
||||
**步驟**:
|
||||
1. 移除 hardcoded DDL(L1076-1640)
|
||||
2. 全部改用 .sql migration
|
||||
3. 引入版本控制系統
|
||||
4. 添加回滾支援
|
||||
|
||||
**預估時間**: 8-12 小時
|
||||
|
||||
---
|
||||
|
||||
## 驗收標準
|
||||
|
||||
- [ ] 多陳述句可執行
|
||||
- [ ] 路徑不依賴 CWD
|
||||
- [ ] Worker 也能執行 migration
|
||||
- [ ] 排序正確
|
||||
- [ ] 所有欄位存在
|
||||
- [ ] M5Max128 測試通過(40/40)
|
||||
|
||||
---
|
||||
|
||||
## 下一步
|
||||
|
||||
1. **立即開始**:修復 postgres_db.rs(多陳述句 + 路徑)
|
||||
2. 添加 worker migration
|
||||
3. 新增缺失欄位的 migration
|
||||
4. 本地測試
|
||||
5. M5Max128 驗證
|
||||
|
||||
---
|
||||
|
||||
**責任**: Core Team
|
||||
**目標**: 完全解決 B-4 問題
|
||||
Reference in New Issue
Block a user