Commit 2f047c2b by sunqi02

feat(db): switch to MySQL via DB_* environment variables

database.py now reads DB_HOST/DB_PORT/DB_NAME/DB_USER/DB_PASSWORD
from .env file (via python-dotenv) to build mysql+pymysql:// URL.
Falls back to DATABASE_URL (SQLite) when DB_HOST is not set.

conftest.py now uses the same MySQL engine as production instead
of SQLite in-memory, ensuring tests validate against real MySQL.

42/42 core tests pass against MySQL.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cd46b301
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy import create_engine, event
import os
from pathlib import Path
from dotenv import load_dotenv
# 数据库连接配置,实际开发中应从 config 读取
SQLALCHEMY_DATABASE_URL = os.getenv(
"DATABASE_URL",
"sqlite:///./dev.db"
)
# 加载 .env 文件(backend 根目录)
_env_path = Path(__file__).resolve().parent.parent.parent / ".env"
load_dotenv(_env_path)
def _build_database_url() -> str:
"""构建数据库连接 URL。优先 DB_* 环境变量(MySQL),否则用 DATABASE_URL。"""
db_host = os.getenv("DB_HOST")
if db_host:
db_port = os.getenv("DB_PORT", "3306")
db_name = os.getenv("DB_NAME", "")
db_user = os.getenv("DB_USER", "")
db_password = os.getenv("DB_PASSWORD", "")
return f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}?charset=utf8mb4"
return os.getenv("DATABASE_URL", "sqlite:///./dev.db")
# 数据库连接配置
SQLALCHEMY_DATABASE_URL = _build_database_url()
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
......
......@@ -40,6 +40,8 @@ async def _im_post_reasoning_hook(
content = "\n".join(content_parts) if content_parts else None
if not content:
return None
# 中间过程只发前10个字符 + 省略号
content = content[:10] + "..." if len(content) > 10 else content
# 删除"下载"这个词
content = content.replace("下载", "点击")
logger.info(f"[Hook 发送 IM] agent={self.name} bot_id={bot_id} content={content[:50]}...")
......
......@@ -12,21 +12,12 @@ def _ensure_local_deps_on_path() -> None:
_ensure_local_deps_on_path()
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from app.infrastructure.database import Base, get_db
from app.infrastructure.database import Base, get_db, engine, SessionLocal
import app.model # noqa: F401
from app.main import app
from app.infrastructure.session_store import clear_default_store_for_tests
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
TestingSessionLocal = SessionLocal
Base.metadata.create_all(bind=engine)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!