Files
ai-showcase-platform/MIGRATION_GUIDE.md

144 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 資料庫遷移指南
## 🚨 問題解決
如果您遇到 SQL 語法錯誤,請按照以下步驟操作:
### 方法一:使用簡化版遷移(推薦)
```bash
# 1. 先執行基本表結構遷移
pnpm run migrate
# 2. 如果觸發器創建失敗,單獨執行觸發器遷移
pnpm run migrate:triggers
```
### 方法二:手動執行 SQL
如果自動遷移仍然失敗,請手動執行:
```bash
# 1. 連接到資料庫
mysql -h mysql.theaken.com -P 33306 -u AI_Platform -p
# 2. 選擇資料庫
USE db_AI_Platform;
# 3. 執行 SQL 文件
source database-schema-simple.sql;
```
### 方法三:分步執行
```bash
# 1. 測試資料庫連接
pnpm run test:db
# 2. 如果連接成功,執行遷移
pnpm run migrate
# 3. 檢查結果
pnpm run test:db
```
## 🔧 常見問題解決
### 問題 1: SQL 語法錯誤
**錯誤**: `You have an error in your SQL syntax`
**解決方案**:
1. 使用 `database-schema-simple.sql` 而不是 `database-schema.sql`
2. 確保 MySQL 版本支援 JSON 類型MySQL 5.7+
3. 檢查字符集設置
### 問題 2: 觸發器創建失敗
**錯誤**: `Trigger creation failed`
**解決方案**:
```bash
# 單獨執行觸發器遷移
pnpm run migrate:triggers
```
### 問題 3: 權限不足
**錯誤**: `Access denied`
**解決方案**:
1. 檢查資料庫用戶權限
2. 確保用戶有 CREATE、DROP、INSERT 權限
3. 聯繫資料庫管理員
### 問題 4: 連接超時
**錯誤**: `Connection timeout`
**解決方案**:
1. 檢查網路連接
2. 確認資料庫服務正在運行
3. 檢查防火牆設置
## 📋 遷移檢查清單
### 遷移前檢查
- [ ] 資料庫服務正在運行
- [ ] 網路連接正常
- [ ] 用戶權限充足
- [ ] 環境變數設置正確
### 遷移後檢查
- [ ] 所有表創建成功
- [ ] 觸發器創建成功
- [ ] 視圖創建成功
- [ ] 初始數據插入成功
### 驗證命令
```bash
# 檢查表數量
mysql -h mysql.theaken.com -P 33306 -u AI_Platform -p -e "SHOW TABLES;" db_AI_Platform
# 檢查觸發器
mysql -h mysql.theaken.com -P 33306 -u AI_Platform -p -e "SHOW TRIGGERS;" db_AI_Platform
# 檢查視圖
mysql -h mysql.theaken.com -P 33306 -u AI_Platform -p -e "SHOW FULL TABLES WHERE Table_type = 'VIEW';" db_AI_Platform
```
## 🆘 緊急恢復
如果遷移過程中出現問題:
### 1. 停止遷移
```bash
# 按 Ctrl+C 停止當前遷移
```
### 2. 檢查資料庫狀態
```bash
# 檢查是否有部分表創建
pnpm run test:db
```
### 3. 清理並重新開始
```bash
# 刪除所有表(慎用!)
mysql -h mysql.theaken.com -P 33306 -u AI_Platform -p -e "DROP DATABASE IF EXISTS db_AI_Platform; CREATE DATABASE db_AI_Platform CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
# 重新執行遷移
pnpm run migrate
```
## 📞 技術支援
如果問題仍然存在,請提供以下信息:
1. 完整的錯誤訊息
2. MySQL 版本
3. 操作系統
4. Node.js 版本
5. 執行的命令
聯繫方式:
- 技術團隊
- 項目維護者
- 查看專案文檔