hyf-backend/scripts/seed_admin.py

42 lines
1.2 KiB
Python
Raw Permalink 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.

#!/usr/bin/env python3
"""创建默认管理员账号 admin@example.com / admin123便于前端登录。"""
import asyncio
import os
import sys
# 在导入 th_agenter 前加载 .env
from pathlib import Path
_root = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(_root))
os.chdir(_root)
from dotenv import load_dotenv
load_dotenv()
# 若未设置,可在此指定(或通过环境变量传入)
# os.environ.setdefault("DATABASE_URL", "mysql+aiomysql://root:xxx@localhost:3306/allm?charset=utf8mb4")
from th_agenter.db.database import AsyncSessionFactory
from th_agenter.services.user import UserService
from utils.util_schemas import UserCreate
async def main():
async with AsyncSessionFactory() as session:
svc = UserService(session)
exists = await svc.get_user_by_email("admin@example.com")
if exists:
print("admin@example.com 已存在,跳过创建")
return
user = await svc.create_user(UserCreate(
username="admin",
email="admin@example.com",
password="admin123",
full_name="Admin",
))
print(f"已创建管理员: {user.username} / admin@example.com / admin123")
if __name__ == "__main__":
asyncio.run(main())