feat(backend): add automatic DB migrations

Add a lightweight migration runner with schema_migrations tracking, run pending migrations during backend startup before the scheduler, and keep a manual backend-migrate entrypoint.

The change also moves the existing lockout and task-thread-ID schema steps into shared migration modules, updates docs, and archives the OpenSpec change.
This commit is contained in:
2026-05-05 01:36:58 +08:00
parent e243dccfd7
commit 3ab845798d
21 changed files with 911 additions and 145 deletions
+8 -4
View File
@@ -190,13 +190,17 @@ export const tagApi = {
### 数据库迁移
```bash
# 修改模型后生成迁移脚本
# 手动创建脚本在 apps/backend/scripts/migrate_*.py
# 后端启动时会在调度器启动前自动执行待迁移项。
# 执行迁移
uv run python apps/backend/scripts/migrate_xxx.py
# 如需手动执行全部待迁移
uv run python main.py backend-migrate
# 或直接调用脚本模块
uv run python -m backend.scripts.run_migrations
```
新增迁移时,将迁移函数注册到 `backend.migrations.MIGRATIONS`,并保持迁移逻辑幂等。迁移只有成功完成后才会写入 `schema_migrations`
### 测试
#### 后端测试