From 4432de4215e20028e21d922693693fc130a0eae9 Mon Sep 17 00:00:00 2001 From: Li Chuangbo Date: Fri, 7 Oct 2022 15:55:59 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20alembic=20?= =?UTF-8?q?=E5=8F=AA=E5=9C=A8=20core=20=E5=92=8C=20plugins=20=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=9F=A5=E6=89=BE=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 因为有可能项目根目录有其他不在 git 里管理的目录存在 models.py 文件,比如 `venv/Lib/site-packages/charset_normalizer/models.py` --- alembic/env.py | 8 +++++--- utils/const.py | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 4ff4e20d..dc1a58e4 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,3 +1,4 @@ +from itertools import chain import os import asyncio from importlib import import_module @@ -13,7 +14,7 @@ from sqlmodel import SQLModel from alembic import context -from utils.const import PROJECT_ROOT +from utils.const import CORE_DIR, PLUGIN_DIR, PROJECT_ROOT from utils.log import logger # this is the Alembic Config object, which provides @@ -27,10 +28,11 @@ if config.config_file_name is not None: def scan_models() -> Iterator[str]: - """扫描所有 models.py 模块。 + """扫描 core 和 plugins 目录下所有 models.py 模块。 我们规定所有插件的 model 都需要放在名为 models.py 的文件里。""" + dirs = [CORE_DIR, PLUGIN_DIR] - for path in PROJECT_ROOT.glob("**/models.py"): + for path in chain(*[d.glob("**/models.py") for d in dirs]): yield str(path.relative_to(PROJECT_ROOT).with_suffix("")).replace(os.sep, ".") diff --git a/utils/const.py b/utils/const.py index 6105d235..626a9f5a 100644 --- a/utils/const.py +++ b/utils/const.py @@ -11,6 +11,8 @@ __all__ = [ # 项目根目录 PROJECT_ROOT = Path(__file__).joinpath('../..').resolve() +# Core 目录 +CORE_DIR = PROJECT_ROOT / 'core' # 插件目录 PLUGIN_DIR = PROJECT_ROOT / 'plugins' # 资源目录