Files
Task_Reporter/openspec/changes/archive/2025-12-07-migrate-sqlite-to-mysql/tasks.md
egg 92834dbe0e feat: Migrate to MySQL and add unified environment configuration
## Database Migration (SQLite → MySQL)
- Add Alembic migration framework
- Add 'tr_' prefix to all tables to avoid conflicts in shared database
- Remove SQLite support, use MySQL exclusively
- Add pymysql driver dependency
- Change ad_token column to Text type for long JWT tokens

## Unified Environment Configuration
- Centralize all hardcoded settings to environment variables
- Backend: Extend Settings class in app/core/config.py
- Frontend: Use Vite environment variables (import.meta.env)
- Docker: Move credentials to environment variables
- Update .env.example files with comprehensive documentation

## Test Organization
- Move root-level test files to tests/ directory:
  - test_chat_room.py → tests/test_chat_room.py
  - test_websocket.py → tests/test_websocket.py
  - test_realtime_implementation.py → tests/test_realtime_implementation.py
- Fix path references in test_realtime_implementation.py

Breaking Changes:
- CORS now requires explicit origins (no more wildcard)
- All database tables renamed with 'tr_' prefix
- SQLite no longer supported

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:15:11 +08:00

1.9 KiB

Tasks: Migrate SQLite to MySQL

1. Dependencies Setup

  • 1.1 新增 pymysql 到 requirements.txt
  • 1.2 新增 alembic 到 requirements.txt
  • 1.3 安裝新相依套件

2. Model Updates (Add tr_ Prefix)

  • 2.1 更新 app/modules/auth/models.py - tr_users, tr_user_sessions
  • 2.2 更新 app/modules/chat_room/models.py - tr_incident_rooms, tr_room_members, tr_room_templates
  • 2.3 更新 app/modules/realtime/models.py - tr_messages, tr_message_reactions, tr_message_edit_history
  • 2.4 更新 app/modules/report_generation/models.py - tr_generated_reports
  • 2.5 更新 app/modules/file_storage/models.py - tr_room_files
  • 2.6 更新所有索引和約束名稱加上 tr_ 前綴

3. Database Core Updates

  • 3.1 更新 app/core/database.py 移除 SQLite 特殊處理,加入 MySQL 連線池設定
  • 3.2 更新 app/main.py 移除 Base.metadata.create_all() 自動建表

4. Alembic Setup

  • 4.1 執行 alembic init alembic 初始化 Alembic
  • 4.2 設定 alembic/env.py 使用環境變數讀取 DATABASE_URL
  • 4.3 更新 alembic/env.py 設定 target_metadata 和 tr_alembic_version 版本表
  • 4.4 建立初始遷移腳本 alembic revision --autogenerate -m "Initial migration - create tr_ prefixed tables"

5. Environment Configuration

  • 5.1 更新 .env 使用 MySQL 連線字串
  • 5.2 更新 .env.example 提供 MySQL 連線範例
  • 5.3 移除 SQLite 相關註解和範例

6. Database Migration

  • 6.1 執行 alembic upgrade head 建立資料表
  • 6.2 驗證所有資料表正確建立於 MySQL (11 個 tr_ 前綴表格)

7. Cleanup

  • 7.1 刪除本地 SQLite 資料庫檔案 task_reporter.db
  • 7.2 確認 .gitignore 包含 *.db 規則

8. Testing

  • 8.1 驗證後端應用程式可正常啟動並連接 MySQL
  • 8.2 驗證資料庫 CRUD 操作正常 (tr_room_templates 查詢成功)