diff --git a/bot.py b/bot.py index 93b22c307..96c5f165f 100644 --- a/bot.py +++ b/bot.py @@ -289,7 +289,7 @@ class DatabaseManager: start_time = time.time() # 使用线程执行器运行潜在的阻塞操作 - await asyncio.to_thread(initialize_sql_database, global_config.database) + await initialize_sql_database( global_config.database) elapsed_time = time.time() - start_time logger.info( f"数据库连接初始化成功,使用 {global_config.database.database_type} 数据库,耗时: {elapsed_time:.2f}秒" diff --git a/src/common/database/database.py b/src/common/database/database.py index 894ec5f2b..c6d9ec2c7 100644 --- a/src/common/database/database.py +++ b/src/common/database/database.py @@ -48,35 +48,6 @@ class DatabaseProxy: return result -class SQLAlchemyTransaction: - """SQLAlchemy 异步事务上下文管理器 (兼容旧代码示例,推荐直接使用 get_db_session)。""" - - def __init__(self): - self._ctx = None - self.session = None - - async def __aenter__(self): - # get_db_session 是一个 async contextmanager - self._ctx = get_db_session() - self.session = await self._ctx.__aenter__() - return self.session - - async def __aexit__(self, exc_type, exc_val, exc_tb): - try: - if self.session: - if exc_type is None: - try: - await self.session.commit() - except Exception: - await self.session.rollback() - raise - else: - await self.session.rollback() - finally: - if self._ctx: - await self._ctx.__aexit__(exc_type, exc_val, exc_tb) - - # 创建全局数据库代理实例 db = DatabaseProxy() diff --git a/src/plugin_system/apis/permission_api.py b/src/plugin_system/apis/permission_api.py index 3c42f9eab..41b6a761b 100644 --- a/src/plugin_system/apis/permission_api.py +++ b/src/plugin_system/apis/permission_api.py @@ -36,7 +36,7 @@ class IPermissionManager(ABC): async def check_permission(self, user: UserInfo, permission_node: str) -> bool: ... @abstractmethod - def is_master(self, user: UserInfo) -> bool: ... # 同步快速判断 + async def is_master(self, user: UserInfo) -> bool: ... # 同步快速判断 @abstractmethod async def register_permission_node(self, node: PermissionNode) -> bool: ... @@ -82,9 +82,9 @@ class PermissionAPI: self._ensure_manager() return await self._permission_manager.check_permission(UserInfo(platform, user_id), permission_node) - def is_master(self, platform: str, user_id: str) -> bool: + async def is_master(self, platform: str, user_id: str) -> bool: self._ensure_manager() - return self._permission_manager.is_master(UserInfo(platform, user_id)) + return await self._permission_manager.is_master(UserInfo(platform, user_id)) async def register_permission_node( self, diff --git a/src/plugins/built_in/permission_management/plugin.py b/src/plugins/built_in/permission_management/plugin.py index 1541e2f1c..be9ffad7c 100644 --- a/src/plugins/built_in/permission_management/plugin.py +++ b/src/plugins/built_in/permission_management/plugin.py @@ -231,7 +231,7 @@ class PermissionCommand(PlusCommand): target_user_id = chat_stream.user_info.user_id # 检查是否为Master用户 - is_master = permission_api.is_master(chat_stream.platform, target_user_id) + is_master = await permission_api.is_master(chat_stream.platform, target_user_id) # 获取用户权限 permissions = await permission_api.get_user_permissions(chat_stream.platform, target_user_id)