修复数据库调用问题
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from . import token
|
||||
from .setting import Setting
|
||||
from .object import Object
|
||||
from .object import Object
|
||||
from .user import User
|
||||
@@ -1,12 +1,13 @@
|
||||
from loguru import logger
|
||||
from sqlmodel import select
|
||||
from .setting import Setting
|
||||
from .user import User
|
||||
from pkg import Password
|
||||
|
||||
default_settings: list[Setting] = [
|
||||
Setting(type='string', name='version', value='1.0.0'),
|
||||
Setting(type='int', name='ver', value='1'),
|
||||
Setting(type='string', name='account', value='admin@yuxiaoqiu.cn'),
|
||||
Setting(type='int', name='jwt_token_exp', value='30'),
|
||||
Setting(type='string', name='server_chan_key', value=''),
|
||||
]
|
||||
|
||||
async def migration(session):
|
||||
@@ -16,21 +17,46 @@ async def migration(session):
|
||||
if await Setting.get(session, Setting.name == 'version'):
|
||||
# 已有数据,说明不是第一次运行,直接返回
|
||||
return
|
||||
|
||||
# 生成初始密码与密钥
|
||||
admin_password = Password.generate()
|
||||
logger.warning(f"密码(请牢记,后续不再显示): {admin_password}")
|
||||
|
||||
settings.append(Setting(type='string', name='password', value=Password.hash(admin_password)))
|
||||
|
||||
settings.append(Setting(type='string', name='SECRET_KEY', value=Password.generate(64)))
|
||||
|
||||
# 读取库里已存在的 name,避免主键冲突
|
||||
names = [s.name for s in settings]
|
||||
exist_stmt = select(Setting.name).where(Setting.name.in_(names))
|
||||
exist_rs = await session.exec(exist_stmt)
|
||||
existed: set[str] = set(exist_rs.all())
|
||||
existed_settings = await Setting.get(
|
||||
session,
|
||||
Setting.name.in_(names),
|
||||
fetch_mode="all"
|
||||
)
|
||||
existed: set[str] = {s.name for s in (existed_settings or [])}
|
||||
|
||||
to_insert = [s for s in settings if s.name not in existed]
|
||||
if to_insert:
|
||||
# 使用你写好的通用新增方法(是类方法),并传入会话
|
||||
await Setting.add(session, to_insert, refresh=False)
|
||||
await Setting.add(session, to_insert, refresh=False)
|
||||
|
||||
if await User.get(session, User.id == 1):
|
||||
# 已有超级管理员用户,说明不是第一次运行
|
||||
|
||||
# 修复数据库id为1的用户不是管理员的问题
|
||||
admin_user = await User.get(session, User.id == 1)
|
||||
if admin_user and not admin_user.is_admin:
|
||||
admin_user.is_admin = True
|
||||
await User.update(session, admin_user, refresh=False)
|
||||
|
||||
# 已有用户,直接返回
|
||||
return
|
||||
|
||||
# 生成初始密码与密钥
|
||||
admin_password = Password.generate()
|
||||
logger.warning("当前无管理员用户,已自动创建初始管理员用户:")
|
||||
logger.warning("邮箱: admin@yxqi.cn")
|
||||
logger.warning(f"密码: {admin_password}")
|
||||
|
||||
admin_user = User(
|
||||
id=1,
|
||||
email='admin@yxqi.cn',
|
||||
username='Admin',
|
||||
password=Password.hash(admin_password),
|
||||
is_admin=True
|
||||
)
|
||||
|
||||
await User.add(session, admin_user, refresh=False)
|
||||
@@ -1,28 +1,45 @@
|
||||
from typing import Literal
|
||||
from sqlmodel import Field, Column, String, DateTime
|
||||
from typing import Literal, TYPE_CHECKING
|
||||
from sqlmodel import Field, Column, String, DateTime, Relationship
|
||||
from .base import TableBase, IdMixin
|
||||
from datetime import datetime
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .user import User
|
||||
|
||||
class Object(IdMixin, TableBase, table=True):
|
||||
|
||||
key: str = Field(index=True, nullable=False, description="物品外部ID")
|
||||
user_id: int = Field(foreign_key="user.id", index=True, nullable=False, description="所属用户ID")
|
||||
key: str = Field(index=True, nullable=False, unique=True, description="物品外部ID")
|
||||
type: Literal['normal', 'car'] = Field(
|
||||
default='normal',
|
||||
description="物品类型",
|
||||
sa_column=Column(String, default='normal', nullable=False)
|
||||
sa_column=Column(
|
||||
String,
|
||||
default='normal',
|
||||
nullable=False
|
||||
)
|
||||
)
|
||||
name: str = Field(nullable=False, description="物品名称")
|
||||
icon: str | None = Field(default=None, description="物品图标")
|
||||
status: Literal['ok', 'lost'] = Field(
|
||||
default='ok',
|
||||
description="物品状态",
|
||||
sa_column=Column(String, default='ok', nullable=False)
|
||||
sa_column=Column(
|
||||
String,
|
||||
default='ok',
|
||||
nullable=False
|
||||
)
|
||||
)
|
||||
phone: str | None = Field(default=None, description="联系电话")
|
||||
context: str | None = Field(default=None, description="物品描述")
|
||||
description: str | None = Field(default=None, description="物品描述")
|
||||
find_ip: str | None = Field(default=None, description="最后一次发现的IP地址")
|
||||
lost_at: datetime | None = Field(
|
||||
default=None,
|
||||
description="物品标记为丢失的时间",
|
||||
sa_column=Column(DateTime, nullable=True)
|
||||
)
|
||||
sa_column=Column(
|
||||
DateTime,
|
||||
nullable=True
|
||||
)
|
||||
)
|
||||
|
||||
user: "User" = Relationship(back_populates="objects")
|
||||
16
model/user.py
Normal file
16
model/user.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from typing import TYPE_CHECKING
|
||||
from sqlmodel import Field, Column, String, Boolean, Relationship
|
||||
from .base import TableBase, IdMixin
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .object import Object
|
||||
|
||||
class User(IdMixin, TableBase, table=True):
|
||||
|
||||
email: str = Field(sa_column=Column(String(100), index=True, unique=True))
|
||||
username: str = Field(sa_column=Column(String(50), index=True, unique=True))
|
||||
password: str = Field(sa_column=Column(String(100)))
|
||||
|
||||
is_admin: bool = Field(default=False, sa_column=Column(Boolean, default=False))
|
||||
|
||||
objects: list["Object"] = Relationship(back_populates="user")
|
||||
Reference in New Issue
Block a user