From bded945ec5d50400cf224d5337e0e896213fd502 Mon Sep 17 00:00:00 2001 From: Lxy Date: Sun, 5 Jul 2026 00:24:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8A=A8=E6=80=81=E8=AF=BB=E5=8F=96=20M?= =?UTF-8?q?ySQLSessionLocal=EF=BC=8C=E9=81=BF=E5=85=8D=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E9=A1=BA=E5=BA=8F=E5=AF=BC=E8=87=B4=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=B7=B3=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config_store.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/config_store.py b/app/config_store.py index db58a7e..d78d417 100644 --- a/app/config_store.py +++ b/app/config_store.py @@ -10,10 +10,18 @@ from typing import Optional from app.models import AppConfig from app.storage_manager import get_storage_manager -from app.mysql_database import MySQLSessionLocal logger = logging.getLogger(__name__) + +def _get_mysql_session_local(): + """动态读取 app.mysql_database.MySQLSessionLocal,避免模块导入顺序问题。""" + import sys + module = sys.modules.get("app.mysql_database") + if module is None: + return None + return getattr(module, "MySQLSessionLocal", None) + CONFIG_DIR = Path(__file__).resolve().parent.parent / "config" CONFIG_FILES = { "symbols": CONFIG_DIR / "symbols_config.json", @@ -47,12 +55,19 @@ class ConfigStore: ): self.storage_manager = storage_manager or get_storage_manager() self.config_dir = config_dir - self.session_maker = session_maker or MySQLSessionLocal + self._session_maker_override = session_maker self.config_files = { "symbols": self.config_dir / "symbols_config.json", "ai": self.config_dir / "ai_config.json", } + @property + def session_maker(self): + """动态读取 MySQLSessionLocal,避免初始化顺序问题。""" + if self._session_maker_override is not None: + return self._session_maker_override + return _get_mysql_session_local() + def get_config(self, key: str, fallback: Optional[dict] = None) -> dict: """读取配置,优先从 MySQL,其次 JSON 文件。""" if key not in self.config_files: