()
| 48 | |
| 49 | # 定义创建数据库及默认管理员的异步函数 |
| 50 | async def create_default_admin(): |
| 51 | try: |
| 52 | async with engine.begin() as conn: |
| 53 | await conn.run_sync(models.Base.metadata.create_all) |
| 54 | async with AsyncSessionLocal() as session: |
| 55 | result = await session.execute(select(Admin).limit(1)) |
| 56 | existing_admin = result.scalars().first() |
| 57 | if not existing_admin: |
| 58 | default_admin = Admin(username="admin") |
| 59 | default_admin.set_password("123456") |
| 60 | session.add(default_admin) |
| 61 | await session.commit() |
| 62 | logger.info("创建默认管理员: admin") |
| 63 | else: |
| 64 | logger.info("默认管理员已存在") |
| 65 | except Exception as e: |
| 66 | logger.error(f"创建默认管理员失败: {e}") |
| 67 | raise |
| 68 | |
| 69 | |
| 70 | # 定义启动事件处理函数 |
no test coverage detected