Files
FastapiAdmin/backend/app/core/database.py
T
zhangtao fce6333722 style: 优化代码格式和导入顺序
refactor: 重构数据库连接模块,提高可维护性

docs: 更新README.md项目描述

style: 调整日志输出格式和内容

refactor: 优化中间件日志记录逻辑

style: 统一代码中的空行和导入顺序

refactor: 分离数据库引擎和会话创建逻辑

style: 清理未使用的导入和代码

refactor: 重命名数据库会话变量

style: 调整jinja2模板变量命名

refactor: 优化异常处理和日志记录

style: 统一字符串格式化方式

refactor: 优化redis连接错误处理

style: 调整注释格式和位置

refactor: 优化数据库连接配置

style: 清理多余空行和注释
2025-11-16 18:43:30 +08:00

127 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
from redis.asyncio import Redis
from redis import exceptions
from fastapi import FastAPI
from sqlalchemy import create_engine, Engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession, AsyncEngine
from app.core.logger import logger
from app.config.setting import settings
from app.core.exceptions import CustomException
def create_engine_and_session(
db_url: str = settings.DB_URI
) -> tuple[Engine, sessionmaker]:
"""
创建同步数据库引擎和会话工厂。
参数:
- db_url (str): 数据库连接URL,默认从配置中获取。
返回:
- tuple[Engine, sessionmaker]: 同步数据库引擎和会话工厂。
"""
try:
if not settings.SQL_DB_ENABLE:
raise CustomException(msg="请先开启数据库连接", data="请启用 app/config/setting.py: SQL_DB_ENABLE")
# 同步数据库引擎
engine: Engine = create_engine(
url=db_url,
echo=settings.DATABASE_ECHO,
pool_pre_ping=settings.POOL_PRE_PING,
pool_recycle=settings.POOL_RECYCLE,
)
except Exception as e:
logger.error(f'❌ 数据库连接失败 {e}')
raise
else:
# 同步数据库会话工厂
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
return engine, SessionLocal
def create_async_engine_and_session(
db_url: str = settings.ASYNC_DB_URI
) -> tuple[AsyncEngine, async_sessionmaker[AsyncSession]]:
"""
获取异步数据库会话连接。
返回:
- tuple[AsyncEngine, async_sessionmaker[AsyncSession]]: 异步数据库引擎和会话工厂。
"""
try:
if not settings.SQL_DB_ENABLE:
raise CustomException(msg="请先开启数据库连接", data="请启用 app/config/setting.py: SQL_DB_ENABLE")
# 异步数据库引擎
async_engine: AsyncEngine = create_async_engine(
url=db_url,
echo=settings.DATABASE_ECHO,
echo_pool=settings.ECHO_POOL,
pool_pre_ping=settings.POOL_PRE_PING,
future=settings.FUTURE,
pool_recycle=settings.POOL_RECYCLE,
pool_size=settings.POOL_SIZE,
max_overflow=settings.MAX_OVERFLOW,
pool_timeout=settings.POOL_TIMEOUT,
pool_use_lifo=settings.POOL_USE_LIFO,
)
except Exception as e:
logger.error(f'❌ 数据库连接失败 {e}')
raise
else:
# 异步数据库会话工厂
AsyncSessionLocal = async_sessionmaker(
bind=async_engine,
autocommit=settings.AUTOCOMMIT,
autoflush=settings.AUTOFETCH,
expire_on_commit=settings.EXPIRE_ON_COMMIT,
class_=AsyncSession
)
return async_engine, AsyncSessionLocal
engine, db_session = create_engine_and_session(settings.DB_URI)
async_engine, async_db_session = create_async_engine_and_session(settings.ASYNC_DB_URI)
async def redis_connect(app: FastAPI, status: bool) -> Redis | None:
"""
创建或关闭Redis连接。
参数:
- app (FastAPI): FastAPI应用实例。
- status (bool): 连接状态,True为创建连接,False为关闭连接。
返回:
- Redis | None: Redis连接实例,如果连接失败则返回None。
"""
if not settings.REDIS_ENABLE:
raise CustomException(msg="请先开启Redis连接", data="请启用 app/core/config.py: REDIS_ENABLE")
if status:
try:
rd = await Redis.from_url(
url=settings.REDIS_URI,
encoding='utf-8',
decode_responses=True,
health_check_interval=20,
max_connections=settings.POOL_SIZE,
socket_timeout=settings.POOL_TIMEOUT
)
app.state.redis = rd
if await rd.ping():
logger.info("✅️ Redis连接成功...")
return rd
except exceptions.AuthenticationError as e:
logger.error(f"❌ 数据库 Redis 认证失败: {e}")
raise
except exceptions.TimeoutError as e:
logger.error(f"❌ 数据库 Redis 连接超时: {e}")
raise
except exceptions.RedisError as e:
logger.error(f"❌ 数据库 Redis 连接错误: {e}")
raise
else:
await app.state.redis.close()
logger.info('✅️ Redis连接已关闭')