From 6a98ae62081667084f37499f3dfe30af3f8465a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=85=E8=AF=BA=E7=8B=90?= <212194964+foxcyber907@users.noreply.github.com> Date: Fri, 19 Sep 2025 20:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 6 +++++- pyproject.toml | 2 ++ src/common/database/database.py | 4 ++-- src/common/database/sqlalchemy_models.py | 8 +++----- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index b4c448670..f382df1e1 100644 --- a/bot.py +++ b/bot.py @@ -193,9 +193,11 @@ class MaiBotMain(BaseMain): logger.error(f"数据库连接初始化失败: {e}") raise e + async def initialize_database_async(self): + """异步初始化数据库表结构""" logger.info("正在初始化数据库表结构...") try: - init_db() + await init_db() logger.info("数据库表结构初始化完成") except Exception as e: logger.error(f"数据库表结构初始化失败: {e}") @@ -229,6 +231,8 @@ if __name__ == "__main__": try: # 执行初始化和任务调度 loop.run_until_complete(main_system.initialize()) + # 异步初始化数据库表结构 + loop.run_until_complete(maibot.initialize_database_async()) initialize_lpmm_knowledge() # Schedule tasks returns a future that runs forever. # We can run console_input_loop concurrently. diff --git a/pyproject.toml b/pyproject.toml index 68b1837e7..ea7bc77f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,6 +72,8 @@ dependencies = [ "uvicorn>=0.35.0", "watchdog>=6.0.0", "websockets>=15.0.1", + "aiomysql>=0.2.0", + "aiosqlite>=0.21.0", ] [[tool.uv.index]] diff --git a/src/common/database/database.py b/src/common/database/database.py index 88dee6464..d196df032 100644 --- a/src/common/database/database.py +++ b/src/common/database/database.py @@ -89,7 +89,7 @@ def get_db(): return _db -def initialize_sql_database(database_config): +async def initialize_sql_database(database_config): """ 根据配置初始化SQL数据库连接(SQLAlchemy版本) @@ -119,7 +119,7 @@ def initialize_sql_database(database_config): # 使用SQLAlchemy初始化 success = initialize_database_compat() if success: - _sql_engine = get_engine() + _sql_engine = await get_engine() logger.info("SQLAlchemy数据库初始化成功") else: logger.error("SQLAlchemy数据库初始化失败") diff --git a/src/common/database/sqlalchemy_models.py b/src/common/database/sqlalchemy_models.py index f9b0ef68d..e5eacee1f 100644 --- a/src/common/database/sqlalchemy_models.py +++ b/src/common/database/sqlalchemy_models.py @@ -7,7 +7,6 @@ from sqlalchemy import Column, String, Float, Integer, Boolean, Text, Index, Dat from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import Mapped, mapped_column -from sqlalchemy.pool import QueuePool import os import datetime import time @@ -621,10 +620,9 @@ async def initialize_database(): } if config.database_type == "mysql": - # MySQL连接池配置 + # MySQL连接池配置 - 异步引擎使用默认连接池 engine_kwargs.update( { - "poolclass": QueuePool, "pool_size": config.connection_pool_size, "max_overflow": config.connection_pool_size * 2, "pool_timeout": config.connection_timeout, @@ -640,10 +638,9 @@ async def initialize_database(): } ) else: - # SQLite配置 - 添加连接池设置以避免连接耗尽 + # SQLite配置 - 异步引擎使用默认连接池 engine_kwargs.update( { - "poolclass": QueuePool, "pool_size": 20, # 增加池大小 "max_overflow": 30, # 增加溢出连接数 "pool_timeout": 60, # 增加超时时间 @@ -678,6 +675,7 @@ async def get_db_session() -> AsyncGenerator[AsyncSession, None]: raise RuntimeError("Database session not initialized") session = SessionLocal() yield session + # await session.commit() except Exception: if session: await session.rollback()