From 1000eae146598c3ce80ce7750fecc6c4a76826b6 Mon Sep 17 00:00:00 2001 From: shyblacktea <33568732+shyblacktea@users.noreply.github.com> Date: Sat, 31 Oct 2026 14:05:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(database-operation):=20=E7=94=A8=20psycopg?= =?UTF-8?q?=203=20=E5=BB=BA=E5=90=8C=E6=AD=A5=E5=BC=95=E6=93=8E=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=BA=20psycopg2=20=E6=8A=A5=E9=94=99=20(?= =?UTF-8?q?#7254)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/database-operation/scripts/mp-db.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/skills/database-operation/scripts/mp-db.py b/skills/database-operation/scripts/mp-db.py index ab09fd16f..8d0f6bbf5 100644 --- a/skills/database-operation/scripts/mp-db.py +++ b/skills/database-operation/scripts/mp-db.py @@ -229,6 +229,24 @@ def _load_settings() -> Any: return settings +def _sync_postgres_url(url: str) -> str: + """ + 返回同步引擎可用的 PostgreSQL URL。 + + SQLAlchemy 对 `postgresql://` 默认使用 psycopg2,而 v3 镜像只内置了 + psycopg 3(`psycopg`)。检测到 psycopg 3 时显式指定驱动,避免 + `ModuleNotFoundError: No module named 'psycopg2'`;没有 psycopg 3 的环境 + 保持原行为,URL 已显式带驱动时也不做改动。 + """ + if "+" in url.split("://", 1)[0]: + return url + try: + import psycopg # noqa: F401 pylint: disable=import-outside-toplevel,unused-import + except ImportError: + return url + return url.replace("postgresql://", "postgresql+psycopg://", 1) + + def _build_engine() -> Engine: """ 根据 MoviePilot 配置创建同步数据库引擎。 @@ -236,7 +254,7 @@ def _build_engine() -> Engine: settings = _load_settings() if str(settings.DB_TYPE).lower() == "postgresql": return create_engine( - settings.DB_POSTGRESQL_URL(), + _sync_postgres_url(settings.DB_POSTGRESQL_URL()), pool_pre_ping=settings.DB_POOL_PRE_PING, pool_recycle=settings.DB_POOL_RECYCLE, )