From a261d9da27176d21c5ebf3bd05d626f77bff1aaa Mon Sep 17 00:00:00 2001 From: iammm0 Date: Thu, 12 Feb 2026 13:33:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=8E=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化 api/database/database.py - 更新 api/requirements.txt Co-authored-by: Cursor --- api/database/database.py | 48 ++++++++++++---------------------------- api/requirements.txt | 2 -- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/api/database/database.py b/api/database/database.py index d56ce6b..9402721 100644 --- a/api/database/database.py +++ b/api/database/database.py @@ -1,6 +1,6 @@ """ 数据库连接和初始化 -支持 PostgreSQL(推荐)和 SQLite(本地开发) +仅支持 PostgreSQL """ import os from sqlalchemy import create_engine @@ -16,59 +16,40 @@ raw_database_url = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@loc def parse_database_url(url: str) -> tuple[str, str]: """ 解析数据库 URL,返回同步和异步版本 - + 支持格式: - PostgreSQL: postgresql://user:pass@host:port/db - PostgreSQL async: postgresql+asyncpg://user:pass@host:port/db - - SQLite: sqlite:///./path/to/db.db - - SQLite async: sqlite+aiosqlite:///./path/to/db.db """ - # PostgreSQL if url.startswith("postgresql+asyncpg://"): async_url = url sync_url = url.replace("postgresql+asyncpg://", "postgresql://") elif url.startswith("postgresql://"): sync_url = url async_url = url.replace("postgresql://", "postgresql+asyncpg://") - # SQLite - elif url.startswith("sqlite+aiosqlite://"): - async_url = url - sync_url = url.replace("sqlite+aiosqlite://", "sqlite://") - elif url.startswith("sqlite://"): - sync_url = url - async_url = url.replace("sqlite://", "sqlite+aiosqlite://") else: - # 默认使用 PostgreSQL print(f"警告: DATABASE_URL 格式不正确 ({url}),使用默认 PostgreSQL") sync_url = "postgresql://postgres:postgres@localhost:5432/life_echo" async_url = "postgresql+asyncpg://postgres:postgres@localhost:5432/life_echo" - + return sync_url, async_url DATABASE_URL, ASYNC_DATABASE_URL = parse_database_url(raw_database_url) -# 创建同步引擎(用于迁移、Celery 任务等) -# SQLite 需要特殊的 connect_args -if DATABASE_URL.startswith("sqlite"): - engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) -else: - # 使用 psycopg (v3) 驱动 - sync_url = DATABASE_URL.replace("postgresql://", "postgresql+psycopg://") - engine = create_engine(sync_url, pool_size=5, max_overflow=10) +# 同步引擎(用于迁移、Celery 任务等),使用 psycopg +sync_url = DATABASE_URL.replace("postgresql://", "postgresql+psycopg://") +engine = create_engine(sync_url, pool_size=5, max_overflow=10) -# 创建异步引擎(用于 FastAPI) -if ASYNC_DATABASE_URL.startswith("sqlite"): - async_engine = create_async_engine(ASYNC_DATABASE_URL, echo=False) -else: - async_engine = create_async_engine( - ASYNC_DATABASE_URL, - echo=False, - pool_size=5, - max_overflow=10 - ) +# 异步引擎(用于 FastAPI) +async_engine = create_async_engine( + ASYNC_DATABASE_URL, + echo=False, + pool_size=5, + max_overflow=10, +) -# 创建会话工厂 +# 会话工厂 SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) AsyncSessionLocal = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False) @@ -98,4 +79,3 @@ async def get_async_db(): raise finally: await session.close() - diff --git a/api/requirements.txt b/api/requirements.txt index 0405afc..205e521 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -14,8 +14,6 @@ greenlet>=3.3.0 # PostgreSQL drivers asyncpg>=0.29.0 psycopg[binary]>=3.1.0 -# SQLite -aiosqlite==0.20.0 # Redis for session storage redis>=5.0.0