feat: 更新机器人配置并添加数据库迁移脚本

- 将bot_config_template.toml中的版本升级至7.9.0
- 增强数据库配置选项以支持PostgreSQL
- 引入一个新脚本,用于在SQLite、MySQL和PostgreSQL之间迁移数据
- 实现一个方言适配器,用于处理特定于数据库的行为和配置
This commit is contained in:
Windpicker-owo
2025-11-27 18:45:01 +08:00
parent 369639a8f1
commit 43483b934e
30 changed files with 1658 additions and 2226 deletions

View File

@@ -1,6 +1,11 @@
"""数据库会话管理
单一职责:提供数据库会话工厂和上下文管理器
支持的数据库类型:
- SQLite: 设置 PRAGMA 参数优化并发
- MySQL: 无特殊会话设置
- PostgreSQL: 可选设置 schema 搜索路径
"""
import asyncio
@@ -53,12 +58,43 @@ async def get_session_factory() -> async_sessionmaker:
return _session_factory
async def _apply_session_settings(session: AsyncSession, db_type: str) -> None:
"""应用数据库特定的会话设置
Args:
session: 数据库会话
db_type: 数据库类型
"""
try:
if db_type == "sqlite":
# SQLite 特定的 PRAGMA 设置
await session.execute(text("PRAGMA busy_timeout = 60000"))
await session.execute(text("PRAGMA foreign_keys = ON"))
elif db_type == "postgresql":
# PostgreSQL 特定设置(如果需要)
# 可以设置 schema 搜索路径等
from src.config.config import global_config
schema = global_config.database.postgresql_schema
if schema and schema != "public":
await session.execute(text(f"SET search_path TO {schema}"))
# MySQL 通常不需要会话级别的特殊设置
except Exception:
# 复用连接时设置可能已存在,忽略错误
pass
@asynccontextmanager
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
"""获取数据库会话上下文管理器
这是数据库操作的主要入口点,通过连接池管理器提供透明的连接复用。
支持的数据库:
- SQLite: 自动设置 busy_timeout 和外键约束
- MySQL: 直接使用,无特殊设置
- PostgreSQL: 支持自定义 schema
使用示例:
async with get_db_session() as session:
result = await session.execute(select(User))
@@ -75,16 +111,10 @@ async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
# 使用连接池管理器(透明复用连接)
async with pool_manager.get_session(session_factory) as session:
# 为SQLite设置特定的PRAGMA
# 获取数据库类型并应用特定设置
from src.config.config import global_config
if global_config.database.database_type == "sqlite":
try:
await session.execute(text("PRAGMA busy_timeout = 60000"))
await session.execute(text("PRAGMA foreign_keys = ON"))
except Exception:
# 复用连接时PRAGMA可能已设置忽略错误
pass
await _apply_session_settings(session, global_config.database.database_type)
yield session
@@ -103,6 +133,11 @@ async def get_db_session_direct() -> AsyncGenerator[AsyncSession, None]:
async with session_factory() as session:
try:
# 应用数据库特定设置
from src.config.config import global_config
await _apply_session_settings(session, global_config.database.database_type)
yield session
except Exception:
await session.rollback()