fix: 启动时自动迁移缺失的数据库列

问题:旧版数据库缺少 user_id 列导致 500 错误
修复:
- init.ts 启动时自动检测并添加缺失列
- 删除 deploy/ 目录,改为手动部署
- 添加 migrate.sql 迁移脚本
- 更新 CLAUDE.md 部署说明
This commit is contained in:
2026-05-29 16:46:39 +08:00
parent 35bfe8293a
commit 9ef960321f
8 changed files with 66 additions and 172 deletions

16
server/src/db/migrate.sql Normal file
View File

@@ -0,0 +1,16 @@
-- 迁移脚本:为缺少 user_id 的表添加该列
-- 适用于旧版数据库升级
-- transactions 表添加 user_id
ALTER TABLE transactions ADD COLUMN user_id INT NOT NULL DEFAULT 1 AFTER id;
ALTER TABLE transactions ADD INDEX idx_user_date (user_id, date);
ALTER TABLE transactions ADD INDEX idx_user_type_date (user_id, type, date);
ALTER TABLE transactions ADD FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
-- budgets 表添加 user_id
ALTER TABLE budgets ADD COLUMN user_id INT NOT NULL DEFAULT 1 AFTER id;
ALTER TABLE budgets ADD UNIQUE KEY uk_user_month (user_id, month);
ALTER TABLE budgets ADD FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
-- categories 表确认 user_id 存在(旧版可能也没有)
ALTER TABLE categories ADD COLUMN IF NOT EXISTS user_id INT DEFAULT 0 COMMENT '0=默认分类, 其他=用户自定义' AFTER id;