Download docs/USER_DATABASE_DESIGN.md from isold23/MeloonTop: direct link, hf CLI and curl.
- Browser
- Download file 4.79 kB
-
https://huggingface.co/spaces/isold23/MeloonTop/resolve/main/docs/USER_DATABASE_DESIGN.md
- Command line
-
hf download hf://spaces/isold23/MeloonTop/docs/USER_DATABASE_DESIGN.md
-
curl -L -o USER_DATABASE_DESIGN.md https://huggingface.co/spaces/isold23/MeloonTop/resolve/main/docs/USER_DATABASE_DESIGN.md
MeloonTop 统一用户与产品资料数据库设计
1. 设计原则
users是三个产品共用的用户主表,只保存登录账号、密码哈希、昵称、头像、角色、状态和通用偏好。- 每个产品使用独立的一对一扩展表:
exam_profile、top_profile、chatbi_profile。 - 产品表通过
user_id外键关联users.id,用户删除时级联删除产品资料。 - 选科、目标分和成绩摘要属于 MeloonExam 数据,不能继续放在通用用户表或旧
profiles表中。
2. 表关系
users
├── exam_profile MeloonExam 用户学情配置
├── top_profile MeloonTop 用户偏好配置
└── chatbi_profile MeloonChatBI 工作区配置
3. 核心字段
| 表 | 主要字段 | 说明 |
|---|---|---|
users |
username, email, password_hash, nickname, avatar_url, role, status |
全产品共用的账号与个人信息 |
exam_profile |
school, grade, stage, selected_subjects, subject_expectations, subject_scores |
MeloonExam 专属资料 |
top_profile |
theme, language, favorite_categories, settings |
MeloonTop 专属偏好 |
chatbi_profile |
default_workspace_name, default_data_source_id, settings |
MeloonChatBI 专属工作区偏好 |
4. 迁移
执行 backend/migrations/20260930_split_users_and_product_profiles.sql,脚本会:
- 为
users补充统一账号字段。 - 创建三个产品 profile 表和索引。
- 在旧
profiles表存在且用户 ID 可匹配时,迁移 MeloonExam 学情字段到exam_profile。 - 不删除旧表,便于灰度切换和回滚。
迁移完成后,新代码只应写入 users 与对应的产品 profile 表;旧 profiles、user_profiles 仅作为兼容数据源逐步下线。
5. 产品表名前缀规范
三个产品的业务表统一使用产品前缀,避免跨产品表名冲突(例如 MeloonTop 与 MeloonExam 都曾使用 comments):
| 产品 | 前缀 | 示例 |
|---|---|---|
| MeloonExam | exam_ |
exam_paper、exam_mistakes、exam_user_favorites、exam_paper_comments |
| MeloonTop | top_ |
top_tools、top_games、top_prompts、top_dynamics、top_ratings、top_comments |
| MeloonChatBI | chatbi_ |
chatbi_conversations、chatbi_messages、chatbi_data_sources、chatbi_dashboards |
通用表不参与前缀化:users(登录注册与通用资料)。产品专属的用户扩展表沿用 exam_profile、top_profile、chatbi_profile 命名。
历史表重命名脚本见 backend/migrations/20260930_rename_product_tables.sql。
6. 产品化认证写入规则
users 是所有产品共用的账号表,但注册与登录必须声明自己属于哪个产品,只写入该产品的扩展表。实现位于 backend/common/auth/service.py(register_user / login_user 的 product 参数)与 backend/common/auth/router.py(create_auth_router(product) 工厂)。
| 产品 | 认证端点 | 写入的表 |
|---|---|---|
| MeloonExam | POST /api/auth/register、POST /api/auth/login(历史默认)、POST /api/exam/auth/* |
users + exam_profile |
| MeloonTop | POST /api/top/auth/*(合并应用);独立部署时即 POST /api/auth/* |
users + top_profile |
| MeloonChatBI | POST /api/v1/auth/* |
users + chatbi_profile |
读写产品资料使用统一的辅助函数:
get_product_profile(user_id, product, db=None):读取该产品的扩展行。upsert_product_profile(user_id, payload, product, db=None):按PRODUCT_PROFILE_COLUMNS白名单写入,不会把其他产品的字段写进来。
MeloonChatBI 另有 GET/PUT /api/v1/profile(backend/MeloonChatBI/routers/profile.py)读写工作区偏好,字段为 default_workspace_name、default_data_source_id、settings。
7. 上线前置条件
产品化认证依赖统一用户模型,部署前必须按顺序在 Supabase SQL Editor 执行:
backend/migrations/20260930_split_users_and_product_profiles.sql—— 为users补充username/nickname/avatar_url等字段,并创建exam_profile、top_profile、chatbi_profile。backend/migrations/20260930_rename_product_tables.sql—— 将产品业务表重命名为top_*/exam_*/chatbi_*。
两个脚本都是幂等的,可重复执行。未执行前:users 缺少 username 列,注册与 identifier 登录会返回 500;chatbi_profile 不存在,ChatBI 的注册与工作区偏好写入会失败。
common/auth 对 MeloonExam 的遗留 exam_profiles 表做了兼容:该表缺失时自动降级到 users 查询,登录不会因此变成 500。该兼容分支可在遗留数据迁移完成后移除。