Files
app_03_pregnancy/backend/scripts/cleanup_demo_users.py
Karlcarlo 409dabafb5 feat: 初始化孕育无忧全栈项目基础代码结构
完成项目初始搭建,包含后端FastAPI+MongoDB服务、前端React+Vite项目的完整基础结构,添加各类配置文件与依赖清单,涵盖核心业务初始框架。
2026-05-21 00:46:48 +08:00

48 lines
1.5 KiB
Python
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.
"""
清理旧的共享 demo_web_user 及其关联数据。
部署新版 demo 登录逻辑后运行一次即可:
python -m scripts.cleanup_demo_users
"""
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
from app.config import Settings
from app.models.user import User
from app.models.report import Report
from app.models.analysis import Analysis
from app.models.chat import ChatSession
from app.models.checkup import CheckupPlan
async def main():
settings = Settings()
client = AsyncIOMotorClient(settings.mongodb_url)
db = client[settings.database_name]
await init_beanie(database=db, document_models=[User, Report, Analysis, ChatSession, CheckupPlan])
user = await User.find_one(User.openid == "demo_web_user")
if not user:
print("未找到 demo_web_user,无需清理。")
return
uid = user.id
r = await Report.find(Report.user_id == uid).delete()
a = await Analysis.find(Analysis.user_id == uid).delete()
c = await ChatSession.find(ChatSession.user_id == uid).delete()
p = await CheckupPlan.find(CheckupPlan.user_id == uid).delete()
await user.delete()
print(f"已清理 demo_web_user (id={uid}):")
print(f" Reports: {r.deleted_count if r else 0}")
print(f" Analyses: {a.deleted_count if a else 0}")
print(f" ChatSessions: {c.deleted_count if c else 0}")
print(f" CheckupPlans: {p.deleted_count if p else 0}")
if __name__ == "__main__":
asyncio.run(main())